@@ -49,12 +49,52 @@ impl From<u8> for LogLevel {
4949pub struct Record < ' a > {
5050 #[ serde_as( as = "serde_with::TimestampMicroSeconds" ) ]
5151 pub ts : chrono:: DateTime < Utc > ,
52+ /// Target of the log call (usually source namespace or `mod`).
53+ ///
54+ /// Provided by the WASM guest as an argument to the `console_log` host function.
55+ ///
56+ /// The special sentinel value [`Record::SENTINEL_INJECTED_TARGET`]` denotes logs injected by the [`SystemLogger`].
5257 pub target : Option < & ' a str > ,
58+ /// Filename of the source location of the log call.
59+ ///
60+ /// Provided by the WASM guest as an argument to the `console_log` host function.
61+ ///
62+ /// The special sentinel value [`Record::SENTINEL_INJECTED_FILENAME`]` denotes logs injected by the [`SystemLogger`].
5363 pub filename : Option < & ' a str > ,
5464 pub line_number : Option < u32 > ,
65+ /// Which exported function (i.e. reducer) was being called when this message was produced.
66+ ///
67+ /// Unlike `target`, `filename` and `line_number`, this is not provided by the WASM guest.
68+ /// Instead, the `WasmInstanceEnv` remembers what function call is in progress and adds it to the record.
69+ ///
70+ /// The special sentinel value [`Record::SENTINEL_INJECTED_FUNCTION`] denotes logs injected by the [`SystemLogger`].
71+ pub function : Option < & ' a str > ,
5572 pub message : & ' a str ,
5673}
5774
75+ impl < ' a > Record < ' a > {
76+ pub const SENTINEL_INJECTED_FUNCTION : Option < & ' static str > = Some ( "__spacetimedb__" ) ;
77+ pub const SENTINEL_INJECTED_TARGET : Option < & ' static str > = Some ( "__spacetimedb__" ) ;
78+ pub const SENTINEL_INJECTED_FILENAME : Option < & ' static str > = Some ( "__spacetimedb__" ) ;
79+
80+ /// Create a log `Record` for a system message, not attributed to any reducer or user filename.
81+ ///
82+ /// The resulting `Record` will draw from [`chrono::Utc::now`] for its timestamp,
83+ /// have `line_number: None`,
84+ /// and will use [`Self::SENTINEL_INJECTED_FILENAME`], [`Self::SENTINEL_INJECTED_FUNCTION`]
85+ /// and [`Self::SENTINEL_INJECTED_TARGET`].
86+ pub fn injected ( message : & ' a str ) -> Self {
87+ Record {
88+ ts : chrono:: Utc :: now ( ) ,
89+ target : Self :: SENTINEL_INJECTED_TARGET ,
90+ filename : Self :: SENTINEL_INJECTED_FILENAME ,
91+ line_number : None ,
92+ function : Self :: SENTINEL_INJECTED_FUNCTION ,
93+ message,
94+ }
95+ }
96+ }
97+
5898pub trait BacktraceProvider {
5999 fn capture ( & self ) -> Box < dyn ModuleBacktrace > ;
60100}
@@ -277,12 +317,6 @@ impl SystemLogger {
277317 }
278318
279319 fn record ( message : & str ) -> Record {
280- Record {
281- ts : Utc :: now ( ) ,
282- target : None ,
283- filename : Some ( "spacetimedb" ) ,
284- line_number : None ,
285- message,
286- }
320+ Record :: injected ( message)
287321 }
288322}
0 commit comments