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 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/target_stdio/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/attributes.h"
16 #include "roc_core/mutex.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_core/singleton.h"
19 
20 #ifndef ROC_MODULE
21 #error "ROC_MODULE not defined"
22 #endif
23 
24 //! Print message to log.
25 #define roc_log(...) \
26  ::roc::core::Logger::instance().print(ROC_STRINGIZE(ROC_MODULE), __VA_ARGS__)
27 
28 namespace roc {
29 
30 //! Log level.
31 enum LogLevel {
32  LogNone, //!< Disable all messages.
33  LogError, //!< Error message.
34  LogInfo, //!< Informational message.
35  LogDebug, //!< Debug message.
36  LogTrace //!< Debug message (extra verbosity).
37 };
38 
39 namespace core {
40 
41 //! Default log level.
43 
44 //! Colors mode.
45 enum ColorsMode {
46  ColorsDisabled, //!< Do not use colored logs.
47  ColorsEnabled //!< Use colored logs.
48 };
49 
50 //! Default colors mode.
52 
53 //! Log handler.
54 typedef void (*LogHandler)(LogLevel level, const char* module, const char* message);
55 
56 //! Logger.
57 class Logger : public NonCopyable<> {
58 public:
59  //! Get logger instance.
60  static Logger& instance() {
62  }
63 
64  //! Print message to log.
65  void print(const char* module, LogLevel level, const char* format, ...)
66  ROC_ATTR_PRINTF(4, 5);
67 
68  //! Get current maximum log level.
69  LogLevel level();
70 
71  //! Set maximum log level.
72  //!
73  //! @remarks
74  //! Messages with higher log level will be dropped.
75  //!
76  //! @note
77  //! Default log level is LogError.
78  void set_level(LogLevel);
79 
80  //! Set log handler.
81  //!
82  //! @remarks
83  //! If @p handler is not NULL, log messages will be passed to @p handler.
84  //! Otherwise, they're printed to stderr.Default log handler is NULL.
85  void set_handler(LogHandler handler);
86 
87  //! Set colors mode.
88  //!
89  //! @note
90  //! Default colors mode is ColorsAuto.
91  void set_colors(ColorsMode mode);
92 
93 private:
94  friend class Singleton<Logger>;
95 
96  Logger();
97 
98  Mutex mutex_;
99 
100  LogLevel level_;
101  LogHandler handler_;
102  ColorsMode colors_;
103 };
104 
105 } // namespace core
106 } // namespace roc
107 
108 #endif // ROC_CORE_LOG_H_
Singleton.
Logger.
Definition: log.h:57
Do not use colored logs.
Definition: log.h:46
void print(const char *module, LogLevel level, const char *format,...) ROC_ATTR_PRINTF(4
Print message to log.
void set_handler(LogHandler handler)
Set log handler.
LogLevel
Log level.
Definition: log.h:31
GCC attributes.
Disable all messages.
Definition: log.h:32
Use colored logs.
Definition: log.h:47
Root namespace.
ColorsMode
Colors mode.
Definition: log.h:45
static Logger & instance()
Get logger instance.
Definition: log.h:60
Mutex.
Definition: mutex.h:27
void LogLevel level()
Get current maximum log level.
Debug message (extra verbosity).
Definition: log.h:36
#define ROC_ATTR_PRINTF(n_fmt_arg, n_var_arg)
Function gets printf-like arguments.
Definition: attributes.h:28
Singleton.
Definition: singleton.h:26
Base class for non-copyable objects.
Definition: noncopyable.h:23
Mutex.
Informational message.
Definition: log.h:34
const ColorsMode DefaultColorsMode
Default colors mode.
Definition: log.h:51
Error message.
Definition: log.h:33
Debug message.
Definition: log.h:35
Non-copyable object.
void set_colors(ColorsMode mode)
Set colors mode.
void set_level(LogLevel)
Set maximum log level.
static T & instance()
Get singleton instance.
Definition: singleton.h:29
const LogLevel DefaultLogLevel
Default log level.
Definition: log.h:42
void(* LogHandler)(LogLevel level, const char *module, const char *message)
Log handler.
Definition: log.h:54