diff --git a/LLama/Common/Logger.cs b/LLama/Common/Logger.cs
index 9bcd927e..227a0dc5 100644
--- a/LLama/Common/Logger.cs
+++ b/LLama/Common/Logger.cs
@@ -1,4 +1,5 @@
-using System;
+using LLama.Native;
+using System;
using System.Diagnostics;
using System.IO;
using static LLama.Common.ILLamaLogger;
@@ -9,13 +10,13 @@ public interface ILLamaLogger
{
public enum LogLevel
{
- Info,
- Debug,
- Warning,
- Error
+ Debug = 1,
+ Error = 2,
+ Warning = 3,
+ Info = 4
}
///
- /// Write the log in cosutomized way
+ /// Write the log in customized way
///
/// The source of the log. It may be a method name or class name.
/// The message.
@@ -25,7 +26,7 @@ public interface ILLamaLogger
///
/// The default logger of LLamaSharp. On default it write to console. User methods of `LLamaLogger.Default` to change the behavior.
-/// It's more recommended to inherit `ILLamaLogger` to cosutomize the behavior.
+/// It's more recommended to inherit `ILLamaLogger` to customize the behavior.
///
public sealed class LLamaDefaultLogger : ILLamaLogger
{
@@ -44,6 +45,16 @@ public sealed class LLamaDefaultLogger : ILLamaLogger
}
+ ///
+ /// Enable logging output from llama.cpp
+ ///
+ ///
+ public LLamaDefaultLogger EnableNative()
+ {
+ EnableNativeLogCallback();
+ return this;
+ }
+
public LLamaDefaultLogger EnableConsole()
{
_toConsole = true;
@@ -157,4 +168,31 @@ public sealed class LLamaDefaultLogger : ILLamaLogger
string formattedDate = now.ToString("yyyy.MM.dd HH:mm:ss");
return $"[{formattedDate}][{level}]: {message}";
}
+
+ ///
+ /// Register native logging callback
+ ///
+ private void EnableNativeLogCallback()
+ {
+ // TODO: Move to a more appropriate place once we have a intitialize method
+ NativeApi.llama_log_set(NativeLogCallback);
+ }
+
+ ///
+ /// Callback for native logging function
+ ///
+ /// The log level
+ /// The log message
+ private void NativeLogCallback(LogLevel level, string message)
+ {
+ if (string.IsNullOrEmpty(message))
+ return;
+
+ // Note that text includes the new line character at the end for most events.
+ // If your logging mechanism cannot handle that, check if the last character is '\n' and strip it
+ // if it exists.
+ // It might not exist for progress report where '.' is output repeatedly.
+ Log(default!, message.TrimEnd('\n'), level);
+ }
+
}
\ No newline at end of file
diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs
index edfb4152..810e0e47 100644
--- a/LLama/Native/NativeApi.cs
+++ b/LLama/Native/NativeApi.cs
@@ -1,12 +1,16 @@
using System;
using System.Runtime.InteropServices;
using System.Text;
+using LLama.Common;
using LLama.Exceptions;
namespace LLama.Native
{
using llama_token = Int32;
- public unsafe partial class NativeApi
+
+ public delegate void LLamaLogCallback(ILLamaLogger.LogLevel level, string message);
+
+ public unsafe partial class NativeApi
{
public static readonly int LLAMA_MAX_DEVICES = 1;
static NativeApi()
@@ -331,5 +335,8 @@ namespace LLama.Native
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_tokenize_with_model(SafeLlamaModelHandle model, byte* text, int* tokens, int n_max_tokens, bool add_bos);
- }
+
+ [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
+ public static extern void llama_log_set(LLamaLogCallback logCallback);
+ }
}