Click here to Skip to main content
14,037,009 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,

I have been trying to find a clever way of removing and adding logging function calls from a C++ programme. The log functions call into a "logger" class, but I would like to remove the different log levels with the least amount of headache for the maintainers.

We already have the normal file #undef WARN, etc. procedure, but I am trying to improve this. Basically, trying to write code such that:

MODULE_LOG ("MyModule", ALARM);

int MyClass::Function (int param)
{
   ALARM ("Print");
   WARN ("No Print");
}


This would ideally compile into just:
int MyClass::Function (int param)
{
   logger::alarm_print_function ("Print");
}


Hence, the "WARN" definition has been removed.

If the maintainer wanted to see the WARN messages, then they could just change the code such that:
MODULE_LOG ("MyModule", ALARM, WARN);


This would ideally compile into just:
int MyClass::Function (int param)
{
   logger::alarm_print_function ("Print");
   logger::warn_print_function ("No Print");
}


This way the maintainer can just update a single module. As said, this can be done through the normal #undef/#define of WARN, etc.

I was wondering if there was a more clever way of doing this?


Thanks.
Posted

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You could use macros such as:
#define ALARM(x) logger::alarm_print_function(x)
#define WARN(x)

which will do what you want. Then just change the macro definitions to generate different code as required.

Sometimes however, it is better to leave all the code enabled and use runtime flags to generate messages such as:
static bool ALARM = false;
static bool WARNING = false;
void MyClass::Function (int param)
{
    if (ALARM)
        logger::alarm_print_function ("Print");
    if (WARNING)
        logger::warn_print_function ("No Print");
}

and add another function to set the relevant variables to true at run time.
   
Comments
RichardS 31-Jan-12 11:17am
   
Thanks - I have slightly adapted the solution, but essentially the same idea. I have set your ALARM/WARNING bool's at start up by passing by creating a class handler that is queried on startup. Not my "ideal" solution, but an extra compare per print is not a major issue. I don't think I was ever going to get everything :)
Richard MacCutchan 31-Jan-12 11:58am
   
I worked on a commercial system that used a similar mechanism with up to 9 levels of log detail messages. The overhead when not logging was not measurable, and even when logging at level 9 (the most comprehensive detail) it did not have too much of an effect.
JackDingler 31-Jan-12 13:29pm
   
You could also dynamically map the message types, and set the warning levels at run time.

How fancy you get is only limited by your imagination.

On systems where heavy logging has resulted in slow server response times, I've created logging engines, that queues the log entries to be processed by a background thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.190424.1 | Last Updated 31 Jan 2012
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100