stabilized logger classes
This commit is contained in:
@@ -101,7 +101,7 @@ public:
|
||||
void clear();
|
||||
|
||||
private:
|
||||
void get_time_stamp(char* timestamp_buffer) const;
|
||||
static void get_time_stamp(char* timestamp_buffer);
|
||||
|
||||
private:
|
||||
static std::map<log_level, std::string> level_strings;
|
||||
|
||||
@@ -8,15 +8,14 @@ namespace matador::logger {
|
||||
/**
|
||||
* Represents all available log levels
|
||||
*/
|
||||
enum class log_level
|
||||
{
|
||||
LVL_FATAL, /**< If a serious error occurred, use FATAL level */
|
||||
LVL_ERROR, /**< On error use ERROR level */
|
||||
LVL_WARN, /**< Warnings should use WARN level */
|
||||
LVL_INFO, /**< Information should go with INFO level */
|
||||
LVL_DEBUG, /**< Debug output should use DEBUG level */
|
||||
LVL_TRACE, /**< Trace information should use TRACE level */
|
||||
LVL_ALL /**< This level represents all log levels and should be used for logging */
|
||||
enum class log_level {
|
||||
Fatal, /**< If a serious error occurred, use FATAL level */
|
||||
Error, /**< On error use ERROR level */
|
||||
Warn, /**< Warnings should use WARN level */
|
||||
Info, /**< Information should go with INFO level */
|
||||
Debug, /**< Debug output should use DEBUG level */
|
||||
Trace, /**< Trace information should use TRACE level */
|
||||
All /**< This level represents all log levels and should be used for logging */
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -31,10 +30,9 @@ std::ostream& operator<<(std::ostream &os, log_level lvl);
|
||||
|
||||
/// @cond MATADOR_DEV
|
||||
|
||||
struct log_level_range
|
||||
{
|
||||
log_level min_level = log_level::LVL_INFO;
|
||||
log_level max_level = log_level::LVL_FATAL;
|
||||
struct log_level_range {
|
||||
log_level min_level = log_level::Info;
|
||||
log_level max_level = log_level::Fatal;
|
||||
};
|
||||
|
||||
/// @endcond
|
||||
|
||||
@@ -123,11 +123,8 @@ public:
|
||||
|
||||
protected:
|
||||
/// @cond MATADOR_DEV
|
||||
log_manager()
|
||||
{
|
||||
default_log_domain_ = log_domain_map_.insert(std::make_pair("default", std::make_shared<log_domain>("default", default_log_level_range_))).first->second;
|
||||
}
|
||||
/// @endcond
|
||||
log_manager();
|
||||
/// @endcond
|
||||
|
||||
private:
|
||||
std::shared_ptr<log_domain> acquire_domain(const std::string &name);
|
||||
@@ -140,6 +137,8 @@ private:
|
||||
std::map<std::string, std::shared_ptr<log_domain>> log_domain_map_;
|
||||
|
||||
static log_level_range default_log_level_range_;
|
||||
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -285,15 +284,10 @@ void log_default(log_level lvl, const std::string &source, const char *message);
|
||||
* @param args The arguments for the message
|
||||
*/
|
||||
template<typename... ARGS>
|
||||
void log(const log_level lvl, const std::string &source, const char *what, ARGS const &... args)
|
||||
{
|
||||
char message_buffer[16384];
|
||||
void log(const log_level lvl, const std::string &source, const char *what, ARGS const &... args) {
|
||||
char message_buffer[logger::buffer_size];
|
||||
|
||||
#ifdef _MSC_VER
|
||||
sprintf_s(message_buffer, 912, what, args...);
|
||||
#else
|
||||
sprintf(message_buffer, what, args...);
|
||||
#endif
|
||||
snprintf(message_buffer, logger::buffer_size, what, args...);
|
||||
|
||||
log_default(lvl, source, message_buffer);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
#include <mutex>
|
||||
|
||||
namespace matador::logger {
|
||||
|
||||
/**
|
||||
* @brief logger to write log messages to log domains
|
||||
*
|
||||
@@ -31,9 +30,9 @@ namespace matador::logger {
|
||||
* All log messages are written through the internal
|
||||
* log_domain object to the sinks.
|
||||
*/
|
||||
class logger final
|
||||
{
|
||||
class logger final {
|
||||
public:
|
||||
static constexpr size_t buffer_size = 16384;
|
||||
|
||||
/**
|
||||
* Create a logger with a given source name connected
|
||||
@@ -69,7 +68,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void fatal(const char *what, ARGS const &... args) const { log(log_level::LVL_FATAL, what, args...); }
|
||||
void fatal(const char *what, ARGS const &... args) const { log(log_level::Fatal, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message string with log level LVL_FATAL
|
||||
@@ -91,7 +90,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void error(const char *what, ARGS const &... args) const { log(log_level::LVL_ERROR, what, args...); }
|
||||
void error(const char *what, ARGS const &... args) const { log(log_level::Error, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message string with log level LVL_FATAL
|
||||
@@ -113,7 +112,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void warn(const char *what, ARGS const &... args) const { log(log_level::LVL_WARN, what, args...); }
|
||||
void warn(const char *what, ARGS const &... args) const { log(log_level::Warn, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message string with log level LVL_FATAL
|
||||
@@ -135,7 +134,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void info(const char *what, ARGS const &... args) const { log(log_level::LVL_INFO, what, args...); }
|
||||
void info(const char *what, ARGS const &... args) const { log(log_level::Info, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message string with log level LVL_FATAL
|
||||
@@ -157,7 +156,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void debug(const char *what, ARGS const &... args) const { log(log_level::LVL_DEBUG, what, args...); }
|
||||
void debug(const char *what, ARGS const &... args) const { log(log_level::Debug, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message string with log level LVL_FATAL
|
||||
@@ -179,7 +178,7 @@ public:
|
||||
* @param args The arguments to be replaced in the message
|
||||
*/
|
||||
template<typename ... ARGS>
|
||||
void trace(const char *what, ARGS const &... args) const { log(log_level::LVL_TRACE, what, args...); }
|
||||
void trace(const char *what, ARGS const &... args) const { log(log_level::Trace, what, args...); }
|
||||
|
||||
/**
|
||||
* Writes a log message represented by a char pointer
|
||||
@@ -222,14 +221,10 @@ private:
|
||||
};
|
||||
|
||||
template<typename... ARGS>
|
||||
void logger::log(log_level lvl, const char *what, ARGS const &... args) const {
|
||||
char message_buffer[16384];
|
||||
void logger::log(const log_level lvl, const char *what, ARGS const &... args) const {
|
||||
char message_buffer[buffer_size];
|
||||
|
||||
#ifdef _MSC_VER
|
||||
sprintf_s(message_buffer, 16384, what, args...);
|
||||
#else
|
||||
sprintf(message_buffer, what, args...);
|
||||
#endif
|
||||
snprintf(message_buffer, buffer_size, what, args...);
|
||||
|
||||
logger_domain_->log(lvl, source_, message_buffer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user