[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/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) {
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<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"
|| data == "--help") {
etk::log::setLevel(etk::log::logLevelInfo);
TK_INFO("etk - help : ");
TK_INFO(" ./" << _argv[0] << " [options]");
TK_INFO(" -l0: debug None");
TK_INFO(" -l1: debug Critical");
TK_INFO(" -l2: debug Error");
TK_INFO(" -l3: debug Warning");
TK_INFO(" -l4: debug Info");
TK_INFO(" -l5: debug Debug");
TK_INFO(" -l6: debug Verbose");
TK_INFO(" " << _argv[0] << " [options]");
TK_INFO(" -l/--etk-log-level= Change the default log level (set all Log lovel):");
TK_INFO(" 0: debug None (default in release)");
TK_INFO(" 1: debug Critical");
TK_INFO(" 2: debug Error");
TK_INFO(" 3: debug Warning");
TK_INFO(" 4: debug Info (default in debug)");
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(" 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_CLASS true
#define DEFAULT_LOG_TIME true
#define DEFAULT_LOG_LIB_NAME true
#else
#define DEFAULT_LOG_LEVEL etk::log::logLevelNone
#define DEFAULT_LOG_COLOR false
@ -82,6 +83,7 @@
#define DEFAULT_LOG_THREAD_NAME false
#define DEFAULT_LOG_CLASS false
#define DEFAULT_LOG_TIME true
#define DEFAULT_LOG_LIB_NAME true
#endif
enum etk::log::level& getDefaultLevel() {
@ -89,8 +91,18 @@ enum etk::log::level& getDefaultLevel() {
return g_val;
}
int32_t& getsizeLog() {
static int32_t g_val = 5;
size_t& getFunctionSizeLog() {
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;
}
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()));
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,11 +354,12 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
}
pointer = handle+strlen(handle);
#endif
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 < getsizeLog()) {
while (strlen(handle) - len < getNameSizeLog()) {
*pointer++ = ' ';
*pointer = '\0';
}
@ -346,6 +367,7 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
*pointer++ = ' ';
*pointer = '\0';
}
}
if(getThreadId() == true) {
// display thread ID
uint32_t iddd = etk::thread::getId();
@ -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<nbSpaceToAdd; ++iii) {
*pointer++ = ' ';
*pointer = '\0';
}
@ -378,26 +403,36 @@ void etk::log::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char*
*pointer = '\0';
}
}
// TODO :Maybe optimize this one ...
if(getFunction() == true) {
int32_t len = strlen(handle);
char tmpName[1024];
char *tmpPointer = tmpName;
if (_className != nullptr) {
snprintf(pointer, 70, "%s::", _className);
pointer = handle+strlen(handle);
snprintf(tmpPointer, 1024, "%s::", _className);
tmpPointer = tmpPointer+strlen(tmpPointer);
}
if (_funcName != nullptr) {
snprintf(pointer, 70, "%s", _funcName);
pointer = handle+strlen(handle);
snprintf(tmpPointer, 1024, "%s", _funcName);
tmpPointer = tmpPointer+strlen(tmpPointer);
}
while (strlen(handle) - len < 60) {
*pointer++ = ' ';
*pointer = '\0';
size_t lenFunc = strlen(tmpName);
if (lenFunc >= getFunctionSizeLog()) {
getFunctionSizeLog() = lenFunc+1;
}
*pointer++ = '|';
*pointer++ = ' ';
*pointer = '\0';
size_t nbSpaceToAdd = getFunctionSizeLog() - lenFunc;
for (size_t iii=0; iii<nbSpaceToAdd; ++iii) {
*tmpPointer++ = ' ';
*tmpPointer = '\0';
}
if (strlen(_log) > 1024-strlen(handle)-20) {
memcpy(pointer, _log, 1024-strlen(handle)-21);
*tmpPointer++ = '|';
*tmpPointer++ = ' ';
*tmpPointer = '\0';
strcat(pointer, tmpName);
pointer += strlen(tmpName);
}
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] = '.';

View File

@ -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__