duvc-ctl 2.0.0
USB Video Class Camera Control Library
Loading...
Searching...
No Matches
logging.cpp
Go to the documentation of this file.
1
6#include <chrono>
8#include <iomanip>
9#include <iostream>
10#include <mutex>
11#include <sstream>
12
13namespace duvc {
14
15// Global logging state
16static std::mutex g_log_mutex;
17static LogCallback g_log_callback = nullptr;
19
20const char *to_string(LogLevel level) {
21 switch (level) {
22 case LogLevel::Debug:
23 return "DEBUG";
24 case LogLevel::Info:
25 return "INFO";
27 return "WARNING";
28 case LogLevel::Error:
29 return "ERROR";
31 return "CRITICAL";
32 default:
33 return "UNKNOWN";
34 }
35}
36
41static std::string get_timestamp() {
42 auto now = std::chrono::system_clock::now();
43 auto time_t = std::chrono::system_clock::to_time_t(now);
44 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
45 now.time_since_epoch()) %
46 1000;
47
48 std::ostringstream ss;
49 ss << std::put_time(std::localtime(&time_t), "%Y-%m-%d %H:%M:%S");
50 ss << '.' << std::setfill('0') << std::setw(3) << ms.count();
51 return ss.str();
52}
53
59static void default_log_callback(LogLevel level, const std::string &message) {
60 std::ostringstream ss;
61 ss << "[" << get_timestamp() << "] "
62 << "[" << to_string(level) << "] " << message << std::endl;
63
64 if (level >= LogLevel::Error) {
65 std::cerr << ss.str();
66 std::cerr.flush();
67 } else {
68 std::cout << ss.str();
69 std::cout.flush();
70 }
71}
72
74 std::lock_guard<std::mutex> lock(g_log_mutex);
76}
77
79 std::lock_guard<std::mutex> lock(g_log_mutex);
81}
82
84 std::lock_guard<std::mutex> lock(g_log_mutex);
85 return g_min_log_level;
86}
87
88void log_message(LogLevel level, const std::string &message) {
89 std::lock_guard<std::mutex> lock(g_log_mutex);
90
91 // Check minimum log level
92 if (level < g_min_log_level) {
93 return;
94 }
95
96 // Use callback if set, otherwise use default
97 if (g_log_callback) {
98 try {
99 g_log_callback(level, message);
100 } catch (...) {
101 // If user callback throws, fall back to default
103 "Exception in user log callback - " + message);
104 }
105 } else {
106 default_log_callback(level, message);
107 }
108}
109
110void log_debug(const std::string &message) {
112}
113
114void log_info(const std::string &message) {
115 log_message(LogLevel::Info, message);
116}
117
118void log_warning(const std::string &message) {
120}
121
122void log_error(const std::string &message) {
124}
125
126void log_critical(const std::string &message) {
128}
129
130} // namespace duvc
Result type that can contain either a value or an error.
Definition result.h:75
Structured logging interface for duvc-ctl.
Definition core.h:13
void log_info(const std::string &message)
Log info message.
Definition logging.cpp:114
static std::mutex g_log_mutex
Definition logging.cpp:16
std::function< void(LogLevel level, const std::string &message)> LogCallback
Log message callback type.
Definition logging.h:36
void log_debug(const std::string &message)
Log debug message.
Definition logging.cpp:110
static std::string get_timestamp()
Get current timestamp as string.
Definition logging.cpp:41
LogLevel
Log levels.
Definition logging.h:16
@ Warning
Warning messages.
@ Critical
Critical errors.
@ Info
Informational messages.
@ Error
Error messages.
@ Debug
Debug information.
void log_error(const std::string &message)
Log error message.
Definition logging.cpp:122
void set_log_callback(LogCallback callback)
Set global log callback.
Definition logging.cpp:73
static LogCallback g_log_callback
Definition logging.cpp:17
const char * to_string(CamProp)
Convert camera property enum to string.
Definition core.cpp:678
void log_message(LogLevel level, const std::string &message)
Log a message.
Definition logging.cpp:88
void log_warning(const std::string &message)
Log warning message.
Definition logging.cpp:118
static LogLevel g_min_log_level
Definition logging.cpp:18
static void default_log_callback(LogLevel level, const std::string &message)
Default logging callback that writes to stderr.
Definition logging.cpp:59
void log_critical(const std::string &message)
Log critical message.
Definition logging.cpp:126
void set_log_level(LogLevel level)
Set minimum log level.
Definition logging.cpp:78
LogLevel get_log_level()
Get current minimum log level.
Definition logging.cpp:83