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
use log::debug;
use serde::{de::DeserializeOwned, Serialize};
use std::{io::Write, path::PathBuf};
use crate::NanonisError;
// Removed LogEntry wrapper - ActionLogEntry already has timestamps
#[derive(Debug)]
pub struct Logger<T>
where
T: Serialize + Clone + DeserializeOwned,
{
buffer: Vec<T>,
buffer_size: usize,
file_path: PathBuf,
final_format_json: bool, // If true, convert to JSON on final flush
flush_failures: usize,
max_flush_failures: usize,
}
impl<T> Logger<T>
where
T: Serialize + Clone + DeserializeOwned,
{
pub fn new<P: Into<PathBuf>>(
file_path: P,
buffer_size: usize,
final_format_json: bool,
) -> Self {
let mut path = file_path.into();
// Automatically add appropriate file extension
if final_format_json {
// For JSON output, ensure .json extension
if path.extension().is_none()
|| path.extension() != Some(std::ffi::OsStr::new("json"))
{
path.set_extension("json");
}
} else {
// For JSONL output, ensure .jsonl extension
if path.extension().is_none()
|| path.extension() != Some(std::ffi::OsStr::new("jsonl"))
{
path.set_extension("jsonl");
}
}
Self {
buffer: Vec::with_capacity(buffer_size),
buffer_size,
file_path: path,
final_format_json,
flush_failures: 0,
max_flush_failures: 10,
}
}
pub fn add(&mut self, data: T) -> Result<(), NanonisError> {
self.buffer.push(data);
if self.buffer.len() >= self.buffer_size {
self.flush()?;
}
Ok(())
}
pub fn flush(&mut self) -> Result<(), NanonisError> {
if self.buffer.is_empty() {
return Ok(());
}
// Always write JSONL for intermediate flushes (efficient)
let file_result = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(&self.file_path);
let file = match file_result {
Ok(f) => f,
Err(e) => {
self.flush_failures += 1;
log::error!(
"Flush failure {}/{}: Failed to open log file: {}",
self.flush_failures,
self.max_flush_failures,
e
);
// Periodic warning
if self.flush_failures > 0 && self.flush_failures % 3 == 0 {
log::warn!(
"Experiencing intermittent flush failures ({}/{})",
self.flush_failures,
self.max_flush_failures
);
}
if self.flush_failures >= self.max_flush_failures {
return Err(NanonisError::Io {
source: e,
context: format!(
"Too many consecutive flush failures ({}) for {:?}",
self.max_flush_failures, self.file_path
),
});
}
// Don't fail the experiment for transient flush errors
return Ok(());
}
};
let mut writer = std::io::BufWriter::new(file);
// Write data
let write_result = (|| {
for data in &self.buffer {
let json_line = serde_json::to_string(data)?;
writeln!(writer, "{}", json_line)?;
}
writer.flush()?;
Ok::<(), NanonisError>(())
})();
match write_result {
Ok(_) => {
self.flush_failures = 0; // Reset on success
self.buffer.clear();
debug!("Logger flushed successfully to file");
Ok(())
}
Err(e) => {
self.flush_failures += 1;
log::error!(
"Flush failure {}/{}: Write error: {}",
self.flush_failures,
self.max_flush_failures,
e
);
// Periodic warning
if self.flush_failures > 0 && self.flush_failures % 3 == 0 {
log::warn!(
"Experiencing intermittent flush failures ({}/{})",
self.flush_failures,
self.max_flush_failures
);
}
if self.flush_failures >= self.max_flush_failures {
return Err(NanonisError::Io {
source: std::io::Error::other(e.to_string()),
context: format!(
"Too many consecutive flush failures ({}) for {:?}",
self.max_flush_failures, self.file_path
),
});
}
// Don't fail the experiment for transient flush errors
Ok(())
}
}
}
/// Convert JSONL file to JSON array format (for final post-experiment analysis)
pub fn finalize_as_json(&mut self) -> Result<(), NanonisError> {
if !self.final_format_json {
return Ok(()); // No conversion needed
}
// First flush any remaining buffer
self.flush()?;
// Read all JSONL entries
let content =
std::fs::read_to_string(&self.file_path).map_err(|source| {
NanonisError::Io {
source,
context: format!(
"Could not read JSONL file at {:?}",
self.file_path
),
}
})?;
let mut entries = Vec::new();
for line in content.lines() {
if !line.trim().is_empty() {
let data: T = serde_json::from_str(line)?;
entries.push(data);
}
}
// Write as JSON array with pretty formatting
let json_output = serde_json::to_string_pretty(&entries)?;
std::fs::write(&self.file_path, json_output).map_err(|source| {
NanonisError::Io {
source,
context: format!(
"Could not write JSON file at {:?}",
self.file_path
),
}
})?;
debug!(
"Converted {} entries from JSONL to JSON format",
entries.len()
);
Ok(())
}
pub fn len(&self) -> usize {
self.buffer.len()
}
pub fn is_empty(&self) -> bool {
self.buffer.len() == 0
}
}
impl<T> Drop for Logger<T>
where
T: Serialize + Clone + DeserializeOwned,
{
fn drop(&mut self) {
let _ = self.flush();
let _ = self.finalize_as_json();
}
}