Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / doc / doxygen / outcomes_logs.doc
1 /**
2 @page outcomes_logs Simulation Logging
3 @brief Log4J-like logging system.
4
5 Using @c printf or @c println to display information is possible, but
6 quickly unpractical, as the logs of all processes get intermixed in
7 your program's output. %As an answer, the SimGrid logging module allow
8 you to sort and filter the logs by emitter, by module and by gravity
9 level. 
10
11 The SimGrid logging module is highly <b>configurable</b>: as in Log4J,
12 the user can choose <i>at runtime</i> what messages to show and what
13 to hide, as well as how messages get displayed. It is also <b>easy to
14 use</b>, both to the programmer (using preprocessor macros black
15 magic) and to the user (with command line options). And finally, its
16 <b>performance</b> is highly optimized.
17
18 @tableofcontents
19
20 @section log_overview Main Concepts
21
22 There is three main concepts in SimGrid's logging mechanism:
23 ** **category**, **priority** and **appender**. These three concepts work
24 together to enable developers to log messages according to message
25 type and priority, and to control at runtime how these messages are
26 formatted and where they are reported.
27
28 @subsection log_cat Logging categories
29
30 In SimGrid, the logs are sorted by topics (called categories) so that
31 the user can chose which topics to display and hide at runtime. These
32 categories are organized as a hierarchy, following the architecture of
33 modules in the simulator.
34
35 The full list of all categories that exist in the SimGrid base library 
36 @ref XBT_log_cats "is available in the documentation".
37 You can also provide ```--help-logs``` on the command line of any
38 SimGrid simulator to see the list of all existing categories that it
39 contains.
40
41 @subsection log_pri Logging priorities
42
43 The user can also specify the desired level of details for each category. This is
44 controlled by the <i>priority</i> concept (which should maybe be renamed to
45 <i>severity</i>). For example, you may want to see every debugging message
46 of MSG while only being interested into the messages at level "error" or
47 higher about the XBT internals.
48
49 @subsection log_app Message appenders
50
51 The message appenders are in charge of actually displaying the
52 message to the user. For now, four appenders exist: 
53 - the default one prints stuff on stderr 
54 - file sends the data to a single file
55 - rollfile overwrites the file when the file grows too large
56 - splitfile creates new files with a specific maximum size
57
58 @subsection log_lay Message layouts
59
60 The message layouts are the elements in charge of choosing how each message
61 will look like. Their result is a string which is then passed to the appender
62 attached to the category to be displayed.
63
64 For now, there is two layouts: The simple one, which is good for most cases,
65 and another one allowing users to specify the format they want.
66 @ref log_use_conf provides more info on this.
67
68 @subsection log_hist History of this module
69
70 Historically, this module is an adaptation of the log4c project, which is dead
71 upstream, and which I was given the permission to fork under the LGPL licence
72 by the log4c's authors. The log4c project itself was loosely based on the
73 Apache project's Log4J, which also inspired Log4CC, Log4py and so on. Our work
74 differs somehow from these projects anyway, because the C programming language
75 is not object oriented.
76
77 @section log_API C Programmer interface
78
79 Please also refer to the @ref XBT_log "full API" for more information.
80
81 @subsection log_API_cat Constructing the category hierarchy
82
83 Every category is declared by providing a name and an optional
84 parent. If no parent is explicitly named, the root category, LOG_ROOT_CAT is
85 the category's parent.
86
87 A category is created by a macro call at the top level of a file.  A
88 category can be created with any one of the following macros:
89
90  - @ref XBT_LOG_NEW_CATEGORY(MyCat,desc); Create a new root
91  - @ref XBT_LOG_NEW_SUBCATEGORY(MyCat, ParentCat,desc);
92     Create a new category being child of the category ParentCat
93  - @ref XBT_LOG_NEW_DEFAULT_CATEGORY(MyCat,desc);
94     Like XBT_LOG_NEW_CATEGORY, but the new category is the default one
95       in this file
96  -  @ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY(MyCat, ParentCat,desc);
97     Like XBT_LOG_NEW_SUBCATEGORY, but the new category is the default one
98       in this file
99
100 The parent cat can be defined in the same file or in another file (in
101 which case you want to use the @ref XBT_LOG_EXTERNAL_CATEGORY macro to make
102 it visible in the current file), but each category may have only one
103 definition. Likewise, you can use a category defined in another file as 
104 default one using @ref XBT_LOG_EXTERNAL_DEFAULT_CATEGORY
105
106 Typically, there will be a Category for each module and sub-module, so you
107 can independently control logging for each module.
108
109 For a list of all existing categories, please refer to the @ref XBT_log_cats
110 section. This file is generated automatically from the SimGrid source code, so
111 it should be complete and accurate. 
112 Also refer to the @ref XBT_log "full API" for more information. 
113
114 @subsection log_API_pri Declaring message priority
115
116 A category may be assigned a threshold priority. The set of priorities are
117 defined by the @ref e_xbt_log_priority_t enum. All logging request under
118 this priority will be discarded.
119
120 If a given category is not assigned a threshold priority, then it inherits
121 one from its closest ancestor with an assigned threshold. To ensure that all
122 categories can eventually inherit a threshold, the root category always has
123 an assigned threshold priority.
124
125 Logging requests are made by invoking a logging macro on a category.  All of
126 the macros have a printf-style format string followed by arguments. If you
127 compile with the -Wall option, gcc will warn you for unmatched arguments, ie
128 when you pass a pointer to a string where an integer was specified by the
129 format. This is usually a good idea.
130
131 Here is an example of the most basic type of macro. This is a logging
132 request with priority <i>warning</i>.
133
134 <code>XBT_CLOG(MyCat, xbt_log_priority_warning, "Values are: %d and '%s'", 5,
135 "oops");</code>
136
137 A logging request is said to be enabled if its priority is higher than or
138 equal to the threshold priority of its category. Otherwise, the request is
139 said to be disabled. A category without an assigned priority will inherit
140 one from the hierarchy.
141
142 It is possible to use any non-negative integer as a priority. If, as in the
143 example, one of the standard priorities is used, then there is a convenience
144 macro that is typically used instead. For example, the above example is
145 equivalent to the shorter:
146
147 <code>XBT_CWARN(MyCat, "Values are: %d and '%s'", 5, "oops");</code>
148
149 @subsection log_API_isenabled Checking if a particular category/priority is enabled
150
151 It is sometimes useful to check whether a particular category is
152 enabled at a particular priority. One example is when you want to do
153 some extra computation to prepare a nice debugging message. There is
154 no use of doing so if the message won't be used afterward because
155 debugging is turned off.
156
157 Doing so is extremely easy, thanks to the XBT_LOG_ISENABLED(category, priority).
158
159 @subsection log_API_subcat Using a default category (the easy interface)
160
161 If @ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY(MyCat, Parent) or
162 @ref XBT_LOG_NEW_DEFAULT_CATEGORY(MyCat) is used to create the
163 category, then the even shorter form can be used:
164
165 <code>XBT_WARN("Values are: %s and '%d'", 5, "oops");</code>
166
167 Only one default category can be created per file, though multiple
168 non-defaults can be created and used.
169
170 @subsection log_API_easy Putting all together: the easy interface
171
172 First of all, each module should register its own category into the categories
173 tree using @ref XBT_LOG_NEW_DEFAULT_SUBCATEGORY.
174
175 Then, logging should be done with the #XBT_DEBUG, #XBT_VERB, #XBT_INFO,
176 #XBT_WARN, #XBT_ERROR and #XBT_CRITICAL macros.
177
178 Under GCC, these macro check there arguments the same way than printf does. So,
179 if you compile with -Wall, the following code will issue a warning:
180 <code>XBT_DEBUG("Found %s (id %d)", some_string, a_double)</code>
181
182 If you want to specify the category to log onto (for example because you
183 have more than one category per file, add a C before the name of the log
184 producing macro (ie, use #XBT_CDEBUG, #XBT_CVERB, #XBT_CINFO, #XBT_CWARN,
185 #XBT_CERROR and #XBT_CCRITICAL and friends), and pass the category name as
186 first argument.
187
188 The TRACE priority is not used the same way than the other. You should use
189 the #XBT_IN, #XBT_OUT and #XBT_HERE macros instead.
190
191 @subsection log_API_example Example of use
192
193 Here is a more complete example:
194
195 @verbatim
196 #include "xbt/log.h"
197
198 / * create a category and a default subcategory * /
199 XBT_LOG_NEW_CATEGORY(VSS);
200 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(SA, VSS);
201
202 int main() {
203        / * Now set the parent's priority.  (the string would typically be a runtime option) * /
204        xbt_log_control_set("SA.thresh:3");
205
206        / * This request is enabled, because WARNING >= INFO. * /
207        XBT_CWARN(VSS, "Low fuel level.");
208
209        / * This request is disabled, because DEBUG < INFO. * /
210        XBT_CDEBUG(VSS, "Starting search for nearest gas station.");
211
212        / * The default category SA inherits its priority from VSS. Thus,
213           the following request is enabled because INFO >= INFO.  * /
214        XBT_INFO("Located nearest gas station.");
215
216        / * This request is disabled, because DEBUG < INFO. * /
217        XBT_DEBUG("Exiting gas station search");
218 }
219 @endverbatim
220
221 Please also refer to the @ref XBT_log "full API" for more information.
222
223 @section log_user User interface
224
225 @subsection log_use_conf Configuration
226
227 Although rarely done, it is possible to configure the logs during
228 program initialization by invoking the xbt_log_control_set() method
229 manually. A more conventional way is to use the --log command line
230 argument. xbt_init() (called by MSG_init() and friends)
231 checks and deals properly with such arguments.
232
233 @subsubsection log_use_conf_thres Threshold configuration
234
235 The most common setting is to control which logging event will get
236 displayed by setting a threshold to each category through the
237 <tt>threshold</tt> keyword.
238
239 For example, @verbatim --log=root.threshold:debug@endverbatim will make
240 SimGrid <b>extremely</b> verbose while @verbatim
241 --log=root.thres:critical@endverbatim should shut it almost
242 completely off.
243
244 Note that the <tt>threshold</tt> keyword can be abbreviated here. For example,
245 all the following notations have the same result.
246 @verbatim
247 --log=root.threshold:debug
248 --log=root.threshol:debug
249 --log=root.thresho:debug
250 --log=root.thresh:debug
251 --log=root.thres:debug
252 --log=root.thre:debug
253 --log=root.thr:debug
254 --log=root.th:debug
255 --log=root.t:debug
256 --log=root.:debug     <--- That's obviously really ugly, but it actually works.
257 @endverbatim
258
259 The full list of recognized thresholds is the following:
260
261  - trace: enter and return of some functions
262  - debug: crufty output
263  - verbose: verbose output for the user wanting more
264  - info: output about the regular functionning
265  - warning: minor issue encountered
266  - error: issue encountered
267  - critical: major issue encountered 
268
269 @subsubsection log_use_conf_multi Passing several settings
270
271 You can provide several of those arguments to change the setting of several
272 categories, they will be applied from left to right. So,
273 @verbatim --log="root.thres:debug root.thres:critical"@endverbatim should
274 disable almost any logging.
275
276 Note that the quotes on above line are mandatory because there is a space in
277 the argument, so we are protecting ourselves from the shell, not from SimGrid.
278 We could also reach the same effect with this:
279 @verbatim --log=root.thres:debug --log=root.thres:critical@endverbatim
280
281 @subsubsection log_use_conf_fmt Format configuration
282
283 You can control the format of log messages through the <tt>fmt</tt>
284 keyword. For example, @verbatim --log=root.fmt:%m@endverbatim reduces
285 the output to the user-message only, removing any decoration such as
286 the date, or the process ID, everything.
287
288 Here are the existing format directives:
289
290  - %%: the % char
291  - %%n: platform-dependent line separator (LOG4J compatible)
292  - %%e: plain old space (SimGrid extension)
293
294  - %%m: user-provided message
295
296  - %%c: Category name (LOG4J compatible)
297  - %%p: Priority name (LOG4J compatible)
298
299  - %%h: Hostname (SimGrid extension)
300  - %%P: Process name (SimGrid extension -- note that with SMPI this is the integer value of the process rank)
301  - %%t: Thread "name" (LOG4J compatible -- actually the address of the thread in memory)
302  - %%i: Process PID (SimGrid extension -- this is a 'i' as in 'i'dea)
303
304  - %%F: file name where the log event was raised (LOG4J compatible)
305  - %%l: location where the log event was raised (LOG4J compatible, like '%%F:%%L' -- this is a l as in 'l'etter)
306  - %%L: line number where the log event was raised (LOG4J compatible)
307  - %%M: function name (LOG4J compatible -- called method name here of course).
308
309  - %%b: full backtrace (Called %%throwable in LOG4J).
310    Defined only under windows or when using the GNU libc because backtrace() is not defined
311    elsewhere, and we only have a fallback for windows boxes, not mac ones for example.
312  - %%B: short backtrace (only the first line of the %%b).
313    Called %%throwable{short} in LOG4J; defined where %%b is.
314
315  - %%d: date (UNIX-like epoch)
316  - %%r: application age (time elapsed since the beginning of the application)
317
318
319 If you want to mimic the simple layout with the format one, you would use this
320 format: '[%%h:%%i:(%%i) %%r] %%l: [%%c/%%p] %%m%%n'. This is not completely correct
321 because the simple layout do not display the message location for messages at
322 priority INFO (thus, the fmt is '[%%h:%%i:(%%i) %%r] [%%c/%%p] %%m%%n' in this
323 case). Moreover, if there is no process name (ie, messages coming from the
324 library itself, or test programs doing strange things) do not display the
325 process identity (thus, fmt is '[%%r] %%l: [%%c/%%p] %%m%%n' in that case, and '[%%r]
326 [%%c/%%p] %%m%%n' if they are at priority INFO).
327
328 For now, there is only two format modifiers: the precision and the
329 width fields. You can for example specify %.4r to get the application
330 age with 4 numbers after the radix, or %15p to get the process name
331 on 15 columns. Finally, you can specify %10.6r to get the time on at
332 most 10 columns, with 6 numbers after the radix. 
333
334 Note that when specifying the width, it is filled with spaces. That
335 is to say that for example %5r in your format is converted to "% 5f"
336 for printf (note the extra space); there is no way to fill the empty
337 columns with 0 (ie, pass "%05f" to printf). Another limitation is
338 that you cannot set specific layouts to the several priorities.
339
340 @subsubsection log_use_conf_app Category appender
341
342 You can control the appender of log messages through the <tt>app</tt>
343 keyword. For example, 
344 @verbatim --log=root.app:file:mylogfile@endverbatim redirects the
345 output to the file mylogfile.
346
347 For splitfile appender, the format is 
348 @verbatim --log=root.app:splitfile:size:mylogfile_%.format@endverbatim
349
350 The size is in bytes, and the % wildcard will be replaced by the number of the
351 file. If no % is present, it will be appended at the end.
352
353 rollfile appender is also available, it can be used as
354 @verbatim --log=root.app:rollfile:size:mylogfile@endverbatim
355 When the file grows to be larger than the size, it will be emptied and new log 
356 events will be sent at its beginning 
357
358 Any appender setup this way have its own layout format (simple one by default),
359 so you may have to change it too afterward. Moreover, the additivity of the log category
360 is also set to false to prevent log event displayed by this appender to "leak" to any other
361 appender higher in the hierarchy. If it is not what you wanted, you can naturally change it
362 manually.
363
364 @subsubsection log_use_conf_add Category additivity
365
366 The <tt>add</tt> keyword allows to specify the additivity of a
367 category (see @ref log_in_app). '0', '1', 'no', 'yes', 'on'
368 and 'off' are all valid values, with 'yes' as default.
369
370 The following example resets the additivity of the xbt category to true (which is its default value).
371 @verbatim --log=xbt.add:yes@endverbatim
372
373 @subsection log_use_misc Misc and Caveats
374
375   - Do not use any of the macros that start with '_'.
376   - Log4J has a 'rolling file appender' which you can select with a run-time
377     option and specify the max file size. This would be a nice default for
378     non-kernel applications.
379   - Careful, category names are global variables.
380   - When writing a log format, you often want to use spaces. If you don't
381     protect these spaces, they are used as configuration elements separators.
382     For example, if you want to remove the date from the logs, you want to pass the following 
383     argument on the command line. The outer quotes are here to protect the string from the shell 
384     interpretation while the inner ones are there to prevent simgrid from splitting the string 
385     in several log parameters (that would be invalid).
386     @verbatim --log="'root.fmt:%l: [%p/%c]: %m%n'"@endverbatim
387     Another option is to use the SimGrid-specific format directive \%e for
388     spaces, like in the following.
389     @verbatim --log="root.fmt:%l:%e[%p/%c]:%e%m%n"@endverbatim
390
391 @section log_internals Internal considerations
392
393 This module is loaded of macro black magic, and when it goes wrong,
394 SimGrid studently loose its ability to explain its problems. When
395 messing around this module, I often find useful to define
396 XBT_LOG_MAYDAY (which turns it back to good old printf) for the time
397 of finding what's going wrong. But things are quite verbose when
398 everything is enabled...
399
400 @subsection log_in_perf Performance
401
402 Except for the first invocation of a given category, a disabled logging request
403 requires an a single comparison of a static variable to a constant.
404
405 There is also compile time constant, @ref XBT_LOG_STATIC_THRESHOLD, which
406 causes all logging requests with a lower priority to be optimized to 0 cost
407 by the compiler. By setting it to xbt_log_priority_infinite, all logging
408 requests are statically disabled at compile time and cost nothing. Released executables
409 <i>might</i>  be compiled with (note that it will prevent users to debug their problems)
410 @verbatim-DXBT_LOG_STATIC_THRESHOLD=xbt_log_priority_infinite@endverbatim
411
412 Compiling with the @verbatim-DNLOG@endverbatim option disables all logging
413 requests at compilation time while the @verbatim-DNDEBUG@endverbatim disables
414 the requests of priority below INFO.
415
416 @todo Logging performance *may* be improved further by improving the message
417 propagation from appender to appender in the category tree.
418
419 @subsection log_in_app Appenders
420
421 Each category has an optional appender. An appender is a pointer to a
422 structure which starts with a pointer to a do_append() function. do_append()
423 prints a message to a log.
424
425 When a category is passed a message by one of the logging macros, the category performs the following actions:
426
427   - if the category has an appender, the message is passed to the
428     appender's do_append() function,
429   - if additivity is true for the category, the message is passed to
430     the category's parent. Additivity is true by default, and can be
431     controlled by xbt_log_additivity_set() or something like --log=root.add:1 (see @ref log_use_conf_add).
432     Also, when you add an appender to a category, its additivity is automatically turned to off.
433     Turn it back on afterward if it is not what you wanted.
434
435 By default, only the root category have an appender, and any other category has
436 its additivity set to true. This causes all messages to be logged by the root
437 category's appender.
438
439 The default appender function currently prints to stderr.
440
441 */