Changes between Version 6 and Version 7 of Logging


Ignore:
Timestamp:
2012-10-05T09:20:54Z (12 years ago)
Author:
Vojtech Horky
Comment:

Fix section about C API

Legend:

Unmodified
Added
Removed
Modified
  • Logging

    v6 v7  
    115115== Using the C API ==
    116116
    117 TODO  log_msg, log_init
     117To 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.
    118118
     119For example, our `tester` application is initialized with
     120{{{
     121#!c
     122log_init("tester");
     123}}}
    119124
    120 == Behind the scene
     125Once 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`.
    121126
    122 TODO - how logger, logset and log_*() works
     127Following code prints the pointer address if the reporting level is at least `debug`.
     128{{{
     129#!c
     130void *p = malloc(...);
     131log_msg(LOG_DEFAULT, LVL_DEBUG, "Allocated at %p.", p);
     132}}}
    123133
     134Notice 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.
    124135
     136To 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.
    125137
    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''':
    132139
    133140{{{
     
    136143#include <io/log.h>
    137144
    138 #define NAME "myapp"
    139145...
    140146
    141147int main(int argc, char *argv[])
    142148{
    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    /* ... */
    149168}
    150169}}}
    151170
    152 The first argument to `log_init` is application name, the second is the
    153 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 level
    155  should be set to LVL_NOTE. That is, messages with level LVL_NOTE or higher
    156  are printed.
    157171
    158 `log_msg` is a `printf`-like function where first argument is level of
    159 the message (its seriousness).
    160 
     172== Behind the scene ==
     173TODO - how logger, logset and log_*() works