Solved

create a function to encapsulate log4cpp stream

Posted on 2013-12-07
4
278 Views
Last Modified: 2013-12-09
I am using to log the statement under warn priority with the following statement

log4cpp::Category::getRoot() << log4cpp::Priority::WARN << "This is my first PVLogger fatal Message";

I wonder if it is possible to create a overload of << operator so I can write the above statement like below statement


StreamWarm  << "This is my first PVLogger fatal Message";

and StreamWarm incorporate log4cpp::Category::getRoot() << log4cpp::Priority::WARN
0
Comment
Question by:tommym121
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 450 total points
ID: 39703335
You can do that in quite a simple way using a #define, e.g.

#define StreamWarn log4cpp::Category::getRoot() << log4cpp::Priority::WARN 

//...

StreamWarm  << "This is my first PVLogger fatal Message";

Open in new window

0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 450 total points
ID: 39703343
BTW, you could implement the other message levels in a similar way:

#define StreamEmerg log4cpp::Category::getRoot() << log4cpp::Priority::EMERG 
#define StreamAlert log4cpp::Category::getRoot() << log4cpp::Priority::ALERT 
#define StreamError log4cpp::Category::getRoot() << log4cpp::Priority::ERROR 
#define StreamWarn log4cpp::Category::getRoot() << log4cpp::Priority::WARN 

Open in new window


or in a bit more general way

#define Stream(x) log4cpp::Category::getRoot() << log4cpp::Priority::X

// ...


Stream(WARN)  << "This is my first PVLogger fatal Message";

Stream(ALERT)  << "This is my first PVLogger fatal Message";

Open in new window

0
 
LVL 8

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 50 total points
ID: 39703881
A typo in jkr's comment
X should be x.

#define Stream(x) log4cpp::Category::getRoot() << log4cpp::Priority::x

The second option(bit more general...) is really a good approach.
0
 

Author Closing Comment

by:tommym121
ID: 39707535
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now