Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Toggle main menu visibility
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
"
19
#include "
roc_core/noncopyable.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
40
namespace
roc
{
41
42
//! Log level.
43
enum
LogLevel
{
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
52
namespace
core {
53
54
//! Colors mode.
55
enum
ColorsMode
{
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.
62
enum
LocationMode
{
63
LocationEnabled
,
//!< Show location.
64
LocationDisabled
//!< Do not show location.
65
};
66
67
//! Log message.
68
struct
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
84
LogMessage()
85
:
level
(
LogNone
)
86
,
module
(NULL)
87
,
file
(NULL)
88
,
line
(0)
89
,
time
(0)
90
,
pid
(0)
91
,
tid
(0)
92
,
text
(NULL)
93
,
location_mode
(
LocationDisabled
)
94
,
colors_mode
(
ColorsDisabled
) {
95
}
96
};
97
98
//! Log handler.
99
typedef
void (*
LogHandler
)(
const
LogMessage
& message,
void
** args);
100
101
//! Logger.
102
class
Logger :
public
NonCopyable<> {
103
public
:
104
//! Get logger instance.
105
static
Logger&
instance
() {
106
return
Singleton<Logger>::instance
();
107
}
108
109
//! Print message to log.
110
ROC_ATTR_PRINTF
(6, 7)
111
void
writef
(
LogLevel
level,
112
const
char
* module,
113
const
char
* file,
114
int
line,
115
const
char
* format,
116
...);
117
118
//! Get current maximum log level.
119
LogLevel
get_level
()
const
{
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.
133
void
set_level
(
LogLevel
);
134
135
//! Set colors mode.
136
//! @note
137
//! Other threads will see the change immediately.
138
void
set_colors
(
ColorsMode
);
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
148
private
:
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_
attributes.h
Compiler attributes.
ROC_ATTR_PRINTF
#define ROC_ATTR_PRINTF(fmt_pos, args_pos)
Function gets printf-like arguments.
Definition
attributes.h:35
roc::core::AtomicOps::load_relaxed
static T load_relaxed(const T &var)
Atomic load (no barrier).
Definition
atomic_ops.h:46
roc::core::LogBackend
Log backend.
Definition
log_backend.h:23
roc::core::Logger
Logger.
Definition
log.h:102
roc::core::Logger::set_handler
void set_handler(LogHandler handler, void **args, size_t n_args)
Set log handler.
roc::core::Logger::get_level
LogLevel get_level() const
Get current maximum log level.
Definition
log.h:119
roc::core::Logger::set_verbosity
void set_verbosity(unsigned)
Set verbosity level.
roc::core::Logger::writef
void writef(LogLevel level, const char *module, const char *file, int line, const char *format,...)
Print message to log.
roc::core::Logger::instance
static Logger & instance()
Get logger instance.
Definition
log.h:105
roc::core::Logger::set_level
void set_level(LogLevel)
Set maximum log level.
roc::core::Logger::set_colors
void set_colors(ColorsMode)
Set colors mode.
roc::core::Mutex
Mutex.
Definition
mutex.h:31
roc::core::Singleton
Singleton.
Definition
singleton.h:26
roc::core::Singleton::instance
static T & instance()
Get singleton instance.
Definition
singleton.h:29
mutex.h
Mutex.
roc::core::LogHandler
void(* LogHandler)(const LogMessage &message, void **args)
Log handler.
Definition
log.h:99
roc::core::ColorsMode
ColorsMode
Colors mode.
Definition
log.h:55
roc::core::ColorsDisabled
@ ColorsDisabled
Do not use colored logs.
Definition
log.h:58
roc::core::ColorsEnabled
@ ColorsEnabled
Use colored logs.
Definition
log.h:57
roc::core::ColorsAuto
@ ColorsAuto
Automatically use colored logs if colors are supported.
Definition
log.h:56
roc::core::LocationMode
LocationMode
Location mode.
Definition
log.h:62
roc::core::LocationDisabled
@ LocationDisabled
Do not show location.
Definition
log.h:64
roc::core::LocationEnabled
@ LocationEnabled
Show location.
Definition
log.h:63
roc::core::nanoseconds_t
int64_t nanoseconds_t
Nanoseconds.
Definition
time.h:58
roc
Root namespace.
roc::LogLevel
LogLevel
Log level.
Definition
log.h:43
roc::LogNote
@ LogNote
Noteworthy debug message.
Definition
log.h:47
roc::LogNone
@ LogNone
Disable all messages.
Definition
log.h:44
roc::LogDebug
@ LogDebug
Regular debug message.
Definition
log.h:48
roc::LogError
@ LogError
Error message.
Definition
log.h:45
roc::LogTrace
@ LogTrace
Extra verbose debug message.
Definition
log.h:49
roc::LogInfo
@ LogInfo
Informational message.
Definition
log.h:46
noncopyable.h
Non-copyable object.
singleton.h
Singleton.
roc::core::LogMessage
Log message.
Definition
log.h:68
roc::core::LogMessage::colors_mode
ColorsMode colors_mode
Whether to enable colors.
Definition
log.h:82
roc::core::LogMessage::module
const char * module
Name of module that originated message.
Definition
log.h:71
roc::core::LogMessage::pid
uint64_t pid
Plaform-specific process ID.
Definition
log.h:76
roc::core::LogMessage::tid
uint64_t tid
Plaform-specific thread ID.
Definition
log.h:77
roc::core::LogMessage::level
LogLevel level
Logging level.
Definition
log.h:69
roc::core::LogMessage::time
nanoseconds_t time
Timestamp, nanoseconds since Unix epoch.
Definition
log.h:75
roc::core::LogMessage::location_mode
LocationMode location_mode
Whether to enable location.
Definition
log.h:81
roc::core::LogMessage::file
const char * file
File path.
Definition
log.h:72
roc::core::LogMessage::line
int line
Line number.
Definition
log.h:73
roc::core::LogMessage::text
const char * text
Message text.
Definition
log.h:79
time.h
Time definitions.
roc_core
log.h
Generated by
1.17.0