Logo AND Algorithmique Numérique Distribuée

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