apps/elftosb/common/Logging.h
/*
* File: Logging.h
*
* Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
* See included license file for license details.
*/
#if !defined(_Logging_h_)
#define _Logging_h_
#include <string>
#include <assert.h>
#include <stdarg.h>
class Logger
{
public:
enum log_level_t
{
URGENT = 0,
ERROR,
WARNING,
INFO,
INFO2,
DEBUG,
DEBUG2
};
public:
Logger()
: m_filter(INFO)
, m_level(INFO)
{
}
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);
protected:
static Logger *s_logger;
public:
class SetOutputLevel
{
public:
SetOutputLevel(Logger::log_level_t level)
: m_logger(Log::getLogger())
, m_saved(Logger::INFO)
{
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);
};
#endif // _Logging_h_
Log::SetOutputLevel::m_logger
Logger * m_logger
The logger instance we&#39;re controlling.
Definition: apps/elftosb/common/Logging.h:204
Logger::~Logger
virtual ~Logger()
Destructor.
Definition: apps/elftosb/common/Logging.h:68
Log::SetOutputLevel::SetOutputLevel
SetOutputLevel(Logger::log_level_t level)
Default constructor.
Definition: apps/elftosb/common/Logging.h:178
Logger::URGENT
The lowest level, for messages that must always be logged.
Definition: apps/elftosb/common/Logging.h:50
Logger::getFilterLevel
log_level_t getFilterLevel() const
Returns the current logging filter level.
Definition: apps/elftosb/common/Logging.h:74
Logger::INFO
The normal log level, for status messages.
Definition: apps/elftosb/common/Logging.h:53
Logger::ERROR
For fatal error messages.
Definition: apps/elftosb/common/Logging.h:51
Logger::DEBUG
For internal reporting.
Definition: apps/elftosb/common/Logging.h:55
Log::setLogger
static void setLogger(Logger *logger)
Sets the global logger singleton instance.
Definition: apps/elftosb/common/Logging.h:130
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: apps/elftosb/common/Logging.h:92
Log::SetOutputLevel::SetOutputLevel
SetOutputLevel(Logger *logger, Logger::log_level_t level)
Constructor.
Definition: apps/elftosb/common/Logging.h:191
Logger::getOutputLevel
log_level_t getOutputLevel() const
Returns the current logging output level.
Definition: apps/elftosb/common/Logging.h:78
Log::s_logger
static Logger * s_logger
The single global logger instance.
Definition: apps/elftosb/common/Logging.h:149
Logger::WARNING
For non-fatal warning messages.
Definition: apps/elftosb/common/Logging.h:52
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: apps/elftosb/common/Logging.h:202
Logger::log
virtual void log(const std::string &msg)
Log a string object.
Definition: apps/elftosb/common/Logging.h:87
Logger::DEBUG2
Highest log level; verbose debug logging.
Definition: apps/elftosb/common/Logging.h:56
Log::SetOutputLevel::m_saved
Logger::log_level_t m_saved
Original logging output level.
Definition: apps/elftosb/common/Logging.h:205
Log
Wraps a set of static functions for easy global logging access.
Definition: apps/elftosb/common/Logging.h:122
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: apps/elftosb/common/Logging.h:76
Logger::Logger
Logger()
Default constructor.
Definition: apps/elftosb/common/Logging.h:61
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
Logger::INFO2
For verbose status messages.
Definition: apps/elftosb/common/Logging.h:54
Logger
Base logger class.
Definition: apps/elftosb/common/Logging.h:44