/src/FreeRDP/winpr/libwinpr/utils/wlog/ConsoleAppender.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /**  | 
2  |  |  * WinPR: Windows Portable Runtime  | 
3  |  |  * WinPR Logger  | 
4  |  |  *  | 
5  |  |  * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>  | 
6  |  |  *  | 
7  |  |  * Licensed under the Apache License, Version 2.0 (the "License");  | 
8  |  |  * you may not use this file except in compliance with the License.  | 
9  |  |  * You may obtain a copy of the License at  | 
10  |  |  *  | 
11  |  |  *     http://www.apache.org/licenses/LICENSE-2.0  | 
12  |  |  *  | 
13  |  |  * Unless required by applicable law or agreed to in writing, software  | 
14  |  |  * distributed under the License is distributed on an "AS IS" BASIS,  | 
15  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
16  |  |  * See the License for the specific language governing permissions and  | 
17  |  |  * limitations under the License.  | 
18  |  |  */  | 
19  |  |  | 
20  |  | #include <winpr/config.h>  | 
21  |  |  | 
22  |  | #include "ConsoleAppender.h"  | 
23  |  | #include "Message.h"  | 
24  |  |  | 
25  |  | #ifdef ANDROID  | 
26  |  | #include <android/log.h>  | 
27  |  | #endif  | 
28  |  |  | 
29  | 0  | #define WLOG_CONSOLE_DEFAULT 0  | 
30  | 0  | #define WLOG_CONSOLE_STDOUT 1  | 
31  | 0  | #define WLOG_CONSOLE_STDERR 2  | 
32  | 0  | #define WLOG_CONSOLE_DEBUG 4  | 
33  |  |  | 
34  |  | typedef struct  | 
35  |  | { | 
36  |  |   WLOG_APPENDER_COMMON();  | 
37  |  |  | 
38  |  |   int outputStream;  | 
39  |  | } wLogConsoleAppender;  | 
40  |  |  | 
41  |  | static BOOL WLog_ConsoleAppender_Open(wLog* log, wLogAppender* appender)  | 
42  | 0  | { | 
43  | 0  |   return TRUE;  | 
44  | 0  | }  | 
45  |  |  | 
46  |  | static BOOL WLog_ConsoleAppender_Close(wLog* log, wLogAppender* appender)  | 
47  | 0  | { | 
48  | 0  |   return TRUE;  | 
49  | 0  | }  | 
50  |  |  | 
51  |  | static BOOL WLog_ConsoleAppender_WriteMessage(wLog* log, wLogAppender* appender,  | 
52  |  |                                               wLogMessage* message)  | 
53  | 0  | { | 
54  | 0  |   FILE* fp = NULL;  | 
55  | 0  |   char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 }; | 
56  | 0  |   wLogConsoleAppender* consoleAppender = NULL;  | 
57  | 0  |   if (!appender)  | 
58  | 0  |     return FALSE;  | 
59  |  |  | 
60  | 0  |   consoleAppender = (wLogConsoleAppender*)appender;  | 
61  |  | 
  | 
62  | 0  |   message->PrefixString = prefix;  | 
63  | 0  |   WLog_Layout_GetMessagePrefix(log, appender->Layout, message);  | 
64  |  | 
  | 
65  |  | #ifdef _WIN32  | 
66  |  |   if (consoleAppender->outputStream == WLOG_CONSOLE_DEBUG)  | 
67  |  |   { | 
68  |  |     OutputDebugStringA(message->PrefixString);  | 
69  |  |     OutputDebugStringA(message->TextString);  | 
70  |  |     OutputDebugStringA("\n"); | 
71  |  |  | 
72  |  |     return TRUE;  | 
73  |  |   }  | 
74  |  | #endif  | 
75  |  | #ifdef ANDROID  | 
76  |  |   (void)fp;  | 
77  |  |   android_LogPriority level;  | 
78  |  |   switch (message->Level)  | 
79  |  |   { | 
80  |  |     case WLOG_TRACE:  | 
81  |  |       level = ANDROID_LOG_VERBOSE;  | 
82  |  |       break;  | 
83  |  |     case WLOG_DEBUG:  | 
84  |  |       level = ANDROID_LOG_DEBUG;  | 
85  |  |       break;  | 
86  |  |     case WLOG_INFO:  | 
87  |  |       level = ANDROID_LOG_INFO;  | 
88  |  |       break;  | 
89  |  |     case WLOG_WARN:  | 
90  |  |       level = ANDROID_LOG_WARN;  | 
91  |  |       break;  | 
92  |  |     case WLOG_ERROR:  | 
93  |  |       level = ANDROID_LOG_ERROR;  | 
94  |  |       break;  | 
95  |  |     case WLOG_FATAL:  | 
96  |  |       level = ANDROID_LOG_FATAL;  | 
97  |  |       break;  | 
98  |  |     case WLOG_OFF:  | 
99  |  |       level = ANDROID_LOG_SILENT;  | 
100  |  |       break;  | 
101  |  |     default:  | 
102  |  |       level = ANDROID_LOG_FATAL;  | 
103  |  |       break;  | 
104  |  |   }  | 
105  |  |  | 
106  |  |   if (level != ANDROID_LOG_SILENT)  | 
107  |  |     __android_log_print(level, log->Name, "%s%s", message->PrefixString, message->TextString);  | 
108  |  |  | 
109  |  | #else  | 
110  | 0  |   switch (consoleAppender->outputStream)  | 
111  | 0  |   { | 
112  | 0  |     case WLOG_CONSOLE_STDOUT:  | 
113  | 0  |       fp = stdout;  | 
114  | 0  |       break;  | 
115  | 0  |     case WLOG_CONSOLE_STDERR:  | 
116  | 0  |       fp = stderr;  | 
117  | 0  |       break;  | 
118  | 0  |     default:  | 
119  | 0  |       switch (message->Level)  | 
120  | 0  |       { | 
121  | 0  |         case WLOG_TRACE:  | 
122  | 0  |         case WLOG_DEBUG:  | 
123  | 0  |         case WLOG_INFO:  | 
124  | 0  |           fp = stdout;  | 
125  | 0  |           break;  | 
126  | 0  |         default:  | 
127  | 0  |           fp = stderr;  | 
128  | 0  |           break;  | 
129  | 0  |       }  | 
130  | 0  |       break;  | 
131  | 0  |   }  | 
132  |  |  | 
133  | 0  |   if (message->Level != WLOG_OFF)  | 
134  | 0  |     (void)fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);  | 
135  | 0  | #endif  | 
136  | 0  |   return TRUE;  | 
137  | 0  | }  | 
138  |  |  | 
139  |  | static int g_DataId = 0;  | 
140  |  |  | 
141  |  | static BOOL WLog_ConsoleAppender_WriteDataMessage(wLog* log, wLogAppender* appender,  | 
142  |  |                                                   wLogMessage* message)  | 
143  | 0  | { | 
144  |  | #if defined(ANDROID)  | 
145  |  |   return FALSE;  | 
146  |  | #else  | 
147  | 0  |   int DataId = 0;  | 
148  | 0  |   char* FullFileName = NULL;  | 
149  |  | 
  | 
150  | 0  |   DataId = g_DataId++;  | 
151  | 0  |   FullFileName = WLog_Message_GetOutputFileName(DataId, "dat");  | 
152  |  | 
  | 
153  | 0  |   WLog_DataMessage_Write(FullFileName, message->Data, message->Length);  | 
154  |  | 
  | 
155  | 0  |   free(FullFileName);  | 
156  |  | 
  | 
157  | 0  |   return TRUE;  | 
158  | 0  | #endif  | 
159  | 0  | }  | 
160  |  |  | 
161  |  | static int g_ImageId = 0;  | 
162  |  |  | 
163  |  | static BOOL WLog_ConsoleAppender_WriteImageMessage(wLog* log, wLogAppender* appender,  | 
164  |  |                                                    wLogMessage* message)  | 
165  | 0  | { | 
166  |  | #if defined(ANDROID)  | 
167  |  |   return FALSE;  | 
168  |  | #else  | 
169  | 0  |   int ImageId = 0;  | 
170  | 0  |   char* FullFileName = NULL;  | 
171  |  | 
  | 
172  | 0  |   ImageId = g_ImageId++;  | 
173  | 0  |   FullFileName = WLog_Message_GetOutputFileName(ImageId, "bmp");  | 
174  |  | 
  | 
175  | 0  |   WLog_ImageMessage_Write(FullFileName, message->ImageData, message->ImageWidth,  | 
176  | 0  |                           message->ImageHeight, message->ImageBpp);  | 
177  |  | 
  | 
178  | 0  |   free(FullFileName);  | 
179  |  | 
  | 
180  | 0  |   return TRUE;  | 
181  | 0  | #endif  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | static int g_PacketId = 0;  | 
185  |  |  | 
186  |  | static BOOL WLog_ConsoleAppender_WritePacketMessage(wLog* log, wLogAppender* appender,  | 
187  |  |                                                     wLogMessage* message)  | 
188  | 0  | { | 
189  |  | #if defined(ANDROID)  | 
190  |  |   return FALSE;  | 
191  |  | #else  | 
192  | 0  |   char* FullFileName = NULL;  | 
193  |  | 
  | 
194  | 0  |   g_PacketId++;  | 
195  |  | 
  | 
196  | 0  |   if (!appender->PacketMessageContext)  | 
197  | 0  |   { | 
198  | 0  |     FullFileName = WLog_Message_GetOutputFileName(-1, "pcap");  | 
199  | 0  |     appender->PacketMessageContext = (void*)Pcap_Open(FullFileName, TRUE);  | 
200  | 0  |     free(FullFileName);  | 
201  | 0  |   }  | 
202  |  | 
  | 
203  | 0  |   if (appender->PacketMessageContext)  | 
204  | 0  |     return WLog_PacketMessage_Write((wPcap*)appender->PacketMessageContext, message->PacketData,  | 
205  | 0  |                                     message->PacketLength, message->PacketFlags);  | 
206  |  |  | 
207  | 0  |   return TRUE;  | 
208  | 0  | #endif  | 
209  | 0  | }  | 
210  |  | static BOOL WLog_ConsoleAppender_Set(wLogAppender* appender, const char* setting, void* value)  | 
211  | 0  | { | 
212  | 0  |   wLogConsoleAppender* consoleAppender = (wLogConsoleAppender*)appender;  | 
213  |  |  | 
214  |  |   /* Just check the value string is not empty */  | 
215  | 0  |   if (!value || (strnlen(value, 2) == 0))  | 
216  | 0  |     return FALSE;  | 
217  |  |  | 
218  | 0  |   if (strcmp("outputstream", setting) != 0) | 
219  | 0  |     return FALSE;  | 
220  |  |  | 
221  | 0  |   if (!strcmp("stdout", value)) | 
222  | 0  |     consoleAppender->outputStream = WLOG_CONSOLE_STDOUT;  | 
223  | 0  |   else if (!strcmp("stderr", value)) | 
224  | 0  |     consoleAppender->outputStream = WLOG_CONSOLE_STDERR;  | 
225  | 0  |   else if (!strcmp("default", value)) | 
226  | 0  |     consoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;  | 
227  | 0  |   else if (!strcmp("debug", value)) | 
228  | 0  |     consoleAppender->outputStream = WLOG_CONSOLE_DEBUG;  | 
229  | 0  |   else  | 
230  | 0  |     return FALSE;  | 
231  |  |  | 
232  | 0  |   return TRUE;  | 
233  | 0  | }  | 
234  |  |  | 
235  |  | static void WLog_ConsoleAppender_Free(wLogAppender* appender)  | 
236  | 0  | { | 
237  | 0  |   if (appender)  | 
238  | 0  |   { | 
239  | 0  |     if (appender->PacketMessageContext)  | 
240  | 0  |     { | 
241  | 0  |       Pcap_Close((wPcap*)appender->PacketMessageContext);  | 
242  | 0  |     }  | 
243  |  | 
  | 
244  | 0  |     free(appender);  | 
245  | 0  |   }  | 
246  | 0  | }  | 
247  |  |  | 
248  |  | wLogAppender* WLog_ConsoleAppender_New(wLog* log)  | 
249  | 0  | { | 
250  | 0  |   wLogConsoleAppender* ConsoleAppender = NULL;  | 
251  |  | 
  | 
252  | 0  |   ConsoleAppender = (wLogConsoleAppender*)calloc(1, sizeof(wLogConsoleAppender));  | 
253  |  | 
  | 
254  | 0  |   if (!ConsoleAppender)  | 
255  | 0  |     return NULL;  | 
256  |  |  | 
257  | 0  |   ConsoleAppender->Type = WLOG_APPENDER_CONSOLE;  | 
258  |  | 
  | 
259  | 0  |   ConsoleAppender->Open = WLog_ConsoleAppender_Open;  | 
260  | 0  |   ConsoleAppender->Close = WLog_ConsoleAppender_Close;  | 
261  | 0  |   ConsoleAppender->WriteMessage = WLog_ConsoleAppender_WriteMessage;  | 
262  | 0  |   ConsoleAppender->WriteDataMessage = WLog_ConsoleAppender_WriteDataMessage;  | 
263  | 0  |   ConsoleAppender->WriteImageMessage = WLog_ConsoleAppender_WriteImageMessage;  | 
264  | 0  |   ConsoleAppender->WritePacketMessage = WLog_ConsoleAppender_WritePacketMessage;  | 
265  | 0  |   ConsoleAppender->Set = WLog_ConsoleAppender_Set;  | 
266  | 0  |   ConsoleAppender->Free = WLog_ConsoleAppender_Free;  | 
267  |  | 
  | 
268  | 0  |   ConsoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;  | 
269  |  | 
  | 
270  |  | #ifdef _WIN32  | 
271  |  |   if (IsDebuggerPresent())  | 
272  |  |     ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;  | 
273  |  | #endif  | 
274  |  | 
  | 
275  | 0  |   return (wLogAppender*)ConsoleAppender;  | 
276  | 0  | }  |