From 64bdda87d3637398f9aa6cd2ffe79d8243998a78 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 5 Oct 2016 22:53:35 +0200 Subject: [PATCH] [DEV] add a display of the backtrace if user request it --- elog/elog.cpp | 7 +++++-- elog/log.cpp | 19 ++++++++++++++++--- elog/log.hpp | 5 +++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/elog/elog.cpp b/elog/elog.cpp index a2d6662..3de48f1 100644 --- a/elog/elog.cpp +++ b/elog/elog.cpp @@ -75,10 +75,12 @@ 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-color")) { + } else if (data == "--elog-color") { elog::setColor(true); - } else if (startWith(data, "--elog-no-color")) { + } else if (data == "--elog-no-color") { elog::setColor(false); + } else if (data == "--elog-back-trace") { + elog::setBackTrace(true); } else if (startWith(data, "--elog-file=")) { std::string value(data.begin()+12, data.end()); if (value.size() == 0) { @@ -146,6 +148,7 @@ void elog::init(int _argc, const char** _argv) { ELOG_PRINT(" --elog-file=pathToFile File to store the logs: (disable console logs)"); 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)"); ELOG_PRINT(" --elog-config= Configure the Log interface"); ELOG_PRINT(" t: diplay time"); #ifdef ELOG_BUILD_ETHREAD diff --git a/elog/log.cpp b/elog/log.cpp index 367cd3f..eab764c 100644 --- a/elog/log.cpp +++ b/elog/log.cpp @@ -36,7 +36,7 @@ void * trace[MAX_DEPTH]; int stack_depth = backtrace(trace, MAX_DEPTH); - ELOG_ERROR("Back-trace : "); + ELOG_PRINT("Back-trace : "); for (int32_t i = 1; i < stack_depth; i++) { Dl_info dlinfo; if(!dladdr(trace[i], &dlinfo)) { @@ -49,8 +49,8 @@ symname = demangled; } if (_removeElement <= 0) { - ELOG_WARNING(" " << dlinfo.dli_fname << ": "); - ELOG_ERROR(" " << symname); + ELOG_PRINT(" " << dlinfo.dli_fname << ": "); + ELOG_PRINT(" " << symname); } _removeElement--; if(demangled != nullptr) { @@ -248,6 +248,14 @@ void elog::setLibName(bool _status) { getLibName() = _status; } +static bool& getDisplayBackTrace() { + static bool g_val = false; + return g_val; +} +void elog::setBackTrace(bool _status) { + getDisplayBackTrace() = _status; +} + static void getDisplayTime(char* data) { #ifdef __TARGET_OS__Android struct timeval now; @@ -588,6 +596,11 @@ void elog::logChar(int32_t _id, int32_t _level, int32_t _ligne, const char* _fun std::this_thread::sleep_for(std::chrono::milliseconds(700)); displayBacktrace(true, 2); } + // Display backtrace to facilitate the error problems + if ( _level == level_error + && getDisplayBackTrace() == true) { + displayBacktrace(false, 2); + } } diff --git a/elog/log.hpp b/elog/log.hpp index c255191..319fea2 100644 --- a/elog/log.hpp +++ b/elog/log.hpp @@ -102,6 +102,11 @@ namespace elog { * @param[in] _status New value. */ void setLibName(bool _status); + /** + * @brief Set back-trace display on Error log enable or disable. + * @param[in] _status New value. + */ + void setBackTrace(bool _status); /** * @brief Call log to display * @param[in] _id Id of the instance type