~aleteoryx/muditaos

ref: def5dbcfa99639957c3769bbb2eca550e287fc8c muditaos/module-utils/log/log.hpp -rw-r--r-- 3.0 KiB
def5dbcf — Mateusz Grzegorzek [EGD-5908] Fix bug in Logger + add log unit test 4 years ago
                                                                                
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
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

/*
 *  This module is wrapper over printf with additional thread safety provided and common logs levels.
 *
 *  USAGE:
 *
 *  Include this header in any file that wishes to write to logger(s).  In
 *  exactly one file (per executable), define LOG_MAIN first (e.g. in your
 *  main .c file).
 *
 *     #define LOG_MAIN
 *     #include "logs/log.h"
 *
 *  This will define the actual objects that all the other units will use.
 *  Then invoke log_Init(VFS_FILE* fp,logger_level level) function to initialize logger utility. If you want
 *additionally store logs into file pass valid file descriptor to init function. By default logger logs info into STDOUT
 *stream. After that logger is ready to use.
 *
 *  Examples:
 *  1)
 *   log_Init(NULL,LOGDEBUG);
 *   Send logs(level higher or equal to LOGDEBUG) to STDOUT stream.
 *
 *  2)
 *   log_Init('valid file pointer',LOGINFO);
 *   Send logs(level higher or equal to LOGINFO) to STDOUT stream and also to file specified by user.
 */

#ifndef LOG_LOG_H_
#define LOG_LOG_H_

#include "debug.hpp"
#include <stdint.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#define RTT_LUART RTT_LUART

#ifdef __cplusplus
extern "C"
{
#endif

    typedef enum
    {
        LOGTRACE,
        LOGDEBUG,
        LOGINFO,
        LOGWARN,
        LOGERROR,
        LOGFATAL
    } logger_level;

#define __FILENAME__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)

    /**
     * Forward declarations
     */
    int log_Log(logger_level level, const char *file, int line, const char *function, const char *fmt, ...)
        __attribute__((format(printf, 5, 6)));
    int log_Printf(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
    void log_WriteToDevice(const uint8_t *pBuffer, unsigned NumBytes);

/**
 * Log functions (one per level).
 */
#define LOG_PRINTF(...)              log_Printf(__VA_ARGS__)
#define LOG_TRACE(...)               log_Log(LOGTRACE, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_DEBUG(...)               log_Log(LOGDEBUG, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_INFO(...)                log_Log(LOGINFO, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_WARN(...)                log_Log(LOGWARN, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_ERROR(...)               log_Log(LOGERROR, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_FATAL(...)               log_Log(LOGFATAL, __FILENAME__, __LINE__, __func__, __VA_ARGS__)
#define LOG_CUSTOM(loggerLevel, ...) log_Log(loggerLevel, __FILENAME__, __LINE__, __func__, __VA_ARGS__)

#ifdef __cplusplus
}
#endif

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
static const size_t LOGGER_BUFFER_SIZE  = 8192;
static const double MAX_BUFFER_UTIL_MEM = 0.8 * LOGGER_BUFFER_SIZE;
#pragma GCC diagnostic pop

#endif /* LOG_LOG_H_ */