Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/Logging.h
1 /*
2  * Copyright (c) 2013-14, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #if !defined(_Logging_h_)
31 #define _Logging_h_
32 
33 #include <string>
34 #include <assert.h>
35 #include <stdarg.h>
36 #include <fstream>
37 
40 
71 class Logger
72 {
73 public:
76  {
77  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; }
104  inline log_level_t getFilterLevel() const { return m_filter; }
106  inline void setOutputLevel(log_level_t level) { m_level = level; }
108  inline log_level_t getOutputLevel() const { return m_level; }
110 
112 
113  virtual void log(const char *fmt, ...);
115 
117  virtual void log(const std::string &msg) { log(msg.c_str()); }
119  virtual void log(log_level_t level, const char *fmt, ...);
120 
122  virtual void log(log_level_t level, const std::string &msg) { log(level, msg.c_str()); }
124  virtual void log(const char *fmt, va_list args);
125 
127  virtual void log(log_level_t level, const char *fmt, va_list args);
129 
130 protected:
133 
134 protected:
136  virtual void _log(const char *msg) = 0;
137 };
138 
152 class Log
153 {
154 public:
156 
157  static inline Logger *getLogger() { return s_logger; }
160  static inline void setLogger(Logger *logger) { s_logger = logger; }
162 
164 
165  static void log(const char *fmt, ...);
167 
169  static void log(const std::string &msg);
170 
172  static void log(Logger::log_level_t level, const char *fmt, ...);
173 
175  static void log(Logger::log_level_t level, const std::string &msg);
177 
181 
182  static void urgent(const char *fmt, ...);
183  static void json(const char *fmt, ...);
184  static void error(const char *fmt, ...);
185  static void warning(const char *fmt, ...);
186  static void info(const char *fmt, ...);
187  static void info2(const char *fmt, ...);
188  static void debug(const char *fmt, ...);
189  static void debug2(const char *fmt, ...);
190 
191 
192 protected:
193  static Logger *s_logger;
194 
195 public:
215  class SetOutputLevel
216  {
217  public:
223  : m_logger(Log::getLogger())
224  , m_saved(Logger::kInfo)
225  {
226  assert(m_logger);
227  m_saved = m_logger->getOutputLevel();
228  m_logger->setOutputLevel(level);
229  }
230 
236  : m_logger(logger)
237  , m_saved(logger->getOutputLevel())
238  {
239  assert(m_logger);
240  m_logger->setOutputLevel(level);
241  }
242 
246  ~SetOutputLevel() { m_logger->setOutputLevel(m_saved); }
247  protected:
248  Logger *m_logger;
249  Logger::log_level_t m_saved;
250  };
251 };
252 
256 class StdoutLogger : public Logger
257 {
258 protected:
260  virtual void _log(const char *msg);
261 };
262 
266 class FileLogger : public Logger
267 {
268 public:
269  FileLogger(const char *file_path);
270  ~FileLogger();
271 
272 protected:
274  virtual void _log(const char *msg);
275 
277  const std::string m_file_path;
279  std::ofstream m_logFile;
280 };
281 
283 
284 #endif // _Logging_h_
The normal log level, for status messages.
Definition: src/blfwk/Logging.h:81
virtual ~Logger()
Destructor.
Definition: src/blfwk/Logging.h:98
SetOutputLevel(Logger::log_level_t level)
Default constructor.
Definition: src/blfwk/Logging.h:222
log_level_t getFilterLevel() const
Returns the current logging filter level.
Definition: src/blfwk/Logging.h:104
For fatal error messages.
Definition: src/blfwk/Logging.h:79
For internal reporting.
Definition: src/blfwk/Logging.h:83
static void setLogger(Logger *logger)
Sets the global logger singleton instance.
Definition: src/blfwk/Logging.h:160
std::ofstream m_logFile
The name of the file, including the path, to log to.
Definition: src/blfwk/Logging.h:279
Alias for kDebug.
Definition: src/blfwk/Logging.h:86
log_level_t m_level
The current log output level.
Definition: apps/elftosb/common/Logging.h:102
virtual void log(log_level_t level, const std::string &msg)
Log a string output at a specific output level.
Definition: src/blfwk/Logging.h:122
SetOutputLevel(Logger *logger, Logger::log_level_t level)
Constructor.
Definition: src/blfwk/Logging.h:235
log_level_t getOutputLevel() const
Returns the current logging output level.
Definition: src/blfwk/Logging.h:108
Alias for kInfo.
Definition: src/blfwk/Logging.h:85
For machine language output only.
Definition: src/blfwk/Logging.h:78
void setFilterLevel(log_level_t level)
Changes the logging level to level.
Definition: apps/elftosb/common/Logging.h:72
~SetOutputLevel()
Destructor.
Definition: src/blfwk/Logging.h:246
virtual void log(const std::string &msg)
Log a string object.
Definition: src/blfwk/Logging.h:117
The lowest level, for messages that must always be logged.
Definition: src/blfwk/Logging.h:77
Wraps a set of static functions for easy global logging access.
Definition: apps/elftosb/common/Logging.h:122
For verbose status messages.
Definition: src/blfwk/Logging.h:82
For non-fatal warning messages.
Definition: src/blfwk/Logging.h:80
virtual void log(const char *fmt,...)
Log with format.
Definition: apps/elftosb/common/Logging.cpp:16
void setOutputLevel(log_level_t level)
Changes the logging output level to level.
Definition: src/blfwk/Logging.h:106
const std::string m_file_path
The name of the file, including the path, to log to.
Definition: src/blfwk/Logging.h:277
Logger()
Default constructor.
Definition: src/blfwk/Logging.h:91
Highest log level; verbose debug logging.
Definition: src/blfwk/Logging.h:84
log_level_t m_filter
The current logging filter level.
Definition: apps/elftosb/common/Logging.h:101
log_level_t
Logging levels.
Definition: apps/elftosb/common/Logging.h:48
virtual void _log(const char *msg)=0
The base pure virtual logging function implemented by subclasses.
Simple logger that writes to stdout.
Definition: apps/elftosb/common/Logging.h:212
Utility class to temporarily change the logging output level.
Definition: apps/elftosb/common/Logging.h:171
Simple logger that writes to a file.
Definition: src/blfwk/Logging.h:266
Base logger class.
Definition: apps/elftosb/common/Logging.h:44