1 // ----------------------------------------------------------------------------
2 // debug.h
3 //
4 // Copyright (C) 2008
5 //           Stelios Bounanos, M0GLD
6 // ----------------------------------------------------------------------------
7 // Copyright (C) 2014
8 //              David Freese, W1HKJ
9 //
10 // This file is part of flrig.
11 //
12 // flrig is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation; either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // flrig is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 // ----------------------------------------------------------------------------
25 
26 
27 #ifndef _DEBUG_H_
28 #define _DEBUG_H_
29 
30 #include "util.h"
31 
32 class debug
33 {
34 public:
35 	enum level_e { QUIET_LEVEL, ERROR_LEVEL, WARN_LEVEL, INFO_LEVEL, DEBUG_LEVEL, LOG_NLEVELS };
36 	enum source_e {
37 		LOG_RIGCONTROL = 1 << 0, LOG_RPC = 1 << 1, LOG_OTHER = 1 << 2
38 	};
39 	static void start(const char* filename);
40 	static void stop(void);
41 	static void log(level_e level, const char* func, const char* srcf, int line,
42 			const char* format, ...) format__(printf, 5, 6);
43 	static void slog(level_e level, const char* func, const char* srcf, int line,
44 			const char* format, ...) format__(printf, 5, 6);
45 
46 	static void elog(const char* func, const char* srcf, int line, const char* text);
47 	static void show(void);
48 	static level_e level;
49 	static uint32_t mask;
50 private:
51 	static void sync_text(void*);
52 	debug(const char* filename);
53 	debug(const debug&);
54 	debug& operator=(const debug&);
55 	~debug();
56 	static debug* inst;
57 };
58 
59 #define LOG(level__, source__, ...)							\
60 	do {										\
61 		if (level__ <= debug::level && source__ & debug::mask)			\
62 			debug::log(level__, __func__, __FILE__, __LINE__, __VA_ARGS__); \
63 	} while (0)
64 
65 #define LOG_DEBUG(...) LOG(debug::DEBUG_LEVEL, log_source_, __VA_ARGS__)
66 #define LOG_INFO(...) LOG(debug::INFO_LEVEL, log_source_, __VA_ARGS__)
67 #define LOG_WARN(...) LOG(debug::WARN_LEVEL, log_source_, __VA_ARGS__)
68 #define LOG_ERROR(...) LOG(debug::ERROR_LEVEL, log_source_, __VA_ARGS__)
69 #define LOG_QUIET(...) LOG(debug::QUIET_LEVEL, log_source_, __VA_ARGS__)
70 
71 #define SLOG(level__, source__, ...)							\
72 	do {										\
73 		if (level__ <= debug::level && source__ & debug::mask)			\
74 			debug::slog(level__, __func__, __FILE__, __LINE__, __VA_ARGS__); \
75 	} while (0)
76 
77 #define SLOG_DEBUG(...) SLOG(debug::DEBUG_LEVEL, log_source_, __VA_ARGS__)
78 #define SLOG_INFO(...) SLOG(debug::INFO_LEVEL, log_source_, __VA_ARGS__)
79 #define SLOG_WARN(...) SLOG(debug::WARN_LEVEL, log_source_, __VA_ARGS__)
80 #define SLOG_ERROR(...) SLOG(debug::ERROR_LEVEL, log_source_, __VA_ARGS__)
81 #define SLOG_QUIET(...) SLOG(debug::QUIET_LEVEL, log_source_, __VA_ARGS__)
82 
83 #define LOG_PERROR(msg__)								\
84 	do {										\
85 		if (debug::ERROR_LEVEL <= debug::level && log_source_ & debug::mask)	\
86 			debug::elog(__func__, __FILE__, __LINE__, msg__);		\
87 	} while (0)
88 
89 unused__ static uint32_t log_source_ = debug::LOG_OTHER;
90 #if defined(__GNUC__) && (__GNUC__ >= 3)
91 #  define LOG_FILE_SOURCE(source__)						\
92 	__attribute__((constructor))						\
93 	static void log_set_source_(void) { log_source_ = source__; }
94 #else
95 #  define LOG_FILE_SOURCE(source__)
96 #endif
97 
98 #define LOG_SET_SOURCE(source__) log_source_ = source__
99 
100 #define SERIAL_DEBUG 0
101 
102 #endif // _DEBUG_H_
103 
104 // Local Variables:
105 // mode: c++
106 // c-file-style: "linux"
107 // End:
108