forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.rs
More file actions
319 lines (282 loc) · 8.55 KB
/
logging.rs
File metadata and controls
319 lines (282 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//! Logging infrastructure for jcode
//!
//! Logs to ~/.jcode/logs/ with automatic rotation
//!
//! Supports thread-local context for server, session, provider, and model info.
use chrono::Local;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock};
static LOGGER: Mutex<Option<Logger>> = Mutex::new(None);
static TASK_LOG_CONTEXTS: OnceLock<Mutex<HashMap<String, LogContext>>> = OnceLock::new();
/// Thread-local logging context
#[derive(Default, Clone)]
pub struct LogContext {
pub server: Option<String>,
pub session: Option<String>,
pub provider: Option<String>,
pub model: Option<String>,
}
thread_local! {
static LOG_CONTEXT: RefCell<LogContext> = RefCell::new(LogContext::default());
}
/// Update just the session in the current context
pub fn set_session(session: &str) {
if with_task_context_mut(|ctx| {
ctx.session = Some(session.to_string());
}) {
return;
}
LOG_CONTEXT.with(|c| {
c.borrow_mut().session = Some(session.to_string());
});
}
/// Update just the server in the current context
pub fn set_server(server: &str) {
if with_task_context_mut(|ctx| {
ctx.server = Some(server.to_string());
}) {
return;
}
LOG_CONTEXT.with(|c| {
c.borrow_mut().server = Some(server.to_string());
});
}
/// Update provider and model in the current context
pub fn set_provider_info(provider: &str, model: &str) {
if with_task_context_mut(|ctx| {
ctx.provider = Some(provider.to_string());
ctx.model = Some(model.to_string());
}) {
return;
}
LOG_CONTEXT.with(|c| {
let mut ctx = c.borrow_mut();
ctx.provider = Some(provider.to_string());
ctx.model = Some(model.to_string());
});
}
/// Get the current context as a prefix string
fn context_prefix() -> String {
if let Some(task_ctx) = task_context_snapshot() {
return context_prefix_for(&task_ctx);
}
LOG_CONTEXT.with(|c| context_prefix_for(&c.borrow()))
}
fn current_task_id() -> Option<String> {
tokio::task::try_id().map(|id| id.to_string())
}
fn with_task_context_mut(update: impl FnOnce(&mut LogContext)) -> bool {
let Some(task_id) = current_task_id() else {
return false;
};
let store = TASK_LOG_CONTEXTS.get_or_init(|| Mutex::new(HashMap::new()));
if let Ok(mut contexts) = store.lock() {
let ctx = contexts.entry(task_id).or_default();
update(ctx);
true
} else {
false
}
}
fn task_context_snapshot() -> Option<LogContext> {
let task_id = current_task_id()?;
let store = TASK_LOG_CONTEXTS.get()?;
let contexts = store.lock().ok()?;
contexts.get(&task_id).cloned()
}
fn context_prefix_for(ctx: &LogContext) -> String {
let mut parts = Vec::new();
if let Some(ref server) = ctx.server {
parts.push(format!("srv:{}", server));
}
if let Some(ref session) = ctx.session {
// Truncate session name if too long
let short = if session.len() > 20 {
&session[..20]
} else {
session
};
parts.push(format!("ses:{}", short));
}
if let Some(ref provider) = ctx.provider {
parts.push(format!("prv:{}", provider));
}
if let Some(ref model) = ctx.model {
// Just use first part of model name
let short = model.split('-').next().unwrap_or(model);
parts.push(format!("mod:{}", short));
}
if parts.is_empty() {
String::new()
} else {
format!("[{}] ", parts.join("|"))
}
}
pub struct Logger {
file: File,
}
fn log_dir() -> Option<PathBuf> {
crate::storage::logs_dir().ok()
}
impl Logger {
fn new() -> Option<Self> {
let log_dir = log_dir()?;
crate::storage::ensure_dir(&log_dir).ok()?;
// Use date-based log file
let date = Local::now().format("%Y-%m-%d");
let path = log_dir.join(format!("jcode-{}.log", date));
let file = OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.ok()?;
Some(Self { file })
}
fn write(&mut self, level: &str, message: &str) {
let timestamp = Local::now().format("%Y-%m-%d %H:%M:%S%.3f");
let ctx = context_prefix();
let line = format!("[{}] [{}] {}{}\n", timestamp, level, ctx, message);
if let Err(err) = self.file.write_all(line.as_bytes()) {
eprintln!("jcode logger write failed: {err}");
return;
}
if let Err(err) = self.file.flush() {
eprintln!("jcode logger flush failed: {err}");
}
}
}
/// Initialize the logger (call once at startup)
pub fn init() {
let mut guard = match LOGGER.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
if guard.is_none() {
*guard = Logger::new();
}
}
/// Log an info message
#[expect(
clippy::collapsible_if,
reason = "Logger lock + optional logger branching is intentionally straightforward"
)]
pub fn info(message: &str) {
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("INFO", message);
}
}
}
/// Log an error message
#[expect(
clippy::collapsible_if,
reason = "Logger lock + optional logger branching is intentionally straightforward"
)]
pub fn error(message: &str) {
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("ERROR", message);
}
}
}
/// Log a warning message
#[expect(
clippy::collapsible_if,
reason = "Logger lock + optional logger branching is intentionally straightforward"
)]
pub fn warn(message: &str) {
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("WARN", message);
}
}
}
/// Log a debug message (only if JCODE_TRACE is set)
#[expect(
clippy::collapsible_if,
reason = "Debug logging keeps env gating and logger access explicit"
)]
pub fn debug(message: &str) {
if std::env::var("JCODE_TRACE").is_ok() {
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("DEBUG", message);
}
}
}
}
/// Log a tool call
#[expect(
clippy::collapsible_if,
reason = "Logger lock + optional logger branching is intentionally straightforward"
)]
pub fn tool_call(name: &str, input: &str, output: &str) {
let msg = format!(
"TOOL[{}] input={} output={}",
name,
truncate(input, 200),
truncate(output, 500)
);
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("TOOL", &msg);
}
}
}
/// Log a crash/panic for auto-debug
#[expect(
clippy::collapsible_if,
reason = "Logger lock + optional logger branching is intentionally straightforward"
)]
pub fn crash(error: &str, context: &str) {
let msg = format!("CRASH: {} | Context: {}", error, context);
if let Ok(mut guard) = LOGGER.lock() {
if let Some(logger) = guard.as_mut() {
logger.write("CRASH", &msg);
}
}
}
/// Get the session ID from the current logging context (thread-local or task-local).
pub fn current_session() -> Option<String> {
if let Some(ctx) = task_context_snapshot() {
return ctx.session;
}
LOG_CONTEXT.with(|c| c.borrow().session.clone())
}
/// Get path to today's log file
pub fn log_path() -> Option<PathBuf> {
let log_dir = log_dir()?;
let date = Local::now().format("%Y-%m-%d");
Some(log_dir.join(format!("jcode-{}.log", date)))
}
/// Clean up old logs (keep last 7 days)
pub fn cleanup_old_logs() {
if let Some(log_dir) = log_dir()
&& let Ok(entries) = fs::read_dir(&log_dir)
{
let cutoff = Local::now() - chrono::Duration::days(7);
for entry in entries.flatten() {
if let Ok(metadata) = entry.metadata()
&& let Ok(modified) = metadata.modified()
{
let modified: chrono::DateTime<Local> = modified.into();
if modified < cutoff
&& let Err(err) = fs::remove_file(entry.path())
{
eprintln!("jcode logger cleanup failed: {err}");
}
}
}
}
}
fn truncate(s: &str, max_len: usize) -> String {
if s.len() > max_len {
format!("{}...", crate::util::truncate_str(s, max_len))
} else {
s.to_string()
}
}