src/blfwk/Logging.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if !defined(_Logging_h_)
#define _Logging_h_
#include <string>
#include <assert.h>
#include <stdarg.h>
#include <fstream>
class Logger
{
public:
enum log_level_t
{
kUrgent = 0,
kJson,
kError,
kWarning,
kInfo,
kInfo2,
kDebug,
kDebug2,
kInfo1 = kInfo,
kDebug1 = kDebug
};
public:
Logger()
: m_filter(kInfo)
, m_level(kInfo)
{
}
virtual ~Logger() {}
inline void setFilterLevel(log_level_t level) { m_filter = level; }
inline log_level_t getFilterLevel() const { return m_filter; }
inline void setOutputLevel(log_level_t level) { m_level = level; }
inline log_level_t getOutputLevel() const { return m_level; }
virtual void log(const char *fmt, ...);
virtual void log(const std::string &msg) { log(msg.c_str()); }
virtual void log(log_level_t level, const char *fmt, ...);
virtual void log(log_level_t level, const std::string &msg) { log(level, msg.c_str()); }
virtual void log(const char *fmt, va_list args);
virtual void log(log_level_t level, const char *fmt, va_list args);
protected:
log_level_t m_filter;
log_level_t m_level;
protected:
virtual void _log(const char *msg) = 0;
};
class Log
{
public:
static inline Logger *getLogger() { return s_logger; }
static inline void setLogger(Logger *logger) { s_logger = logger; }
static void log(const char *fmt, ...);
static void log(const std::string &msg);
static void log(Logger::log_level_t level, const char *fmt, ...);
static void log(Logger::log_level_t level, const std::string &msg);
static void urgent(const char *fmt, ...);
static void json(const char *fmt, ...);
static void error(const char *fmt, ...);
static void warning(const char *fmt, ...);
static void info(const char *fmt, ...);
static void info2(const char *fmt, ...);
static void debug(const char *fmt, ...);
static void debug2(const char *fmt, ...);
protected:
static Logger *s_logger;
public:
class SetOutputLevel
{
public:
SetOutputLevel(Logger::log_level_t level)
: m_logger(Log::getLogger())
, m_saved(Logger::kInfo)
{
assert(m_logger);
m_saved = m_logger->getOutputLevel();
m_logger->setOutputLevel(level);
}
SetOutputLevel(Logger *logger, Logger::log_level_t level)
: m_logger(logger)
, m_saved(logger->getOutputLevel())
{
assert(m_logger);
m_logger->setOutputLevel(level);
}
~SetOutputLevel() { m_logger->setOutputLevel(m_saved); }
protected:
Logger *m_logger;
Logger::log_level_t m_saved;
};
};
class StdoutLogger : public Logger
{
protected:
virtual void _log(const char *msg);
};
class FileLogger : public Logger
{
public:
FileLogger(const char *file_path);
~FileLogger();
protected:
virtual void _log(const char *msg);
const std::string m_file_path;
std::ofstream m_logFile;
};
#endif // _Logging_h_
Logger::kInfo
The normal log level, for status messages.
Definition: src/blfwk/Logging.h:81
Logger::~Logger
virtual ~Logger()
Destructor.
Definition: src/blfwk/Logging.h:98
Log::SetOutputLevel::SetOutputLevel
SetOutputLevel(Logger::log_level_t level)
Default constructor.
Definition: src/blfwk/Logging.h:222
Logger::getFilterLevel
log_level_t getFilterLevel() const
Returns the current logging filter level.
Definition: src/blfwk/Logging.h:104
Logger::kError
For fatal error messages.
Definition: src/blfwk/Logging.h:79
Logger::kDebug
For internal reporting.
Definition: src/blfwk/Logging.h:83
Log::setLogger
static void setLogger(Logger *logger)
Sets the global logger singleton instance.
Definition: src/blfwk/Logging.h:160
FileLogger::m_logFile
std::ofstream m_logFile
The name of the file, including the path, to log to.
Definition: src/blfwk/Logging.h:279
Logger::kDebug1
Alias for kDebug.
Definition: src/blfwk/Logging.h:86
Logger::m_level
log_level_t m_level
The current log output level.
Definition: apps/elftosb/common/Logging.h:102
Logger::log
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
Log::SetOutputLevel::SetOutputLevel
SetOutputLevel(Logger *logger, Logger::log_level_t level)
Constructor.
Definition: src/blfwk/Logging.h:235
Logger::getOutputLevel
log_level_t getOutputLevel() const
Returns the current logging output level.
Definition: src/blfwk/Logging.h:108
Logger::kInfo1
Alias for kInfo.
Definition: src/blfwk/Logging.h:85
Logger::kJson
For machine language output only.
Definition: src/blfwk/Logging.h:78
Logger::setFilterLevel
void setFilterLevel(log_level_t level)
Changes the logging level to level.
Definition: apps/elftosb/common/Logging.h:72
Log::SetOutputLevel::~SetOutputLevel
~SetOutputLevel()
Destructor.
Definition: src/blfwk/Logging.h:246
Logger::log
virtual void log(const std::string &msg)
Log a string object.
Definition: src/blfwk/Logging.h:117
Logger::kUrgent
The lowest level, for messages that must always be logged.
Definition: src/blfwk/Logging.h:77
Log
Wraps a set of static functions for easy global logging access.
Definition: apps/elftosb/common/Logging.h:122
Logger::kInfo2
For verbose status messages.
Definition: src/blfwk/Logging.h:82
Logger::kWarning
For non-fatal warning messages.
Definition: src/blfwk/Logging.h:80
Logger::log
virtual void log(const char *fmt,...)
Log with format.
Definition: apps/elftosb/common/Logging.cpp:16
Logger::setOutputLevel
void setOutputLevel(log_level_t level)
Changes the logging output level to level.
Definition: src/blfwk/Logging.h:106
FileLogger::m_file_path
const std::string m_file_path
The name of the file, including the path, to log to.
Definition: src/blfwk/Logging.h:277
Logger::Logger
Logger()
Default constructor.
Definition: src/blfwk/Logging.h:91
Logger::kDebug2
Highest log level; verbose debug logging.
Definition: src/blfwk/Logging.h:84
Logger::m_filter
log_level_t m_filter
The current logging filter level.
Definition: apps/elftosb/common/Logging.h:101
Logger::log_level_t
log_level_t
Logging levels.
Definition: apps/elftosb/common/Logging.h:48
Logger::_log
virtual void _log(const char *msg)=0
The base pure virtual logging function implemented by subclasses.
StdoutLogger
Simple logger that writes to stdout.
Definition: apps/elftosb/common/Logging.h:212
Log::SetOutputLevel
Utility class to temporarily change the logging output level.
Definition: apps/elftosb/common/Logging.h:171
FileLogger
Simple logger that writes to a file.
Definition: src/blfwk/Logging.h:266
Logger
Base logger class.
Definition: apps/elftosb/common/Logging.h:44