Added central logging
This commit is contained in:
parent
64883b208f
commit
b476d2a7e8
@ -11,6 +11,7 @@
|
|||||||
#include "AssertFailed.h"
|
#include "AssertFailed.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "backtrace.h"
|
#include "backtrace.h"
|
||||||
|
#include "../logging/logging.h"
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
namespace _assert {
|
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) {
|
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) {
|
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();
|
abort();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,11 @@
|
|||||||
#include <cxxabi.h>
|
#include <cxxabi.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include "../logging/logging.h"
|
||||||
|
|
||||||
using std::string;
|
using std::string;
|
||||||
using std::ostringstream;
|
using std::ostringstream;
|
||||||
|
using namespace cpputils::logging;
|
||||||
|
|
||||||
//TODO Use the following? https://github.com/bombela/backward-cpp
|
//TODO Use the following? https://github.com/bombela/backward-cpp
|
||||||
|
|
||||||
@ -56,7 +58,7 @@ namespace cpputils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void sigsegv_handler(int) {
|
void sigsegv_handler(int) {
|
||||||
std::cerr << "Error: SIGSEGV\n" << backtrace() << std::endl;
|
LOG(ERROR) << "SIGSEGV\n" << backtrace();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
google/gmock: 4
|
google/gmock: 4
|
||||||
google/gtest: 11
|
google/gtest: 11
|
||||||
messmer/cmake: 3
|
messmer/cmake: 3
|
||||||
|
messmer/spdlog: 0
|
||||||
|
|
||||||
[parent]
|
[parent]
|
||||||
messmer/cpp-utils: 3
|
messmer/cpp-utils: 3
|
||||||
|
@ -10,6 +10,9 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "../logging/logging.h"
|
||||||
|
|
||||||
|
using namespace cpputils::logging;
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
|
|
||||||
@ -31,13 +34,13 @@ namespace cpputils {
|
|||||||
// Create a new SID for the child process
|
// Create a new SID for the child process
|
||||||
pid_t sid = setsid();
|
pid_t sid = setsid();
|
||||||
if (sid < 0) {
|
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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change the current working directory to a directory that's always existin
|
// Change the current working directory to a directory that's always existin
|
||||||
if ((chdir("/")) < 0) {
|
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);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
35
logging/Logger.h
Normal file
35
logging/Logger.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef MESSMER_CPPUTILS_LOGGING_LOGGER_H
|
||||||
|
#define MESSMER_CPPUTILS_LOGGING_LOGGER_H
|
||||||
|
|
||||||
|
#include <messmer/spdlog/include/spdlog/spdlog.h>
|
||||||
|
#include "../macros.h"
|
||||||
|
|
||||||
|
namespace cpputils {
|
||||||
|
namespace logging {
|
||||||
|
class Logger {
|
||||||
|
public:
|
||||||
|
Logger() : _logger(spdlog::stdout_logger_mt("Log")) { }
|
||||||
|
|
||||||
|
void setLogger(std::shared_ptr<spdlog::logger> logger) {
|
||||||
|
_logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
spdlog::logger *operator->() {
|
||||||
|
return _logger.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::shared_ptr<spdlog::logger> _logger;
|
||||||
|
|
||||||
|
DISALLOW_COPY_AND_ASSIGN(Logger);
|
||||||
|
};
|
||||||
|
|
||||||
|
inline Logger &logger() {
|
||||||
|
static Logger singleton;
|
||||||
|
return singleton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
32
logging/logging.h
Normal file
32
logging/logging.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#ifndef MESSMER_CPPUTILS_LOGGING_LOGGING_H
|
||||||
|
#define MESSMER_CPPUTILS_LOGGING_LOGGING_H
|
||||||
|
|
||||||
|
#include "Logger.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
namespace cpputils {
|
||||||
|
namespace logging {
|
||||||
|
//TODO Test whole logging folder
|
||||||
|
|
||||||
|
enum Level {
|
||||||
|
ERROR, WARN, INFO, DEBUG
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void setLogger(std::shared_ptr<spdlog::logger> 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
|
@ -1,4 +1,7 @@
|
|||||||
#include "LoopThread.h"
|
#include "LoopThread.h"
|
||||||
|
#include "../logging/logging.h"
|
||||||
|
|
||||||
|
using namespace cpputils::logging;
|
||||||
|
|
||||||
namespace cpputils {
|
namespace cpputils {
|
||||||
|
|
||||||
@ -26,10 +29,10 @@ namespace cpputils {
|
|||||||
//Do nothing, exit thread.
|
//Do nothing, exit thread.
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
//TODO Think about logging
|
//TODO Think about logging
|
||||||
std::cerr << "LoopThread crashed: " << e.what() << std::endl;
|
LOG(ERROR) << "LoopThread crashed: " << e.what();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
//TODO Think about logging
|
//TODO Think about logging
|
||||||
std::cerr << "LoopThread crashed" << std::endl;
|
LOG(ERROR) << "LoopThread crashed";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user