diff --git a/etk/etk.cpp b/etk/etk.cpp index 1e10f77..520d12a 100644 --- a/etk/etk.cpp +++ b/etk/etk.cpp @@ -12,39 +12,103 @@ #include #include +static etk::log::level getLogLevel(const std::string& _value) { + if (_value == "0") { + return etk::log::logLevelNone; + } else if (_value == "1") { + return etk::log::logLevelCritical; + } else if (_value == "2") { + return etk::log::logLevelError; + } else if (_value == "3") { + return etk::log::logLevelWarning; + } else if (_value == "4") { + return etk::log::logLevelInfo; + } else if (_value == "5") { + return etk::log::logLevelDebug; + } else if (_value == "6") { + return etk::log::logLevelVerbose; + } + TK_ERROR("Unknow log level : " << _value); + return etk::log::logLevelVerbose; +} void etk::init(int _argc, char** _argv) { + TK_INFO("E-TK system init (BEGIN)"); etk::setArgZero(_argv[0]); - //etk::initDefaultFolder("river_sample_read"); for (int32_t iii=0; iii<_argc ; ++iii) { std::string data = _argv[iii]; - if (data == "-l0") { - etk::log::setLevel(etk::log::logLevelNone); - } else if (data == "-l1") { - etk::log::setLevel(etk::log::logLevelCritical); - } else if (data == "-l2") { - etk::log::setLevel(etk::log::logLevelError); - } else if (data == "-l3") { - etk::log::setLevel(etk::log::logLevelWarning); - } else if (data == "-l4") { - etk::log::setLevel(etk::log::logLevelInfo); - } else if (data == "-l5") { - etk::log::setLevel(etk::log::logLevelDebug); - } else if (data == "-l6") { - etk::log::setLevel(etk::log::logLevelVerbose); + if (etk::start_with(data, "--etk-log-level=")) { + etk::log::setLevel(getLogLevel(std::string(data.begin()+16, data.end()))); + } else if (etk::start_with(data, "-l=")) { + etk::log::setLevel(getLogLevel(std::string(data.begin()+2, data.end()))); + } else if (etk::start_with(data, "--etk-log-color")) { + etk::log::setColor(true); + } else if (etk::start_with(data, "--etk-log-no-color")) { + etk::log::setColor(false); + } else if (etk::start_with(data, "--etk-log-config=")) { + std::string value(data.begin()+17, data.end()); + etk::log::setTime(false); + etk::log::setLine(false); + etk::log::setFunction(false); + etk::log::setLibName(false); + etk::log::setThreadId(false); + etk::log::setThreadNameEnable(false); + for (size_t iii=0; iii >& getList() { @@ -105,8 +117,8 @@ int32_t etk::log::registerInstance(const std::string& _name) { } } getList().push_back(std::make_pair(_name, getDefaultLevel())); - if (_name.size() >= getsizeLog()) { - getsizeLog() = _name.size()+1; + if (_name.size() >= getNameSizeLog()) { + getNameSizeLog() = _name.size()+1; } //std::cout << "register log : '" << _name << "'=" << getList().size()-1 << std::endl; return getList().size()-1; @@ -222,18 +234,25 @@ void etk::log::setFunction(bool _status) { getFunction() = _status; } +static bool& getLibName() { + static bool g_val = DEFAULT_LOG_LIB_NAME; + return g_val; +} +void etk::log::setLibName(bool _status) { + getLibName() = _status; +} static void getDisplayTime(char* data) { #ifdef __TARGET_OS__Android struct timeval now; gettimeofday(&now, nullptr); - sprintf(data, " %2dh%2d'%2d | ", (int32_t)(now.tv_sec/3600)%24, (int32_t)(now.tv_sec/60)%60, (int32_t)(now.tv_sec%60)); + sprintf(data, " %2dh%2d'%2d ", (int32_t)(now.tv_sec/3600)%24, (int32_t)(now.tv_sec/60)%60, (int32_t)(now.tv_sec%60)); #else time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); - sprintf(data, " %2dh%2d'%2d | ", (timeinfo->tm_hour)%24, timeinfo->tm_min, timeinfo->tm_sec); + sprintf(data, " %2dh%2d'%2d ", (timeinfo->tm_hour)%24, timeinfo->tm_min, timeinfo->tm_sec); #endif } @@ -269,11 +288,12 @@ static void getDisplayTime(char* data) { //go to the Top of bash #define ETK_BASH_GO_TOP "\e[0;0f" +#define LENGHT_MAX_LOG (2048) void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* _className, const char* _funcName, const char* _log) { static std11::mutex g_lock; - char handle[1024] = ""; - memset(handle, ' ', 1024); + char handle[LENGHT_MAX_LOG] = ""; + memset(handle, ' ', LENGHT_MAX_LOG); handle[0] = '\0'; char* pointer = handle; if(getColor() == true) { @@ -334,17 +354,19 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* } pointer = handle+strlen(handle); #endif - if (_id >= 0) { - int32_t len = strlen(handle); - strcat(pointer, getList()[_id].first.c_str()); - pointer = handle+strlen(handle); - while (strlen(handle) - len < getsizeLog()) { + if (getLibName() == true) { + if (_id >= 0) { + int32_t len = strlen(handle); + strcat(pointer, getList()[_id].first.c_str()); + pointer = handle+strlen(handle); + while (strlen(handle) - len < getNameSizeLog()) { + *pointer++ = ' '; + *pointer = '\0'; + } + *pointer++ = '|'; *pointer++ = ' '; *pointer = '\0'; } - *pointer++ = '|'; - *pointer++ = ' '; - *pointer = '\0'; } if(getThreadId() == true) { // display thread ID @@ -359,10 +381,13 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* if(getThreadNameEnable() == true) { // display thread ID std::string name = etk::thread::getName(); - int32_t len = strlen(handle); - snprintf(pointer, 20, "%s", name.c_str()); + if (name.size() >= getThreadSizeLog() ) { + getThreadSizeLog() = name.size() + 1; + } + sprintf(pointer, "%s", name.c_str()); pointer = handle+strlen(handle); - while (strlen(handle) - len < 20) { + size_t nbSpaceToAdd = getThreadSizeLog()-name.size(); + for (size_t iii=0; iii= getFunctionSizeLog()) { + getFunctionSizeLog() = lenFunc+1; } - *pointer++ = '|'; - *pointer++ = ' '; - *pointer = '\0'; + size_t nbSpaceToAdd = getFunctionSizeLog() - lenFunc; + for (size_t iii=0; iii 1024-strlen(handle)-20) { - memcpy(pointer, _log, 1024-strlen(handle)-21); + if (strlen(_log) > LENGHT_MAX_LOG - strlen(handle)-20) { + memcpy(pointer, _log, LENGHT_MAX_LOG - strlen(handle)-21); handle[1024-25] = ' '; handle[1024-24] = '.'; handle[1024-23] = '.'; diff --git a/etk/log.h b/etk/log.h index 90c2d99..a2f09a1 100644 --- a/etk/log.h +++ b/etk/log.h @@ -89,6 +89,11 @@ namespace etk { * @param[in] _status New value. */ void setThreadNameEnable(bool _status); + /** + * @brief Set library display enable or disable. + * @param[in] _status New value. + */ + void setLibName(bool _status); /** * @brief Call log to display * @param[in] _id Id of the instance type @@ -110,21 +115,6 @@ namespace etk { * @param[in] _breakAtEnd assert program when backtrace is printed */ void displayBacktrace(bool _breakAtEnd = false, int32_t _removeElement=0); - // TODO : Remove this from heare, this is a first version ... - /** - * @brief Set the current thread name - * @param[in] name of the thread - */ - void setThreadName(const std::string& _name); - /** - * @brief Get the current thread name - * @return name of the thread - */ - std::string getThreadName(); - /** - * @brief get human readable thread ID. (not the std::thread::get_id()) - */ - uint32_t getThreadID(); }; }; #ifdef __class__