From 9d35def14d88e2579fe6b3c83e122e2d84b61605 Mon Sep 17 00:00:00 2001 From: Mateusz Grzegorzek Date: Fri, 27 Nov 2020 11:26:02 +0100 Subject: [PATCH] [EGD-4477] Dump logs to a file for linux (#1081) [EGD-4477] Implement dumping logs to a file for linux --- module-sys/SystemManager/SystemManager.cpp | 1 - module-utils/board/linux/log.cpp | 14 +++++++++++++- module-utils/log/log.hpp | 5 +++++ source/main.cpp | 5 ++--- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/module-sys/SystemManager/SystemManager.cpp b/module-sys/SystemManager/SystemManager.cpp index d34f329cf37a4590a0c98102c93a12453920ff87..b0161da8d6dd137a0f98dc444b622f709a8872be 100644 --- a/module-sys/SystemManager/SystemManager.cpp +++ b/module-sys/SystemManager/SystemManager.cpp @@ -94,7 +94,6 @@ namespace sys void SystemManager::StartSystem(InitFunction init) { - LOG_FATAL("Initializing system..."); powerManager = std::make_unique(); // Switch system to full functionality(clocks and power domains configured to max values) diff --git a/module-utils/board/linux/log.cpp b/module-utils/board/linux/log.cpp index 1ff6f9436406360a58179dc2fb9ad58470cc4068..da9e99eaab65230308f2e8c5617301a916b32ff3 100644 --- a/module-utils/board/linux/log.cpp +++ b/module-utils/board/linux/log.cpp @@ -6,6 +6,8 @@ #include #include #include +#include +#include #define LOGGER_BUFFER_SIZE 4096 @@ -38,6 +40,7 @@ struct Logger { Logger(logger_level level = LOGTRACE) : level{level} {} + std::mutex lock; logger_level level; }; @@ -45,6 +48,13 @@ struct Logger static Logger logger; static char loggerBuffer[LOGGER_BUFFER_SIZE] = {0}; +void dumpToFile(std::string_view log, size_t length) +{ + static std::fstream logFile(logFileName, std::fstream::out); + logFile.write(log.data(), length); + logFile.flush(); +} + void log_Printf(const char *fmt, ...) { /* Acquire lock */ @@ -54,10 +64,11 @@ void log_Printf(const char *fmt, ...) va_list args; va_start(args, fmt); - vsnprintf(ptr, LOGGER_BUFFER_SIZE - 1, fmt, args); + ptr += vsnprintf(ptr, LOGGER_BUFFER_SIZE - 1, fmt, args); va_end(args); std::cout << loggerBuffer; + dumpToFile(loggerBuffer, ptr - loggerBuffer); } static void _log_Log( @@ -92,6 +103,7 @@ static void _log_Log( ptr += snprintf(ptr, &loggerBuffer[LOGGER_BUFFER_SIZE] - ptr, "\n"); std::cout << loggerBuffer; + dumpToFile(loggerBuffer, ptr - loggerBuffer); } __attribute__((weak)) void log_Log( diff --git a/module-utils/log/log.hpp b/module-utils/log/log.hpp index c557dd43d2541a4cbec7f249625256202e9e610d..2551d5459314fda3a97059825ff841288dd1867f 100644 --- a/module-utils/log/log.hpp +++ b/module-utils/log/log.hpp @@ -82,4 +82,9 @@ extern "C" } #endif +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-variable" +static const char *logFileName = "MuditaOS.log"; +#pragma GCC diagnostic pop + #endif /* LOG_LOG_H_ */ diff --git a/source/main.cpp b/source/main.cpp index 92e62ed73f02f1a996c515c60d0605b1f3785dc1..0b895d6cf8f3cd7a28db6ba251bf9b1936efa776 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -66,9 +66,6 @@ int main() bsp::BoardInit(); - LOG_PRINTF("Launching PurePhone \n"); - LOG_PRINTF("commit: %s tag: %s branch: %s\n", GIT_REV, GIT_TAG, GIT_BRANCH); - auto sysmgr = std::make_shared(5000); sysmgr->StartSystem([sysmgr]() { @@ -159,6 +156,8 @@ int main() return ret; }); + LOG_PRINTF("Launching PurePhone \n"); + LOG_PRINTF("commit: %s tag: %s branch: %s\n", GIT_REV, GIT_TAG, GIT_BRANCH); cpp_freertos::Thread::StartScheduler();