<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - logger_ex.cpp</title></head><body bgcolor='white'><pre> <font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt </font> <font color='#009900'>/* This is a simple example illustrating the use of the logger object from the dlib C++ Library. The output of this program looks like this: 0 INFO [0] example: This is an informational message. 0 DEBUG [0] example: The integer variable is set to 8 0 WARN [0] example: The variable is bigger than 4! Its value is 8 0 INFO [0] example: we are going to sleep for half a second. 503 INFO [0] example: we just woke up 503 INFO [0] example: program ending The first column shows the number of milliseconds since program start at the time the message was printed, then the logging level of the message, then the thread that printed the message, then the logger's name and finally the message itself. */</font> <font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>logger.h<font color='#5555FF'>></font> <font color='#0000FF'>#include</font> <font color='#5555FF'><</font>dlib<font color='#5555FF'>/</font>misc_api.h<font color='#5555FF'>></font> <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib; <font color='#009900'>// Create a logger object somewhere. It is usually convenient to make it at the global scope </font><font color='#009900'>// which is what I am doing here. The following statement creates a logger that is named example. </font>logger <b><a name='dlog'></a>dlog</b><font face='Lucida Console'>(</font>"<font color='#CC0000'>example</font>"<font face='Lucida Console'>)</font>; <font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#009900'>// Every logger has a logging level (given by dlog.level()). Each log message is tagged with a </font> <font color='#009900'>// level and only levels equal to or higher than dlog.level() will be printed. By default all </font> <font color='#009900'>// loggers start with level() == LERROR. In this case I'm going to set the lowest level LALL </font> <font color='#009900'>// which means that dlog will print all logging messages it gets. </font> dlog.<font color='#BB00BB'>set_level</font><font face='Lucida Console'>(</font>LALL<font face='Lucida Console'>)</font>; <font color='#009900'>// print our first message. It will go to cout because that is the default. </font> dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>This is an informational message.</font>"; <font color='#009900'>// now print a debug message. </font> <font color='#0000FF'><u>int</u></font> variable <font color='#5555FF'>=</font> <font color='#979000'>8</font>; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LDEBUG <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>The integer variable is set to </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> variable; <font color='#009900'>// the logger can be used pretty much like any ostream object. But you have to give a logging </font> <font color='#009900'>// level first. But after that you can chain << operators like normal. </font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>variable <font color='#5555FF'>></font> <font color='#979000'>4</font><font face='Lucida Console'>)</font> dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LWARN <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>The variable is bigger than 4! Its value is </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> variable; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>we are going to sleep for half a second.</font>"; <font color='#009900'>// sleep for half a second </font> dlib::<font color='#BB00BB'>sleep</font><font face='Lucida Console'>(</font><font color='#979000'>500</font><font face='Lucida Console'>)</font>; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>we just woke up</font>"; dlog <font color='#5555FF'><</font><font color='#5555FF'><</font> LINFO <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>program ending</font>"; <b>}</b> </pre></body></html>