Changes between Version 6 and Version 7 of Logging
- Timestamp:
- 2012-10-05T09:20:54Z (12 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Logging
v6 v7 115 115 == Using the C API == 116 116 117 T ODO log_msg, log_init117 To use the logging functions, include `<io/log.h>` into your source files. Before logging can be used, you have to initialize the logging subsystem by calling `log_init()`. This function takes the log name as an argument. It would be typically name of your application. 118 118 119 For example, our `tester` application is initialized with 120 {{{ 121 #!c 122 log_init("tester"); 123 }}} 119 124 120 == Behind the scene 125 Once the logging is initialized, you may use the `log_msg()` which actually prints the messages. The `log_msg` is a printf-like function, first arguments are the target log and the level of the message. For starters, you may use the default log `LOG_DEFAULT`. 121 126 122 TODO - how logger, logset and log_*() works 127 Following code prints the pointer address if the reporting level is at least `debug`. 128 {{{ 129 #!c 130 void *p = malloc(...); 131 log_msg(LOG_DEFAULT, LVL_DEBUG, "Allocated at %p.", p); 132 }}} 123 133 134 Notice that there is no new-line at the end of the message. The new-line is automatically appended before writing the message to the file. 124 135 136 To create a sub-log, use the `log_create()` function. It takes two arguments - log name and its parent log. The parent log could be either `LOG_DEFAULT` or log created during previous call to `log_create()`. Note that `log_create()` always returns a valid `log_t`. A footnote: yes, log creation can fail but rarely and the worst thing that can happen is that the messages would go to the parent which is not that big problem. 125 137 126 == Obsoleted == 127 128 '''Information below is obsoleted and will be rewritten soon. ''' 129 130 To use logging functions, include `io/log.h` and initialize the logging 131 subsystem. After that, you can use the `log_msg` to do the logging. 138 '''Example''': 132 139 133 140 {{{ … … 136 143 #include <io/log.h> 137 144 138 #define NAME "myapp"139 145 ... 140 146 141 147 int main(int argc, char *argv[]) 142 148 { 143 log_init(NAME, LVL_NOTE); 144 ... 145 if (rc != EOK) { 146 log_msg(LVL_ERROR, "Something failed (rc=%d), trying fallback...", rc); 147 ... 148 } 149 log_init("myapp"); 150 151 /* The alpha log would be directly under the default log. */ 152 log_t log_alpha = log_create("alpha", LOG_DEFAULT); 153 154 /* The bravo log would be under alpha. */ 155 log_t log_bravo = log_create("bravo", log_alpha); 156 157 158 log_msg(LOG_DEFAULT, LVL_WARN, "Dummy warning."); 159 160 /* ... */ 161 162 log_msg(log_alpha, LVL_NOTE, "Alpha is running."); 163 164 /* Notice how log_t is printed. */ 165 log_msg(log_bravo, LVL_DEBUG2, "This log id is %" PRIlogctx ".", log_bravo); 166 167 /* ... */ 149 168 } 150 169 }}} 151 170 152 The first argument to `log_init` is application name, the second is the153 default logging level. Messages with higher level won't be printed at all.154 Normally the (when an application is not being debugged) the logging level155 should be set to LVL_NOTE. That is, messages with level LVL_NOTE or higher156 are printed.157 171 158 `log_msg` is a `printf`-like function where first argument is level of 159 the message (its seriousness). 160 172 == Behind the scene == 173 TODO - how logger, logset and log_*() works