From b476d2a7e8e6a2f21a14483ce5f812bd899e4d05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Me=C3=9Fmer?= Date: Sat, 17 Oct 2015 03:08:56 +0200 Subject: [PATCH] Added central logging --- assert/assert.h | 9 +++++++-- assert/backtrace.cpp | 4 +++- biicode.conf | 1 + daemon/daemonize.cpp | 7 +++++-- logging/Logger.h | 35 +++++++++++++++++++++++++++++++++++ logging/logging.h | 32 ++++++++++++++++++++++++++++++++ random/LoopThread.cpp | 7 +++++-- 7 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 logging/Logger.h create mode 100644 logging/logging.h diff --git a/assert/assert.h b/assert/assert.h index 761e27c8..bcce66c9 100644 --- a/assert/assert.h +++ b/assert/assert.h @@ -11,6 +11,7 @@ #include "AssertFailed.h" #include #include "backtrace.h" +#include "../logging/logging.h" namespace cpputils { namespace _assert { @@ -20,11 +21,15 @@ namespace cpputils { } inline void assert_fail_release [[noreturn]] (const char *expr, const char *message, const char *file, int line) { - throw AssertFailed(format(expr, message, file, line)); + auto msg = format(expr, message, file, line); + using namespace logging; + LOG(ERROR) << msg; + throw AssertFailed(msg); } inline void assert_fail_debug [[noreturn]] (const char *expr, const char *message, const char *file, int line) { - std::cerr << format(expr, message, file, line) << std::endl; + using namespace logging; + LOG(ERROR) << format(expr, message, file, line); abort(); } } diff --git a/assert/backtrace.cpp b/assert/backtrace.cpp index 4a3bc0d9..24885380 100644 --- a/assert/backtrace.cpp +++ b/assert/backtrace.cpp @@ -6,9 +6,11 @@ #include #include #include +#include "../logging/logging.h" using std::string; using std::ostringstream; +using namespace cpputils::logging; //TODO Use the following? https://github.com/bombela/backward-cpp @@ -56,7 +58,7 @@ namespace cpputils { } void sigsegv_handler(int) { - std::cerr << "Error: SIGSEGV\n" << backtrace() << std::endl; + LOG(ERROR) << "SIGSEGV\n" << backtrace(); exit(1); } diff --git a/biicode.conf b/biicode.conf index bf66e418..af4a83fc 100644 --- a/biicode.conf +++ b/biicode.conf @@ -5,6 +5,7 @@ google/gmock: 4 google/gtest: 11 messmer/cmake: 3 + messmer/spdlog: 0 [parent] messmer/cpp-utils: 3 diff --git a/daemon/daemonize.cpp b/daemon/daemonize.cpp index bb22d5b9..4e72c480 100644 --- a/daemon/daemonize.cpp +++ b/daemon/daemonize.cpp @@ -10,6 +10,9 @@ #include #include #include +#include "../logging/logging.h" + +using namespace cpputils::logging; namespace cpputils { @@ -31,13 +34,13 @@ namespace cpputils { // Create a new SID for the child process pid_t sid = setsid(); if (sid < 0) { - std::cerr << "Failed to get SID for daemon process" << std::endl; + LOG(ERROR) << "Failed to get SID for daemon process"; exit(EXIT_FAILURE); } // Change the current working directory to a directory that's always existin if ((chdir("/")) < 0) { - std::cerr << "Failed to change working directory for daemon process" << std::endl; + LOG(ERROR) << "Failed to change working directory for daemon process"; exit(EXIT_FAILURE); } diff --git a/logging/Logger.h b/logging/Logger.h new file mode 100644 index 00000000..fef18b4a --- /dev/null +++ b/logging/Logger.h @@ -0,0 +1,35 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_LOGGING_LOGGER_H +#define MESSMER_CPPUTILS_LOGGING_LOGGER_H + +#include +#include "../macros.h" + +namespace cpputils { +namespace logging { + class Logger { + public: + Logger() : _logger(spdlog::stdout_logger_mt("Log")) { } + + void setLogger(std::shared_ptr logger) { + _logger = logger; + } + + spdlog::logger *operator->() { + return _logger.get(); + } + + private: + std::shared_ptr _logger; + + DISALLOW_COPY_AND_ASSIGN(Logger); + }; + + inline Logger &logger() { + static Logger singleton; + return singleton; + } +} +} + +#endif diff --git a/logging/logging.h b/logging/logging.h new file mode 100644 index 00000000..8b682bb3 --- /dev/null +++ b/logging/logging.h @@ -0,0 +1,32 @@ +#pragma once +#ifndef MESSMER_CPPUTILS_LOGGING_LOGGING_H +#define MESSMER_CPPUTILS_LOGGING_LOGGING_H + +#include "Logger.h" +#include + +namespace cpputils { + namespace logging { + //TODO Test whole logging folder + + enum Level { + ERROR, WARN, INFO, DEBUG + }; + + inline void setLogger(std::shared_ptr newLogger) { + logger().setLogger(newLogger); + } + + inline spdlog::details::line_logger LOG(Level level) { + switch(level) { + case ERROR: return logger()->error(); + case WARN: return logger()->warn(); + case INFO: return logger()->info(); + case DEBUG: return logger()->debug(); + } + throw std::logic_error("Unknown logger level"); + } + } +} + +#endif diff --git a/random/LoopThread.cpp b/random/LoopThread.cpp index 4b1b00f5..d53df0ff 100644 --- a/random/LoopThread.cpp +++ b/random/LoopThread.cpp @@ -1,4 +1,7 @@ #include "LoopThread.h" +#include "../logging/logging.h" + +using namespace cpputils::logging; namespace cpputils { @@ -26,10 +29,10 @@ namespace cpputils { //Do nothing, exit thread. } catch (const std::exception &e) { //TODO Think about logging - std::cerr << "LoopThread crashed: " << e.what() << std::endl; + LOG(ERROR) << "LoopThread crashed: " << e.what(); } catch (...) { //TODO Think about logging - std::cerr << "LoopThread crashed" << std::endl; + LOG(ERROR) << "LoopThread crashed"; } } } \ No newline at end of file