Linda Harrison
Linda Harrison

Reputation: 257

C++ - Logging statements according to the level

I have the following statements:

static Logging::Logger* common_logger = new Logging::Logger(Logging::Logger::LEVEL);

In the Logger.h i have

  class Logger {
public:

    enum LEVEL {
        Debug,
        Warning,
        Notification,
        Error
    };
  }

I have included the file Logger.h inside my another class as :

    Logging::log(CustomDialog::logger, Logging::Entry, CustomDialog::CLASSNAME, "CustomDialog");

I need to know if this is the right way to do the reason why i am doing this is to get logs based upon the level.

Regards,

Upvotes: 0

Views: 1016

Answers (2)

count0
count0

Reputation: 2621

You can use ACE_DEBUG, it seems old-school (ala printf) but thread-safe, reliable and fully configurable (use logfiles, stdout etc..) You'll have to link against libACE(Adaptive Communication Framework) of course, but it's development packages are easily available in many linux distros per default nowadays. I've been looking over the list from that C++ logging libraries post, mentioned by Als, but it seems most people are running into mem leaks with many of the frameworks and boost::Log is not out yet.

Another point is that most logging libraries using streams, for example like this:

// from thread 1
mlog(mlog::DEBUG) << "Debug message goes here" << mlog::endl;

// from thread 2
mlog(mlog::INFO) << "Info message goes here" << mlog::endl;

will not work as expected in a multithreaded environment, while ACE will perform correctly there.

The output of the above will look something like this:

[thread1 | 12:04.23] Debug me[thread2 | 12:04.24] Info message goesssage goes herehere

Upvotes: 1

Joris Timmermans
Joris Timmermans

Reputation: 10988

Take a look at Log4cxx - it's easy to use and contains just about every feature you might want in a logging framework for C++. It's extensible, it can be configured through configuration files, and it even supports remote logging out of the box.

Upvotes: 4

Related Questions