Skip to content

Commit

Permalink
Prefix public enum values
Browse files Browse the repository at this point in the history
I decided to prefix all public enum values to make sure there are no
name conflicts. This should fix Windows error because Microsoft defines
ERROR in some header file
  • Loading branch information
crapp committed May 24, 2016
1 parent adb9ff0 commit 19d68a1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 72 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace eal = ealogger;
namespace con = ealogger::constants;
std::unique_ptr<eal::Logger> log = std::unique_ptr<eal::Logger>(
new eal::Logger());
log->set_min_lvl(con::LOGGER_SINK::CONSOLEm, con::LOG_LEVEL::INFO);
log->set_min_lvl(con::LOGGER_SINK::EAL_CONSOLE, con::LOG_LEVEL::EAL_INFO);
log->eal_debug("Do you see me?");
log->eal_info("An info message")
log->eal_warn("A warning message");
Expand Down
8 changes: 4 additions & 4 deletions examples/ealogger_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ int main(void)
}

// change minimum severity for the console sink to warning
log->set_min_lvl(con::LOGGER_SINK::CONSOLE, con::LOG_LEVEL::WARNING);
log->set_min_lvl(con::LOGGER_SINK::EAL_CONSOLE, con::LOG_LEVEL::EAL_WARNING);

// this message will not appear in the console
log->eal_info("Info is not visible because minimum severity is WARNING");
Expand All @@ -61,17 +61,17 @@ int main(void)
}

// set severity to info
log->set_min_lvl(con::LOGGER_SINK::CONSOLE, con::LOG_LEVEL::INFO);
log->set_min_lvl(con::LOGGER_SINK::EAL_CONSOLE, con::LOG_LEVEL::EAL_INFO);
// the next message will be visible again
log->eal_info("This message should be visible in the console");

// change the datetime conversion pattern
log->set_datetime_pattern(con::LOGGER_SINK::CONSOLE, "%A %r");
log->set_datetime_pattern(con::LOGGER_SINK::EAL_CONSOLE, "%A %r");
log->eal_info("You should now see the new datetime conversion pattern");

// change the message template for the console sink
// datetime [file name:"line number" "function name"] severity: log message
log->set_msg_template(con::LOGGER_SINK::CONSOLE, "%d [%f:%l %u] %s: %m");
log->set_msg_template(con::LOGGER_SINK::EAL_CONSOLE, "%d [%f:%l %u] %s: %m");
log->eal_info("You should now see a new message template in use");
log->eal_info("The change will affect all messages currently in the queue");

Expand Down
46 changes: 23 additions & 23 deletions include/ealogger/ealogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,44 +63,44 @@ namespace ealogger
* @def eal_debug(msg)
* @brief Write a debug message
*/
#define eal_debug(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::DEBUG, __FILE__, __LINE__, \
__func__)
#define eal_debug(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::EAL_DEBUG, __FILE__, \
__LINE__, __func__)
/**
* @def eal_info(msg)
* @brief Write a info message
*/
#define eal_info(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::INFO, __FILE__, __LINE__, \
__func__)
#define eal_info(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::EAL_INFO, __FILE__, \
__LINE__, __func__)
/**
* @def eal_warn(msg)
* @brief Write a warning message
*/
#define eal_warn(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::WARNING, __FILE__, __LINE__, \
__func__)
#define eal_warn(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::EAL_WARNING, __FILE__, \
__LINE__, __func__)
/**
* @def eal_error(msg)
* @brief Write an error message
*/
#define eal_error(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::ERROR, __FILE__, __LINE__, \
__func__)
#define eal_error(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::EAL_ERROR, __FILE__, \
__LINE__, __func__)
/**
* @def eal_fatal(msg)
* @brief Write a fatal message
*/
#define eal_fatal(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::FATAL, __FILE__, __LINE__, \
__func__)
#define eal_fatal(msg) \
write_log(msg, ealogger::constants::LOG_LEVEL::EAL_FATAL, __FILE__, \
__LINE__, __func__)
/**
* @def eal_stack()
* @brief Write a message with a stacktrace
*/
#define eal_stack() \
write_log("", ealogger::constants::LOG_LEVEL::STACK, __FILE__, __LINE__, \
__func__)
#define eal_stack() \
write_log("", ealogger::constants::LOG_LEVEL::EAL_STACK, __FILE__, \
__LINE__, __func__)

/**
* @brief ealogger main class
Expand Down Expand Up @@ -179,10 +179,10 @@ class Logger
* This method is called by the macros that are defined in this header file
* for the different log levels. You can of course call this method yourself
* @code
* mylogger.write_log("This is a warning", ealogger::constants::LOG_LEVEL::WARNING,
* mylogger.write_log("This is a warning", ealogger::constants::LOG_LEVEL::EAL_WARNING,
* __FILE__, __LINE__, __func__);
* mylogger.write_log("This is a warning without line file and func",
* ealogger::constants::LOG_LEVEL::WARNING);
* ealogger::constants::LOG_LEVEL::EAL_WARNING);
* @endcode
*/
void write_log(std::string msg, ealogger::constants::LOG_LEVEL lvl,
Expand Down Expand Up @@ -223,7 +223,7 @@ class Logger
*/
void init_syslog_sink(bool enabled = true,
ealogger::constants::LOG_LEVEL min_lvl =
ealogger::constants::LOG_LEVEL::DEBUG,
ealogger::constants::LOG_LEVEL::EAL_DEBUG,
std::string msg_template = "%s: %m",
std::string datetime_pattern = "%F %T");
/**
Expand All @@ -243,7 +243,7 @@ class Logger
*/
void init_console_sink(bool enabled = true,
ealogger::constants::LOG_LEVEL min_lvl =
ealogger::constants::LOG_LEVEL::DEBUG,
ealogger::constants::LOG_LEVEL::EAL_DEBUG,
std::string msg_template = "%d %s: %m",
std::string datetime_pattern = "%F %T");

Expand Down Expand Up @@ -272,7 +272,7 @@ class Logger
*/
void init_file_sink(bool enabled = true,
ealogger::constants::LOG_LEVEL min_lvl =
ealogger::constants::LOG_LEVEL::DEBUG,
ealogger::constants::LOG_LEVEL::EAL_DEBUG,
std::string msg_template = "%d %s [%f:%l] %m",
std::string datetime_pattern = "%F %T",
std::string logfile = "ealogger_logfile.log",
Expand Down
20 changes: 10 additions & 10 deletions include/ealogger/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ namespace constants
* @brief Supported logger Sinks
*/
enum class LOGGER_SINK {
CONSOLE = 0, /**< Sink writing to a console SinkConsole */
SYSLOG, /**< Sink writing to linux syslog SinkSyslog */
FILE_SIMPLE /**< Sink writing to a file SinkFile */
EAL_CONSOLE = 0, /**< Sink writing to a console SinkConsole */
EAL_SYSLOG, /**< Sink writing to linux syslog SinkSyslog */
EAL_FILE_SIMPLE /**< Sink writing to a file SinkFile */
};
// enum CONVERSION_PATTERN {};

Expand All @@ -56,13 +56,13 @@ enum class LOGGER_SINK {
* minimum loglevel.
*/
enum class LOG_LEVEL {
DEBUG = 0, /**< Debug message */
INFO, /**< Info message */
WARNING, /**< Warning message */
ERROR, /**< Error message */
FATAL, /**< Fatal Message */
STACK, /**< Stack log message */
INTERNAL /**< Internal Message, do not use this loglevel yourself */
EAL_DEBUG = 0, /**< Debug message */
EAL_INFO, /**< Info message */
EAL_WARNING, /**< Warning message */
EAL_ERROR, /**< Error message */
EAL_FATAL, /**< Fatal Message */
EAL_STACK, /**< Stack log message */
EAL_INTERNAL /**< Internal Message, do not use this loglevel yourself */
};
}
}
Expand Down
36 changes: 17 additions & 19 deletions src/ealogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ namespace con = ealogger::constants;
eal::Logger::Logger(bool async) : async(async)
{
this->logger_mutex_map.emplace(
con::LOGGER_SINK::CONSOLE,
con::LOGGER_SINK::EAL_CONSOLE,
std::unique_ptr<std::mutex>(new std::mutex()));
this->logger_mutex_map.emplace(
con::LOGGER_SINK::SYSLOG, std::unique_ptr<std::mutex>(new std::mutex()));
con::LOGGER_SINK::EAL_SYSLOG,
std::unique_ptr<std::mutex>(new std::mutex()));
this->logger_mutex_map.emplace(
con::LOGGER_SINK::FILE_SIMPLE,
con::LOGGER_SINK::EAL_FILE_SIMPLE,
std::unique_ptr<std::mutex>(new std::mutex()));
// TODO: Make registration of signal handler configurable
#ifdef __linux__
Expand Down Expand Up @@ -59,7 +60,7 @@ eal::Logger::~Logger()
this->set_logger_thread_stop(true);
// we need to write one more log message to wakeup the background logger
// thread it will pop the last message from the queue.
this->write_log("Logger EXIT", con::LOG_LEVEL::INTERNAL, "", 0, "");
this->write_log("Logger EXIT", con::LOG_LEVEL::EAL_INTERNAL, "", 0, "");
try {
logger_thread.join();
} catch (const std::system_error &ex) {
Expand All @@ -74,7 +75,7 @@ void eal::Logger::write_log(std::string msg, con::LOG_LEVEL lvl,
{
std::shared_ptr<LogMessage> m;
LogMessage::LOGTYPE type = LogMessage::LOGTYPE::DEFAULT;
if (lvl == con::LOG_LEVEL::STACK) {
if (lvl == con::LOG_LEVEL::EAL_STACK) {
type = LogMessage::LOGTYPE::STACK;
std::vector<std::string> stack_vec;
// TODO: Stack size is hard coded
Expand Down Expand Up @@ -105,8 +106,8 @@ void eal::Logger::init_syslog_sink(bool enabled, con::LOG_LEVEL min_lvl,
{
try {
std::lock_guard<std::mutex> lock(
*(this->logger_mutex_map[con::LOGGER_SINK::SYSLOG].get()));
this->logger_sink_map[con::LOGGER_SINK::SYSLOG] =
*(this->logger_mutex_map[con::LOGGER_SINK::EAL_SYSLOG].get()));
this->logger_sink_map[con::LOGGER_SINK::EAL_SYSLOG] =
std::make_shared<SinkSyslog>(std::move(msg_template),
std::move(datetime_pattern), enabled,
min_lvl);
Expand All @@ -119,8 +120,8 @@ void eal::Logger::init_console_sink(bool enabled, con::LOG_LEVEL min_lvl,
{
try {
std::lock_guard<std::mutex> lock(
*(this->logger_mutex_map[con::LOGGER_SINK::CONSOLE].get()));
this->logger_sink_map[con::LOGGER_SINK::CONSOLE] =
*(this->logger_mutex_map[con::LOGGER_SINK::EAL_CONSOLE].get()));
this->logger_sink_map[con::LOGGER_SINK::EAL_CONSOLE] =
std::make_shared<SinkConsole>(std::move(msg_template),
std::move(datetime_pattern), enabled,
min_lvl);
Expand All @@ -130,17 +131,15 @@ void eal::Logger::init_console_sink(bool enabled, con::LOG_LEVEL min_lvl,
void eal::Logger::init_file_sink(bool enabled, con::LOG_LEVEL min_lvl,
std::string msg_template,
std::string datetime_pattern,
std::string logfile,
bool flush_buffer)
std::string logfile, bool flush_buffer)
{
try {
std::lock_guard<std::mutex> lock(
*(this->logger_mutex_map[con::LOGGER_SINK::FILE_SIMPLE].get()));
this->logger_sink_map[con::LOGGER_SINK::FILE_SIMPLE] =
std::make_shared<SinkFile>(std::move(msg_template),
std::move(datetime_pattern), enabled,
min_lvl, std::move(logfile),
flush_buffer);
*(this->logger_mutex_map[con::LOGGER_SINK::EAL_FILE_SIMPLE].get()));
this->logger_sink_map[con::LOGGER_SINK::EAL_FILE_SIMPLE] =
std::make_shared<SinkFile>(
std::move(msg_template), std::move(datetime_pattern), enabled,
min_lvl, std::move(logfile), flush_buffer);
} catch (const std::exception &ex) {
}
}
Expand Down Expand Up @@ -215,8 +214,7 @@ bool eal::Logger::is_initialized(con::LOGGER_SINK sink)
if (this->logger_sink_map.find(sink) != this->logger_sink_map.end()) {
ret = true;
}
} catch(const std::exception &ex) {

} catch (const std::exception &ex) {
}
return ret;
}
Expand Down
16 changes: 8 additions & 8 deletions src/sink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ eal::Sink::Sink(std::string msg_template, std::string datetime_pattern,
min_level(min_lvl)
{
this->fill_conv_patterns(true);
this->loglevel_lookup = {{con::LOG_LEVEL::DEBUG, "DEBUG"},
{con::LOG_LEVEL::INFO, "INFO"},
{con::LOG_LEVEL::WARNING, "WARNING"},
{con::LOG_LEVEL::ERROR, "ERROR"},
{con::LOG_LEVEL::FATAL, "FATAL"},
{con::LOG_LEVEL::STACK, "Stacktrace"},
{con::LOG_LEVEL::INTERNAL, "INTERNAL"}};
this->loglevel_lookup = {{con::LOG_LEVEL::EAL_DEBUG, "DEBUG"},
{con::LOG_LEVEL::EAL_INFO, "INFO"},
{con::LOG_LEVEL::EAL_WARNING, "WARNING"},
{con::LOG_LEVEL::EAL_ERROR, "ERROR"},
{con::LOG_LEVEL::EAL_FATAL, "FATAL"},
{con::LOG_LEVEL::EAL_STACK, "Stacktrace"},
{con::LOG_LEVEL::EAL_INTERNAL, "INTERNAL"}};
}
eal::Sink::~Sink() {}
void eal::Sink::set_msg_template(std::string msg_template)
Expand Down Expand Up @@ -84,7 +84,7 @@ void eal::Sink::prepare_log_message(

#ifndef EALOGGER_PRINT_INTERNAL
// Print INTERNAL messages only when defined
if (msg_lvl == con::LOG_LEVEL::INTERNAL) {
if (msg_lvl == con::LOG_LEVEL::EAL_INTERNAL) {
return;
}
#endif
Expand Down
14 changes: 7 additions & 7 deletions src/sink_syslog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ eal::SinkSyslog::SinkSyslog(std::string msg_template,
{
#ifdef EALOGGER_SYSLOG
this->loglevel_syslog_lookup = {
{con::LOG_LEVEL::DEBUG, LOG_DEBUG},
{con::LOG_LEVEL::INFO, LOG_INFO},
{con::LOG_LEVEL::WARNING, LOG_WARNING},
{con::LOG_LEVEL::ERROR, LOG_ERR},
{con::LOG_LEVEL::FATAL, LOG_CRIT},
{con::LOG_LEVEL::STACK, LOG_CRIT},
{con::LOG_LEVEL::INTERNAL,
{con::LOG_LEVEL::EAL_DEBUG, LOG_DEBUG},
{con::LOG_LEVEL::EAL_INFO, LOG_INFO},
{con::LOG_LEVEL::EAL_WARNING, LOG_WARNING},
{con::LOG_LEVEL::EAL_ERROR, LOG_ERR},
{con::LOG_LEVEL::EAL_FATAL, LOG_CRIT},
{con::LOG_LEVEL::EAL_STACK, LOG_CRIT},
{con::LOG_LEVEL::EAL_INTERNAL,
LOG_DEBUG}}; // mapping internal to syslog debug
#else
this->loglevel_syslog_lookup = {};
Expand Down

0 comments on commit 19d68a1

Please sign in to comment.