diff --git a/clkmgr/common/print.cpp b/clkmgr/common/print.cpp index 771b65b4..666ffda9 100644 --- a/clkmgr/common/print.cpp +++ b/clkmgr/common/print.cpp @@ -13,6 +13,8 @@ #include #include +#include +#include __CLKMGR_NAMESPACE_USE; @@ -20,21 +22,42 @@ using namespace std; enum LogLevel { DEBUG, INFO, ERROR }; static LogLevel currentLogLevel = INFO; +bool useSyslog = false; +static rtpi::mutex errMutex; + +void clkmgr::PrintStartLog(const char *me) +{ + openlog(me, LOG_PID, LOG_DAEMON); + useSyslog = true; +} +void clkmgr::PrintStopLog() +{ + closelog(); +} void clkmgr::_PrintError(string msg, uint16_t line, const char *file, const char *func, errno_type errnum) { + string ebuf; + if(errnum != (errno_type)(-1)) { + unique_lock lck(errMutex); + ebuf = strerror(errnum); + } + if(useSyslog) + syslog(LOG_ERR, "*** Error: %s %s at line %u in %s: %s", + msg.c_str(), ebuf.c_str(), line, file, func); fprintf(stderr, "*** Error: %s %s at line %u in %s: %s\n", - msg.c_str(), - errnum != (errno_type)(-1) ? strerror(errnum) : "", - line, file, func); + msg.c_str(), ebuf.c_str(), line, file, func); fflush(stderr); } void clkmgr::_PrintDebug(string msg, uint16_t line, const char *file, const char *func) { + if(useSyslog) + syslog(LOG_DEBUG, "*** Debug: %s at line %u in %s: %s", + msg.c_str(), line, file, func); if(currentLogLevel <= DEBUG) { fprintf(stderr, "*** Debug: %s at line %u in %s: %s\n", msg.c_str(), line, file, func); @@ -45,6 +68,9 @@ void clkmgr::_PrintDebug(string msg, uint16_t line, void clkmgr::_PrintInfo(string msg, uint16_t line, const char *file, const char *func) { + if(useSyslog) + syslog(LOG_INFO, "* Info: %s at line %u in %s: %s", + msg.c_str(), line, file, func); if(currentLogLevel <= INFO) { fprintf(stderr, "* Info: %s at line %u in %s: %s\n", msg.c_str(), line, file, func); @@ -56,12 +82,22 @@ void clkmgr::_PrintInfo(string msg, uint16_t line, const char *file, void clkmgr::_DumpOctetArray(string msg, const uint8_t *arr, size_t length, uint16_t line, const char *file, const char *func) { - if(currentLogLevel > DEBUG) + if(!useSyslog && currentLogLevel > DEBUG) return; - fprintf(stderr, "* Info: %s at line %u in %s:%s", + char buf[2000]; + string str; + snprintf(buf, sizeof buf, "* Info: %s at line %u in %s:%s", msg.c_str(), line, file, func); - for(size_t i = 0; i < length; i++) - fprintf(stderr, "%s0x%.2x", i % HEX_DIGITS_PER_LINE ? " " : "\n", arr[i]); - fprintf(stderr, "\n"); - fflush(stderr); + str = buf; + for(size_t i = 0; i < length; i++) { + snprintf(buf, sizeof buf, "%s0x%.2x", + i % HEX_DIGITS_PER_LINE ? " " : "\n", arr[i]); + str += buf; + } + if(useSyslog) + syslog(LOG_DEBUG, "%s", str.c_str()); + if(currentLogLevel <= DEBUG) { + fprintf(stderr, "%s\n", str.c_str()); + fflush(stderr); + } } diff --git a/clkmgr/common/print.hpp b/clkmgr/common/print.hpp index 02baa759..7794a38c 100644 --- a/clkmgr/common/print.hpp +++ b/clkmgr/common/print.hpp @@ -44,6 +44,9 @@ void _PrintInfo(std::string msg, uint16_t line, const char *file, void _DumpOctetArray(std::string msg, const uint8_t *arr, size_t length, uint16_t line, const char *file, const char *func); +void PrintStartLog(const char *me); +void PrintStopLog(); + __CLKMGR_NAMESPACE_END #endif /* PRINT_HPP */ diff --git a/clkmgr/proxy/main.cpp b/clkmgr/proxy/main.cpp index 0b15e442..bfdfab0e 100644 --- a/clkmgr/proxy/main.cpp +++ b/clkmgr/proxy/main.cpp @@ -58,6 +58,7 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } } + PrintStartLog(argv[0]); BlockStopSignal(); if(!ProxyTransport::init()) { PrintError("Transport init failed"); @@ -82,5 +83,6 @@ int main(int argc, char *argv[]) PrintError("finalize failed"); return EXIT_FAILURE; } + PrintStopLog(); return EXIT_SUCCESS; }