eRPC Generator (erpcgen)  Rev. 1.7.2
NXP Semiconductors
Logging.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * Copyright 2016 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 #if !defined(_Logging_h_)
10 #define _Logging_h_
11 
12 #include <cassert>
13 #include <cstdarg>
14 #include <string>
15 
46 class Logger
47 {
48 public:
51  {
52  kUrgent = 0,
61  };
62 
63 public:
66  : m_filter(kInfo)
67  , m_level(kInfo)
68  {
69  }
70 
72  virtual ~Logger() {}
74 
75  inline void setFilterLevel(log_level_t level) { m_filter = level; }
77 
79  inline log_level_t getFilterLevel() const { return m_filter; }
80 
82  inline void setOutputLevel(log_level_t level) { m_level = level; }
83 
85  inline log_level_t getOutputLevel() const { return m_level; }
87 
89 
90  virtual void log(const char *fmt, ...);
92 
94  virtual void log(const std::string &msg) { log(msg.c_str()); }
95 
97  virtual void log(log_level_t level, const char *fmt, ...);
98 
100  virtual void log(log_level_t level, const std::string &msg) { log(level, msg.c_str()); }
102  virtual void log(const char *fmt, va_list args);
103 
105  virtual void log(log_level_t level, const char *fmt, va_list args);
107 
108 protected:
111 
112 protected:
114  virtual void _log(Logger::log_level_t level, const char *msg) = 0;
115 };
116 
130 class Log
131 {
132 public:
134 
135  static inline Logger *getLogger() { return s_logger; }
137 
139  static inline void setLogger(Logger *logger) { s_logger = logger; }
141 
143 
144  static void log(const char *fmt, ...);
146 
148  static void log(const std::string &msg);
149 
151  static void log(Logger::log_level_t level, const char *fmt, ...);
152 
154  static void log(Logger::log_level_t level, const std::string &msg);
156 
160 
161  static void urgent(const char *fmt, ...);
162  static void error(const char *fmt, ...);
163  static void warning(const char *fmt, ...);
164  static void info(const char *fmt, ...);
165  static void info2(const char *fmt, ...);
166  static void debug(const char *fmt, ...);
167  static void debug2(const char *fmt, ...);
168 
169 
170 protected:
171  static Logger *s_logger;
172 
173 public:
194  {
195  public:
201  : m_logger(Log::getLogger())
202  , m_saved(Logger::kInfo)
203  {
204  assert(m_logger);
205  m_saved = m_logger->getOutputLevel();
206  m_logger->setOutputLevel(level);
207  }
208 
214  : m_logger(logger)
215  , m_saved(logger->getOutputLevel())
216  {
217  assert(m_logger);
218  m_logger->setOutputLevel(level);
219  }
220 
224  ~SetOutputLevel() { m_logger->setOutputLevel(m_saved); }
225 
226  protected:
229  };
230 };
231 
235 class StdoutLogger : public Logger
236 {
237 public:
240  : m_stderrLevel(stderrLevel)
241  {
242  }
243 
244 protected:
247 
249  virtual void _log(Logger::log_level_t level, const char *msg);
250 };
251 
252 #endif // _Logging_h_
For internal reporting.
Definition: Logging.h:57
log_level_t getFilterLevel() const
Returns the current logging filter level.
Definition: Logging.h:79
Highest log level; verbose debug logging.
Definition: Logging.h:58
SetOutputLevel(Logger::log_level_t level)
Default constructor.
Definition: Logging.h:200
log_level_t
Logging levels.
Definition: Logging.h:50
Base logger class.
Definition: Logging.h:46
log_level_t getOutputLevel() const
Returns the current logging output level.
Definition: Logging.h:85
void setOutputLevel(log_level_t level)
Changes the logging output level to level.
Definition: Logging.h:82
static Logger * s_logger
The single global logger instance.
Definition: Logging.h:171
virtual void log(const char *fmt,...)
Log with format.
Definition: Logging.cpp:20
log_level_t m_filter
The current logging filter level.
Definition: Logging.h:109
Alias for kInfo.
Definition: Logging.h:59
SetOutputLevel(Logger *logger, Logger::log_level_t level)
Constructor.
Definition: Logging.h:213
For verbose status messages.
Definition: Logging.h:56
virtual void _log(Logger::log_level_t level, const char *msg)=0
The base pure virtual logging function implemented by subclasses.
~SetOutputLevel()
Destructor.
Definition: Logging.h:224
Logger::log_level_t m_saved
Original logging output level.
Definition: Logging.h:228
virtual void log(const std::string &msg)
Log a string object.
Definition: Logging.h:94
Simple logger that writes to stdout and stderr.
Definition: Logging.h:235
virtual void log(log_level_t level, const std::string &msg)
Log a string output at a specific output level.
Definition: Logging.h:100
Logger::log_level_t m_stderrLevel
Logs at and below this level get sent to stderr.
Definition: Logging.h:246
Alias for kDebug.
Definition: Logging.h:60
For fatal error messages.
Definition: Logging.h:53
For non-fatal warning messages.
Definition: Logging.h:54
Logger * m_logger
The logger instance we&#39;re controlling.
Definition: Logging.h:227
The lowest level, for messages that must always be logged.
Definition: Logging.h:52
static void setLogger(Logger *logger)
Sets the global logger singleton instance.
Definition: Logging.h:139
Wraps a set of static functions for easy global logging access.
Definition: Logging.h:130
The normal log level, for status messages.
Definition: Logging.h:55
void setFilterLevel(log_level_t level)
Changes the logging level to level.
Definition: Logging.h:76
StdoutLogger(Logger::log_level_t stderrLevel=Logger::kWarning)
Default constructor.
Definition: Logging.h:239
Utility class to temporarily change the logging output level.
Definition: Logging.h:193
Logger()
Default constructor.
Definition: Logging.h:65
log_level_t m_level
The current log output level.
Definition: Logging.h:110
virtual ~Logger()
Destructor.
Definition: Logging.h:72