Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/log.h
10//! @brief Logging.
11
12#ifndef ROC_CORE_LOG_H_
13#define ROC_CORE_LOG_H_
14
15#include "roc_core/atomic_ops.h"
16#include "roc_core/attributes.h"
17#include "roc_core/log_backend.h"
18#include "roc_core/mutex.h"
20#include "roc_core/singleton.h"
21#include "roc_core/time.h"
22
23#ifndef ROC_MODULE
24#error "ROC_MODULE not defined"
25#endif
26
27//! Print message to log.
28//! @remarks
29//! If the given log level is disabled, this call does not insert memory barriers
30//! and does not evaluate arguments except @p level.
31#define roc_log(level, ...) \
32 do { \
33 ::roc::core::Logger& logger = ::roc::core::Logger::instance(); \
34 if ((level) <= logger.get_level()) { \
35 logger.writef((level), ROC_STRINGIZE(ROC_MODULE), __FILE__, __LINE__, \
36 __VA_ARGS__); \
37 } \
38 } while (0)
39
40namespace roc {
41
42//! Log level.
44 LogNone, //!< Disable all messages.
45 LogError, //!< Error message.
46 LogInfo, //!< Informational message.
47 LogNote, //!< Noteworthy debug message.
48 LogDebug, //!< Regular debug message.
49 LogTrace //!< Extra verbose debug message.
50};
51
52namespace core {
53
54//! Colors mode.
56 ColorsAuto, //!< Automatically use colored logs if colors are supported.
57 ColorsEnabled, //!< Use colored logs.
58 ColorsDisabled, //!< Do not use colored logs.
59};
60
61//! Location mode.
63 LocationEnabled, //!< Show location.
64 LocationDisabled //!< Do not show location.
65};
66
67//! Log message.
68struct LogMessage {
69 LogLevel level; //!< Logging level.
70
71 const char* module; //!< Name of module that originated message.
72 const char* file; //!< File path.
73 int line; //!< Line number.
74
75 nanoseconds_t time; //!< Timestamp, nanoseconds since Unix epoch.
76 uint64_t pid; //!< Plaform-specific process ID.
77 uint64_t tid; //!< Plaform-specific thread ID.
78
79 const char* text; //!< Message text.
80
81 LocationMode location_mode; //!< Whether to enable location.
82 ColorsMode colors_mode; //!< Whether to enable colors.
83
85 : level(LogNone)
86 , module(NULL)
87 , file(NULL)
88 , line(0)
89 , time(0)
90 , pid(0)
91 , tid(0)
92 , text(NULL)
95 }
96};
97
98//! Log handler.
99typedef void (*LogHandler)(const LogMessage& message, void** args);
100
101//! Logger.
102class Logger : public NonCopyable<> {
103public:
104 //! Get logger instance.
105 static Logger& instance() {
107 }
108
109 //! Print message to log.
110 ROC_ATTR_PRINTF(6, 7)
112 const char* module,
113 const char* file,
114 int line,
115 const char* format,
116 ...);
117
118 //! Get current maximum log level.
120 return (LogLevel)AtomicOps::load_relaxed(level_);
121 }
122
123 //! Set verbosity level.
124 //! @remarks
125 //! Sets logging level according to requested verbosity level.
126 void set_verbosity(unsigned);
127
128 //! Set maximum log level.
129 //! @remarks
130 //! Messages with higher log level will be dropped.
131 //! @note
132 //! Other threads are not guaranteed to see the change immediately.
134
135 //! Set colors mode.
136 //! @note
137 //! Other threads will see the change immediately.
139
140 //! Set log handler.
141 //! @remarks
142 //! If @p handler is not NULL, log messages and @p arg will be passed to
143 //! @p handler. Otherwise, they're printed to stderr.
144 //! @note
145 //! Other threads will see the change immediately.
146 void set_handler(LogHandler handler, void** args, size_t n_args);
147
148private:
149 friend class Singleton<Logger>;
150
151 enum { MaxArgs = 8 };
152
153 Logger();
154
155 int level_;
156
157 Mutex mutex_;
158
159 LogHandler handler_;
160 void* handler_args_[MaxArgs];
161
162 LogBackend backend_;
163
164 ColorsMode colors_mode_;
165 LocationMode location_mode_;
166};
167
168} // namespace core
169} // namespace roc
170
171#endif // ROC_CORE_LOG_H_
Compiler attributes.
#define ROC_ATTR_PRINTF(fmt_pos, args_pos)
Function gets printf-like arguments.
Definition attributes.h:35
static T load_relaxed(const T &var)
Atomic load (no barrier).
Definition atomic_ops.h:46
Logger.
Definition log.h:102
void set_handler(LogHandler handler, void **args, size_t n_args)
Set log handler.
LogLevel get_level() const
Get current maximum log level.
Definition log.h:119
void set_verbosity(unsigned)
Set verbosity level.
void writef(LogLevel level, const char *module, const char *file, int line, const char *format,...)
Print message to log.
static Logger & instance()
Get logger instance.
Definition log.h:105
void set_level(LogLevel)
Set maximum log level.
void set_colors(ColorsMode)
Set colors mode.
Base class for non-copyable objects.
Definition noncopyable.h:23
Shared ownership intrusive pointer.
Definition shared_ptr.h:32
static T & instance()
Get singleton instance.
Definition singleton.h:29
Mutex.
void(* LogHandler)(const LogMessage &message, void **args)
Log handler.
Definition log.h:99
ColorsMode
Colors mode.
Definition log.h:55
@ ColorsDisabled
Do not use colored logs.
Definition log.h:58
@ ColorsEnabled
Use colored logs.
Definition log.h:57
@ ColorsAuto
Automatically use colored logs if colors are supported.
Definition log.h:56
LocationMode
Location mode.
Definition log.h:62
@ LocationDisabled
Do not show location.
Definition log.h:64
@ LocationEnabled
Show location.
Definition log.h:63
int64_t nanoseconds_t
Nanoseconds.
Definition time.h:58
Root namespace.
LogLevel
Log level.
Definition log.h:43
@ LogNote
Noteworthy debug message.
Definition log.h:47
@ LogNone
Disable all messages.
Definition log.h:44
@ LogDebug
Regular debug message.
Definition log.h:48
@ LogError
Error message.
Definition log.h:45
@ LogTrace
Extra verbose debug message.
Definition log.h:49
@ LogInfo
Informational message.
Definition log.h:46
Non-copyable object.
Singleton.
Log message.
Definition log.h:68
ColorsMode colors_mode
Whether to enable colors.
Definition log.h:82
uint64_t pid
Plaform-specific process ID.
Definition log.h:76
uint64_t tid
Plaform-specific thread ID.
Definition log.h:77
LogLevel level
Logging level.
Definition log.h:69
const char *const char * file
< Name of module that originated message.
Definition log.h:72
nanoseconds_t time
Timestamp, nanoseconds since Unix epoch.
Definition log.h:75
LocationMode location_mode
Whether to enable location.
Definition log.h:81
int line
Line number.
Definition log.h:73
const char * text
Message text.
Definition log.h:79
Time definitions.