Skip to content

Commit

Permalink
feat: logger: Add log level and allow to disable colors
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-blasz committed Dec 12, 2024
1 parent 5780b58 commit 4055fde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 6 additions & 0 deletions languages/cpp/src/shared/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ option(FIREBOLT_ENABLE_STATIC_LIB "Create Firebolt library as Static library" OF
option(ENABLE_TESTS "Build openrpc native test" ON)
option(ENABLE_UNIT_TESTS "Enable unit test" ON)
option(ENABLE_COVERAGE "Enable code coverage build." ON)
option(FIREBOLT_PLAIN_LOG "Disable log coloring" OFF)

if (FIREBOLT_PLAIN_LOG)
add_compile_definitions(LOGGER_NO_COLOR)
endif ()


if (FIREBOLT_ENABLE_STATIC_LIB)
set(FIREBOLT_LIBRARY_TYPE STATIC)
Expand Down
14 changes: 12 additions & 2 deletions languages/cpp/src/shared/src/Logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,22 @@ namespace FireboltSDK {
char formattedMsg[Logger::MaxBufSize];
const string time = WPEFramework::Core::Time::Now().ToTimeOnly(true);
const string categoryName = WPEFramework::Core::EnumerateType<Logger::Category>(category).Data();
const string levelName = WPEFramework::Core::EnumerateType<Logger::LogLevel>(logLevel).Data();

#ifdef LOGGER_NO_COLOR
char colorOn[] = "";
char colorOff[] = "";
#else
char colorOn[] = "\033[1;32m";
char colorOff[] = "\033[0m";
#endif

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-truncation"
if (categoryName.empty() != true) {
snprintf(formattedMsg, sizeof(formattedMsg), "--->\033[1;32m[%s]:[%s]:[%s][%s:%d](%s)<PID:%d><TID:%ld> : %s\n", time.c_str(), categoryName.c_str(), module.c_str(), WPEFramework::Core::File::FileName(file).c_str(), line, function.c_str(), TRACE_PROCESS_ID, TRACE_THREAD_ID, msg);
snprintf(formattedMsg, sizeof(formattedMsg), "%s%s: [%s][%s]:[%s][%s:%d](%s)<PID:%d><TID:%ld> : %s%s\n", colorOn, time.c_str(), levelName.c_str(), categoryName.c_str(), module.c_str(), WPEFramework::Core::File::FileName(file).c_str(), line, function.c_str(), TRACE_PROCESS_ID, TRACE_THREAD_ID, msg, colorOff);
} else {
snprintf(formattedMsg, sizeof(formattedMsg), "--->\033[1;32m[%s]:[%s][%s:%d](%s)<PID:%d><TID:%ld> : %s\n", time.c_str(), module.c_str(), WPEFramework::Core::File::FileName(file).c_str(), line, function.c_str(), TRACE_PROCESS_ID, TRACE_THREAD_ID, msg);
snprintf(formattedMsg, sizeof(formattedMsg), "%s%s: [%s][%s][%s:%d](%s)<PID:%d><TID:%ld> : %s%s\n", colorOn, time.c_str(), levelName.c_str(), module.c_str(), WPEFramework::Core::File::FileName(file).c_str(), line, function.c_str(), TRACE_PROCESS_ID, TRACE_THREAD_ID, msg, colorOff);
}
#pragma GCC diagnostic pop
LOG_MESSAGE(formattedMsg);
Expand Down

0 comments on commit 4055fde

Please sign in to comment.