Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Bug fix in GTNets docummentation.
[simgrid.git] / doc / gtut-tour-06-logs.doc
1 /**
2 @page GRAS_tut_tour_logs Lesson 6: Logging informations properly
3
4 \section GRAS_tut_tour_logs_toc Table of Contents
5  - \ref GRAS_tut_tour_logs_intro
6  - \ref GRAS_tut_tour_logs_practice
7  - \ref GRAS_tut_tour_logs_recap
8  - \ref GRAS_tut_tour_logs_config
9    - \ref GRAS_tut_tour_logs_config_prio
10    - \ref GRAS_tut_tour_logs_config_layout
11    
12 <hr>
13
14 \section GRAS_tut_tour_logs_intro Introduction
15
16 Let's have another look at the output of the program we came up with in
17 lesson 5:
18 \include 05-globals.output
19
20 It is a bit difficult to read, isn't it? Indeed, it is hard to identify
21 which process printed which line. It would be possible to add [server] in
22 any messages comming from the server and do the same for every process
23 running. Idealy, we would also add the location at which the message was
24 generated (using __FILE__ and __LINE__) to help debuging, as well as a
25 timestamping so that we can still reorder the messages in RL when they get
26 intermixed (yeah, it happen, and there is not much to do against it).
27 At the end, each time we would like to print a little "hello" debugging
28 message, we would have to write 3 lines of arguments to fprintf, which is
29 not that practical.
30
31 That is why there is a support for proper logging in GRAS. Technically
32 speaking, it is not part of GRAS but of XBT, which is the toolbox on which
33 the whole SimGrid library is built, but that's the same for us.
34
35 This logging library follows the spirit of another one called log4j, which
36 is more or less the reference in the domain. The original version is for
37 Java, as the name implies, and there was reimplementation for Python
38 (log4py), C/C++ (log4c) and so on. Since one of the credo of the GRAS
39 framework is that we don't want any external dependency to ease the
40 deployment in grid settings, we reimplemented a version of our own.
41
42 One of the strong idea of log4j is that log events get structured to give
43 the user a fine control at run time of what gets displayed and what don't.
44 For that, <i>log event</i> are produced into <i>log channels</i> at a given
45 <i>log priority</i>. Then, you can select the minimal priority an event
46 should have on a given channel to get displayed. 
47
48 Then, to keep things managable even when the number of channels increase,
49 the channels form a tree and properties get inherited from parent channel to
50 childs. Have a look at the existing channels in SimGrid: \ref XBT_log_cats.
51 You see that for example, the <tt>gras</tt> channel have 5 subchannels (at
52 time of writing): <tt>gras_ddt</tt>, <tt>gras_msg</tt>, <tt>gras_timer</tt>,
53 <tt>gras_trp</tt> and <tt>gras_virtu</tt>. If you open or close the
54 <tt>gras</tt> channel, it automatically affects all those subchannels (and
55 their respective subchannels too). Finally, channels are not just open or
56 closed, but filter messages below a given priority (as we said). The
57 priorities are defined by type #e_xbt_log_priority_t.
58             
59 That is all you really need to know about the logs before diving into
60 practice. If you want more information on that topic, refer to the \ref
61 XBT_log section, which contains much more information than this page.
62
63 \section GRAS_tut_tour_logs_practice Putting logs into action
64
65 Enough with theory, let's change our example so that it uses proper
66 loggings. The first thing to do is to add a new channel in the existing
67 hierarchy. There is 4 macros to create log channels, depending on the kind
68 of channel we want:
69 - XBT_LOG_NEW_CATEGORY(MyCat,desc); Create a new root
70 - XBT_LOG_NEW_SUBCATEGORY(MyCat, ParentCat,desc); Create a new category being child of the category ParentCat
71 - XBT_LOG_NEW_DEFAULT_CATEGORY(MyCat,desc); Like XBT_LOG_NEW_CATEGORY, but the new category is the default one in this file
72 - XBT_LOG_NEW_DEFAULT_SUBCATEGORY(MyCat, ParentCat,desc); Like XBT_LOG_NEW_SUBCATEGORY, but the new category is the default one in this file
73
74 What we want here is a root category (it does not belong to any existing
75 channel, for sure), and we want it to be the default one in our file (of
76 course, it's the only one).
77 \dontinclude 06-logs.c
78 \skip XBT_LOG
79 \until XBT_LOG
80
81 Then, we change any call to fprintf to one of the logging macros. There is a
82 plenty of them, called &lt;priority&gt;&lt;nb args&gt;, such as #DEBUG10,
83 which produces a debuging log event onto the default category. You have to
84 declare the name of arguments as part of the macro name for compatibility
85 with old compilers not accepting variable number of arguments. Here is a
86 partial list of the existing macros: #DEBUG10, #VERB10, #INFO10, #WARN10,
87 #ERROR10 and #CRITICAL10. For each priority, this is the biggest macro (and
88 the others are not documented for sake of clarity in the relevant document).
89 Ie, VERB20 does not exist, but VERB0 does exist. I may add more if someone
90 wants more, but that should be enough for most purposes.
91
92 Note also that there is no need to add a '\\n' at the end of your format
93 line, it gets automatically added.
94
95 \section GRAS_tut_tour_logs_recap Recapping everything together
96
97 Once we changed any fprintf of our code to some of these macros, the program
98 may read:  
99 \include 06-logs.c
100
101 And the output now looks better:
102 \include 06-logs.output
103
104 \section GRAS_tut_tour_logs_config The user side: configuring logs at run time
105
106 \section GRAS_tut_tour_logs_config_prio Choosing what gets displayed
107
108 Once we changed our program to use proper logging, it is naturally possible
109 to choose at run time what we want to see. For example, if we want more
110 details about our code, we should pass <tt>--log=test.thres:verbose</tt>
111 on the command line of our programs to change the <tt>thres</tt>old.
112 Note that a VERBOSE line appears on client side:
113 \include 06-logs.output.verbose
114
115 On the contrary, if we want to reduce the amount of logging, we may want to
116 do pass <tt>--log=test.thres:error</tt>: 
117
118 \section GRAS_tut_tour_logs_config_layout Choosing how things get displayed
119
120 As with SimGrid 3.3, it is also possible to change how each and every
121 message get displayed from the command line without even recompiling
122 your code. This is done by changing the <tt>fmt</tt> field of the
123 category you want to change. The value of this field is somehow
124 similar to printf's format strings, with several existing specifiers.
125
126 For example, if you just want the message you passed to the macro
127 without any decoration about the host which raised it, its pid and
128 everything, just pass <tt>--log=test.fmt:%m</tt>:
129 \include 06-logs.output.fmt
130
131 For debuging purpose, you can even ask to get the backtrace at each
132 execution point:
133 \include 06-logs.output.fmt-bt
134
135
136 Again, you should refer to the \ref XBT_log section for more information on
137 how to configure the logs. Or you can proceed with the next lesson, of
138 course.
139
140 Go to \ref GRAS_tut_tour_timers
141 */