MediaProcessors
log.h
1 /*
2  * Copyright (c) 2017, 2018 Rafael Antoniello
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of copyright holders nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef UTILS_SRC_LOG_H_
31 #define UTILS_SRC_LOG_H_
32 
33 
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <inttypes.h>
37 #include <string.h>
38 
39 /* **** Definitions **** */
40 
44 #define LOG_FORCE_USING_STDOUT
45 
47 typedef enum {
48  LOG_VERBOSE= 0,
49  LOG_DEBUG,
50  LOG_WARNING,
51  LOG_ERROR,
52  LOG_RAW,
53  LOG_EVENT,
54  LOG_TYPE_MAX
55 } log_level_t;
56 
58 typedef enum log_verbose_level_enum {
59  LOG_SILENT= 0,
60  LOG_INFORMATIVE,
61  LOG_VERBOSE_DBG,
62  LOG_VERBOSE_LEVEL_MAX
63 } log_verbose_level_t;
64 
68 #define LOG_LINE_SIZE 1024
69 #define LOG_DATE_SIZE 64
70 
74 #define LOG_BUF_LINES_NUM 15
75 
76 typedef struct log_line_ctx_s {
77  char code[LOG_LINE_SIZE];
78  char desc[LOG_LINE_SIZE];
79  char date[LOG_DATE_SIZE];
80  uint64_t ts; // Monotonic time-stamp in seconds (for the sake of comparison)
81  uint64_t count; // zero means not initialized / not valid line
83 
84 /* Forward declarations */
85 typedef struct log_ctx_s log_ctx_t;
86 typedef struct llist_s llist_t;
87 
89 #define __FILENAME__ strrchr("/" __FILE__, '/') + 1
90 
91 #ifdef LOG_CTX_DEFULT // To be defined specifically in source files
92  #define _LOG(TYPE, FORMAT, ...) \
93  log_trace(TYPE, NULL, __FILENAME__, __LINE__, FORMAT, ##__VA_ARGS__)
94 #else
95  #define LOG_CTX_INIT(CTX) \
96  log_ctx_t *__log_ctx= CTX
97  #define LOG_CTX_SET(CTX) \
98  __log_ctx= CTX
99  #define LOG_CTX_GET() __log_ctx
100  #define _LOG(TYPE, FORMAT, ...) \
101  log_trace(TYPE, __log_ctx, __FILENAME__, __LINE__, FORMAT, \
102  ##__VA_ARGS__)
103 #endif
104 
105 #define LOG(FORMAT, ...) _LOG(LOG_RAW, FORMAT, ##__VA_ARGS__)
106 #define LOGV(FORMAT, ...) _LOG(LOG_VERBOSE, FORMAT, ##__VA_ARGS__)
107 #define LOGW(FORMAT, ...) _LOG(LOG_WARNING, FORMAT, ##__VA_ARGS__)
108 #define LOGE(FORMAT, ...) _LOG(LOG_ERROR, FORMAT, ##__VA_ARGS__)
109 #define LOGEV(FORMAT, ...) _LOG(LOG_EVENT, FORMAT, ##__VA_ARGS__)
110 
111 /* **** Prototypes **** */
112 
116 int log_module_open();
117 
121 void log_module_close();
122 
126 log_ctx_t* log_open(int id);
127 
131 void log_close(log_ctx_t **ref_log_ctx);
132 
136 void log_trace(log_level_t type, log_ctx_t *log_ctx, const char *filename,
137  int line, const char *format, ...);
138 
142 const llist_t* log_get(log_ctx_t *log_ctx);
143 
147 void log_clear(log_ctx_t *log_ctx);
148 
152 log_line_ctx_t* log_line_ctx_allocate();
153 
157 log_line_ctx_t* log_line_ctx_dup(const log_line_ctx_t* log_line_ctx);
158 
162 void log_line_ctx_release(log_line_ctx_t **ref_log_line_ctx);
163 
173 #define LOG_TRACE_BYTE_TABLE(LABEL, DATA, LEN, XSIZE) \
174  log_trace_byte_table(LABEL, __FILENAME__, __LINE__, DATA, LEN, XSIZE);
175 void log_trace_byte_table(const char *label, const char *file, int line,
176  uint8_t *data, size_t len, size_t xsize);
177 
178 #endif /* UTILS_SRC_LOG_H_ */
Definition: llist.h:49
Definition: log.c:102