Skip to content

Commit

Permalink
clkmgr: add system log
Browse files Browse the repository at this point in the history
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
  • Loading branch information
erezgeva committed Feb 21, 2025
1 parent b6e87a2 commit 6f942b9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
54 changes: 45 additions & 9 deletions clkmgr/common/print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,51 @@

#include <cstdio>
#include <cstring>
#include <syslog.h>
#include <rtpi/mutex.hpp>

__CLKMGR_NAMESPACE_USE;

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<rtpi::mutex> 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);
Expand All @@ -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);
Expand All @@ -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);
}
}
3 changes: 3 additions & 0 deletions clkmgr/common/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
2 changes: 2 additions & 0 deletions clkmgr/proxy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
}
PrintStartLog(argv[0]);
BlockStopSignal();
if(!ProxyTransport::init()) {
PrintError("Transport init failed");
Expand All @@ -82,5 +83,6 @@ int main(int argc, char *argv[])
PrintError("finalize failed");
return EXIT_FAILURE;
}
PrintStopLog();
return EXIT_SUCCESS;
}

0 comments on commit 6f942b9

Please sign in to comment.