1 /*
2  * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3  *           (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
4  *
5  * This file is part of lsp-plugins
6  * Created on: 14 окт. 2015 г.
7  *
8  * lsp-plugins is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * any later version.
12  *
13  * lsp-plugins is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with lsp-plugins. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef CORE_DEBUG_H_
23 #define CORE_DEBUG_H_
24 
25 // Include <stdio.h> to perform debugging output
26 #include <stdio.h>
27 #include <stdarg.h>
28 
29 #ifdef LSP_LOG_FD
30     #undef LSP_LOG_FD
31 #endif /* LSP_LOG_FD */
32 
33 #ifdef LSP_TRACEFILE
34     #define LSP_LOG_FD              ::lsp::log_fd
35 #else
36     #define LSP_LOG_FD              stderr
37 #endif /* LSP_TRACEFILE */
38 
39 // Check trace level
40 #ifdef LSP_TRACE
41     #define lsp_trace(msg, ...)     { fprintf(LSP_LOG_FD, "[TRC][%s:%4d] %s: " msg "\n", __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); fflush(LSP_LOG_FD); }
42 
43     // Debug is always turned on when trace is turned on
44     #ifndef LSP_DEBUG
45         #define LSP_DEBUG
46     #endif /* LSP_DEBUG */
47 #else
48     #define lsp_trace(msg, ...)
49 #endif /* LSP_TRACE */
50 
51 // Check debug level
52 #ifdef LSP_DEBUG
53     #define lsp_nprintf(msg, ...)       fprintf(LSP_LOG_FD, msg, ## __VA_ARGS__)
54     #define lsp_nvprintf(msg, va_args)  vfprintf(LSP_LOG_FD, msg, va_args)
55     #define lsp_printf(msg, ...)        { fprintf(LSP_LOG_FD, msg "\n", ## __VA_ARGS__); fflush(LSP_LOG_FD); }
56     #define lsp_debug(msg, ...)         { fprintf(LSP_LOG_FD, "[DBG][%s:%4d] %s: " msg "\n", __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); fflush(LSP_LOG_FD); }
57     #define lsp_dumpf(s, fmt, p, n)     ::lsp::__lsp_dumpf(s, fmt, p, n)
58     #define lsp_dumpb(s, p, sz)         ::lsp::__lsp_dumpb(s, p, sz)
59 #else
60     #define lsp_nprintf(msg, ...)
61     #define lsp_nvprintf(msg, va_args)
62     #define lsp_printf(msg, ...)
63     #define lsp_debug(msg, ...)
64     #define lsp_dumpf(s, fmt, p, n)
65     #define lsp_dumpb(s, p, sz)
66 #endif /* LSP_DEBUG */
67 
68 #ifdef LSP_DEBUG
69     #define lsp_error(msg, ...)     { fprintf(LSP_LOG_FD, "[ERR][%s:%4d] %s: " msg "\n", __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); fflush(LSP_LOG_FD); }
70     #define lsp_warn(msg, ...)      { fprintf(LSP_LOG_FD, "[WRN][%s:%4d] %s: " msg "\n", __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); fflush(LSP_LOG_FD); }
71     #define lsp_info(msg, ...)      { fprintf(LSP_LOG_FD, "[INF][%s:%4d] %s: " msg "\n", __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); fflush(LSP_LOG_FD); }
72 #else
73     #define lsp_error(msg, ...)     { fprintf(LSP_LOG_FD, "[ERR] " msg "\n", ## __VA_ARGS__); fflush(LSP_LOG_FD); }
74     #define lsp_warn(msg, ...)      { fprintf(LSP_LOG_FD, "[WRN] " msg "\n", ## __VA_ARGS__); fflush(LSP_LOG_FD); }
75     #define lsp_info(msg, ...)      { fprintf(LSP_LOG_FD, "[INF] " msg "\n", ## __VA_ARGS__); fflush(LSP_LOG_FD); }
76 #endif /* LSP_DEBUG */
77 
78 // Define assertions
79 #ifdef LSP_DEBUG
80     #define lsp_paranoia(...)   { __VA_ARGS__; }
81 
82     #define lsp_guard_assert(...) __VA_ARGS__;
83     #define lsp_assert(x)           if (!(x)) { lsp_error("Assertion failed: %s", #x); fflush(LSP_LOG_FD); }
84     #define lsp_assert_msg(x, msg, ...)  \
85             if (!(x)) { \
86                 fprintf(LSP_LOG_FD, "[ERR][%s:%4d] %s: Assertion failed: %s, nested message: " msg "\n", \
87                     __FILE__, __LINE__, __FUNCTION__, #x, ## __VA_ARGS__); \
88                 fflush(LSP_LOG_FD); \
89             }
90 
91 #else
92     #define lsp_paranoia(...)
93 
94     #define lsp_guard_assert(...)
95     #define lsp_assert(x)
96     #define lsp_assert_msg(x, ...)
97 #endif /* ASSERTIONS */
98 
99 namespace lsp
100 {
101 // Define initialization function
102 #ifdef LSP_TRACEFILE
103     #define lsp_debug_init(subsystem)        lsp::init_debug(subsystem)
104 
105     #ifdef LSP_TRACEFILE
106         extern FILE *log_fd;
107     #endif /* LSP_TRACEFILE */
108 
109     void init_debug(const char *subsystem);
110 #else
111     #define lsp_debug_init(subsystem)
112 #endif /* LSP_DEBUG */
113 
114 #ifdef LSP_DEBUG
115     void __lsp_dumpf(const char *s, const char *fmt, const float *f, size_t n);
116     void __lsp_dumpb(const char *s, const void *b, size_t sz);
117 #endif /* LSP_DEBUG */
118 }
119 
120 #endif /* CORE_DEBUG_H_ */
121