473,480 Members | 3,135 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Efficient Logging in C++

Hi all,

I am trying to create a simple but efficient C++ logging class. I know
there are lots of them out there but I want something simple and
efficient. The number one requirement is the possibility of shutting
logging down at compile time and suffer no performance penalty
whatsoever for getting logging on whenever I wish. Of course that I
would need to recompile the project each time I want to turn logging on
or off. But given a LOGGING variable what's the best way to turn log on
or off with no performance penalty?

My idea is to have a singleton logger class to log to a file or to
stdout and then provide some macros that would be used all around the
code that would either not do anything or call logging object methods
if LOGGING is defined.

Are any better ideas out there?

Cheers,

Paulo Matos

Jul 23 '05 #1
6 7295
"pmatos" <po**@sat.inesc-id.pt> wrote in message news:<11**********************@o13g2000cwo.googleg roups.com>...
I am trying to create a simple but efficient C++ logging class. I know
there are lots of them out there but I want something simple and
efficient. The number one requirement is the possibility of shutting
logging down at compile time and suffer no performance penalty
whatsoever for getting logging on whenever I wish. Of course that I
would need to recompile the project each time I want to turn logging on
or off. But given a LOGGING variable what's the best way to turn log on
or off with no performance penalty?

My idea is to have a singleton logger class to log to a file or to
stdout and then provide some macros that would be used all around the
code that would either not do anything or call logging object methods
if LOGGING is defined.

Are any better ideas out there?


Consider logging to std::clog. You can replace the clog streambuf
with the streambuf of your choice (to a file, socket, window,
whatever). Principle of least surprise for your readers.

If you disable logging by using an "if (loggingIsEnabled) { std::clog
<< ...; }" then unless logging is enabled you pay no performance
penalty, and the enabling is done at run time so you don't have two
code streams to test.

--
Stephen M. Webb
Jul 23 '05 #2
Well, yeah, but having to test at run-time if(loggingIsEnabled) is
already a performance penalty if I do it a zillion times, right?

Jul 23 '05 #3
Hi Paulo,

Using the Singleton Pattern is a good idea, and provides a centralised
point of control over the logging. One way to control the type of
logging is to provide different derived classes implementing the
logging in different ways, including a class that does no logging (
i.e. different logging Strategies ).
class Logging
{
public:
static Logging& instance()
{ if ( 0 == Logging::singleton )
{ if ( LOGGING_TYPE == COUT_LOGGING )
{ Logging::singleton = new CoutLogging();
}
else if ( LOGGING_TYPE == FILE_LOGGING )
{ Logging::singleton = new FileLogging();
}
// etc...
else
{ Logging::singleton = new NoLogging();
}
}
return *Logging::singleton;
}

public:
virtual Logging& loggingInterfaceMethods() = 0;

protected:
Logging();

private:
Logging( const Logging& );
Logging& operator=( const Logging& );

private:
static Logging* singleton;
};

class NoLogging : public Logging
{
public:
virtual Logging& loggingInterfaceMethods() { /* NOOP */ }
};

// etc ...
The selecting value LOGGING_TYPE could either be a #define in source
or in the make file / project settings for compile time selection, or
you could make it a dynamic property of the Logger to allow the
logging to vary at runtime after resetting the Logging class.

If your application peformance really is sensitive to even the ( 0 ==
Logging::singleton ) test ( which you can't avoid for this style of
Singleton Idiom in C++ ), then its worth considering if you really
need ( or even can afford ) to log the performance critical section of
code.

A possible alternative to the Singleton Pattern would be to use
Template Meta Programming techniques whereby the Logging Strategy
becomes a Policy template and the compiler can be used to ensure that
NoLogging Policy never generates any code at all ( similiar effect to
using MACROs, but far superior ). I'll refer you to the Modern C++
Design book if you're keen to learn those ideas.

You can in principle combine the two ideas, but now we're diverging
away from the quick and simple...

IMHO, I think the log4cpp library is excellent, and a good place to
start if looking for ideas ( http://log4cpp.sourceforge.net/ )

Regards,

Dieter Beaven
Jul 23 '05 #4
You want to use a macro interface to your logging API. This way, you
can define the macros to no-ops if necessary. /david

Jul 23 '05 #5
da********@warpmail.net wrote in message news:<11*********************@g14g2000cwa.googlegr oups.com>...
You want to use a macro interface to your logging API. This way, you
can define the macros to no-ops if necessary. /david


Or, use a constant switch set to true or false, then the compiler can
select to include the logging call or not at compile time.

You may not gain runtime flexibility, but you get the 'no overhead
when not used' benefit without having to pollute your code with
macros.

Macro values remain at the mercy of any third party headers you may
#include and the compiler may never warn you.

-Fazl
Jul 23 '05 #6
pmatos wrote:
Are any better ideas out there?


There's no sense in reinventing the wheel. Check this out:
http://www.torjo.com/code/logging-v131.zip Even if you don't use it, it
will give you some ideas.

To compile it, unzip it in the boost directory tree (www.boost.org) and
run bjam.

-Matt
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2113
by: Huzefa | last post by:
I am working on a amll project in Java that includes many classes. Each of the classes has a Logger object. I have associated a FileHandler with each of these Logger objects. The file is the same...
15
2950
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values)...
0
395
by: Karuppasamy | last post by:
H I am trying to use the Logging Module provided by Microsoft Application Blocks for .Net I installed everything as per the Instructions given in the 'Development Using the Logging Block' ...
6
5402
by: idesilva | last post by:
Hi, I have an application which sends/receives messages at a very high rate. This app needs to 'log' the contents of these messages. Since the msg rate is high, 'logging' each and every msg to a...
2
8458
by: Vance M. Allen | last post by:
Greetings, I am establishing a database for the purpose of logging access to my secure webserver and am wanting to make the database as efficient as I can because it will be doing a lot of work...
4
7782
by: krivenok.dmitry | last post by:
Hello All! I know that Boost doesn't offer any logging facilities. Is there expansible, crossplatform, well-designed (all features are required) logging library for C++? Any suggestions?
6
2025
by: Frank Rizzo | last post by:
I have the following situation: 1. Application X1 runs under a regular user account (this user is also the currently logged on user). 2. Application X1 kicks off Application X2 using an...
4
3415
by: lfhenry | last post by:
I am a newbie to HADR and Admin of DB2 (I am websphere guy). My question relates to DB2 logging. I've read that HADR does not allow Infinite logging (-1). I am expecting my Database to grow...
6
2388
by: Piotrekk | last post by:
Hi I have theoretical problem. In my web service project there was a need to implement file download/upload using WS. I have created UploadChunk and DownloadChunk methods which transfer files in...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7061
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7110
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5367
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3015
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
210
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.