daemonize() redirects logger to syslog

This commit is contained in:
Sebastian Meßmer 2015-10-17 03:27:49 +02:00
parent b476d2a7e8
commit 02c49d986b
2 changed files with 7 additions and 2 deletions

View File

@ -18,7 +18,7 @@ namespace cpputils {
//TODO Test daemonize() //TODO Test daemonize()
void daemonize() { void daemonize(const std::string &daemonName) {
pid_t pid = fork(); pid_t pid = fork();
if (pid < 0) { if (pid < 0) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -44,6 +44,9 @@ namespace cpputils {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Setup logging to syslog.
cpputils::logging::setLogger(spdlog::syslog_logger(daemonName, daemonName, LOG_PID));
// Close out the standard file descriptors. The daemon can't use them anyhow. // Close out the standard file descriptors. The daemon can't use them anyhow.
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);

View File

@ -2,8 +2,10 @@
#ifndef MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H #ifndef MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
#define MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H #define MESSMER_CPPUTILS_DAEMON_DAEMONIZE_H
#include <string>
namespace cpputils { namespace cpputils {
void daemonize(); void daemonize(const std::string &daemonName);
} }
#endif #endif