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
# File logging level.
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.
filePath: .
# Full path for the directory to store the activity log files.

@ -24,6 +24,8 @@ log:
fileLevel: 1
# Full path for the directory to store the log files.
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.
activityFilePath: .
# Log filename prefix.

@ -16,6 +16,7 @@
#include "network/BaseNetwork.h"
#include <sys/time.h>
#include <syslog.h>
#if defined(CATCH2_TEST_COMPILATION)
#include <catch2/catch_test_macros.hpp>
@ -52,6 +53,8 @@ static FILE* m_fpLog = nullptr;
uint32_t g_logDisplayLevel = 2U;
bool g_disableTimeDisplay = false;
bool g_useSyslog = false;
static struct tm m_tm;
static std::ostream m_outStream { std::cerr.rdbuf() };
@ -92,6 +95,7 @@ static bool LogOpen()
if (m_fileLevel == 0U)
return true;
if (!g_useSyslog) {
time_t now;
::time(&now);
@ -114,6 +118,30 @@ static bool LogOpen()
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>
/// Internal helper to set an output stream to direct logging to.
@ -153,13 +181,16 @@ void LogSetNetwork(void* network)
/// <param name="fileLevel">File logging level.</param>
/// <param name="displayLevel">Console logging level.</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_fileRoot = fileRoot;
m_fileLevel = fileLevel;
g_logDisplayLevel = displayLevel;
g_disableTimeDisplay = disableTimeDisplay;
if (!g_useSyslog)
g_useSyslog = useSyslog;
return ::LogOpen();
}
@ -173,6 +204,8 @@ void LogFinalise()
#endif
if (m_fpLog != nullptr)
::fclose(m_fpLog);
if (g_useSyslog)
closelog();
}
/// <summary>
@ -189,7 +222,7 @@ void Log(uint32_t level, const char *module, const char* fmt, ...)
g_disableTimeDisplay = true;
#endif
char buffer[LOG_BUFFER_LEN];
if (!g_disableTimeDisplay) {
if (!g_disableTimeDisplay && !g_useSyslog) {
struct timeval now;
::gettimeofday(&now, NULL);
@ -243,22 +276,52 @@ void Log(uint32_t level, const char *module, const char* fmt, ...)
#endif
if (level >= m_fileLevel && m_fileLevel != 0U) {
if (!g_useSyslog) {
bool ret = ::LogOpen();
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;
}
if (level >= g_logDisplayLevel && g_logDisplayLevel != 0U) {
syslog(syslogLevel, "%s", buffer);
}
}
if (!g_useSyslog && level >= g_logDisplayLevel && g_logDisplayLevel != 0U) {
::fprintf(stdout, "%s" EOL, buffer);
::fflush(stdout);
}
// fatal error (specially allow any log levels above 9999)
if (level >= 6U && level < 9999U) {
if (m_fpLog != nullptr)
::fclose(m_fpLog);
if (g_useSyslog)
::closelog();
exit(1);
}
}

@ -53,6 +53,8 @@
extern uint32_t g_logDisplayLevel;
extern bool g_disableTimeDisplay;
extern bool g_useSyslog;
// ---------------------------------------------------------------------------
// Global Functions
// ---------------------------------------------------------------------------
@ -73,7 +75,7 @@ extern HOST_SW_API void* LogGetNetwork();
extern HOST_SW_API void LogSetNetwork(void* network);
/// <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>
extern HOST_SW_API void LogFinalise();
/// <summary>Writes a new entry to the diagnostics log.</summary>

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

@ -99,7 +99,7 @@ int HostFNE::run()
// initialize system logging
yaml::Node logConf = m_conf["log"];
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) {
::fatal("unable to open the log file\n");
}

@ -174,7 +174,7 @@ int Host::run()
// initialize system logging
yaml::Node logConf = m_conf["log"];
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) {
::fatal("unable to open the log file\n");
}

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

Loading…
Cancel
Save

Powered by TurnKey Linux.