20 #include <winpr/config.h>
22 #include "ConsoleAppender.h"
26 #include <android/log.h>
29 #define WLOG_CONSOLE_DEFAULT 0
30 #define WLOG_CONSOLE_STDOUT 1
31 #define WLOG_CONSOLE_STDERR 2
32 #define WLOG_CONSOLE_DEBUG 4
36 WLOG_APPENDER_COMMON();
39 } wLogConsoleAppender;
41 static BOOL WLog_ConsoleAppender_Open(wLog* log, wLogAppender* appender)
46 static BOOL WLog_ConsoleAppender_Close(wLog* log, wLogAppender* appender)
51 static BOOL WLog_ConsoleAppender_WriteMessage(wLog* log, wLogAppender* appender,
55 char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
56 wLogConsoleAppender* consoleAppender = NULL;
60 consoleAppender = (wLogConsoleAppender*)appender;
62 message->PrefixString = prefix;
63 WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
66 if (consoleAppender->outputStream == WLOG_CONSOLE_DEBUG)
68 OutputDebugStringA(message->PrefixString);
69 OutputDebugStringA(message->TextString);
70 OutputDebugStringA(
"\n");
77 android_LogPriority level;
78 switch (message->Level)
81 level = ANDROID_LOG_VERBOSE;
84 level = ANDROID_LOG_DEBUG;
87 level = ANDROID_LOG_INFO;
90 level = ANDROID_LOG_WARN;
93 level = ANDROID_LOG_ERROR;
96 level = ANDROID_LOG_FATAL;
99 level = ANDROID_LOG_SILENT;
102 level = ANDROID_LOG_FATAL;
106 if (level != ANDROID_LOG_SILENT)
107 __android_log_print(level, log->Name,
"%s%s", message->PrefixString, message->TextString);
110 switch (consoleAppender->outputStream)
112 case WLOG_CONSOLE_STDOUT:
115 case WLOG_CONSOLE_STDERR:
119 switch (message->Level)
133 if (message->Level != WLOG_OFF)
134 (void)fprintf(fp,
"%s%s\n", message->PrefixString, message->TextString);
139 static int g_DataId = 0;
141 static BOOL WLog_ConsoleAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
148 char* FullFileName = NULL;
151 FullFileName = WLog_Message_GetOutputFileName(DataId,
"dat");
153 WLog_DataMessage_Write(FullFileName, message->Data, message->Length);
161 static int g_ImageId = 0;
163 static BOOL WLog_ConsoleAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
170 char* FullFileName = NULL;
172 ImageId = g_ImageId++;
173 FullFileName = WLog_Message_GetOutputFileName(ImageId,
"bmp");
175 WLog_ImageMessage_Write(FullFileName, message->ImageData, message->ImageWidth,
176 message->ImageHeight, message->ImageBpp);
184 static int g_PacketId = 0;
186 static BOOL WLog_ConsoleAppender_WritePacketMessage(wLog* log, wLogAppender* appender,
192 char* FullFileName = NULL;
196 if (!appender->PacketMessageContext)
198 FullFileName = WLog_Message_GetOutputFileName(-1,
"pcap");
199 appender->PacketMessageContext = (
void*)Pcap_Open(FullFileName, TRUE);
203 if (appender->PacketMessageContext)
204 return WLog_PacketMessage_Write((
wPcap*)appender->PacketMessageContext, message->PacketData,
205 message->PacketLength, message->PacketFlags);
210 static BOOL WLog_ConsoleAppender_Set(wLogAppender* appender,
const char* setting,
void* value)
212 wLogConsoleAppender* consoleAppender = (wLogConsoleAppender*)appender;
215 if (!value || (strnlen(value, 2) == 0))
218 if (strcmp(
"outputstream", setting) != 0)
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;
235 static void WLog_ConsoleAppender_Free(wLogAppender* appender)
239 if (appender->PacketMessageContext)
241 Pcap_Close((
wPcap*)appender->PacketMessageContext);
248 wLogAppender* WLog_ConsoleAppender_New(wLog* log)
250 wLogConsoleAppender* ConsoleAppender = NULL;
252 ConsoleAppender = (wLogConsoleAppender*)calloc(1,
sizeof(wLogConsoleAppender));
254 if (!ConsoleAppender)
257 ConsoleAppender->Type = WLOG_APPENDER_CONSOLE;
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;
268 ConsoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;
271 if (IsDebuggerPresent())
272 ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
275 return (wLogAppender*)ConsoleAppender;