Skip to content

Commit

Permalink
Final patch for closing bug #66: Refactoring Logger.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.mech.kuleuven.be/repos/orocos/orocos-trunk@5145 ce417995-dfc9-0310-95a0-acdaff106893
  • Loading branch information
psoetens committed Jul 20, 2006
1 parent 12ac168 commit 2dc70a9
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/corelib/common/current/doc/orocos-corelib.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1563,17 +1563,21 @@ emergencyStop.emit(); // User not notified</programlisting>
<programlisting> #include &lt;corelib/Logger.hpp>

Logger::In in("MyModule");
<classname>ORO_CoreLib::Logger</classname>::log() &lt;&lt; Logger::Error &lt;&lt; "An error Occured : " &lt;&lt; 333 &lt;&lt; "." &lt;&lt; Logger::endl;
<classname>ORO_CoreLib::Logger</classname>::log() &lt;&lt; Logger::Debug &lt;&lt; debugstring &lt;&lt; data &lt;&lt; Logger::endl;
<classname>ORO_CoreLib::Logger</classname>::log() &lt;&lt; " more debug info." &lt;&lt; data &lt;&lt; Logger::endl;</programlisting>
log( Error ) &lt;&lt; "An error Occured : " &lt;&lt; 333 &lt;&lt; "." &lt;&lt; endlog();
log( Debug ) &lt;&lt; debugstring &lt;&lt; data &lt;&lt; endlog();
log() &lt;&lt; " more debug info." &lt;&lt; data &lt;&lt; endlog();
log() &lt;&lt; "A warning." &lt;&lt; endlog( Warning );</programlisting>
<para>As you can see, the Logger can be used like the standard C++ input streams.
You may change the Log message's level using the <classname>ORO_CoreLib::Logger::...</classname>
enums. The above message could result in :
You may change the Log message's level using the <classname>ORO_CoreLib::LogLevel</classname>
enums in front (using log() ) or at the end (using endlog()) of the log message.
When no log level is specified, the previously set level is used.
The above message could result in :
</para>
<screen>
0.123 [ ERROR ][MyModule] An error Occured : 333
0.124 [ Debug ][MyModule] &lt;contents of debugstring and data >
0.125 [ Debug ][MyModule] more debug info. &lt;...data...> </screen>
0.125 [ Debug ][MyModule] more debug info. &lt;...data...>
0.125 [ WARNING][MyModule] A warning.</screen>
</example>
</section>
</article>
56 changes: 56 additions & 0 deletions packages/corelib/reporting/current/include/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ namespace ORO_CoreLib
*/
const char* getLogModule() const;

/**
* Function signature of the functions that influence the
* log stream.
*/
typedef std::ostream& (*LogFunction)(std::ostream&);

/**
* Insert a newline '\n' in the ostream. (Why is this not in the standard ?)
*/
Expand Down Expand Up @@ -165,6 +171,11 @@ namespace ORO_CoreLib
*/
static Logger& log();

/**
* As log(), but also specify the LogLevel of the next message.
*/
static Logger& log(LogLevel ll);

/**
* Print a 'welcome' string in Info and reset log timestamp.
*/
Expand Down Expand Up @@ -266,6 +277,51 @@ namespace ORO_CoreLib

static Logger* _instance;
};

/**
* Enumerate all log-levels from absolute silence to
* everything.
* @warning If you enable 'RealTime' logging, this may break realtime performance. Use With Care and NOT
* on production systems.
* @see Logger::allowRealTime()
*/
enum LoggerLevel { Never = 0, Fatal, Critical, Error, Warning, Info, Debug, RealTime };

/**
* Free function in order to access the Logger instance.
*/
static inline Logger& log() { return Logger::log(); }

/**
* Free function in order to access the Logger instance and set the
* LoggerLevel of next message.
*/
static inline Logger& log(LoggerLevel ll) { return Logger::log(Logger::LogLevel(ll)); }

/**
* Function to tell the logger that the log message ended.
* Usage: log() << "Message" << endlog();
*/
static inline Logger::LogFunction endlog() {return Logger::endl; }

/**
* Function to tell the logger that the log message ended and
* specify the LoggerLevel of that message
* Usage: log() << "Error Message" << endlog(Error);
*/
static inline Logger::LogFunction endlog(LoggerLevel ll) { log(ll); return Logger::endl; }

/**
* Function to tell the logger that a newline may be inserted in the log message.
* Usage: log() << "Message on line 1" << nlog() << "Message on line 2" << endlog();
*/
static inline Logger::LogFunction nlog() {return Logger::nl; }

/**
* Function to tell the logger that the logs may be flushed.
* Usage: log() << "Message on line 1" << flushlog();
*/
static inline Logger::LogFunction flushlog() {return Logger::flush; }
}

#include "Logger.inl"
Expand Down
4 changes: 4 additions & 0 deletions packages/corelib/reporting/current/include/Logger.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ namespace ORO_CoreLib
return *_instance;
}

inline Logger& Logger::log(LogLevel) {
return *_instance;
}

inline bool Logger::mayLog() const {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/corelib/reporting/current/src/Logger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ namespace ORO_CoreLib
return *Instance();
}

Logger& Logger::log(LogLevel ll) {
return Instance()->operator<<( ll );
}

/**
* This hidden struct stores all data structures required for logging.
*/
Expand Down

0 comments on commit 2dc70a9

Please sign in to comment.