Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
debug.h
Go to the documentation of this file.
1#ifndef DEBUG_H
2#define DEBUG_H
3
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <memory>
8#include <pthread.h>
9#include <string>
10#include <chrono>
11#include <iomanip>
12#include <sys/syscall.h>
13#include <unistd.h>
14#include "api/traits.h"
15
16class Debug
17{
18 public:
19 Debug() : _message_started(false) {}
21 // Flush any remaining content when object is destroyed
22 if (_message_started) {
23 flush_buffer();
24 }
25 }
26
27 template<typename T>
29 get_buffer() << p;
30 return *this;
31 }
32
33 struct Begl {};
34 struct Err {};
35
36 Debug & operator<<(const Begl & begl) {
37 // Start a new log entry - flush any existing buffer and prepare for new message
38 if (_message_started) {
39 flush_buffer();
40 }
41 _message_started = true;
42 get_buffer() << get_timestamp_and_thread() << " ";
43 return *this;
44 }
45
46 Debug & operator<<(const Err & err) {
47 get_buffer() << "[ERROR] ";
48 return *this;
49 }
50
51 // Flush buffer when newline is encountered
52 Debug & operator<<(const char* str) {
53 std::string s(str);
54 get_buffer() << s;
55 if (s.find('\n') != std::string::npos) {
56 flush_buffer();
57 _message_started = false;
58 }
59 return *this;
60 }
61
62 static void set_log_file(const std::string &filename) {
63 pthread_mutex_lock(&_global_mutex);
64 _file_stream = std::make_unique<std::ofstream>(filename, std::ios::out);
65 if (!_file_stream->is_open()) {
66 std::cerr << "Erro ao abrir arquivo de log: " << filename << std::endl;
67 _stream = &std::cout; // Fallback to cout
68 } else {
69 _stream = _file_stream.get();
70 }
71 pthread_mutex_unlock(&_global_mutex);
72 }
73
74 static void close_log_file() {
75 pthread_mutex_lock(&_global_mutex);
76 if (_file_stream && _file_stream->is_open()) {
77 _file_stream->close();
78 _stream = &std::cout; // Fallback to cout
79 }
80 pthread_mutex_unlock(&_global_mutex);
81 }
82
83 static Debug & instance() {
84 static Debug debug;
85 return debug;
86 }
87
88 static void init() {
89 pthread_mutex_init(&_global_mutex, nullptr);
90 }
91
92 static void cleanup() {
93 pthread_mutex_destroy(&_global_mutex);
94 }
95
96 private:
97 void flush_buffer() {
98 std::string content = get_buffer().str();
99 if (!content.empty()) {
100 pthread_mutex_lock(&_global_mutex);
101 if (_stream) {
102 (*_stream) << content << std::flush;
103 }
104 pthread_mutex_unlock(&_global_mutex);
105 get_buffer().str(""); // Clear buffer
106 get_buffer().clear(); // Clear error flags
107 }
108 }
109
110 static std::ostringstream& get_buffer() {
111 thread_local std::ostringstream buffer;
112 return buffer;
113 }
114
115 static std::string get_timestamp_and_thread() {
116 auto now = std::chrono::system_clock::now();
117 auto time_t = std::chrono::system_clock::to_time_t(now);
118 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
119 now.time_since_epoch()) % 1000;
120
121 std::ostringstream oss;
122 oss << "[" << std::put_time(std::localtime(&time_t), "%H:%M:%S");
123 oss << "." << std::setfill('0') << std::setw(3) << ms.count();
124 oss << " T" << syscall(SYS_gettid) << "]";
125 return oss.str();
126 }
127
128 static inline std::unique_ptr<std::ofstream> _file_stream;
129 static inline std::ostream* _stream = &std::cout;
130 static inline pthread_mutex_t _global_mutex = PTHREAD_MUTEX_INITIALIZER;
131
132 bool _message_started; // Track if a message has been started
133
134 public:
135 static inline Begl begl;
136 static inline Err error;
137};
138
140{
141public:
142 template<typename T>
143 Null_Debug & operator<<(const T & o) { return *this; }
144
145 template<typename T>
146 Null_Debug & operator<<(const T * o) { return *this; }
147
148 Null_Debug & operator<<(const char* str) { return *this; }
149
150 struct Begl {};
151 struct Err {};
152 Null_Debug & operator<<(const Begl & begl) { return *this; }
153 Null_Debug & operator<<(const Err & err) { return *this; }
154};
155
156template<bool debugged>
157class Select_Debug: public Debug {};
158template<>
159class Select_Debug<false>: public Null_Debug {};
160
161// Error
162enum Debug_Error {ERR = 1};
163
164template<typename T>
173
174template<typename T1, typename T2>
183
184// Warning
186
187template<typename T>
196
197template<typename T1, typename T2>
206
207// Info
208enum Debug_Info {INF = 3};
209
210template<typename T>
219
220template<typename T1, typename T2>
229
230// Trace
231enum Debug_Trace {TRC = 4};
232
233template<typename T>
242
243template<typename T1, typename T2>
252
253// Call this at program start
255public:
262};
263
264static DebugInitializer debugInitializer;
265
266#endif // DEBUG_H
Definition debug.h:254
~DebugInitializer()
Definition debug.h:259
DebugInitializer()
Definition debug.h:256
Definition debug.h:17
static void cleanup()
Definition debug.h:92
static Err error
Definition debug.h:136
Debug()
Definition debug.h:19
static Debug & instance()
Definition debug.h:83
Debug & operator<<(const Err &err)
Definition debug.h:46
static void init()
Definition debug.h:88
~Debug()
Definition debug.h:20
Debug & operator<<(T p)
Definition debug.h:28
static void set_log_file(const std::string &filename)
Definition debug.h:62
static Begl begl
Definition debug.h:135
static void close_log_file()
Definition debug.h:74
Debug & operator<<(const char *str)
Definition debug.h:52
Debug & operator<<(const Begl &begl)
Definition debug.h:36
Definition debug.h:140
Null_Debug & operator<<(const char *str)
Definition debug.h:148
Null_Debug & operator<<(const Begl &begl)
Definition debug.h:152
Null_Debug & operator<<(const T &o)
Definition debug.h:143
Null_Debug & operator<<(const T *o)
Definition debug.h:146
Null_Debug & operator<<(const Err &err)
Definition debug.h:153
Definition debug.h:157
Debug_Info
Definition debug.h:208
@ INF
Definition debug.h:208
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
Debug_Error
Definition debug.h:162
@ ERR
Definition debug.h:162
Debug_Trace
Definition debug.h:231
@ TRC
Definition debug.h:231
Debug_Warning
Definition debug.h:185
@ WRN
Definition debug.h:185
Definition debug.h:33
Definition debug.h:34
Definition debug.h:150
Definition debug.h:151
Definition traits.h:45