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