LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-19-2014, 04:56 PM   #1
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165
Blog Entries: 1

Rep: Reputation: 22
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?
 
Old 08-19-2014, 07:01 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
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.
 
Old 08-20-2014, 10:55 AM   #3
yaplej
Member
 
Registered: Apr 2009
Distribution: CentOS, Ubuntu, openSuSE
Posts: 165

Original Poster
Blog Entries: 1

Rep: Reputation: 22
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");
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kvm --enable-debug logs guardian1 Linux - Virtualization and Cloud 0 09-03-2012 08:43 AM
How to enable XFS debug mode? luludede Linux - Kernel 1 10-04-2009 11:50 AM
How to Debug an application vikasumit Programming 2 06-14-2006 08:40 AM
./configure --enable-debug ? a_hic Linux - Newbie 2 06-05-2002 09:21 AM
2.4.17 crashing; how do you set up logging and debug? system Linux - General 6 01-28-2002 11:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration