FreeRDP
ConsoleAppender.c
1 
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 #define WLOG_CONSOLE_DEFAULT 0
30 #define WLOG_CONSOLE_STDOUT 1
31 #define WLOG_CONSOLE_STDERR 2
32 #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 {
43  return TRUE;
44 }
45 
46 static BOOL WLog_ConsoleAppender_Close(wLog* log, wLogAppender* appender)
47 {
48  return TRUE;
49 }
50 
51 static BOOL WLog_ConsoleAppender_WriteMessage(wLog* log, wLogAppender* appender,
52  wLogMessage* message)
53 {
54  FILE* fp = NULL;
55  char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
56  wLogConsoleAppender* consoleAppender = NULL;
57  if (!appender)
58  return FALSE;
59 
60  consoleAppender = (wLogConsoleAppender*)appender;
61 
62  message->PrefixString = prefix;
63  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  switch (consoleAppender->outputStream)
111  {
112  case WLOG_CONSOLE_STDOUT:
113  fp = stdout;
114  break;
115  case WLOG_CONSOLE_STDERR:
116  fp = stderr;
117  break;
118  default:
119  switch (message->Level)
120  {
121  case WLOG_TRACE:
122  case WLOG_DEBUG:
123  case WLOG_INFO:
124  fp = stdout;
125  break;
126  default:
127  fp = stderr;
128  break;
129  }
130  break;
131  }
132 
133  if (message->Level != WLOG_OFF)
134  (void)fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
135 #endif
136  return TRUE;
137 }
138 
139 static int g_DataId = 0;
140 
141 static BOOL WLog_ConsoleAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
142  wLogMessage* message)
143 {
144 #if defined(ANDROID)
145  return FALSE;
146 #else
147  int DataId = 0;
148  char* FullFileName = NULL;
149 
150  DataId = g_DataId++;
151  FullFileName = WLog_Message_GetOutputFileName(DataId, "dat");
152 
153  WLog_DataMessage_Write(FullFileName, message->Data, message->Length);
154 
155  free(FullFileName);
156 
157  return TRUE;
158 #endif
159 }
160 
161 static int g_ImageId = 0;
162 
163 static BOOL WLog_ConsoleAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
164  wLogMessage* message)
165 {
166 #if defined(ANDROID)
167  return FALSE;
168 #else
169  int ImageId = 0;
170  char* FullFileName = NULL;
171 
172  ImageId = g_ImageId++;
173  FullFileName = WLog_Message_GetOutputFileName(ImageId, "bmp");
174 
175  WLog_ImageMessage_Write(FullFileName, message->ImageData, message->ImageWidth,
176  message->ImageHeight, message->ImageBpp);
177 
178  free(FullFileName);
179 
180  return TRUE;
181 #endif
182 }
183 
184 static int g_PacketId = 0;
185 
186 static BOOL WLog_ConsoleAppender_WritePacketMessage(wLog* log, wLogAppender* appender,
187  wLogMessage* message)
188 {
189 #if defined(ANDROID)
190  return FALSE;
191 #else
192  char* FullFileName = NULL;
193 
194  g_PacketId++;
195 
196  if (!appender->PacketMessageContext)
197  {
198  FullFileName = WLog_Message_GetOutputFileName(-1, "pcap");
199  appender->PacketMessageContext = (void*)Pcap_Open(FullFileName, TRUE);
200  free(FullFileName);
201  }
202 
203  if (appender->PacketMessageContext)
204  return WLog_PacketMessage_Write((wPcap*)appender->PacketMessageContext, message->PacketData,
205  message->PacketLength, message->PacketFlags);
206 
207  return TRUE;
208 #endif
209 }
210 static BOOL WLog_ConsoleAppender_Set(wLogAppender* appender, const char* setting, void* value)
211 {
212  wLogConsoleAppender* consoleAppender = (wLogConsoleAppender*)appender;
213 
214  /* Just check the value string is not empty */
215  if (!value || (strnlen(value, 2) == 0))
216  return FALSE;
217 
218  if (strcmp("outputstream", setting) != 0)
219  return FALSE;
220 
221  if (!strcmp("stdout", value))
222  consoleAppender->outputStream = WLOG_CONSOLE_STDOUT;
223  else if (!strcmp("stderr", value))
224  consoleAppender->outputStream = WLOG_CONSOLE_STDERR;
225  else if (!strcmp("default", value))
226  consoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;
227  else if (!strcmp("debug", value))
228  consoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
229  else
230  return FALSE;
231 
232  return TRUE;
233 }
234 
235 static void WLog_ConsoleAppender_Free(wLogAppender* appender)
236 {
237  if (appender)
238  {
239  if (appender->PacketMessageContext)
240  {
241  Pcap_Close((wPcap*)appender->PacketMessageContext);
242  }
243 
244  free(appender);
245  }
246 }
247 
248 wLogAppender* WLog_ConsoleAppender_New(wLog* log)
249 {
250  wLogConsoleAppender* ConsoleAppender = NULL;
251 
252  ConsoleAppender = (wLogConsoleAppender*)calloc(1, sizeof(wLogConsoleAppender));
253 
254  if (!ConsoleAppender)
255  return NULL;
256 
257  ConsoleAppender->Type = WLOG_APPENDER_CONSOLE;
258 
259  ConsoleAppender->Open = WLog_ConsoleAppender_Open;
260  ConsoleAppender->Close = WLog_ConsoleAppender_Close;
261  ConsoleAppender->WriteMessage = WLog_ConsoleAppender_WriteMessage;
262  ConsoleAppender->WriteDataMessage = WLog_ConsoleAppender_WriteDataMessage;
263  ConsoleAppender->WriteImageMessage = WLog_ConsoleAppender_WriteImageMessage;
264  ConsoleAppender->WritePacketMessage = WLog_ConsoleAppender_WritePacketMessage;
265  ConsoleAppender->Set = WLog_ConsoleAppender_Set;
266  ConsoleAppender->Free = WLog_ConsoleAppender_Free;
267 
268  ConsoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;
269 
270 #ifdef _WIN32
271  if (IsDebuggerPresent())
272  ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
273 #endif
274 
275  return (wLogAppender*)ConsoleAppender;
276 }