-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented native logging through serilog instead of directly to stdout
- Loading branch information
Showing
10 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Text; | ||
using Serilog; | ||
using Serilog.Events; | ||
|
||
namespace JKMP.Plugin.Multiplayer.Native | ||
{ | ||
public static class Logging | ||
{ | ||
private static readonly OnLogCallback OnLogCallback; | ||
|
||
static Logging() | ||
{ | ||
// We need a reference to the callback to be able to call it from native code | ||
// If passed directly to the native function, it will be garbage collected | ||
OnLogCallback = OnLog; | ||
} | ||
|
||
public static void Initialize() | ||
{ | ||
Bindings.initialize_logging(OnLogCallback); | ||
} | ||
|
||
private static void OnLog(LogLevel logLevel, Sliceu8 utf8Message) | ||
{ | ||
string message; | ||
|
||
unsafe | ||
{ | ||
fixed (byte* messagePtr = &utf8Message.ReadOnlySpan.GetPinnableReference()) | ||
{ | ||
message = Encoding.UTF8.GetString(messagePtr, utf8Message.Count); | ||
} | ||
} | ||
|
||
var logEventLevel = (LogEventLevel)logLevel; | ||
NativeLog.Logger.Write(logEventLevel, (Exception?)null, "{nativeMessage}", message); | ||
} | ||
} | ||
|
||
internal static class NativeLog | ||
{ | ||
public static readonly ILogger Logger = Log.Logger.ForContext(typeof(NativeLog)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use interoptopus::patterns::slice::FFISlice; | ||
use interoptopus::{callback, ffi_function, ffi_type}; | ||
use log::{LevelFilter, Metadata, Record}; | ||
|
||
// Note that this enum needs to match serilog's enum, otherwise casting will not work correctly | ||
#[repr(C)] | ||
#[ffi_type] | ||
pub enum LogLevel { | ||
Verbose, | ||
Debug, | ||
Info, | ||
Warning, | ||
Error, | ||
Fatal, | ||
} | ||
|
||
callback!(OnLogCallback(log_level: LogLevel, message: FFISlice<u8>)); | ||
|
||
#[ffi_function] | ||
#[no_mangle] | ||
pub extern "C" fn initialize_logging(on_log: OnLogCallback) -> bool { | ||
log::set_boxed_logger(Box::new(CallbackLogger { | ||
on_log_callback: on_log, | ||
})) | ||
.map(|()| log::set_max_level(LevelFilter::Trace)) | ||
.is_ok() | ||
} | ||
|
||
struct CallbackLogger { | ||
on_log_callback: OnLogCallback, | ||
} | ||
|
||
impl log::Log for CallbackLogger { | ||
fn enabled(&self, _: &Metadata) -> bool { | ||
true | ||
} | ||
|
||
fn log(&self, record: &Record) { | ||
let log_level = match record.level() { | ||
log::Level::Error => LogLevel::Error, | ||
log::Level::Warn => LogLevel::Warning, | ||
log::Level::Info => LogLevel::Info, | ||
log::Level::Debug => LogLevel::Debug, | ||
log::Level::Trace => LogLevel::Verbose, | ||
}; | ||
let mut message = String::new(); | ||
|
||
message.push_str(format!("{}", record.args()).as_str()); | ||
|
||
self.on_log_callback | ||
.call(log_level, message.as_bytes().into()); | ||
} | ||
|
||
fn flush(&self) {} | ||
} |