Skip to main content

AgentLogger

Struct AgentLogger 

Source
pub struct AgentLogger { /* private fields */ }
Expand description

The structured logger. One instance == one run == one JSONL file.

Thread-safe: the underlying file handle is guarded by a Mutex and the sequence counter is atomic.

Implementations§

Source§

impl AgentLogger

Source

pub fn new( program: &str, command: Option<&str>, log_dir: impl AsRef<Path>, rid: Option<&str>, ) -> Result<Self>

Create a new logger (and its log file).

  • program – program name (filename component, sanitised).
  • command – sub-command; None → pid{PID}.
  • log_dir – directory for log files (./logs is typical).
  • rid – run id override; None → random 8 hex chars.
Examples found in repository?
examples/rust_emit.rs (line 5)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn new_compact( program: &str, command: Option<&str>, log_dir: impl AsRef<Path>, rid: Option<&str>, compact: bool, ) -> Result<Self>

Like [new], but enables compact-key mode (contract §4).

Source

pub fn file_path(&self) -> &Path

Absolute path of the active log file.

Examples found in repository?
examples/rust_emit.rs (line 13)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn rid(&self) -> &str

Source

pub fn program(&self) -> &str

Source

pub fn command(&self) -> &str

Source

pub fn save_traceback( &self, exc_type: &str, exc_msg: &str, traceback: &str, ) -> Result<String>

Persist a traceback to the .tracebacks sidecar; returns the generated tid.

Source

pub fn save_traceback_text( &self, tid: &str, exc_type: &str, exc_msg: &str, traceback: &str, ) -> Result<()>

Source

pub fn info(&self, msg: &str, module: Option<&str>) -> Result<()>

logger.info(msg, module, dur?, error_code?, tid?, ctx?)

Source

pub fn info_full( &self, msg: &str, module: Option<&str>, dur: Option<u64>, error_code: Option<&str>, tid: Option<&str>, ctx: Option<Value>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 6)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn warn(&self, msg: &str, module: Option<&str>) -> Result<()>

Source

pub fn warn_full( &self, msg: &str, module: Option<&str>, dur: Option<u64>, error_code: Option<&str>, tid: Option<&str>, ctx: Option<Value>, ) -> Result<()>

Source

pub fn error( &self, msg: &str, module: Option<&str>, error_code: ErrorCode, tid: Option<&str>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 8)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn error_full( &self, msg: &str, module: Option<&str>, error_code: &str, tid: Option<&str>, dur: Option<u64>, ctx: Option<Value>, ) -> Result<()>

Source

pub fn tool_call( &self, tool: &str, cmd: &str, exit: i64, dur: u64, error_code: Option<&str>, tid: Option<&str>, stdout: Option<&str>, stderr: Option<&str>, ctx: Option<Value>, ) -> Result<()>

tool_call(tool, cmd, exit, dur, error_code?, tid?, stdout?, stderr?, ctx?). Like Python, requires error_code semantics for failures are the caller’s responsibility (this SDK accepts it optionally).

Examples found in repository?
examples/rust_emit.rs (line 7)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn file_op( &self, op: &str, path: &str, ok: bool, size: Option<u64>, error_code: Option<&str>, tid: Option<&str>, dur: Option<u64>, ctx: Option<Value>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 9)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn decision( &self, choice: &str, alts: Option<&[&str]>, reason: Option<&str>, confidence: Option<f64>, module: Option<&str>, ctx: Option<Value>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 10)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn code_gen( &self, lang: &str, path: &str, lines: Option<u64>, funcs: Option<&[&str]>, imports: Option<&[&str]>, module: Option<&str>, ctx: Option<Value>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 11)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}
Source

pub fn context_switch( &self, to_task: &str, from_task: Option<&str>, reason: Option<&str>, module: Option<&str>, ctx: Option<Value>, ) -> Result<()>

Examples found in repository?
examples/rust_emit.rs (line 12)
3fn main() {
4    let dir = std::env::args().nth(1).unwrap_or_else(|| "/tmp/xlang_rust".into());
5    let lg = AgentLogger::new("rust_probe", Some("demo"), &dir, Some("cafebabe")).unwrap();
6    lg.info_full("Processing started", Some("parser"), Some(12), None, None, Some(json!({"file":"data.json","size":1024}))).ok();
7    lg.tool_call("bash", "npm install", 0, 1234, None, None, Some("added 50 pkgs"), None, None).ok();
8    lg.error("Build failed", Some("build"), ErrorCode::ExecNonZero, Some("tb_abcd1234")).ok();
9    lg.file_op("write", "/p/f.rs", true, Some(2048), None, None, Some(5), None).ok();
10    lg.decision("use_redis", Some(&["redis","memcached"]), Some("perf"), Some(0.85), Some("arch"), None).ok();
11    lg.code_gen("rust", "src/main.rs", Some(50), Some(&["main","helper"]), None, None, None).ok();
12    lg.context_switch("test", Some("build"), Some("done"), None, None).ok();
13    println!("{}", lg.file_path().display());
14}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.