[DEV] set the configuration of the ETK and libs

This commit is contained in:
Edouard DUPIN 2015-03-26 21:21:38 +01:00
parent 10d5116851
commit a06aee64cc
3 changed files with 158 additions and 69 deletions

View File

@ -12,39 +12,103 @@
#include <etk/debug.h> #include <etk/debug.h>
#include <etk/os/FSNode.h> #include <etk/os/FSNode.h>
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) { void etk::init(int _argc, char** _argv) {
TK_INFO("E-TK system init (BEGIN)");
etk::setArgZero(_argv[0]); etk::setArgZero(_argv[0]);
//etk::initDefaultFolder("river_sample_read");
for (int32_t iii=0; iii<_argc ; ++iii) { for (int32_t iii=0; iii<_argc ; ++iii) {
std::string data = _argv[iii]; std::string data = _argv[iii];
if (data == "-l0") { if (etk::start_with(data, "--etk-log-level=")) {
etk::log::setLevel(etk::log::logLevelNone); etk::log::setLevel(getLogLevel(std::string(data.begin()+16, data.end())));
} else if (data == "-l1") { } else if (etk::start_with(data, "-l=")) {
etk::log::setLevel(etk::log::logLevelCritical); etk::log::setLevel(getLogLevel(std::string(data.begin()+2, data.end())));
} else if (data == "-l2") { } else if (etk::start_with(data, "--etk-log-color")) {
etk::log::setLevel(etk::log::logLevelError); etk::log::setColor(true);
} else if (data == "-l3") { } else if (etk::start_with(data, "--etk-log-no-color")) {
etk::log::setLevel(etk::log::logLevelWarning); etk::log::setColor(false);
} else if (data == "-l4") { } else if (etk::start_with(data, "--etk-log-config=")) {
etk::log::setLevel(etk::log::logLevelInfo); std::string value(data.begin()+17, data.end());
} else if (data == "-l5") { etk::log::setTime(false);
etk::log::setLevel(etk::log::logLevelDebug); etk::log::setLine(false);
} else if (data == "-l6") { etk::log::setFunction(false);
etk::log::setLevel(etk::log::logLevelVerbose); etk::log::setLibName(false);
etk::log::setThreadId(false);
etk::log::setThreadNameEnable(false);
for (size_t iii=0; iii<value.size(); ++iii) {
if (value[iii] == 't') {
etk::log::setTime(true);
} else if (value[iii] == 'T') {
etk::log::setThreadId(true);
} else if (value[iii] == 'N') {
etk::log::setThreadNameEnable(true);
} else if (value[iii] == 'L') {
etk::log::setLine(true);
} else if (value[iii] == 'l') {
etk::log::setLibName(true);
} else if (value[iii] == 'f') {
etk::log::setFunction(true);
} else {
TK_ERROR("In program argument: --etk-log-config= , the value '" << value[iii] << "' is not supported");
}
}
} else if (etk::start_with(data, "--etk-log-lib=")) {
std::string value(data.begin()+14, data.end());
std::vector<std::string> list = etk::split(value, ':');
if (list.size() != 2) {
TK_ERROR("Can not set the --etk-log-lib= with value='" << value << "' not formated name:X");
continue;
}
etk::log::setLevel(list[0], getLogLevel(list[1]));
} else if ( data == "-h" } else if ( data == "-h"
|| data == "--help") { || data == "--help") {
etk::log::setLevel(etk::log::logLevelInfo); etk::log::setLevel(etk::log::logLevelInfo);
TK_INFO("etk - help : "); TK_INFO("etk - help : ");
TK_INFO(" ./" << _argv[0] << " [options]"); TK_INFO(" " << _argv[0] << " [options]");
TK_INFO(" -l0: debug None"); TK_INFO(" -l/--etk-log-level= Change the default log level (set all Log lovel):");
TK_INFO(" -l1: debug Critical"); TK_INFO(" 0: debug None (default in release)");
TK_INFO(" -l2: debug Error"); TK_INFO(" 1: debug Critical");
TK_INFO(" -l3: debug Warning"); TK_INFO(" 2: debug Error");
TK_INFO(" -l4: debug Info"); TK_INFO(" 3: debug Warning");
TK_INFO(" -l5: debug Debug"); TK_INFO(" 4: debug Info (default in debug)");
TK_INFO(" -l6: debug Verbose"); TK_INFO(" 5: debug Debug");
TK_INFO(" 6: debug Verbose");
TK_INFO(" --etk-log-lib=name:X Set a library specific level:");
TK_INFO(" name Name of the library");
TK_INFO(" X Log level to set [0..6]");
TK_INFO(" --etk-log-color Enable color in log (default in Linux/debug)");
TK_INFO(" --etk-log-no-color Disable color in log (default in Linux/release and Other)");
TK_INFO(" --etk-log-config= Configure the Log interface");
TK_INFO(" t: diplay time");
TK_INFO(" T: diplay thread id");
TK_INFO(" N: diplay thread name");
TK_INFO(" L: diplay line number");
TK_INFO(" l: diplay lib name");
TK_INFO(" f: diplay fundtion name");
TK_INFO(" -h/--help: this help"); TK_INFO(" -h/--help: this help");
TK_INFO(" example:");
TK_INFO(" " << _argv[0] << " --etk-log-color --etk-log-level=2 --etk-log-lib=etk:5 --etk-log-lib=appl:6 --etk-log-config=NLlf");
} else if (etk::start_with(data, "--etk")) {
TK_ERROR("Can not parse the argument : '" << data << "'");
} }
} }
TK_INFO("E-TK system init (END)");
} }

View File

@ -74,6 +74,7 @@
#define DEFAULT_LOG_THREAD_NAME true #define DEFAULT_LOG_THREAD_NAME true
#define DEFAULT_LOG_CLASS true #define DEFAULT_LOG_CLASS true
#define DEFAULT_LOG_TIME true #define DEFAULT_LOG_TIME true
#define DEFAULT_LOG_LIB_NAME true
#else #else
#define DEFAULT_LOG_LEVEL etk::log::logLevelNone #define DEFAULT_LOG_LEVEL etk::log::logLevelNone
#define DEFAULT_LOG_COLOR false #define DEFAULT_LOG_COLOR false
@ -82,6 +83,7 @@
#define DEFAULT_LOG_THREAD_NAME false #define DEFAULT_LOG_THREAD_NAME false
#define DEFAULT_LOG_CLASS false #define DEFAULT_LOG_CLASS false
#define DEFAULT_LOG_TIME true #define DEFAULT_LOG_TIME true
#define DEFAULT_LOG_LIB_NAME true
#endif #endif
enum etk::log::level& getDefaultLevel() { enum etk::log::level& getDefaultLevel() {
@ -89,8 +91,18 @@ enum etk::log::level& getDefaultLevel() {
return g_val; return g_val;
} }
int32_t& getsizeLog() { size_t& getFunctionSizeLog() {
static int32_t g_val = 5; static size_t g_val = 5;
return g_val;
}
size_t& getThreadSizeLog() {
static size_t g_val = 5;
return g_val;
}
size_t& getNameSizeLog() {
static size_t g_val = 5;
return g_val; return g_val;
} }
static std::vector<std::pair<std::string, enum etk::log::level> >& getList() { static std::vector<std::pair<std::string, enum etk::log::level> >& getList() {
@ -105,8 +117,8 @@ int32_t etk::log::registerInstance(const std::string& _name) {
} }
} }
getList().push_back(std::make_pair(_name, getDefaultLevel())); getList().push_back(std::make_pair(_name, getDefaultLevel()));
if (_name.size() >= getsizeLog()) { if (_name.size() >= getNameSizeLog()) {
getsizeLog() = _name.size()+1; getNameSizeLog() = _name.size()+1;
} }
//std::cout << "register log : '" << _name << "'=" << getList().size()-1 << std::endl; //std::cout << "register log : '" << _name << "'=" << getList().size()-1 << std::endl;
return getList().size()-1; return getList().size()-1;
@ -222,18 +234,25 @@ void etk::log::setFunction(bool _status) {
getFunction() = _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) { static void getDisplayTime(char* data) {
#ifdef __TARGET_OS__Android #ifdef __TARGET_OS__Android
struct timeval now; struct timeval now;
gettimeofday(&now, nullptr); 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 #else
time_t rawtime; time_t rawtime;
struct tm * timeinfo; struct tm * timeinfo;
time(&rawtime); time(&rawtime);
timeinfo = localtime(&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 #endif
} }
@ -269,11 +288,12 @@ static void getDisplayTime(char* data) {
//go to the Top of bash //go to the Top of bash
#define ETK_BASH_GO_TOP "\e[0;0f" #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) { 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; static std11::mutex g_lock;
char handle[1024] = ""; char handle[LENGHT_MAX_LOG] = "";
memset(handle, ' ', 1024); memset(handle, ' ', LENGHT_MAX_LOG);
handle[0] = '\0'; handle[0] = '\0';
char* pointer = handle; char* pointer = handle;
if(getColor() == true) { 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); pointer = handle+strlen(handle);
#endif #endif
if (_id >= 0) { if (getLibName() == true) {
int32_t len = strlen(handle); if (_id >= 0) {
strcat(pointer, getList()[_id].first.c_str()); int32_t len = strlen(handle);
pointer = handle+strlen(handle); strcat(pointer, getList()[_id].first.c_str());
while (strlen(handle) - len < getsizeLog()) { pointer = handle+strlen(handle);
while (strlen(handle) - len < getNameSizeLog()) {
*pointer++ = ' ';
*pointer = '\0';
}
*pointer++ = '|';
*pointer++ = ' '; *pointer++ = ' ';
*pointer = '\0'; *pointer = '\0';
} }
*pointer++ = '|';
*pointer++ = ' ';
*pointer = '\0';
} }
if(getThreadId() == true) { if(getThreadId() == true) {
// display thread ID // 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) { if(getThreadNameEnable() == true) {
// display thread ID // display thread ID
std::string name = etk::thread::getName(); std::string name = etk::thread::getName();
int32_t len = strlen(handle); if (name.size() >= getThreadSizeLog() ) {
snprintf(pointer, 20, "%s", name.c_str()); getThreadSizeLog() = name.size() + 1;
}
sprintf(pointer, "%s", name.c_str());
pointer = handle+strlen(handle); pointer = handle+strlen(handle);
while (strlen(handle) - len < 20) { size_t nbSpaceToAdd = getThreadSizeLog()-name.size();
for (size_t iii=0; iii<nbSpaceToAdd; ++iii) {
*pointer++ = ' '; *pointer++ = ' ';
*pointer = '\0'; *pointer = '\0';
} }
@ -378,26 +403,36 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
*pointer = '\0'; *pointer = '\0';
} }
} }
// TODO :Maybe optimize this one ...
if(getFunction() == true) { if(getFunction() == true) {
int32_t len = strlen(handle); int32_t len = strlen(handle);
char tmpName[1024];
char *tmpPointer = tmpName;
if (_className != nullptr) { if (_className != nullptr) {
snprintf(pointer, 70, "%s::", _className); snprintf(tmpPointer, 1024, "%s::", _className);
pointer = handle+strlen(handle); tmpPointer = tmpPointer+strlen(tmpPointer);
} }
if (_funcName != nullptr) { if (_funcName != nullptr) {
snprintf(pointer, 70, "%s", _funcName); snprintf(tmpPointer, 1024, "%s", _funcName);
pointer = handle+strlen(handle); tmpPointer = tmpPointer+strlen(tmpPointer);
} }
while (strlen(handle) - len < 60) { size_t lenFunc = strlen(tmpName);
*pointer++ = ' '; if (lenFunc >= getFunctionSizeLog()) {
*pointer = '\0'; getFunctionSizeLog() = lenFunc+1;
} }
*pointer++ = '|'; size_t nbSpaceToAdd = getFunctionSizeLog() - lenFunc;
*pointer++ = ' '; for (size_t iii=0; iii<nbSpaceToAdd; ++iii) {
*pointer = '\0'; *tmpPointer++ = ' ';
*tmpPointer = '\0';
}
*tmpPointer++ = '|';
*tmpPointer++ = ' ';
*tmpPointer = '\0';
strcat(pointer, tmpName);
pointer += strlen(tmpName);
} }
if (strlen(_log) > 1024-strlen(handle)-20) { if (strlen(_log) > LENGHT_MAX_LOG - strlen(handle)-20) {
memcpy(pointer, _log, 1024-strlen(handle)-21); memcpy(pointer, _log, LENGHT_MAX_LOG - strlen(handle)-21);
handle[1024-25] = ' '; handle[1024-25] = ' ';
handle[1024-24] = '.'; handle[1024-24] = '.';
handle[1024-23] = '.'; handle[1024-23] = '.';

View File

@ -89,6 +89,11 @@ namespace etk {
* @param[in] _status New value. * @param[in] _status New value.
*/ */
void setThreadNameEnable(bool _status); 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 * @brief Call log to display
* @param[in] _id Id of the instance type * @param[in] _id Id of the instance type
@ -110,21 +115,6 @@ namespace etk {
* @param[in] _breakAtEnd assert program when backtrace is printed * @param[in] _breakAtEnd assert program when backtrace is printed
*/ */
void displayBacktrace(bool _breakAtEnd = false, int32_t _removeElement=0); 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__ #ifdef __class__