11/*
2- * Copyright (c) 2014-2016 Cesanta Software Limited
2+ * Copyright (c) 2014-2018 Cesanta Software Limited
33 * 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.
416 */
517
618#ifndef CS_COMMON_CS_DBG_H_
719#define CS_COMMON_CS_DBG_H_
820
21+ #include "common/platform.h"
22+
923#if CS_ENABLE_STDIO
1024#include <stdio.h>
1125#endif
1428#define CS_ENABLE_DEBUG 0
1529#endif
1630
31+ #ifndef CS_LOG_PREFIX_LEN
32+ #define CS_LOG_PREFIX_LEN 24
33+ #endif
34+
1735#ifndef CS_LOG_ENABLE_TS_DIFF
1836#define CS_LOG_ENABLE_TS_DIFF 0
1937#endif
2240extern "C" {
2341#endif /* __cplusplus */
2442
43+ /*
44+ * Log level; `LL_INFO` is the default. Use `cs_log_set_level()` to change it.
45+ */
2546enum cs_log_level {
2647 LL_NONE = -1 ,
2748 LL_ERROR = 0 ,
@@ -34,29 +55,78 @@ enum cs_log_level {
3455 _LL_MAX = 5 ,
3556};
3657
58+ /*
59+ * Set max log level to print; messages with the level above the given one will
60+ * not be printed.
61+ */
3762void cs_log_set_level (enum cs_log_level level );
3863
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+ */
75+ void 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+ */
84+ int cs_log_print_prefix (enum cs_log_level level , const char * fname , int line );
85+
86+ extern enum cs_log_level cs_log_level ;
87+
3988#if CS_ENABLE_STDIO
4089
90+ /*
91+ * Set file to write logs into. If `NULL`, logs go to `stderr`.
92+ */
4193void cs_log_set_file (FILE * file );
4294
43- extern enum cs_log_level cs_log_threshold ;
44- void cs_log_print_prefix (const char * func );
45- void cs_log_printf (const char * fmt , ...);
95+ /*
96+ * Prints log to the current log file, appends "\n" in the end and flushes the
97+ * stream.
98+ */
99+ void 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)
46117
47- #define LOG ( l , x ) \
48- if (cs_log_threshold >= l) { \
49- cs_log_print_prefix(__func__); \
50- cs_log_printf x; \
51- }
118+ #else
119+
120+ #define LOG ( l , x ) ((void) l)
121+
122+ #endif
52123
53124#ifndef CS_NDEBUG
54125
55- #define DBG (x ) \
56- if (cs_log_threshold >= LL_VERBOSE_DEBUG) { \
57- cs_log_print_prefix(__func__); \
58- cs_log_printf x; \
59- }
126+ /*
127+ * Shortcut for `LOG(LL_VERBOSE_DEBUG, (...))`
128+ */
129+ #define DBG (x ) LOG(LL_VERBOSE_DEBUG, x)
60130
61131#else /* NDEBUG */
62132
0 commit comments