summaryrefslogtreecommitdiff
path: root/include/ouroboros/logs.h
blob: db49ae32085775fc3a905d36b4128f72ad6d62d1 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Logging facilities
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#ifndef OUROBOROS_LIB_LOGS_H
#define OUROBOROS_LIB_LOGS_H

#ifndef OUROBOROS_PREFIX
#error You must define OUROBOROS_PREFIX before including this file
#endif

#include <ouroboros/hash.h>

#include <unistd.h>
#include <stdio.h>
#include <stdbool.h>
#include <syslog.h>

#define CLR_RED     "\x1b[31m"
#define CLR_GREEN   "\x1b[32m"
#define CLR_YELLOW  "\x1b[33m"
#define CLR_RESET   "\x1b[0m"

#define DEBUG_CODE "DB"
#define ERROR_CODE "EE"
#define WARN_CODE  "WW"
#define INFO_CODE  "II"

extern bool log_syslog;

void log_init(bool sysout);

void log_fini(void);


#define __olog(CLR, LVL, SYSLVL, ...)                                         \
        do {                                                                  \
                if (log_syslog) {                                             \
                        syslog(SYSLVL, __VA_ARGS__);                          \
                } else {                                                      \
                        printf(CLR "==%05d== " OUROBOROS_PREFIX               \
                               "(" LVL "): ", getpid());                      \
                        printf(__VA_ARGS__);                                  \
                        printf(CLR_RESET "\n");                               \
                        fflush(stdout);                                       \
                }                                                             \
        } while (0)

#define __olog_id(CLR, LVL, SYSLVL, id, fmt, ...)                             \
        do {                                                                  \
                if (log_syslog) {                                             \
                        syslog(SYSLVL, "[" HASH_FMT64 "] " fmt,               \
                               HASH_VAL64(id), ## __VA_ARGS__);               \
                } else {                                                      \
                        printf(CLR "==%05d== " OUROBOROS_PREFIX               \
                               "(" LVL "): ", getpid());                      \
                        printf("[" HASH_FMT64 "] " fmt,                       \
                               HASH_VAL64(id), ## __VA_ARGS__);               \
                        printf(CLR_RESET "\n");                               \
                        fflush(stdout);                                       \
                }                                                             \
        } while (0)

#define log_err(...)                                                          \
         __olog(CLR_RED, ERROR_CODE, LOG_ERR, __VA_ARGS__)
#define log_warn(...)                                                         \
        __olog(CLR_YELLOW, WARN_CODE, LOG_WARNING, __VA_ARGS__)
#define log_info(...)                                                         \
        __olog(CLR_GREEN, INFO_CODE, LOG_INFO, __VA_ARGS__)


#define log_err_id(id, fmt, ...)                                              \
        __olog_id(CLR_RED, ERROR_CODE, LOG_ERR, id, fmt, ## __VA_ARGS__)
#define log_warn_id(id, fmt, ...)                                             \
        __olog_id(CLR_YELLOW, WARN_CODE, LOG_WARNING, id, fmt, ## __VA_ARGS__)
#define log_info_id(id, fmt, ...)                                             \
        __olog_id(CLR_GREEN, INFO_CODE, LOG_INFO, id, fmt, ## __VA_ARGS__)

#ifdef CONFIG_OUROBOROS_DEBUG
#define log_dbg(...)  __olog("", DEBUG_CODE, LOG_DEBUG, __VA_ARGS__)
#define log_dbg_id(id, fmt, ...)                                              \
        __olog_id("", DEBUG_CODE, LOG_DEBUG, id, fmt, ## __VA_ARGS__)
#else
#define log_dbg(...)  do { } while (0)
#define log_dbg_id(...)  do { } while (0)
#endif

#endif /* OUROBOROS_LIB_LOGS_H */