1 /**
2  * WinPR: Windows Portable Runtime
3  * WinPR Logger
4  *
5  * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  * Copyright 2015 Thincast Technologies GmbH
7  * Copyright 2015 Bernhard Miklautz <bernhard.miklautz@thincast.com>
8  *
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #ifndef WINPR_LOG_H
24 #define WINPR_LOG_H
25 
26 #ifdef __cplusplus
27 extern "C"
28 {
29 #endif
30 
31 #include <stdarg.h>
32 #include <winpr/wtypes.h>
33 #include <winpr/synch.h>
34 #include <winpr/thread.h>
35 
36 /**
37  * Log Levels
38  */
39 #define WLOG_TRACE 0
40 #define WLOG_DEBUG 1
41 #define WLOG_INFO 2
42 #define WLOG_WARN 3
43 #define WLOG_ERROR 4
44 #define WLOG_FATAL 5
45 #define WLOG_OFF 6
46 #define WLOG_LEVEL_INHERIT 0xFFFF
47 
48 /**
49  * Log Message
50  */
51 #define WLOG_MESSAGE_TEXT 0
52 #define WLOG_MESSAGE_DATA 1
53 #define WLOG_MESSAGE_IMAGE 2
54 #define WLOG_MESSAGE_PACKET 3
55 
56 /**
57  * Log Appenders
58  */
59 #define WLOG_APPENDER_CONSOLE 0
60 #define WLOG_APPENDER_FILE 1
61 #define WLOG_APPENDER_BINARY 2
62 #define WLOG_APPENDER_CALLBACK 3
63 #define WLOG_APPENDER_SYSLOG 4
64 #define WLOG_APPENDER_JOURNALD 5
65 #define WLOG_APPENDER_UDP 6
66 
67 	struct _wLogMessage
68 	{
69 		DWORD Type;
70 
71 		DWORD Level;
72 
73 		LPSTR PrefixString;
74 
75 		LPCSTR FormatString;
76 		LPSTR TextString;
77 
78 		DWORD LineNumber;    /* __LINE__ */
79 		LPCSTR FileName;     /* __FILE__ */
80 		LPCSTR FunctionName; /* __FUNCTION__ */
81 
82 		/* Data Message */
83 
84 		void* Data;
85 		int Length;
86 
87 		/* Image Message */
88 
89 		void* ImageData;
90 		int ImageWidth;
91 		int ImageHeight;
92 		int ImageBpp;
93 
94 		/* Packet Message */
95 
96 		void* PacketData;
97 		int PacketLength;
98 		DWORD PacketFlags;
99 	};
100 	typedef struct _wLogMessage wLogMessage;
101 	typedef struct _wLogLayout wLogLayout;
102 	typedef struct _wLogAppender wLogAppender;
103 	typedef struct _wLog wLog;
104 
105 #define WLOG_PACKET_INBOUND 1
106 #define WLOG_PACKET_OUTBOUND 2
107 
108 	WINPR_API BOOL WLog_PrintMessage(wLog* log, DWORD type, DWORD level, DWORD line,
109 	                                 const char* file, const char* function, ...);
110 	WINPR_API BOOL WLog_PrintMessageVA(wLog* log, DWORD type, DWORD level, DWORD line,
111 	                                   const char* file, const char* function, va_list args);
112 
113 	WINPR_API wLog* WLog_GetRoot(void);
114 	WINPR_API wLog* WLog_Get(LPCSTR name);
115 	WINPR_API DWORD WLog_GetLogLevel(wLog* log);
116 	WINPR_API BOOL WLog_IsLevelActive(wLog* _log, DWORD _log_level);
117 
118 #define WLog_Print(_log, _log_level, ...)                                              \
119 	do                                                                                 \
120 	{                                                                                  \
121 		if (WLog_IsLevelActive(_log, _log_level))                                      \
122 		{                                                                              \
123 			WLog_PrintMessage(_log, WLOG_MESSAGE_TEXT, _log_level, __LINE__, __FILE__, \
124 			                  __FUNCTION__, __VA_ARGS__);                              \
125 		}                                                                              \
126 	} while (0)
127 
128 #define WLog_Print_tag(_tag, _log_level, ...)                 \
129 	do                                                        \
130 	{                                                         \
131 		static wLog* _log_cached_ptr = NULL;                  \
132 		if (!_log_cached_ptr)                                 \
133 			_log_cached_ptr = WLog_Get(_tag);                 \
134 		WLog_Print(_log_cached_ptr, _log_level, __VA_ARGS__); \
135 	} while (0)
136 
137 #define WLog_PrintVA(_log, _log_level, _args)                                            \
138 	do                                                                                   \
139 	{                                                                                    \
140 		if (WLog_IsLevelActive(_log, _log_level))                                        \
141 		{                                                                                \
142 			WLog_PrintMessageVA(_log, WLOG_MESSAGE_TEXT, _log_level, __LINE__, __FILE__, \
143 			                    __FUNCTION__, _args);                                    \
144 		}                                                                                \
145 	} while (0)
146 
147 #define WLog_Data(_log, _log_level, ...)                                               \
148 	do                                                                                 \
149 	{                                                                                  \
150 		if (WLog_IsLevelActive(_log, _log_level))                                      \
151 		{                                                                              \
152 			WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, \
153 			                  __FUNCTION__, __VA_ARGS__);                              \
154 		}                                                                              \
155 	} while (0)
156 
157 #define WLog_Image(_log, _log_level, ...)                                              \
158 	do                                                                                 \
159 	{                                                                                  \
160 		if (WLog_IsLevelActive(_log, _log_level))                                      \
161 		{                                                                              \
162 			WLog_PrintMessage(_log, WLOG_MESSAGE_DATA, _log_level, __LINE__, __FILE__, \
163 			                  __FUNCTION__, __VA_ARGS__);                              \
164 		}                                                                              \
165 	} while (0)
166 
167 #define WLog_Packet(_log, _log_level, ...)                                               \
168 	do                                                                                   \
169 	{                                                                                    \
170 		if (WLog_IsLevelActive(_log, _log_level))                                        \
171 		{                                                                                \
172 			WLog_PrintMessage(_log, WLOG_MESSAGE_PACKET, _log_level, __LINE__, __FILE__, \
173 			                  __FUNCTION__, __VA_ARGS__);                                \
174 		}                                                                                \
175 	} while (0)
176 
177 #define WLog_LVL(tag, lvl, ...) WLog_Print_tag(tag, lvl, __VA_ARGS__)
178 #define WLog_VRB(tag, ...) WLog_Print_tag(tag, WLOG_TRACE, __VA_ARGS__)
179 #define WLog_DBG(tag, ...) WLog_Print_tag(tag, WLOG_DEBUG, __VA_ARGS__)
180 #define WLog_INFO(tag, ...) WLog_Print_tag(tag, WLOG_INFO, __VA_ARGS__)
181 #define WLog_WARN(tag, ...) WLog_Print_tag(tag, WLOG_WARN, __VA_ARGS__)
182 #define WLog_ERR(tag, ...) WLog_Print_tag(tag, WLOG_ERROR, __VA_ARGS__)
183 #define WLog_FATAL(tag, ...) WLog_Print_tag(tag, WLOG_FATAL, __VA_ARGS__)
184 
185 	WINPR_API BOOL WLog_SetLogLevel(wLog* log, DWORD logLevel);
186 	WINPR_API BOOL WLog_SetStringLogLevel(wLog* log, LPCSTR level);
187 	WINPR_API BOOL WLog_AddStringLogFilters(LPCSTR filter);
188 
189 	WINPR_API BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType);
190 	WINPR_API wLogAppender* WLog_GetLogAppender(wLog* log);
191 	WINPR_API BOOL WLog_OpenAppender(wLog* log);
192 	WINPR_API BOOL WLog_CloseAppender(wLog* log);
193 	WINPR_API BOOL WLog_ConfigureAppender(wLogAppender* appender, const char* setting, void* value);
194 
195 	WINPR_API wLogLayout* WLog_GetLogLayout(wLog* log);
196 	WINPR_API BOOL WLog_Layout_SetPrefixFormat(wLog* log, wLogLayout* layout, const char* format);
197 
198 #if !defined(DEFINE_NO_DEPRECATED)
199 	/** Deprecated */
200 	WINPR_API WINPR_DEPRECATED(BOOL WLog_Init(void));
201 	/** Deprecated */
202 	WINPR_API WINPR_DEPRECATED(BOOL WLog_Uninit(void));
203 #endif
204 
205 	typedef BOOL (*wLogCallbackMessage_t)(const wLogMessage* msg);
206 	typedef BOOL (*wLogCallbackData_t)(const wLogMessage* msg);
207 	typedef BOOL (*wLogCallbackImage_t)(const wLogMessage* msg);
208 	typedef BOOL (*wLogCallbackPackage_t)(const wLogMessage* msg);
209 
210 	struct _wLogCallbacks
211 	{
212 		wLogCallbackData_t data;
213 		wLogCallbackImage_t image;
214 		wLogCallbackMessage_t message;
215 		wLogCallbackPackage_t package;
216 	};
217 	typedef struct _wLogCallbacks wLogCallbacks;
218 
219 #ifdef __cplusplus
220 }
221 #endif
222 
223 #endif /* WINPR_WLOG_H */
224