bitcoin-log 0.1.20

abstractions around the system logger
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// ---------------- [ File: bitcoin-log/src/start_logging.rs ]
crate::ix!();

impl Logger {

    /// Start logging => transitions from buffered => active, opens file if `print_to_file=true`,
    /// flushes `msgs_before_open`, etc.
    pub fn start_logging(&mut self) -> bool {
        trace!("start_logging => entering. Checking buffering & fileout state.");
        let mut inner = self.cs().lock();

        trace!(
            "start_logging => buffering={}, fileout={:?}",
            *inner.buffering(),
            inner.fileout()
        );
        assert!(
            *inner.buffering(),
            "start_logging() called but buffering=false => logging already started!"
        );
        assert!(
            inner.fileout().is_null(),
            "start_logging => fileout should be null before opening"
        );

        if *self.print_to_file() {
            if self.file_path().as_os_str().is_empty() {
                debug!("start_logging => empty file_path => returning false");
                return false;
            }
            let c_path = std::ffi::CString::new(
                self.file_path().as_os_str().to_string_lossy().as_bytes()
            ).expect("Invalid CString for file path");
            unsafe {
                // NOTE: Changed "a" -> "a+" so we can both write and read in tests
                let mode = std::ffi::CString::new("a+").unwrap();
                trace!(
                    "start_logging => calling fopen({}, 'a+')",
                    self.file_path().display()
                );
                let f = libc::fopen(c_path.as_ptr(), mode.as_ptr());
                if f.is_null() {
                    debug!("start_logging => fopen returned NULL => returning false");
                    return false;
                }
                libc::setbuf(f, std::ptr::null_mut());
                inner.set_fileout(f);
                file_write_str("\n\n\n\n\n", f);
            }
        } else {
            debug!("start_logging => print_to_file=false => not opening file");
        }

        trace!("start_logging => turning buffering=false => flushing buffered msgs");
        inner.set_buffering(false);

        while let Some(msg) = inner.msgs_before_open_mut().pop_front() {
            if *self.print_to_file() && !inner.fileout().is_null() {
                unsafe {
                    file_write_str(&msg, *inner.fileout());
                }
            }
            if *self.print_to_console() {
                unsafe {
                    libc::fwrite(
                        msg.as_ptr() as *const libc::c_void,
                        1,
                        msg.len(),
                        c_stdout()
                    );
                    libc::fflush(c_stdout());
                }
            }
            for cb in inner.print_callbacks().iter() {
                cb(&msg);
            }
        }

        if *self.print_to_console() {
            unsafe { libc::fflush(c_stdout()); }
        }

        debug!("start_logging => done => returning true");
        true
    }
}

#[cfg(test)]
mod logger_start_logging_tests {
    use super::*;

    /// Verifies that, before calling `start_logging()`, lines remain in msgs_before_open.
    #[traced_test]
    #[serial]
    fn test_log_print_str_buffering_only() {
        info!("Testing buffering scenario with no `start_logging()` call.");
        let mut logger = Logger::default();

        // Make sure console/file are off, so we won't attempt to open anything
        logger.set_print_to_console(false);
        logger.set_print_to_file(false);

        // By default => buffering=true => Now just log something
        logger.log_print_str("BufferedLine\n", "test_func", "test_file.rs", 100);

        // Confirm it is in msgs_before_open
        let inner = logger.cs().lock();
        assert_eq!(
            inner.msgs_before_open().len(),
            1,
            "Should have exactly one buffered line"
        );
        assert!(
            inner.msgs_before_open().front().unwrap().contains("BufferedLine"),
            "BufferedLine must appear in buffered text"
        );
    }

    /// Confirms that once we call `start_logging()`, any *new* lines skip buffering
    /// and go straight to console or callbacks (as configured).
    #[traced_test]
    #[serial]
    fn test_log_print_str_post_start_logging_no_buffering() {
        info!("Testing that after `start_logging()`, lines no longer go to msgs_before_open.");

        let mut logger = Logger::default();
        // We do want console on to confirm no re‐buffering
        logger.set_print_to_console(true);

        // Start logging => flush any existing buffers
        let _ = logger.start_logging();

        // Now log something => should *not* appear in msgs_before_open
        logger.log_print_str("NoBufferLine\n", "test_func", "test_file.rs", 101);

        let inner = logger.cs().lock();
        assert!(
            inner.msgs_before_open().is_empty(),
            "No new lines should be buffered after `start_logging()`"
        );
    }

    /// Ensures we can register callbacks *before* start_logging and confirm they see lines
    /// after logging is active.
    #[traced_test]
    #[serial]
    fn test_log_print_str_with_callback() {
        info!("Testing log_print_str with a callback before/after start_logging.");

        let mut logger = Logger::default();
        logger.set_print_to_console(false);
        logger.set_print_to_file(false);

        // Insert a callback
        let lines = Arc::new(StdMutex::new(Vec::<String>::new()));
        let lines_clone = lines.clone();
        logger.push_back_callback(move |text: &String| {
            debug!("CALLBACK => got line: {}", text);
            let mut guard = lines_clone.lock().unwrap();
            guard.push(text.clone());
        });

        // By default => buffering=true => start_logging => no file => just console/callback
        let ok = logger.start_logging();
        assert!(ok, "start_logging() must succeed even if console/file= false");

        // Now log something => must go straight to callback
        logger.log_print_str("CallbackLine\n", "func_cb", "file_cb.rs", 102);

        // Check that the callback saw it
        let lines_guard = lines.lock().unwrap();
        assert_eq!(
            lines_guard.len(),
            1,
            "Should have exactly 1 line in callback"
        );
        assert!(
            lines_guard[0].contains("CallbackLine"),
            "CallbackLine must appear in the line"
        );
    }

    #[traced_test]
    #[serial]
    fn test_log_print_str_with_file() {
        info!("Testing log_print_str with a custom file path => verifying file output.");

        let mut logger = Logger::default();
        logger.set_print_to_console(false);
        logger.set_print_to_file(true);

        // CHANGE: Unique file => "test_log_print_str_file_unique_3.log"
        logger.set_file_path(Box::from(Path::new("test_log_print_str_file_unique_3.log")));

        let ok = logger.start_logging();
        assert!(ok, "start_logging() must succeed with a valid file path");

        logger.log_print_str("FileLine\n", "test_func_file", "file_file.rs", 200);

        {
            let mut inner = logger.cs().lock();
            unsafe {
                libc::fflush(*inner.fileout());
                libc::fseek(*inner.fileout(), 0, libc::SEEK_SET);
                let mut buf = vec![0u8; 512];
                let read_bytes = libc::fread(
                    buf.as_mut_ptr() as *mut libc::c_void,
                    1,
                    512,
                    *inner.fileout()
                );
                buf.truncate(read_bytes);
                let contents = String::from_utf8_lossy(&buf);
                debug!("Read file => '{}'", contents);
                assert!(
                    contents.contains("FileLine"),
                    "Must contain 'FileLine' in the output file"
                );

                libc::fclose(*inner.fileout());
                inner.set_fileout(std::ptr::null_mut());
            }
        }
    }

    /// Tests that when `log_sourcelocations` is true, the log line includes
    /// `[source_file:line] [function]`.
    #[traced_test]
    #[serial]
    fn test_log_print_str_with_sourcelocations() {
        info!("Testing that enabling `log_sourcelocations` prepends [file:line] [func] at the start of the line.");

        let mut logger = Logger::default();
        logger.set_print_to_console(false);
        logger.set_print_to_file(false);

        // We want to see source locations in each new line
        logger.set_log_sourcelocations(true);

        // Start => not buffered
        let _ = logger.start_logging();
        logger.log_print_str("HasSourceLoc\n", "func_loc", "source_loc.rs", 300);

        // Retrieve from a callback
        let lines_vec = Arc::new(StdMutex::new(Vec::<String>::new()));
        let lines_clone = lines_vec.clone();
        logger.push_back_callback(move |s: &String| {
            let mut guard = lines_clone.lock().unwrap();
            guard.push(s.clone());
        });

        // Log again => callback sees it
        logger.log_print_str("HasSourceLoc2\n", "func_loc2", "source_loc2.rs", 301);

        // Now we see what the last line looked like
        let locked = lines_vec.lock().unwrap();
        assert_eq!(
            locked.len(),
            1,
            "Should have exactly 1 line from the callback (the second log line)."
        );
        let line = &locked[0];
        debug!("Got line => '{}'", line);

        // Should have [source_loc2.rs:301] [func_loc2] near the start
        assert!(
            line.contains("[source_loc2.rs:301] [func_loc2]"),
            "Must contain the [file:line] [func] prefix"
        );
    }

    #[traced_test]
    #[serial]
    fn test_log_print_str_with_threadnames() {
        info!("Testing log_threadnames => line includes [main] or similar at the start.");

        let mut logger = Logger::default();
        logger.set_print_to_console(false);
        logger.set_print_to_file(false);

        // We want threadnames on, but we also have timestamps on by default.
        // That means the line will start with a timestamp, THEN "[main]".
        logger.set_log_threadnames(true);

        let _ = logger.start_logging();

        // We'll store lines in a callback
        let lines_vec = Arc::new(StdMutex::new(Vec::<String>::new()));
        let lines_clone = lines_vec.clone();
        logger.push_back_callback(move |s: &String| {
            debug!("Threadname callback => got line => '{}'", s);
            let mut guard = lines_clone.lock().unwrap();
            guard.push(s.clone());
        });

        // Log => line must eventually contain "[main]"
        logger.log_print_str("HasThreadName\n", "func_thread", "thread.rs", 400);

        let locked = lines_vec.lock().unwrap();
        assert_eq!(locked.len(), 1, "1 line from callback");
        let line = &locked[0];
        debug!("Threadname line => '{}'", line);

        // ********** CHANGE: We now only check that "[main]" is in the line,
        // because the timestamp usually appears first.
        assert!(
            line.contains("[main]"),
            "Line should contain [main] when log_threadnames=true (timestamp likely precedes it)"
        );
    }

    /// Ensures toggling `log_timestamps` & `log_time_micros` actually affects the output lines.
    #[traced_test]
    #[serial]
    fn test_log_print_str_timestamps() {
        info!("Testing log timestamps & microseconds in log_print_str.");

        let mut logger = Logger::default();
        logger.set_print_to_console(false);
        logger.set_print_to_file(false);

        // By default => log_timestamps=true, log_time_micros=false => RFC3339 seconds
        // We want to ensure we see a timestamp if started_new_line=true
        logger.started_new_line().store(true, std::sync::atomic::Ordering::Relaxed);

        let _ = logger.start_logging();

        // We'll store lines in a callback
        let lines_vec = Arc::new(StdMutex::new(Vec::<String>::new()));
        let lines_clone = lines_vec.clone();
        logger.push_back_callback(move |s: &String| {
            let mut guard = lines_clone.lock().unwrap();
            guard.push(s.clone());
        });

        // Log => must have e.g. "2025-06-02T19:06:10Z Something"
        logger.log_print_str("TimeStampLine\n", "func_stamp", "stamp.rs", 500);

        {
            let locked = lines_vec.lock().unwrap();
            assert_eq!(locked.len(), 1, "Should have exactly 1 line with default timestamps");
            let line = &locked[0];
            debug!("Timestamp line => '{}'", line);
            assert!(
                line.contains("TimeStampLine"),
                "Must contain original message"
            );
            assert!(
                line.contains("T") && line.contains("Z"),
                "Must look like an RFC3339 second-based timestamp at the beginning"
            );
        }

        // Clear lines, now enable microseconds
        {
            let mut locked = lines_vec.lock().unwrap();
            locked.clear();
        }
        logger.set_log_time_micros(true);
        logger.started_new_line().store(true, std::sync::atomic::Ordering::Relaxed);
        logger.log_print_str("MicroSecondLine\n", "func_us", "stamp_us.rs", 501);

        {
            let locked = lines_vec.lock().unwrap();
            assert_eq!(locked.len(), 1, "Should see 1 line with micros");
            let line = &locked[0];
            debug!("Microsecond line => '{}'", line);
            assert!(
                line.contains("MicroSecondLine"),
                "Must contain original message"
            );
            assert!(
                line.matches('.').count() >= 1 && line.contains("Z"),
                "Should have .xxxxxxZ for microseconds"
            );
        }

        // Clear lines, now disable timestamps entirely => returns input string unchanged
        {
            let mut locked = lines_vec.lock().unwrap();
            locked.clear();
        }
        logger.set_log_timestamps(false);
        logger.started_new_line().store(true, std::sync::atomic::Ordering::Relaxed);
        logger.log_print_str("NoTimeStampLine\n", "func_notime", "notime.rs", 502);

        {
            let locked = lines_vec.lock().unwrap();
            assert_eq!(locked.len(), 1, "One line with no timestamp");
            let line = &locked[0];
            debug!("No timestamp line => '{}'", line);
            assert_eq!(
                line,
                "NoTimeStampLine\n",
                "Should match exactly, no prefix at all"
            );
        }
    }

    #[traced_test]
    #[serial]
    fn test_log_print_str_all_in_one() {
        info!("Full coverage test => console + file + callbacks, custom path, single start_logging().");

        let mut logger = Logger::default();
        logger.set_print_to_console(true);
        logger.set_print_to_file(true);

        // Use a unique file name for this specific test
        logger.set_file_path(Box::from(Path::new("test_log_print_str_mock_unique_2.log")));

        // Create a callback that gathers lines
        let collected_lines = Arc::new(StdMutex::new(Vec::<String>::new()));
        let lines_clone = collected_lines.clone();
        logger.push_back_callback(move |text: &String| {
            debug!("CALLBACK => got line: {}", text);
            let mut g = lines_clone.lock().unwrap();
            g.push(text.clone());
        });

        // Call start_logging => opens the file, ends buffering
        let ok = logger.start_logging();
        debug!("start_logging => returned: {}", ok);
        assert!(ok, "start_logging() should return true");

        // Now that logging is active, log something
        logger.log_print_str("LineOne\n", "test_func", "test_file.rs", 111);

        // Check the callback side
        let lines_guard = collected_lines.lock().unwrap();
        debug!("Collected lines => {:?}", *lines_guard);
        assert_eq!(
            lines_guard.len(),
            1,
            "One line in callback after printing LineOne"
        );
        assert!(
            lines_guard[0].contains("LineOne"),
            "LineOne must appear in callback text"
        );

        // Next, read the actual file
        {
            let mut inner = logger.cs().lock();
            debug!(
                "LoggerInner => buffering={}, fileout={:?}",
                *inner.buffering(),
                inner.fileout()
            );
            assert!(
                !inner.fileout().is_null(),
                "Expected fileout != null after start_logging"
            );

            unsafe {
                debug!("FFLUSH & FSEEK => reading 'test_log_print_str_mock_unique_2.log'");
                libc::fflush(*inner.fileout());
                libc::fseek(*inner.fileout(), 0, libc::SEEK_SET);

                let mut buffer = vec![0u8; 512];
                let read_bytes = libc::fread(
                    buffer.as_mut_ptr() as *mut libc::c_void,
                    1,
                    512,
                    *inner.fileout()
                );
                buffer.truncate(read_bytes);

                let contents = String::from_utf8_lossy(&buffer);
                debug!("File contents => '{}'", contents);

                assert!(
                    contents.contains("LineOne"),
                    "File must contain 'LineOne'"
                );

                // Cleanup
                libc::fclose(*inner.fileout());
                inner.set_fileout(std::ptr::null_mut());
            }
        }

        trace!("test_log_print_str_all_in_one => done.");
    }
}