1 //
2 // Debug.hh for pekwm
3 // Copyright (C) 2021 Claes Nästén <pekdon@gmail.com>
4 // Copyright (C) 2012-2013 Andreas Schlick <ioerror@lavabit.com>
5 //
6 // This program is licensed under the GNU GPL.
7 // See the LICENSE file for more information.
8 //
9 
10 #ifndef _PEKWM_DEBUG_HH_
11 #define _PEKWM_DEBUG_HH_
12 
13 #include "Compat.hh"
14 
15 #include <fstream>
16 #include <iomanip>
17 #include <iostream>
18 #include <vector>
19 #include <sstream>
20 #include <string>
21 
22 namespace Debug
23 {
24 	enum Level {
25 		LEVEL_ERR,
26 		LEVEL_WARN,
27 		LEVEL_INFO,
28 		LEVEL_DEBUG,
29 		LEVEL_TRACE
30 	};
31 
32 	Level getLevel(const std::string& str);
33 	bool isLevel(Level level);
34 	Level getLevel(void);
35 	void setLevel(Level level);
36 
37 	void doAction(const std::string& action);
38 
39 	std::ostream& getStream(const char* prefix);
40 	std::ostream& getStream(const char* file, int line, const char* prefix);
41 	bool setLogFile(const std::string& path);
42 }
43 
44 #define USER_INFO(M)				\
45 	Debug::getStream("") << M << std::endl;
46 #define USER_WARN(M)						\
47 	Debug::getStream("WARNING: ") << M << std::endl;
48 
49 #define P_TRACE(M)							\
50 	if (Debug::isLevel(Debug::LEVEL_TRACE)) {			\
51 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "TRACE:   ") \
52 			<< M << std::endl;				\
53 	}
54 
55 #define P_DBG(M)							\
56 	if (Debug::isLevel(Debug::LEVEL_DEBUG)) {			\
57 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "DEBUG:   ") \
58 			<< M << std::endl;				\
59 	}
60 
61 #define P_LOG(M)							\
62 	if (Debug::isLevel(Debug::LEVEL_INFO)) {			\
63 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "") << M << std::endl; \
64 	}
65 
66 #define P_LOG_IF(C, M)							\
67 	if ((C) && Debug::isLevel(Debug::LEVEL_INFO)) {			\
68 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "") << M << std::endl; \
69 	}
70 
71 #define P_WARN(M)							\
72 	if (Debug::isLevel(Debug::LEVEL_WARN)) {			\
73 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "WARNING: ") \
74 			<< M << std::endl;				\
75 	}
76 
77 #define P_ERR(M)							\
78 	if (Debug::isLevel(Debug::LEVEL_ERR)) {				\
79 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "ERROR:   ") \
80 			<< M << std::endl;				\
81 	}
82 
83 #define P_ERR_IF(C, M)							\
84 	if ((C) && Debug::isLevel(Debug::LEVEL_ERR)) {			\
85 		Debug::getStream(__PRETTY_FUNCTION__, __LINE__, "ERROR:   ") \
86 			<< M << std::endl;				\
87 	}
88 
89 #define FMT_HEX(V)				\
90 	"0x" << std::hex << (V) << std::dec
91 
92 #endif // _PEKWM_DEBUG_HH_
93