From 373f8e33be5e98e29bb795bfe1aeceacaa6c7424 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 4 Jan 2017 22:31:03 +0100 Subject: [PATCH] [DEV] add basic of maximum number line in a log file --- elog/elog.cpp | 7 ++++++- elog/log.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++-- elog/log.hpp | 9 +++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/elog/elog.cpp b/elog/elog.cpp index 3de48f1..6ab8e76 100644 --- a/elog/elog.cpp +++ b/elog/elog.cpp @@ -75,6 +75,11 @@ void elog::init(int _argc, const char** _argv) { if (startWith(data, "--elog-level=")) { ELOG_INFO("Change global level at " << getLogLevel(std::string(data.begin()+13, data.end()))); elog::setLevel(getLogLevel(std::string(data.begin()+13, data.end()))); + } else if (startWith(data, "--elog-max-line=")) { + std::string dataToParse = &data[16]; + int value = 0; + sscanf(dataToParse.c_str(), "%d", &value); + elog::setMaxLineNumberInFile(value); } else if (data == "--elog-color") { elog::setColor(true); } else if (data == "--elog-no-color") { @@ -146,6 +151,7 @@ void elog::init(int _argc, const char** _argv) { ELOG_PRINT(" X Log level to set [0..6]"); ELOG_PRINT(" note: ':' can be replace with '/' or '+'"); ELOG_PRINT(" --elog-file=pathToFile File to store the logs: (disable console logs)"); + ELOG_PRINT(" --elog-max-line=XXX Number of line in the log file"); ELOG_PRINT(" --elog-color Enable color in log (default in Linux/debug)"); ELOG_PRINT(" --elog-no-color Disable color in log (default in Linux/release and Other)"); ELOG_PRINT(" --elog-back-trace Enable back-trace when an error log level is generated (to get a fast debug)"); @@ -185,4 +191,3 @@ void elog::init(int _argc, const char** _argv) { ELOG_INFO("E-LOG system init (END)"); } - diff --git a/elog/log.cpp b/elog/log.cpp index 4ed2fcd..b1bb643 100644 --- a/elog/log.cpp +++ b/elog/log.cpp @@ -116,6 +116,7 @@ size_t& getNameSizeLog() { static size_t g_val = 5; return g_val; } + static std::vector >& getList() { static std::vector > g_val; return g_val; @@ -277,9 +278,27 @@ static void getDisplayTime(char* data) { static std::mutex g_lock; static elog::callbackLog callbackUserLog(nullptr); +static std::string& getLogFileName() { + static std::string g_val=""; + return g_val; +} static FILE*& getLogFile() { - static FILE* file=nullptr; - return file; + static FILE* g_val=nullptr; + return g_val; +} + +static size_t& getLogFileCount() { + static size_t g_val = 0; + return g_val; +} + +static size_t& getLogFileMaxCount() { + static size_t g_val = 0; + return g_val; +} + +void elog::setMaxLineNumberInFile(size_t _status) { + getLogFileMaxCount() = _status; } @@ -352,6 +371,7 @@ static bool FSNODE_LOCAL_exist(const std::string& _path) { void elog::setLogInFile(const std::string& _filename) { elog::unsetLogInFile(); ELOG_PRINT("Log in file: '" << _filename << "'"); + getLogFileName() = _filename; FILE*& file = getLogFile(); // create path of the file: size_t found=_filename.find_last_of("/\\"); @@ -368,6 +388,7 @@ void elog::setLogInFile(const std::string& _filename) { } void elog::unsetLogInFile() { + getLogFileName() = ""; g_lock.lock(); FILE*& file = getLogFile(); // close file only if needed ... @@ -618,6 +639,8 @@ void elog::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* _fun g_lock.lock(); { FILE*& file = getLogFile(); + size_t& fileCurrentCount = getLogFileCount(); + size_t& fileMaxCount = getLogFileMaxCount(); // close file only if needed ... if (file != nullptr) { *pointer++ = '\n'; @@ -631,6 +654,24 @@ void elog::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* _fun fflush(file); break; } + // Increment the line counter in file + fileCurrentCount++; + if (fileMaxCount != 0) { + // the user request a maximum line number in the file: + if (fileCurrentCount > fileMaxCount) { + fileCurrentCount = 0; + fflush(file); + fclose(file); + std::string tmpFileName = getLogFileName(); + if ( tmpFileName.size() > 0 + && tmpFileName[tmpFileName.size()-1] == '2') { + getLogFileName().pop_back(); + } else { + getLogFileName() += "2"; + } + file = fopen(getLogFileName().c_str(), "w"); + } + } // if we log in file, we have no need to log otherwise ... just "tail -f log.txt" g_lock.unlock(); return; @@ -681,3 +722,13 @@ void elog::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* _fun } +void elog::flush() { + g_lock.lock(); + FILE*& file = getLogFile(); + if (file != nullptr) { + fflush(file); + } + fflush(stdout); + g_lock.unlock(); + return; +} \ No newline at end of file diff --git a/elog/log.hpp b/elog/log.hpp index 319fea2..a71437e 100644 --- a/elog/log.hpp +++ b/elog/log.hpp @@ -107,6 +107,11 @@ namespace elog { * @param[in] _status New value. */ void setBackTrace(bool _status); + /** + * @brief Set back-trace display on Error log enable or disable. + * @param[in] _status New number of lines. + */ + void setMaxLineNumberInFile(size_t _status); /** * @brief Call log to display * @param[in] _id Id of the instance type @@ -169,6 +174,10 @@ namespace elog { * @brief Disable log in a file */ void unsetLogInFile(); + /** + * @brief When loggin in file, the flush is not done automaticaly ==> need to do it manualy + */ + void flush(); }; /**