implement support to send logs to the syslog;

3.6-maint
Bryan Biedenkapp 2 years ago
parent 9cf87ff00c
commit 2f918d759d

@ -23,6 +23,8 @@ log:
displayLevel: 1 displayLevel: 1
# File logging level. # File logging level.
fileLevel: 1 fileLevel: 1
# Flag indicating file logs should be sent to syslog instead of a file.
useSyslog: false
# Full path for the directory to store the log files. # Full path for the directory to store the log files.
filePath: . filePath: .
# Full path for the directory to store the activity log files. # Full path for the directory to store the activity log files.

@ -24,6 +24,8 @@ log:
fileLevel: 1 fileLevel: 1
# Full path for the directory to store the log files. # Full path for the directory to store the log files.
filePath: . filePath: .
# Flag indicating file logs should be sent to syslog instead of a file.
useSyslog: false
# Full path for the directory to store the activity log files. # Full path for the directory to store the activity log files.
activityFilePath: . activityFilePath: .
# Log filename prefix. # Log filename prefix.

@ -16,6 +16,7 @@
#include "network/BaseNetwork.h" #include "network/BaseNetwork.h"
#include <sys/time.h> #include <sys/time.h>
#include <syslog.h>
#if defined(CATCH2_TEST_COMPILATION) #if defined(CATCH2_TEST_COMPILATION)
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
@ -52,6 +53,8 @@ static FILE* m_fpLog = nullptr;
uint32_t g_logDisplayLevel = 2U; uint32_t g_logDisplayLevel = 2U;
bool g_disableTimeDisplay = false; bool g_disableTimeDisplay = false;
bool g_useSyslog = false;
static struct tm m_tm; static struct tm m_tm;
static std::ostream m_outStream { std::cerr.rdbuf() }; static std::ostream m_outStream { std::cerr.rdbuf() };
@ -92,27 +95,52 @@ static bool LogOpen()
if (m_fileLevel == 0U) if (m_fileLevel == 0U)
return true; return true;
time_t now; if (!g_useSyslog) {
::time(&now); time_t now;
::time(&now);
struct tm* tm = ::gmtime(&now); struct tm* tm = ::gmtime(&now);
if (tm->tm_mday == m_tm.tm_mday && tm->tm_mon == m_tm.tm_mon && tm->tm_year == m_tm.tm_year) { if (tm->tm_mday == m_tm.tm_mday && tm->tm_mon == m_tm.tm_mon && tm->tm_year == m_tm.tm_year) {
if (m_fpLog != nullptr) if (m_fpLog != nullptr)
return true; return true;
} }
else { else {
if (m_fpLog != nullptr) if (m_fpLog != nullptr)
::fclose(m_fpLog); ::fclose(m_fpLog);
} }
char filename[200U]; char filename[200U];
::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); ::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
m_fpLog = ::fopen(filename, "a+t"); m_fpLog = ::fopen(filename, "a+t");
m_tm = *tm; m_tm = *tm;
return m_fpLog != nullptr; return m_fpLog != nullptr;
}
else {
switch (m_fileLevel) {
case 1U:
setlogmask(LOG_UPTO(LOG_DEBUG));
break;
case 2U:
setlogmask(LOG_UPTO(LOG_INFO));
break;
case 3U:
setlogmask(LOG_UPTO(LOG_NOTICE));
break;
case 4U:
setlogmask(LOG_UPTO(LOG_WARNING));
break;
case 5U:
default:
setlogmask(LOG_UPTO(LOG_ERR));
break;
}
openlog(m_fileRoot.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
return true;
}
} }
/// <summary> /// <summary>
@ -153,13 +181,16 @@ void LogSetNetwork(void* network)
/// <param name="fileLevel">File logging level.</param> /// <param name="fileLevel">File logging level.</param>
/// <param name="displayLevel">Console logging level.</param> /// <param name="displayLevel">Console logging level.</param>
/// <param name="disableTimeDisplay">Disable display of date/time on the console log.</param> /// <param name="disableTimeDisplay">Disable display of date/time on the console log.</param>
bool LogInitialise(const std::string& filePath, const std::string& fileRoot, uint32_t fileLevel, uint32_t displayLevel, bool disableTimeDisplay) /// <param name="useSyslog">Flag indicating whether or not logging should be sent to the syslog.</param>
bool LogInitialise(const std::string& filePath, const std::string& fileRoot, uint32_t fileLevel, uint32_t displayLevel, bool disableTimeDisplay, bool useSyslog)
{ {
m_filePath = filePath; m_filePath = filePath;
m_fileRoot = fileRoot; m_fileRoot = fileRoot;
m_fileLevel = fileLevel; m_fileLevel = fileLevel;
g_logDisplayLevel = displayLevel; g_logDisplayLevel = displayLevel;
g_disableTimeDisplay = disableTimeDisplay; g_disableTimeDisplay = disableTimeDisplay;
if (!g_useSyslog)
g_useSyslog = useSyslog;
return ::LogOpen(); return ::LogOpen();
} }
@ -173,6 +204,8 @@ void LogFinalise()
#endif #endif
if (m_fpLog != nullptr) if (m_fpLog != nullptr)
::fclose(m_fpLog); ::fclose(m_fpLog);
if (g_useSyslog)
closelog();
} }
/// <summary> /// <summary>
@ -189,7 +222,7 @@ void Log(uint32_t level, const char *module, const char* fmt, ...)
g_disableTimeDisplay = true; g_disableTimeDisplay = true;
#endif #endif
char buffer[LOG_BUFFER_LEN]; char buffer[LOG_BUFFER_LEN];
if (!g_disableTimeDisplay) { if (!g_disableTimeDisplay && !g_useSyslog) {
struct timeval now; struct timeval now;
::gettimeofday(&now, NULL); ::gettimeofday(&now, NULL);
@ -243,22 +276,52 @@ void Log(uint32_t level, const char *module, const char* fmt, ...)
#endif #endif
if (level >= m_fileLevel && m_fileLevel != 0U) { if (level >= m_fileLevel && m_fileLevel != 0U) {
bool ret = ::LogOpen(); if (!g_useSyslog) {
if (!ret) bool ret = ::LogOpen();
return; if (!ret)
return;
::fprintf(m_fpLog, "%s\n", buffer);
::fflush(m_fpLog);
} else {
// convert our log level into syslog level
int syslogLevel = LOG_INFO;
switch (level) {
case 1U:
syslogLevel = LOG_DEBUG;
break;
case 2U:
syslogLevel = LOG_NOTICE;
break;
case 3U:
syslogLevel = LOG_INFO;
break;
case 4U:
syslogLevel = LOG_WARNING;
break;
case 5U:
syslogLevel = LOG_ERR;
break;
default:
syslogLevel = LOG_EMERG;
break;
}
::fprintf(m_fpLog, "%s\n", buffer); syslog(syslogLevel, "%s", buffer);
::fflush(m_fpLog); }
} }
if (level >= g_logDisplayLevel && g_logDisplayLevel != 0U) { if (!g_useSyslog && level >= g_logDisplayLevel && g_logDisplayLevel != 0U) {
::fprintf(stdout, "%s" EOL, buffer); ::fprintf(stdout, "%s" EOL, buffer);
::fflush(stdout); ::fflush(stdout);
} }
// fatal error (specially allow any log levels above 9999) // fatal error (specially allow any log levels above 9999)
if (level >= 6U && level < 9999U) { if (level >= 6U && level < 9999U) {
::fclose(m_fpLog); if (m_fpLog != nullptr)
::fclose(m_fpLog);
if (g_useSyslog)
::closelog();
exit(1); exit(1);
} }
} }

@ -53,6 +53,8 @@
extern uint32_t g_logDisplayLevel; extern uint32_t g_logDisplayLevel;
extern bool g_disableTimeDisplay; extern bool g_disableTimeDisplay;
extern bool g_useSyslog;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Global Functions // Global Functions
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -73,7 +75,7 @@ extern HOST_SW_API void* LogGetNetwork();
extern HOST_SW_API void LogSetNetwork(void* network); extern HOST_SW_API void LogSetNetwork(void* network);
/// <summary>Initializes the diagnostics log.</summary> /// <summary>Initializes the diagnostics log.</summary>
extern HOST_SW_API bool LogInitialise(const std::string& filePath, const std::string& fileRoot, uint32_t fileLevel, uint32_t displayLevel, bool disableTimeDisplay = false); extern HOST_SW_API bool LogInitialise(const std::string& filePath, const std::string& fileRoot, uint32_t fileLevel, uint32_t displayLevel, bool disableTimeDisplay = false, bool useSyslog = false);
/// <summary>Finalizes the diagnostics log.</summary> /// <summary>Finalizes the diagnostics log.</summary>
extern HOST_SW_API void LogFinalise(); extern HOST_SW_API void LogFinalise();
/// <summary>Writes a new entry to the diagnostics log.</summary> /// <summary>Writes a new entry to the diagnostics log.</summary>

@ -100,12 +100,15 @@ void usage(const char* message, const char* arg)
::fprintf(stdout, ::fprintf(stdout,
"usage: %s [-vhf]" "usage: %s [-vhf]"
"[--syslog]"
"[-c <configuration file>]" "[-c <configuration file>]"
"\n\n" "\n\n"
" -v show version information\n" " -v show version information\n"
" -h show this screen\n" " -h show this screen\n"
" -f foreground mode\n" " -f foreground mode\n"
"\n" "\n"
" --syslog force logging to syslog\n"
"\n"
" -c <file> specifies the configuration file to use\n" " -c <file> specifies the configuration file to use\n"
"\n" "\n"
" -- stop handling options\n", " -- stop handling options\n",
@ -140,6 +143,9 @@ int checkArgs(int argc, char* argv[])
else if (IS("-f")) { else if (IS("-f")) {
g_foreground = true; g_foreground = true;
} }
else if (IS("--syslog")) {
g_useSyslog = true;
}
else if (IS("-c")) { else if (IS("-c")) {
if (argc-- <= 0) if (argc-- <= 0)
usage("error: %s", "must specify the configuration file to use"); usage("error: %s", "must specify the configuration file to use");

@ -99,7 +99,7 @@ int HostFNE::run()
// initialize system logging // initialize system logging
yaml::Node logConf = m_conf["log"]; yaml::Node logConf = m_conf["log"];
ret = ::LogInitialise(logConf["filePath"].as<std::string>(), logConf["fileRoot"].as<std::string>(), ret = ::LogInitialise(logConf["filePath"].as<std::string>(), logConf["fileRoot"].as<std::string>(),
logConf["fileLevel"].as<uint32_t>(0U), logConf["displayLevel"].as<uint32_t>(0U)); logConf["fileLevel"].as<uint32_t>(0U), logConf["displayLevel"].as<uint32_t>(0U), false, logConf["useSyslog"].as<bool>(false));
if (!ret) { if (!ret) {
::fatal("unable to open the log file\n"); ::fatal("unable to open the log file\n");
} }

@ -174,7 +174,7 @@ int Host::run()
// initialize system logging // initialize system logging
yaml::Node logConf = m_conf["log"]; yaml::Node logConf = m_conf["log"];
ret = ::LogInitialise(logConf["filePath"].as<std::string>(), logConf["fileRoot"].as<std::string>(), ret = ::LogInitialise(logConf["filePath"].as<std::string>(), logConf["fileRoot"].as<std::string>(),
logConf["fileLevel"].as<uint32_t>(0U), logConf["displayLevel"].as<uint32_t>(0U)); logConf["fileLevel"].as<uint32_t>(0U), logConf["displayLevel"].as<uint32_t>(0U), false, logConf["useSyslog"].as<bool>(false));
if (!ret) { if (!ret) {
::fatal("unable to open the log file\n"); ::fatal("unable to open the log file\n");
} }

@ -119,6 +119,7 @@ void usage(const char* message, const char* arg)
::fprintf(stdout, ::fprintf(stdout,
"usage: %s [-vhdf]" "usage: %s [-vhdf]"
"[--syslog]"
#if defined(ENABLE_SETUP_TUI) #if defined(ENABLE_SETUP_TUI)
"[--setup]" "[--setup]"
#else #else
@ -132,6 +133,8 @@ void usage(const char* message, const char* arg)
" -d force modem debug\n" " -d force modem debug\n"
" -f foreground mode\n" " -f foreground mode\n"
"\n" "\n"
" --syslog force logging to syslog\n"
"\n"
#if defined(ENABLE_SETUP_TUI) #if defined(ENABLE_SETUP_TUI)
" --setup setup and calibration mode\n" " --setup setup and calibration mode\n"
#else #else
@ -179,6 +182,9 @@ int checkArgs(int argc, char* argv[])
else if (IS("-f")) { else if (IS("-f")) {
g_foreground = true; g_foreground = true;
} }
else if (IS("--syslog")) {
g_useSyslog = true;
}
else if (IS("--cal")) { else if (IS("--cal")) {
g_calibrate = true; g_calibrate = true;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.