aboutsummaryrefslogtreecommitdiffstats
path: root/examples/print-tty.c
blob: e38cf573a67c55dd1e43964c0c6da823e7c47915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ==========================================================================
    Licensed under BSD 2clause license See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ========================================================================== */

#include "embedlog.h"
#include <termios.h>

int main(void)
{
    /*
     * first we nned to initialize logger to known state
     */

    el_init();

    /*
     * to use logger you need to enable at least one output, without it logs
     * will be printed to /dev/null. Here we set output to serial device.
     */

    el_option(EL_OUT, EL_OUT_TTY);

    /*
     * enbaling tty output is not enough, we still need to configure which
     * device we want to use and at what speed. Transmission parameters are
     * 8N1 by default. Baudrate should be taken from termios (3).
     */

    if (el_option(EL_TTY_DEV, "/dev/ttyUSB1", B9600) != 0)
    {
        perror("tty set failed");
        return 1;
    }

    /*
     * now we can simply print messages like we would do it with ordinary
     * printf - we just need to log level macro as a first argument
     */

    el_print(ELI, "Info message");
    el_print(ELF, "Fatal message with additional argument %d", 42);
    el_print(ELD, "Debug message that won't be printed due to log level");

    /*
     * we can change log level in runtime as we see fit, now enable debug
     * prints
     */

    el_option(EL_LEVEL, EL_DBG);
    el_print(ELD, "But now debug will be printed");

    /*
     * altough embedlog does not use dynamic allocation by itself, system may
     * allocate some resources (like opened file descriptors when printing to
     * file), with el_cleanup, we can make sure all resources are freed. In
     * this example, this function will close opened tty file descriptor.
     */

    el_cleanup();

    return 0;
}