LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Enable debug logging in application? (https://www.linuxquestions.org/questions/programming-9/enable-debug-logging-in-application-4175515503/)

yaplej 08-19-2014 04:56 PM

Enable debug logging in application?
 
Hello everyone,

Im working on an application that I want to have the option to enable debug logging in realtime. Right now a log of message are written to the log while I test/develop things. Rather than leave all this logging on constantly I want to have it off by default and optionally turn it on.

So far my thoughts are to have a module variable (or possibly several) each enable different debug options. Then in the code I could do something like:
Code:

if (DEBUG_THIS_THING == true) {
 //log some stuff.
}

Then in my application I can have a command "debug this_think" and "no debug this_thing" to turn the logs on/off as needed without having to recompile or anything.

Is there a better way of doing this? Maybe a bitmask that would allow multiple logging levels for each component?

evo2 08-19-2014 07:01 PM

Hi,

I'd be tempted to use existing libraries for this. You didn't say what language, but from your pseudo code it looks like it could be c++, so perhaps you could use boost.
http://www.boost.org/doc/libs/1_56_0...tml/index.html

HTH,

Evo2.

yaplej 08-20-2014 10:55 AM

I have kept non-packaged libraries to a min in my application so far (I have one). The application is in C sorry I didn't mention that. Boost has some great capabilities though. I could possibly emulate some of those maybe?


Code:

int DEBUG_THIS_TRACE = 0;
int DEBUG_THIS_DEBUG = 0;
int DEBUG_THIS_INFO = 0;
int DEBUG_THIS_WARNING = 0;
int DEBUG_THIS_ERROR = 0;
int DEBUG_THIS_FATAL = 0;

int mylogger(int debug, char *message) {

    if(debug) {
        writetomylog(message);
    }
}

int main(int, char*[]){
    mylogger(DEBUG_THIS_INFO, "log this");
}

Another idea is to use a single variable with a bitmask? Im not even sure how to do this but if ALL is logged then any error level is logged or you should be able to enable them individually.

Code:

#define LOG_OFF                0        \\ Logging Level 0
#define LOG_FATAL        1        \\ Logging Level 1
#define LOG_ERROR        2        \\ Logging Level 2
#define LOG_WARN        4        \\ Logging Level 3
#define LOG_DEBUG        8        \\ Logging Level 4
#define LOG_INFO        16        \\ Logging Level 5
#define LOG_TRACE        32        \\ Logging Level 6
#define LOG_ALL                64        \\ Logging Level 7

int LOG_LEVEL        =        LOG_WARN;        \\ Default log everything up to WARN messages (regardless of individual component setting).



int DEBUG_THIS        =        LOG_INFO;        \\ Specific logging option.

int mylogger(int level, int debug, char *message) {

        if((level >= LOG_LEVEL) || ((level & debug) >= level)) {
                writetomylog(message);
        }
}

int main(int, char*[]){
        mylogger(LOG_INFO,DEBUG_THIS, "[info] about something");
}



All times are GMT -5. The time now is 09:48 PM.