Solved

create a function to encapsulate log4cpp stream

Posted on 2013-12-07
4
286 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

786 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