eRPC Generator (erpcgen)  Rev. 1.7.0
NXP Semiconductors
Logging.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
4  * Copyright 2016 NXP
5  * All rights reserved.
6  *
7  *
8  * Redistribution and use in source and binary forms, with or without modification,
9  * are permitted (subject to the limitations in the disclaimer below) provided
10  * that the following conditions are met:
11  *
12  * o Redistributions of source code must retain the above copyright notice, this list
13  * of conditions and the following disclaimer.
14  *
15  * o Redistributions in binary form must reproduce the above copyright notice, this
16  * list of conditions and the following disclaimer in the documentation and/or
17  * other materials provided with the distribution.
18  *
19  * o Neither the name of the copyright holder nor the names of its
20  * contributors may be used to endorse or promote products derived from this
21  * software without specific prior written permission.
22  *
23  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
28  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 #if !defined(_Logging_h_)
36 #define _Logging_h_
37 
38 #include <cassert>
39 #include <cstdarg>
40 #include <string>
41 
72 class Logger
73 {
74 public:
77  {
78  kUrgent = 0,
87  };
88 
89 public:
92  : m_filter(kInfo)
93  , m_level(kInfo)
94  {
95  }
96 
98  virtual ~Logger() {}
100 
101  inline void setFilterLevel(log_level_t level) { m_filter = level; }
103 
105  inline log_level_t getFilterLevel() const { return m_filter; }
106 
108  inline void setOutputLevel(log_level_t level) { m_level = level; }
109 
111  inline log_level_t getOutputLevel() const { return m_level; }
113 
115 
116  virtual void log(const char *fmt, ...);
118 
120  virtual void log(const std::string &msg) { log(msg.c_str()); }
121 
123  virtual void log(log_level_t level, const char *fmt, ...);
124 
126  virtual void log(log_level_t level, const std::string &msg) { log(level, msg.c_str()); }
128  virtual void log(const char *fmt, va_list args);
129 
131  virtual void log(log_level_t level, const char *fmt, va_list args);
133 
134 protected:
137 
138 protected:
140  virtual void _log(Logger::log_level_t level, const char *msg) = 0;
141 };
142 
156 class Log
157 {
158 public:
160 
161  static inline Logger *getLogger() { return s_logger; }
163 
165  static inline void setLogger(Logger *logger) { s_logger = logger; }
167 
169 
170  static void log(const char *fmt, ...);
172 
174  static void log(const std::string &msg);
175 
177  static void log(Logger::log_level_t level, const char *fmt, ...);
178 
180  static void log(Logger::log_level_t level, const std::string &msg);
182 
186 
187  static void urgent(const char *fmt, ...);
188  static void error(const char *fmt, ...);
189  static void warning(const char *fmt, ...);
190  static void info(const char *fmt, ...);
191  static void info2(const char *fmt, ...);
192  static void debug(const char *fmt, ...);
193  static void debug2(const char *fmt, ...);
194 
195 
196 protected:
197  static Logger *s_logger;
198 
199 public:
220  {
221  public:
227  : m_logger(Log::getLogger())
228  , m_saved(Logger::kInfo)
229  {
230  assert(m_logger);
231  m_saved = m_logger->getOutputLevel();
232  m_logger->setOutputLevel(level);
233  }
234 
240  : m_logger(logger)
241  , m_saved(logger->getOutputLevel())
242  {
243  assert(m_logger);
244  m_logger->setOutputLevel(level);
245  }
246 
250  ~SetOutputLevel() { m_logger->setOutputLevel(m_saved); }
251 
252  protected:
255  };
256 };
257 
261 class StdoutLogger : public Logger
262 {
263 public:
266  : m_stderrLevel(stderrLevel)
267  {
268  }
269 
270 protected:
273 
275  virtual void _log(Logger::log_level_t level, const char *msg);
276 };
277 
278 #endif // _Logging_h_
For internal reporting.
Definition: Logging.h:83
log_level_t getFilterLevel() const
Returns the current logging filter level.
Definition: Logging.h:105
Highest log level; verbose debug logging.
Definition: Logging.h:84
SetOutputLevel(Logger::log_level_t level)
Default constructor.
Definition: Logging.h:226
log_level_t
Logging levels.
Definition: Logging.h:76
Base logger class.
Definition: Logging.h:72
log_level_t getOutputLevel() const
Returns the current logging output level.
Definition: Logging.h:111
void setOutputLevel(log_level_t level)
Changes the logging output level to level.
Definition: Logging.h:108
static Logger * s_logger
The single global logger instance.
Definition: Logging.h:197
virtual void log(const char *fmt,...)
Log with format.
Definition: Logging.cpp:46
log_level_t m_filter
The current logging filter level.
Definition: Logging.h:135
Alias for kInfo.
Definition: Logging.h:85
SetOutputLevel(Logger *logger, Logger::log_level_t level)
Constructor.
Definition: Logging.h:239
For verbose status messages.
Definition: Logging.h:82
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:250
Logger::log_level_t m_saved
Original logging output level.
Definition: Logging.h:254
virtual void log(const std::string &msg)
Log a string object.
Definition: Logging.h:120
Simple logger that writes to stdout and stderr.
Definition: Logging.h:261
virtual void log(log_level_t level, const std::string &msg)
Log a string output at a specific output level.
Definition: Logging.h:126
Logger::log_level_t m_stderrLevel
Logs at and below this level get sent to stderr.
Definition: Logging.h:272
Alias for kDebug.
Definition: Logging.h:86
For fatal error messages.
Definition: Logging.h:79
For non-fatal warning messages.
Definition: Logging.h:80
Logger * m_logger
The logger instance we&#39;re controlling.
Definition: Logging.h:253
The lowest level, for messages that must always be logged.
Definition: Logging.h:78
static void setLogger(Logger *logger)
Sets the global logger singleton instance.
Definition: Logging.h:165
Wraps a set of static functions for easy global logging access.
Definition: Logging.h:156
The normal log level, for status messages.
Definition: Logging.h:81
void setFilterLevel(log_level_t level)
Changes the logging level to level.
Definition: Logging.h:102
StdoutLogger(Logger::log_level_t stderrLevel=Logger::kWarning)
Default constructor.
Definition: Logging.h:265
Utility class to temporarily change the logging output level.
Definition: Logging.h:219
Logger()
Default constructor.
Definition: Logging.h:91
log_level_t m_level
The current log output level.
Definition: Logging.h:136
virtual ~Logger()
Destructor.
Definition: Logging.h:98