Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
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 LogDebug, //!< Debug message.
48 LogTrace //!< Debug message (extra verbosity).
49};
50
51namespace core {
52
53//! Colors mode.
55 ColorsAuto, //!< Automatically use colored logs if colors are supported.
56 ColorsEnabled, //!< Use colored logs.
57 ColorsDisabled, //!< Do not use colored logs.
58};
59
60//! Location mode.
62 LocationEnabled, //!< Show location.
63 LocationDisabled //!< Do not show location.
64};
65
66//! Log message.
67struct LogMessage {
68 LogLevel level; //!< Logging level.
69
70 const char* module; //!< Name of module that originated message.
71 const char* file; //!< File path.
72 int line; //!< Line number.
73
74 nanoseconds_t time; //!< Timestamp, nanoseconds since Unix epoch.
75 uint64_t pid; //!< Plaform-specific process ID.
76 uint64_t tid; //!< Plaform-specific thread ID.
77
78 const char* text; //!< Message text.
79
80 LocationMode location_mode; //!< Whether to enable location.
81 ColorsMode colors_mode; //!< Whether to enable colors.
82
84 : level(LogNone)
85 , module(NULL)
86 , file(NULL)
87 , line(0)
88 , time(0)
89 , pid(0)
90 , tid(0)
91 , text(NULL)
94 }
95};
96
97//! Log handler.
98typedef void (*LogHandler)(const LogMessage& message, void** args);
99
100//! Logger.
101class Logger : public NonCopyable<> {
102public:
103 //! Get logger instance.
104 static Logger& instance() {
106 }
107
108 //! Print message to log.
109 ROC_ATTR_PRINTF(6, 7)
110 void writef(LogLevel level,
111 const char* module,
112 const char* file,
113 int line,
114 const char* format,
115 ...);
116
117 //! Get current maximum log level.
119 return (LogLevel)AtomicOps::load_relaxed(level_);
120 }
121
122 //! Set verbosity level.
123 //! @remarks
124 //! Sets logging level according to requested verbosity level.
125 void set_verbosity(unsigned);
126
127 //! Set maximum log level.
128 //! @remarks
129 //! Messages with higher log level will be dropped.
130 //! @note
131 //! Other threads are not guaranteed to see the change immediately.
133
134 //! Set colors mode.
135 //! @note
136 //! Other threads will see the change immediately.
138
139 //! Set log handler.
140 //! @remarks
141 //! If @p handler is not NULL, log messages and @p arg will be passed to
142 //! @p handler. Otherwise, they're printed to stderr.
143 //! @note
144 //! Other threads will see the change immediately.
145 void set_handler(LogHandler handler, void** args, size_t n_args);
146
147private:
148 friend class Singleton<Logger>;
149
150 enum { MaxArgs = 8 };
151
152 Logger();
153
154 int level_;
155
156 Mutex mutex_;
157
158 LogHandler handler_;
159 void* handler_args_[MaxArgs];
160
161 LogBackend backend_;
162
163 ColorsMode colors_mode_;
164 LocationMode location_mode_;
165};
166
167} // namespace core
168} // namespace roc
169
170#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:101
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:118
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:104
void set_level(LogLevel)
Set maximum log level.
void set_colors(ColorsMode)
Set colors mode.
Mutex.
Definition: mutex.h:31
Base class for non-copyable objects.
Definition: noncopyable.h:23
static T & instance()
Get singleton instance.
Definition: singleton.h:29
Mutex.
void(* LogHandler)(const LogMessage &message, void **args)
Log handler.
Definition: log.h:98
ColorsMode
Colors mode.
Definition: log.h:54
@ ColorsDisabled
Do not use colored logs.
Definition: log.h:57
@ ColorsEnabled
Use colored logs.
Definition: log.h:56
@ ColorsAuto
Automatically use colored logs if colors are supported.
Definition: log.h:55
LocationMode
Location mode.
Definition: log.h:61
@ LocationDisabled
Do not show location.
Definition: log.h:63
@ LocationEnabled
Show location.
Definition: log.h:62
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
LogLevel
Log level.
Definition: log.h:43
@ LogNone
Disable all messages.
Definition: log.h:44
@ LogDebug
Debug message.
Definition: log.h:47
@ LogError
Error message.
Definition: log.h:45
@ LogTrace
Debug message (extra verbosity).
Definition: log.h:48
@ LogInfo
Informational message.
Definition: log.h:46
Non-copyable object.
Singleton.
Log message.
Definition: log.h:67
ColorsMode colors_mode
Whether to enable colors.
Definition: log.h:81
const char * module
Name of module that originated message.
Definition: log.h:70
uint64_t pid
Plaform-specific process ID.
Definition: log.h:75
uint64_t tid
Plaform-specific thread ID.
Definition: log.h:76
LogLevel level
Logging level.
Definition: log.h:68
nanoseconds_t time
Timestamp, nanoseconds since Unix epoch.
Definition: log.h:74
LocationMode location_mode
Whether to enable location.
Definition: log.h:80
const char * file
File path.
Definition: log.h:71
int line
Line number.
Definition: log.h:72
const char * text
Message text.
Definition: log.h:78
Time definitions.