← Back to team overview

unity-dev team mailing list archive

[Ayatana-dev] [C++] Logging in nux and unity

 

Hi people,

Getting some unified logging infrastructure in place has been one of my goals 
with unity and nux.

We now nice logging streams in unity and nux.

There are two primary components to care about for consumers of the logging 
infrastructure:  nux::logging::Logger instances, and the logging macros.

Simply demonstrated as follows

--- SomeSourceFile.cpp

#include <NuxCore/Logger.h>

namespace
{
   nux::logger::Logger logger("dotted.module");
}

void SomeFunc(...)
{
   LOG_DEBUG(logger) << "This is streamed out, and uses ostream& ";

   // ...

   LOG_INFO(logger) << "yadda yadda: " << some_var << ": "
           << some_func(some_var);
}

-- end of example

The line is sent at the end of the statement.  A carriage return is implied, 
and not needed to be added to the end.  In fact if you do add one, you'll get 
an extra line in the log file.  The line is actually sent on flush[1].

The dotted module name is used to determine the enabled logging levels.

"dotted.module" inherits settings from "dotted", which in turn inherits 
settings from the root module.

By default only warnings and above are emitted.  This can be configured through 
an environment variable now, and soon to be changable using dbus for the 
running process.

export UNITY_LOG_SEVERITY="<root>=INFO;nux=WARNING"

Semi-colon separated values with the dotted module assigned a sensible name:
  TRACE, DEBUG, INFO, WARNING, ERROR

<root> is a special string used to identify the root logger.

If the logging level is not enabled for the logger, then the streaming 
operators are not executed.

There should probably only be one logger defined for any given source file, 
although this isn't enforced.

The source file and line number are also emitted to the logging listeners.  
Right now we only have standard output getting sent the message (kinda like 
what we have right now), although in review I have a rolling file appender that 
writes asynchronously which will put a log file in the standard place.  Once 
this code is landed, I'll email again with more magic environment variables.

Tim

[1] flushing a stream happens with std::flush, or std::endl (or closing the 
stream).  In the case of the logging macros, it is a temporary log stream 
object that is constructed and destroyed.  The destructor calls flush and sends 
the content on.

Attachment: signature.asc
Description: This is a digitally signed message part.


Follow ups