rogue
Loading...
Searching...
No Matches
Logging.h
Go to the documentation of this file.
1
17#ifndef __ROGUE_LOGGING_H__
18#define __ROGUE_LOGGING_H__
19#include "rogue/Directives.h"
20
21#include <stdint.h>
22
23#include <atomic>
24#include <exception>
25#include <memory>
26#include <mutex>
27#include <string>
28#include <thread>
29#include <vector>
30
31namespace rogue {
32
34class LogFilter {
35 public:
37 std::string name_;
39 uint32_t level_;
40
46 LogFilter(std::string name, uint32_t level) {
47 name_ = name;
48 level_ = level;
49 }
50};
51
59class Logging {
60 // Global logging level
61 static uint32_t gblLevel_;
62
63 // Logging-level lock
64 static std::mutex levelMtx_;
65
66 // Name/level filters
67 static std::vector<rogue::LogFilter*> filters_;
68
69 // Active logger instances
70 static std::vector<rogue::Logging*> loggers_;
71
72 // Optional forwarding of Rogue C++ logs into Python logging
73 static bool forwardPython_;
74
75 // Optional emission of Rogue C++ logs to stdout
76 static bool emitStdout_;
77
78 void intLog(uint32_t level, const char* format, va_list args);
79
80 void updateLevelLocked();
81
82 // Local logger level
83 std::atomic<uint32_t> level_;
84
85 // Logger name
86 std::string name_;
87
88 public:
90 static const uint32_t Critical = 50;
92 static const uint32_t Error = 40;
94 static const uint32_t Thread = 35;
96 static const uint32_t Warning = 30;
98 static const uint32_t Info = 20;
100 static const uint32_t Debug = 10;
101
116 static std::shared_ptr<rogue::Logging> create(const std::string& name, bool quiet = false);
117
128 explicit Logging(const std::string& name, bool quiet = false);
130 ~Logging();
131
136 static void setLevel(uint32_t level);
137
143 static void setFilter(const std::string& filter, uint32_t level);
144
150 static void setForwardPython(bool enable);
151
155 static bool forwardPython();
156
162 static void setEmitStdout(bool enable);
163
167 static bool emitStdout();
168
174 static std::string normalizeName(const std::string& name);
175
181 void log(uint32_t level, const char* fmt, ...);
182
184 void critical(const char* fmt, ...);
186 void error(const char* fmt, ...);
188 void warning(const char* fmt, ...);
190 void info(const char* fmt, ...);
192 void debug(const char* fmt, ...);
193
195 void logThreadId();
196
198 const std::string& name() const;
199
201 static void setup_python();
202};
203
205typedef std::shared_ptr<rogue::Logging> LoggingPtr;
206} // namespace rogue
207
208#endif
Per-logger level override filter entry.
Definition Logging.h:34
LogFilter(std::string name, uint32_t level)
Constructs a filter entry.
Definition Logging.h:46
uint32_t level_
Minimum enabled level for matching names.
Definition Logging.h:39
std::string name_
Logger name prefix to match.
Definition Logging.h:37
Structured Rogue logging helper.
Definition Logging.h:59
~Logging()
Destroys the logger instance.
Definition Logging.cpp:117
void info(const char *fmt,...)
Emits a formatted message at Info level.
Definition Logging.cpp:287
static const uint32_t Thread
Thread-trace severity level constant.
Definition Logging.h:94
static void setEmitStdout(bool enable)
Enables or disables direct stdout emission of Rogue C++ logs.
Definition Logging.cpp:180
static bool forwardPython()
Returns whether Rogue C++ logs are currently forwarded to Python logging.
Definition Logging.cpp:172
void logThreadId()
Emits the current thread id through this logger.
Definition Logging.cpp:301
static void setup_python()
Registers Python bindings for Logging.
Definition Logging.cpp:310
static std::shared_ptr< rogue::Logging > create(const std::string &name, bool quiet=false)
Creates a logger instance.
Definition Logging.cpp:95
static void setForwardPython(bool enable)
Enables or disables forwarding Rogue C++ logs into Python logging.
Definition Logging.cpp:166
static bool emitStdout()
Returns whether Rogue C++ logs are currently emitted to stdout.
Definition Logging.cpp:186
static void setFilter(const std::string &filter, uint32_t level)
Sets name-based filter level override.
Definition Logging.cpp:152
void warning(const char *fmt,...)
Emits a formatted message at Warning level.
Definition Logging.cpp:280
const std::string & name() const
Returns the fully-qualified emitted logger name.
Definition Logging.cpp:306
static const uint32_t Debug
Debug severity level constant.
Definition Logging.h:100
void log(uint32_t level, const char *fmt,...)
Emits a formatted log message at a specified level.
Definition Logging.cpp:259
static const uint32_t Error
Error severity level constant.
Definition Logging.h:92
static const uint32_t Info
Informational severity level constant.
Definition Logging.h:98
static std::string normalizeName(const std::string &name)
Normalizes logger names to the emitted Rogue namespace.
Definition Logging.cpp:100
static const uint32_t Warning
Warning severity level constant.
Definition Logging.h:96
static void setLevel(uint32_t level)
Sets the global default logging level.
Definition Logging.cpp:143
void critical(const char *fmt,...)
Emits a formatted message at Critical level.
Definition Logging.cpp:266
void debug(const char *fmt,...)
Emits a formatted message at Debug level.
Definition Logging.cpp:294
static const uint32_t Critical
Critical severity level constant.
Definition Logging.h:90
void error(const char *fmt,...)
Emits a formatted message at Error level.
Definition Logging.cpp:273
std::shared_ptr< rogue::Logging > LoggingPtr
Shared pointer alias for Logging.
Definition Logging.h:205