refactor(logger): Restrict trace logs to debug builds

This commit is contained in:
Michael Carlberg 2016-06-21 04:29:33 +02:00
parent 4bed34156e
commit 4d444da0e4
3 changed files with 62 additions and 36 deletions

View File

@ -21,7 +21,11 @@
#define log_warning(s) get_logger()->warning(s) #define log_warning(s) get_logger()->warning(s)
#define log_info(s) get_logger()->info(s) #define log_info(s) get_logger()->info(s)
#define log_debug(s) get_logger()->debug(s) #define log_debug(s) get_logger()->debug(s)
#ifdef DEBUG
#define log_trace(s) get_logger()->trace(__FILE__, __FUNCTION__, __LINE__, s) #define log_trace(s) get_logger()->trace(__FILE__, __FUNCTION__, __LINE__, s)
#else
#define log_trace(s) if (0) {}
#endif
enum LogLevel enum LogLevel
{ {
@ -31,7 +35,11 @@ enum LogLevel
LEVEL_INFO = 1 << 4, LEVEL_INFO = 1 << 4,
LEVEL_DEBUG = 1 << 8, LEVEL_DEBUG = 1 << 8,
LEVEL_TRACE = 1 << 16, LEVEL_TRACE = 1 << 16,
#ifdef DEBUG
LEVEL_ALL = LEVEL_ERROR | LEVEL_WARNING | LEVEL_INFO | LEVEL_DEBUG | LEVEL_TRACE LEVEL_ALL = LEVEL_ERROR | LEVEL_WARNING | LEVEL_INFO | LEVEL_DEBUG | LEVEL_TRACE
#else
LEVEL_ALL = LEVEL_ERROR | LEVEL_WARNING | LEVEL_INFO | LEVEL_DEBUG
#endif
}; };
class Logger class Logger
@ -41,24 +49,27 @@ class Logger
int level = LogLevel::LEVEL_ERROR | LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO; int level = LogLevel::LEVEL_ERROR | LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO;
int fd = LOGGER_FD; int fd = LOGGER_FD;
void output(std::string tag, std::string msg);
public: public:
Logger(); Logger();
// void set_level(int level); void set_level(int level);
void add_level(int level); void add_level(int level);
void fatal(const std::string& msg); void fatal(std::string msg);
void error(const std::string& msg); void error(std::string msg);
void warning(const std::string& msg); void warning(std::string msg);
void info(const std::string& msg); void info(std::string msg);
void debug(const std::string& msg); void debug(std::string msg);
void trace(const char *file, const char *fn, int lineno, const std::string& msg);
void fatal(int msg) { fatal(std::to_string(msg)); } void fatal(int msg) { fatal(std::to_string(msg)); }
void error(int msg) { error(std::to_string(msg)); } void error(int msg) { error(std::to_string(msg)); }
void warning(int msg) { warning(std::to_string(msg)); } void warning(int msg) { warning(std::to_string(msg)); }
void info(int msg) { info(std::to_string(msg)); } void info(int msg) { info(std::to_string(msg)); }
void debug(int msg) { debug(std::to_string(msg)); } void debug(int msg) { debug(std::to_string(msg)); }
void trace(const char *file, const char *fn, int lineno, std::string msg);
void trace(const char *file, const char *fn, int lineno, int msg) { void trace(const char *file, const char *fn, int lineno, int msg) {
trace(file, fn, lineno, std::to_string(msg)); trace(file, fn, lineno, std::to_string(msg));
} }

View File

@ -56,7 +56,7 @@ int main(int argc, char **argv)
cli::add_option("-h", "--help", "Show help options"); cli::add_option("-h", "--help", "Show help options");
cli::add_option("-c", "--config", "FILE", "Path to the configuration file"); cli::add_option("-c", "--config", "FILE", "Path to the configuration file");
cli::add_option("-p", "--pipe", "FILE", "Path to the input pipe"); cli::add_option("-p", "--pipe", "FILE", "Path to the input pipe");
cli::add_option("-l", "--log", "LEVEL", "Set the logging verbosity", {"info","debug","trace"}); cli::add_option("-l", "--log", "LEVEL", "Set the logging verbosity", {"warning","info","debug","trace"});
cli::add_option("-d", "--dump", "PARAM", "Show value of PARAM in section [bar_name]"); cli::add_option("-d", "--dump", "PARAM", "Show value of PARAM in section [bar_name]");
cli::add_option("-x", "--print-exec", "Print the generated command line string used to start the lemonbar process"); cli::add_option("-x", "--print-exec", "Print the generated command line string used to start the lemonbar process");
cli::add_option("-w", "--print-wmname", "Print the generated WM_NAME"); cli::add_option("-w", "--print-wmname", "Print the generated WM_NAME");
@ -74,12 +74,20 @@ int main(int argc, char **argv)
* Set logging verbosity * Set logging verbosity
*/ */
if (cli::has_option("log")) { if (cli::has_option("log")) {
if (cli::match_option_value("log", "info")) logger->set_level(LogLevel::LEVEL_ERROR);
logger->add_level(LogLevel::LEVEL_INFO);
if (cli::match_option_value("log", "warning"))
logger->set_level(LogLevel::LEVEL_WARNING);
else if (cli::match_option_value("log", "info"))
logger->add_level(LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO);
else if (cli::match_option_value("log", "debug")) else if (cli::match_option_value("log", "debug"))
logger->add_level(LogLevel::LEVEL_INFO | LogLevel::LEVEL_DEBUG); logger->add_level(LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO | LogLevel::LEVEL_DEBUG);
else if (cli::match_option_value("log", "trace")) else if (cli::match_option_value("log", "trace")) {
logger->add_level(LogLevel::LEVEL_INFO | LogLevel::LEVEL_DEBUG | LogLevel::LEVEL_TRACE); logger->add_level(LogLevel::LEVEL_WARNING | LogLevel::LEVEL_INFO | LogLevel::LEVEL_DEBUG);
#ifdef DEBUG
logger->add_level(LogLevel::LEVEL_TRACE);
#endif
}
} }
/** /**

View File

@ -16,55 +16,62 @@ std::shared_ptr<Logger> &get_logger()
Logger::Logger() Logger::Logger()
{ {
if (isatty(LOGGER_FD)) { if (isatty(LOGGER_FD))
dup2(LOGGER_FD, this->fd); dup2(LOGGER_FD, this->fd);
}
} }
// void Logger::set_level(int mask) void Logger::set_level(int mask) {
// { this->level = mask;
// this->level = mask; }
// }
void Logger::add_level(int mask) void Logger::add_level(int mask) {
{
this->level |= mask; this->level |= mask;
} }
void Logger::fatal(const std::string& msg) void Logger::output(std::string tag, std::string msg) {
dprintf(this->fd, "%s%s\n", tag.c_str(), msg.c_str());
}
void Logger::fatal(std::string msg)
{ {
dprintf(this->fd, "%s%s\n", LOGGER_TAG_FATAL, msg.c_str()); this->output(LOGGER_TAG_FATAL, msg);
std::exit(EXIT_FAILURE); std::exit(EXIT_FAILURE);
} }
void Logger::error(const std::string& msg) void Logger::error(std::string msg)
{ {
if (this->level & LogLevel::LEVEL_ERROR) if (this->level & LogLevel::LEVEL_ERROR)
dprintf(this->fd, "%s%s\n", LOGGER_TAG_ERR, msg.c_str()); this->output(LOGGER_TAG_ERR, msg);
} }
void Logger::warning(const std::string& msg) void Logger::warning(std::string msg)
{ {
if (this->level & LogLevel::LEVEL_WARNING) if (this->level & LogLevel::LEVEL_WARNING)
dprintf(this->fd, "%s%s\n", LOGGER_TAG_WARN, msg.c_str()); this->output(LOGGER_TAG_WARN, msg);
} }
void Logger::info(const std::string& msg) void Logger::info(std::string msg)
{ {
if (this->level & LogLevel::LEVEL_INFO) if (this->level & LogLevel::LEVEL_INFO)
dprintf(this->fd, "%s%s\n", LOGGER_TAG_INFO, msg.c_str()); this->output(LOGGER_TAG_INFO, msg);
} }
void Logger::debug(const std::string& msg) void Logger::debug(std::string msg)
{ {
if (this->level & LogLevel::LEVEL_DEBUG) if (this->level & LogLevel::LEVEL_DEBUG)
dprintf(this->fd, "%s%s\n", LOGGER_TAG_DEBUG, msg.c_str()); this->output(LOGGER_TAG_DEBUG, msg);
} }
void Logger::trace(const char *file, const char *fn, int lineno, const std::string& msg) void Logger::trace(const char *file, const char *fn, int lineno, std::string msg)
{ {
if (this->level & LogLevel::LEVEL_TRACE && !msg.empty()) #ifdef DEBUG
dprintf(this->fd, "%s%s (%s:%d) -> %s\n", LOGGER_TAG_TRACE, &file[0], fn, lineno, msg.c_str()); if (!(this->level & LogLevel::LEVEL_TRACE))
else if (msg.empty()) return;
dprintf(this->fd, "%s%s (%s:%d)\n", LOGGER_TAG_TRACE, &file[0], fn, lineno); char trace_msg[1024];
if (!msg.empty())
sprintf(trace_msg, "%s (%s:%d) -> %s", &file[0], fn, lineno, msg.c_str());
else
sprintf(trace_msg, "%s (%s:%d)", &file[0], fn, lineno);
this->output(LOGGER_TAG_TRACE, trace_msg);
#endif
} }