A.R.G.U.S 2.1.0
Adaptive Real-Time Guardian for Unsafe Situations
Loading...
Searching...
No Matches
MetricsLogger.cpp
Go to the documentation of this file.
1
6#include "MetricsLogger.hpp"
7#include <iostream>
8
9MetricsLogger::MetricsLogger() : running(true) {
10 // Open log file
11 logFile.open("guardian_log.txt", std::ios::out | std::ios::app);
12
13 if (!logFile.is_open()) {
14 std::cerr << "Warning: Could not open log file, logging to console only\n";
15 }
16
17 // Start background logging thread
18 logThread = std::thread(&MetricsLogger::processLogs, this);
19}
20
22 // Signal thread to stop
23 running = false;
24 queueCV.notify_one();
25
26 // Wait for thread to finish
27 if (logThread.joinable()) {
28 logThread.join();
29 }
30
31 // Close log file
32 if (logFile.is_open()) {
33 logFile.close();
34 }
35}
36
38 static MetricsLogger instance;
39 return instance;
40}
41
42std::string MetricsLogger::getCurrentTimestamp() {
43 auto now = std::chrono::system_clock::now();
44 auto time_t_now = std::chrono::system_clock::to_time_t(now);
45 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
46 now.time_since_epoch()) % 1000;
47
48 std::stringstream ss;
49 ss << std::put_time(std::localtime(&time_t_now), "%Y-%m-%d %H:%M:%S");
50 ss << '.' << std::setfill('0') << std::setw(3) << ms.count();
51 return ss.str();
52}
53
54void MetricsLogger::logEvent(const std::string& category, const std::string& message) {
55 LogEntry entry;
56 entry.timestamp = getCurrentTimestamp();
57 entry.category = category;
58 entry.message = message;
59
60 // Lock only for queue insertion (minimal critical section)
61 {
62 std::lock_guard<std::mutex> lock(queueMutex);
63 logQueue.push(entry);
64 }
65
66 // Notify background thread
67 queueCV.notify_one();
68}
69
70void MetricsLogger::processLogs() {
71 while (running) {
72 std::unique_lock<std::mutex> lock(queueMutex);
73
74 // Wait for logs or shutdown signal
75 queueCV.wait(lock, [this] { return !logQueue.empty() || !running; });
76
77 // Process all queued logs
78 while (!logQueue.empty()) {
79 LogEntry entry = logQueue.front();
80 logQueue.pop();
81
82 // Unlock while doing I/O (don't block producers)
83 lock.unlock();
84
85 // Format and write log
86 std::string logLine = "[" + entry.timestamp + "] " +
87 entry.category + ": " + entry.message;
88
89 // Write to console
90 std::cout << logLine << '\n';
91
92 // Write to file
93 if (logFile.is_open()) {
94 logFile << logLine << '\n';
95 }
96
97 // Re-lock for next iteration
98 lock.lock();
99 }
100
101 queueCV.notify_all();
102
103 // Flush file periodically
104 if (logFile.is_open()) {
105 logFile.flush();
106 }
107 }
108}
109
111 std::unique_lock<std::mutex> lock(queueMutex);
112 queueCV.wait(lock, [this] { return logQueue.empty(); });
113 lock.unlock();
114
115 if (logFile.is_open()) {
116 logFile.flush();
117 }
118}
Thread-safe, non-blocking logging system for real-time applications.
Singleton logger with non-blocking, thread-safe operation.
~MetricsLogger()
Destructor - stops logging thread.
static MetricsLogger & getInstance()
Gets singleton instance.
void flush()
Flushes all pending logs (blocking - use only at shutdown)
void logEvent(const std::string &category, const std::string &message)
Logs an event (non-blocking, thread-safe)