1 // ----------------------------------------------------------------------------
2 //      debug.h
3 //
4 // Copyright (C) 2008
5 //              Stelios Bounanos, M0GLD
6 //
7 // This file is part of fldigi.
8 //
9 // fldigi is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // fldigi is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 // ----------------------------------------------------------------------------
22 
23 #ifndef _DEBUG_H_
24 #define _DEBUG_H_
25 
26 #define DEBUG_PSKMAIL 0
27 
28 #include <config.h>
29 
30 #include <string>
31 #include <iostream>
32 
33 #include "util.h"
34 
35 extern void rotate_log(std::string);
36 
37 class debug
38 {
39 public:
40 	enum level_e {
41 		QUIET_LEVEL, ERROR_LEVEL, WARN_LEVEL, INFO_LEVEL,
42 		VERBOSE_LEVEL, DEBUG_LEVEL, LOG_NLEVELS
43 	};
44 #ifdef FLARQ_DEBUG
45 	enum source_e {
46 		LOG_ARQCONTROL = 1 << 0,
47 		LOG_RPC_SERVER = 1 << 1,
48 		LOG_OTHER = 1 << 2
49 	};
50 #else
51 	enum source_e {
52 		LOG_ARQCONTROL = 1 << 0,
53 		LOG_AUDIO = 1 << 1,
54 		LOG_MODEM = 1 << 2,
55 		LOG_RIGCONTROL = 1 << 3,
56 		LOG_RPC_CLIENT = 1 << 4,
57 		LOG_RPC_SERVER = 1 << 5,
58 		LOG_SPOTTER = 1 << 6,
59 		LOG_DATASOURCES = 1 << 7,
60 		LOG_SYNOP = 1 << 8,
61 		LOG_KML = 1 << 9,
62 		LOG_KISSCONTROL = 1 << 10,
63 		LOG_MACLOGGER = 1 << 11,
64 		LOG_FD = 1 << 12,
65 		LOG_N3FJP = 1 << 13,
66 		LOG_OTHER = 1 << 14
67 	};
68 #endif
69 	static void start(const char* filename);
70 	static void stop(void);
71 	static void hex_dump(const char * func, const char * data, int len);
72 	static void log(level_e level, const char* func, const char* srcf, int line,
73 			const char* format, ...) format__(printf, 5, 6);
74 	static void elog(const char* func, const char* srcf, int line, const char* text);
75 	static void show(void);
76 	static level_e level;
77 	static unsigned int mask;
78 private:
79 	static void sync_text(void*);
80 	debug(const char* filename);
81 	debug(const debug&);
82 	debug& operator=(const debug&);
83 	~debug();
84 	static debug* inst;
85 };
86 
87 #define LOG(level__, source__, ...)							                \
88 	do {										                            \
89 		if (level__ <= debug::level && source__ & debug::mask)			    \
90 			debug::log(level__, __func__, __FILE__, __LINE__, __VA_ARGS__); \
91 	} while (0)
92 
93 
94 #define LOG_DEBUG(...) LOG(debug::DEBUG_LEVEL, log_source_, __VA_ARGS__)
95 #define LOG_VERBOSE(...) LOG(debug::VERBOSE_LEVEL, log_source_, __VA_ARGS__)
96 #define LOG_INFO(...) LOG(debug::INFO_LEVEL, log_source_, __VA_ARGS__)
97 #define LOG_WARN(...) LOG(debug::WARN_LEVEL, log_source_, __VA_ARGS__)
98 #define LOG_ERROR(...) LOG(debug::ERROR_LEVEL, log_source_, __VA_ARGS__)
99 
100 #define LOG_HD(source__, a, b)               \
101 	do {									 \
102 		if (source__ & debug::mask)          \
103 			debug::hex_dump(__func__, a, b); \
104 	} while (0)
105 
106 #define LOG_HEX(a,b) LOG_HD(log_source_, a, b)
107 
108 #define LOG_PERROR(msg__)								\
109 	do {										\
110 		if (debug::ERROR_LEVEL <= debug::level && log_source_ & debug::mask)	\
111 			debug::elog(__func__, __FILE__, __LINE__, msg__);		\
112 	} while (0)
113 
114 unused__ static unsigned int log_source_ = debug::LOG_OTHER;
115 #if defined(__GNUC__) && (__GNUC__ >= 3)
116 #  define LOG_FILE_SOURCE(source__)						\
117 	__attribute__((constructor))						\
118 	static void log_set_source_(void) { log_source_ = source__; }
119 #else
120 #  define LOG_FILE_SOURCE(source__)
121 #endif
122 
123 #define LOG_SET_SOURCE(source__) log_source_ = source__
124 
125 extern void mnu_debug_level_cb();
126 extern void btn_debug_source_cb(int n);
127 extern void clear_debug();
128 extern void set_debug_mask(int mask);
129 
130 #endif // _DEBUG_H_
131 
132 // Local Variables:
133 // mode: c++
134 // c-file-style: "linux"
135 // End:
136