Flipper Zero Firmware
Loading...
Searching...
No Matches
cs_dbg.h
1/*
2 * Copyright (c) 2014-2018 Cesanta Software Limited
3 * All rights reserved
4 *
5 * Licensed under the Apache License, Version 2.0 (the ""License"");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an ""AS IS"" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef CS_COMMON_CS_DBG_H_
19#define CS_COMMON_CS_DBG_H_
20
21#include "platform.h"
22
23#if CS_ENABLE_STDIO
24#include <stdio.h>
25#endif
26
27#ifndef CS_ENABLE_DEBUG
28#define CS_ENABLE_DEBUG 0
29#endif
30
31#ifndef CS_LOG_PREFIX_LEN
32#define CS_LOG_PREFIX_LEN 24
33#endif
34
35#ifndef CS_LOG_ENABLE_TS_DIFF
36#define CS_LOG_ENABLE_TS_DIFF 0
37#endif
38
39#ifdef __cplusplus
40extern "C" {
41#endif /* __cplusplus */
42
43/*
44 * Log level; `LL_INFO` is the default. Use `cs_log_set_level()` to change it.
45 */
46enum cs_log_level {
47 LL_NONE = -1,
48 LL_ERROR = 0,
49 LL_WARN = 1,
50 LL_INFO = 2,
51 LL_DEBUG = 3,
52 LL_VERBOSE_DEBUG = 4,
53
54 _LL_MIN = -2,
55 _LL_MAX = 5,
56};
57
58/*
59 * Set max log level to print; messages with the level above the given one will
60 * not be printed.
61 */
62void cs_log_set_level(enum cs_log_level level);
63
64/*
65 * A comma-separated set of prefix=level.
66 * prefix is matched against the log prefix exactly as printed, including line
67 * number, but partial match is ok. Check stops on first matching entry.
68 * If nothing matches, default level is used.
69 *
70 * Examples:
71 * main.c:=4 - everything from main C at verbose debug level.
72 * mongoose.c=1,mjs.c=1,=4 - everything at verbose debug except mg_* and mjs_*
73 *
74 */
75void cs_log_set_file_level(const char* file_level);
76
77/*
78 * Helper function which prints message prefix with the given `level`.
79 * If message should be printed (according to the current log level
80 * and filter), prints the prefix and returns 1, otherwise returns 0.
81 *
82 * Clients should typically just use `LOG()` macro.
83 */
84int cs_log_print_prefix(enum cs_log_level level, const char* fname, int line);
85
86extern enum cs_log_level cs_log_level;
87
88#if CS_ENABLE_STDIO
89
90/*
91 * Set file to write logs into. If `NULL`, logs go to `stderr`.
92 */
93void cs_log_set_file(FILE* file);
94
95/*
96 * Prints log to the current log file, appends "\n" in the end and flushes the
97 * stream.
98 */
99void cs_log_printf(const char* fmt, ...) PRINTF_LIKE(1, 2);
100
101#if CS_ENABLE_STDIO
102
103/*
104 * Format and print message `x` with the given level `l`. Example:
105 *
106 * ```c
107 * LOG(LL_INFO, ("my info message: %d", 123));
108 * LOG(LL_DEBUG, ("my debug message: %d", 123));
109 * ```
110 */
111#define LOG(l, x) \
112 do { \
113 if(cs_log_print_prefix(l, __FILE__, __LINE__)) { \
114 cs_log_printf x; \
115 } \
116 } while(0)
117
118#else
119
120#define LOG(l, x) ((void)l)
121
122#endif
123
124#ifndef CS_NDEBUG
125
126/*
127 * Shortcut for `LOG(LL_VERBOSE_DEBUG, (...))`
128 */
129#define DBG(x) LOG(LL_VERBOSE_DEBUG, x)
130
131#else /* NDEBUG */
132
133#define DBG(x)
134
135#endif
136
137#else /* CS_ENABLE_STDIO */
138
139#define LOG(l, x)
140#define DBG(x)
141
142#endif
143
144#ifdef __cplusplus
145}
146#endif /* __cplusplus */
147
148#endif /* CS_COMMON_CS_DBG_H_ */