[DEV] add basic of maximum number line in a log file
This commit is contained in:
parent
2b4f3eaed2
commit
373f8e33be
@ -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)");
|
||||
}
|
||||
|
||||
|
||||
|
55
elog/log.cpp
55
elog/log.cpp
@ -116,6 +116,7 @@ size_t& getNameSizeLog() {
|
||||
static size_t g_val = 5;
|
||||
return g_val;
|
||||
}
|
||||
|
||||
static std::vector<std::pair<std::string, enum elog::level> >& getList() {
|
||||
static std::vector<std::pair<std::string, enum elog::level> > 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;
|
||||
}
|
@ -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();
|
||||
};
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user