|Offline Documentation: [Offline Category] [FAQ] [Howto] [Reference] [Manual]|
|This article is part |
of the Offline Documentation
Logging means printing messages to the screen or a file.
Gaudi has a "Message Service" that controls logging. It is based on levels:
- use when the logging occurs very frequently such as inside a loop. Try to use this for logging that occurs many times per event.
- use when logging occurs outside a loop and/or once per event but is not useful information for normal running.
- use sparingly for giving important information to the user
- use to indicate an exceptional thing happened but it was not so bad as to abandon running
- use to indicate an exceptional thing happened and the job should be terminated as soon as possible.
Logging from a Gaudi Component
Low level (Algorithm, AlgTool)
In low level Gaudi Component code one must create a message stream and set the level manually. This is done like:
MsgStream log(msgSvc()); log << MSG::INFO << "An info message" << endreq;
msgSvc() method is provided by the base class.
Higher level (GaudiAlgorithm, GaudiTool)
The Gaudi* classes provide a nicer interface to logging:
info() << "This is an info message" << endreq;
Knowing the Output Level
There are times where one would like to know the current output level of a message stream inside the code of a component. There are two levels that matter:
MsgSream log = ...; MSG::Level report_level = log.level(); MSG::Level current_level = log.currentLevel();
- report level
- This sets which messages are reported. Any message with equal or higher level will be reported.
- current level
- This sets the level of the most recent messages that have been fed into the log.
Logging outside of a Gaudi Component
If you have code that will work in a Gaudi application but is not directly a Gaudi component you need to do a bit more to access a MsgStream:
#include "GaudiKernel/ServiceHandle.h" #include "GaudiKernel/IMessageSvc.h" #include "GaudiKernel/MsgStream.h" // ... ServiceHandle<IMessageSvc> msgh("MessageSvc","MyName"); MsgStream log(&(*msgh), "MyName"); log << MSG::INFO << "Hello World!" << endreq;
Simplifying with GaudiMessages
There is a set of functions provided by the Utilities/GaudiMessages package that simplify accessing message streams outside of a Gaudi component:
#include "GaudiMessages/GaudiMessages.h" static GaudiMsgStreams logger("My Stream Name"); // ... logger.info() << "An info message to an unnamed stream" << endreq;
Logging from Python
You can also use logging from Python;
A pure-Python logging mechanism exists but it is not tied to Gaudi's. You can use it like:
from GaudiKernel.Logging import log log.warning("Danger, danger!")
Gaudi Logging from a Python GaudiAlgo
You can access Gaudi's logging from inside a pure-Python
msgSvc = self.svc('IMessageSvc','MessageSvc') from GaudiKernel.Constants import * msgSvc.reportMessage("MyAlg",ERROR,"This is an error")
Gaudi Logging from arbitrary Python code
Also defined in Utilities/GaudiMessages is a Python friendly class that provides access to Gaudi message streams. Use it like:
import PyCintex gbl = PyCintex.makeNamespace("") log = gbl.GaudiMessaging("my stream name") log.info("An info message") log.warning("Hello World, better watch out!")
|Offline Software Documentation: [Offline Categories] [FAQ] [Offline Documentation Category]|