mirror of
https://github.com/binary-kitchen/doorlockd
synced 2024-12-22 10:24:26 +01:00
Improved Logging
- removed daemon.cpp - Added additional output logfile to Logger class
This commit is contained in:
parent
f504cf4e92
commit
54f22393a4
@ -75,8 +75,6 @@ set(LIBDOORLOCK_SRCS
|
|||||||
lib/util.h)
|
lib/util.h)
|
||||||
|
|
||||||
set(DOORLOCKD_SRCS
|
set(DOORLOCKD_SRCS
|
||||||
daemon/daemon.cpp
|
|
||||||
daemon/daemon.h
|
|
||||||
daemon/doorlockd.cpp)
|
daemon/doorlockd.cpp)
|
||||||
|
|
||||||
set(DOORLOCK_CLIENT_SRCS
|
set(DOORLOCK_CLIENT_SRCS
|
||||||
|
@ -22,7 +22,7 @@ const static std::string version =
|
|||||||
const static std::string gitversion =
|
const static std::string gitversion =
|
||||||
DOORLOCK_GIT_BRANCH "-" DOORLOCK_GIT_COMMIT_HASH;
|
DOORLOCK_GIT_BRANCH "-" DOORLOCK_GIT_COMMIT_HASH;
|
||||||
|
|
||||||
const static Logger &l = Logger::get();
|
static Logger &l = Logger::get();
|
||||||
|
|
||||||
namespace po = boost::program_options;
|
namespace po = boost::program_options;
|
||||||
namespace ba = boost::asio;
|
namespace ba = boost::asio;
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
#include <stdexcept>
|
|
||||||
#include <cstring>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
#include <sys/resource.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
|
|
||||||
#include "daemon.h"
|
|
||||||
|
|
||||||
void daemonize(const std::string &dir,
|
|
||||||
const std::string &stdinfile,
|
|
||||||
const std::string &stdoutfile,
|
|
||||||
const std::string &stderrfile)
|
|
||||||
{
|
|
||||||
umask(0);
|
|
||||||
|
|
||||||
rlimit rl;
|
|
||||||
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
|
|
||||||
{
|
|
||||||
throw std::runtime_error(strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!dir.empty() && chdir(dir.c_str()) < 0)
|
|
||||||
{
|
|
||||||
throw std::runtime_error(strerror(errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rl.rlim_max == RLIM_INFINITY)
|
|
||||||
{
|
|
||||||
rl.rlim_max = 1024;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < rl.rlim_max; i++)
|
|
||||||
{
|
|
||||||
close(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
int fd0 = open(stdinfile.c_str(), O_RDONLY);
|
|
||||||
int fd1 = open(stdoutfile.c_str(),
|
|
||||||
O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
|
|
||||||
int fd2 = open(stderrfile.c_str(),
|
|
||||||
O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
|
|
||||||
|
|
||||||
if (fd0 != STDIN_FILENO || fd1 != STDOUT_FILENO || fd2 != STDERR_FILENO)
|
|
||||||
{
|
|
||||||
throw std::runtime_error("new standard file descriptors were not opened as expected");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
#ifndef DAEMON_H
|
|
||||||
#define DAEMON_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
// Daemonizes the process if daemonize is true.
|
|
||||||
// If daemonize is true, it will write the new PID to the file "pidFile"
|
|
||||||
//
|
|
||||||
// This function will also redirect stdin, out and err to the
|
|
||||||
// specified files
|
|
||||||
void daemonize(const std::string &dir,
|
|
||||||
const std::string &stdinfile,
|
|
||||||
const std::string &stdoutfile,
|
|
||||||
const std::string &stderrfile);
|
|
||||||
|
|
||||||
#endif
|
|
@ -9,7 +9,6 @@
|
|||||||
#include "../lib/logic.h"
|
#include "../lib/logic.h"
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "daemon.h"
|
|
||||||
|
|
||||||
namespace po = boost::program_options;
|
namespace po = boost::program_options;
|
||||||
namespace ba = boost::asio;
|
namespace ba = boost::asio;
|
||||||
@ -24,7 +23,7 @@ const static std::string gitversion =
|
|||||||
// The receive buffer length of the TCP socket
|
// The receive buffer length of the TCP socket
|
||||||
const int constexpr SOCKET_BUFFERLENGTH = 2048;
|
const int constexpr SOCKET_BUFFERLENGTH = 2048;
|
||||||
|
|
||||||
const static Logger &l = Logger::get();
|
static Logger &l = Logger::get();
|
||||||
|
|
||||||
static std::unique_ptr<Logic> logic = nullptr;
|
static std::unique_ptr<Logic> logic = nullptr;
|
||||||
static ba::io_service io_service;
|
static ba::io_service io_service;
|
||||||
@ -167,10 +166,6 @@ int main(int argc, char** argv)
|
|||||||
std::string serDev;
|
std::string serDev;
|
||||||
unsigned int baudrate;
|
unsigned int baudrate;
|
||||||
|
|
||||||
l((std::string)"Hello, this is " + version + " built on " + gitversion,
|
|
||||||
LogLevel::info);
|
|
||||||
l(LogLevel::notice, "Starting doorlockd");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
unsigned int timeout;
|
unsigned int timeout;
|
||||||
po::options_description desc("doorlockd (" + version + " built on " + gitversion + ")");
|
po::options_description desc("doorlockd (" + version + " built on " + gitversion + ")");
|
||||||
@ -209,26 +204,25 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
std::cout << desc << std::endl;
|
std::cout << desc << std::endl;
|
||||||
retval = 0;
|
retval = 0;
|
||||||
goto out;
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
po::notify(vm);
|
po::notify(vm);
|
||||||
|
|
||||||
tokenTimeout = std::chrono::seconds(timeout);
|
tokenTimeout = std::chrono::seconds(timeout);
|
||||||
|
|
||||||
|
l.setLogFile(logfile);
|
||||||
|
l.logFile(true);
|
||||||
}
|
}
|
||||||
catch(const std::exception &e)
|
catch(const std::exception &e)
|
||||||
{
|
{
|
||||||
std::cerr << "Error: " << e.what() << "\n";
|
std::cerr << "Error: " << e.what() << "\n";
|
||||||
goto out;
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
daemonize("/",
|
|
||||||
"/dev/null",
|
|
||||||
logfile,
|
|
||||||
logfile);
|
|
||||||
// Resend version string after redirection stdout
|
|
||||||
l((std::string)"Hello, this is " + version + " built on " + gitversion,
|
l((std::string)"Hello, this is " + version + " built on " + gitversion,
|
||||||
LogLevel::info);
|
LogLevel::info);
|
||||||
|
l(LogLevel::notice, "Starting doorlockd");
|
||||||
|
|
||||||
signal(SIGINT, signal_handler);
|
signal(SIGINT, signal_handler);
|
||||||
signal(SIGKILL, signal_handler);
|
signal(SIGKILL, signal_handler);
|
||||||
@ -237,6 +231,8 @@ int main(int argc, char** argv)
|
|||||||
signal(SIGUSR2, signal_handler);
|
signal(SIGUSR2, signal_handler);
|
||||||
|
|
||||||
l(LogLevel::info, "Starting Doorlock Logic");
|
l(LogLevel::info, "Starting Doorlock Logic");
|
||||||
|
|
||||||
|
retval = 0;
|
||||||
try {
|
try {
|
||||||
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
|
logic = std::unique_ptr<Logic>(new Logic(tokenTimeout,
|
||||||
ldapUri,
|
ldapUri,
|
||||||
@ -250,12 +246,8 @@ int main(int argc, char** argv)
|
|||||||
catch (...) {
|
catch (...) {
|
||||||
l(LogLevel::error, "Fatal error, shutting down");
|
l(LogLevel::error, "Fatal error, shutting down");
|
||||||
retval = -1;
|
retval = -1;
|
||||||
goto out;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = 0;
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (logic) {
|
if (logic) {
|
||||||
l(LogLevel::info, "Stopping Doorlock Logic");
|
l(LogLevel::info, "Stopping Doorlock Logic");
|
||||||
logic.reset();
|
logic.reset();
|
||||||
|
@ -66,7 +66,7 @@ private:
|
|||||||
|
|
||||||
DoorCallback _doorCallback = { };
|
DoorCallback _doorCallback = { };
|
||||||
|
|
||||||
const Logger &_logger;
|
Logger &_logger;
|
||||||
|
|
||||||
// Writed command to AVR board
|
// Writed command to AVR board
|
||||||
bool writeCMD(char c);
|
bool writeCMD(char c);
|
||||||
|
@ -65,6 +65,8 @@ Logger::Logger(const LogLevel level) :
|
|||||||
|
|
||||||
Logger::~Logger()
|
Logger::~Logger()
|
||||||
{
|
{
|
||||||
|
if (_logFile.is_open())
|
||||||
|
_logFile.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger &Logger::get()
|
Logger &Logger::get()
|
||||||
@ -73,7 +75,7 @@ Logger &Logger::get()
|
|||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::operator ()(const std::string &message, const LogLevel level) const
|
void Logger::operator ()(const std::string &message, const LogLevel level)
|
||||||
{
|
{
|
||||||
if(level > _level)
|
if(level > _level)
|
||||||
{
|
{
|
||||||
@ -104,10 +106,16 @@ void Logger::operator ()(const std::string &message, const LogLevel level) const
|
|||||||
lock_guard<mutex> lock(_ostreamMutex);
|
lock_guard<mutex> lock(_ostreamMutex);
|
||||||
|
|
||||||
#if defined(USE_COLORIZED_LOGS) && !defined(_WIN32)
|
#if defined(USE_COLORIZED_LOGS) && !defined(_WIN32)
|
||||||
|
if (_consoleActive) {
|
||||||
cerr << prefix.str() << message << suffix_ansireset << endl;
|
cerr << prefix.str() << message << suffix_ansireset << endl;
|
||||||
cerr.flush();
|
cerr.flush();
|
||||||
|
}
|
||||||
|
if (_logFileActive && _logFile.is_open()) {
|
||||||
|
_logFile << prefix.str() << message << suffix_ansireset << endl;
|
||||||
|
}
|
||||||
#elif defined(USE_COLORIZED_LOGS) && defined(_WIN32)
|
#elif defined(USE_COLORIZED_LOGS) && defined(_WIN32)
|
||||||
|
|
||||||
|
if (_consoleActive) {
|
||||||
// taken from GTEST
|
// taken from GTEST
|
||||||
const HANDLE stdout_handle = GetStdHandle(STD_ERROR_HANDLE);
|
const HANDLE stdout_handle = GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
|
||||||
@ -128,20 +136,29 @@ void Logger::operator ()(const std::string &message, const LogLevel level) const
|
|||||||
|
|
||||||
// Restores the text color.
|
// Restores the text color.
|
||||||
SetConsoleTextAttribute(stdout_handle, old_color_attrs);
|
SetConsoleTextAttribute(stdout_handle, old_color_attrs);
|
||||||
|
}
|
||||||
|
if (_logFileActive && _logFile.is_open()) {
|
||||||
|
_logFile << prefix.str() << message << endl;
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
if (_consoleActive) {
|
||||||
cerr << prefix.str() << message << endl;
|
cerr << prefix.str() << message << endl;
|
||||||
cerr.flush();
|
cerr.flush();
|
||||||
|
}
|
||||||
|
if (_logFileActive && _logFile.is_open()) {
|
||||||
|
_logFile << prefix.str() << message << endl;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::operator ()(const std::ostringstream &message, const LogLevel level) const
|
void Logger::operator ()(const std::ostringstream &message, const LogLevel level)
|
||||||
{
|
{
|
||||||
(*this)(message.str(), level);
|
(*this)(message.str(), level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::operator ()(const LogLevel level, const char* format, ...) const
|
void Logger::operator ()(const LogLevel level, const char* format, ...)
|
||||||
{
|
{
|
||||||
if(level > _level)
|
if(level > _level)
|
||||||
{
|
{
|
||||||
@ -188,5 +205,34 @@ LogLevel Logger::level() const
|
|||||||
return _level;
|
return _level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void foo(void) {
|
bool Logger::console() const
|
||||||
|
{
|
||||||
|
return _consoleActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::console(const bool active)
|
||||||
|
{
|
||||||
|
lock_guard<mutex> lock(_ostreamMutex);
|
||||||
|
_consoleActive = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Logger::logFile() const
|
||||||
|
{
|
||||||
|
return _logFileActive;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::logFile(const bool active)
|
||||||
|
{
|
||||||
|
lock_guard<mutex> lock(_ostreamMutex);
|
||||||
|
_logFileActive = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Logger::setLogFile(const std::string &logFile)
|
||||||
|
{
|
||||||
|
if (_logFile.is_open())
|
||||||
|
_logFile.close();
|
||||||
|
|
||||||
|
_logFile.open(logFile, std::ofstream::out | std::ofstream::app);
|
||||||
|
if (!_logFile.is_open())
|
||||||
|
throw std::runtime_error("Unable to open " + logFile);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#ifndef LOGGER_H
|
#ifndef LOGGER_H
|
||||||
#define LOGGER_H
|
#define LOGGER_H
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -32,19 +33,28 @@ public:
|
|||||||
static Logger &get();
|
static Logger &get();
|
||||||
|
|
||||||
/// Log a string
|
/// Log a string
|
||||||
void operator() (const std::string &message, const LogLevel level = LogLevel::debug) const;
|
void operator() (const std::string &message, const LogLevel level = LogLevel::debug);
|
||||||
|
|
||||||
/// Log a ostringstream
|
/// Log a ostringstream
|
||||||
void operator() (const std::ostringstream &message, const LogLevel level = LogLevel::debug) const;
|
void operator() (const std::ostringstream &message, const LogLevel level = LogLevel::debug);
|
||||||
|
|
||||||
/// Log a format string
|
/// Log a format string
|
||||||
void operator() (const LogLevel level, const char* format, ...) const;
|
void operator() (const LogLevel level, const char* format, ...);
|
||||||
|
|
||||||
/// Set minimum required log level to generate output
|
/// Set minimum required log level to generate output
|
||||||
void level(const LogLevel level);
|
void level(const LogLevel level);
|
||||||
/// Get minimum required log level to generate output
|
/// Get minimum required log level to generate output
|
||||||
LogLevel level() const;
|
LogLevel level() const;
|
||||||
|
|
||||||
|
/// Getter/Setter for console output
|
||||||
|
bool console() const;
|
||||||
|
void console(const bool active);
|
||||||
|
|
||||||
|
/// Getter/Setter for logfile output
|
||||||
|
bool logFile() const;
|
||||||
|
void logFile(const bool active);
|
||||||
|
void setLogFile(const std::string &logFile);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,6 +64,11 @@ private:
|
|||||||
Logger(const LogLevel level);
|
Logger(const LogLevel level);
|
||||||
~Logger();
|
~Logger();
|
||||||
|
|
||||||
|
bool _consoleActive = { true };
|
||||||
|
bool _logFileActive = { false };
|
||||||
|
|
||||||
|
std::ofstream _logFile = {};
|
||||||
|
|
||||||
LogLevel _level;
|
LogLevel _level;
|
||||||
mutable std::mutex _ostreamMutex;
|
mutable std::mutex _ostreamMutex;
|
||||||
};
|
};
|
||||||
|
@ -62,7 +62,7 @@ private:
|
|||||||
|
|
||||||
void _doorCallback(Doormessage doormessage);
|
void _doorCallback(Doormessage doormessage);
|
||||||
|
|
||||||
const Logger &_logger;
|
Logger &_logger;
|
||||||
|
|
||||||
// The door
|
// The door
|
||||||
Door _door;
|
Door _door;
|
||||||
|
@ -23,7 +23,7 @@ Request::Command Request::_commandFromString(const std::string &command)
|
|||||||
Request Request::fromJson(const Json::Value &root)
|
Request Request::fromJson(const Json::Value &root)
|
||||||
{
|
{
|
||||||
Request retval;
|
Request retval;
|
||||||
const auto &l = Logger::get();
|
auto &l = Logger::get();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const auto commandStr =
|
const auto commandStr =
|
||||||
|
Loading…
Reference in New Issue
Block a user