22 #include <winpr/config.h>
24 #include "BinaryAppender.h"
25 #include <winpr/crt.h>
26 #include <winpr/assert.h>
27 #include <winpr/file.h>
28 #include <winpr/path.h>
29 #include <winpr/stream.h>
33 WLOG_APPENDER_COMMON();
41 static BOOL WLog_BinaryAppender_Open(wLog* log, wLogAppender* appender)
43 wLogBinaryAppender* binaryAppender = NULL;
44 if (!log || !appender)
47 binaryAppender = (wLogBinaryAppender*)appender;
48 if (!binaryAppender->FileName)
50 binaryAppender->FileName = (
char*)malloc(MAX_PATH);
51 if (!binaryAppender->FileName)
53 (void)sprintf_s(binaryAppender->FileName, MAX_PATH,
"%" PRIu32
".wlog",
54 GetCurrentProcessId());
57 if (!binaryAppender->FilePath)
59 binaryAppender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP,
"wlog");
60 if (!binaryAppender->FilePath)
64 if (!binaryAppender->FullFileName)
66 binaryAppender->FullFileName =
67 GetCombinedPath(binaryAppender->FilePath, binaryAppender->FileName);
68 if (!binaryAppender->FullFileName)
72 if (!winpr_PathFileExists(binaryAppender->FilePath))
74 if (!winpr_PathMakePath(binaryAppender->FilePath, 0))
76 UnixChangeFileMode(binaryAppender->FilePath, 0xFFFF);
79 binaryAppender->FileDescriptor = winpr_fopen(binaryAppender->FullFileName,
"a+");
81 if (!binaryAppender->FileDescriptor)
87 static BOOL WLog_BinaryAppender_Close(wLog* log, wLogAppender* appender)
89 wLogBinaryAppender* binaryAppender = NULL;
94 binaryAppender = (wLogBinaryAppender*)appender;
95 if (!binaryAppender->FileDescriptor)
98 if (binaryAppender->FileDescriptor)
99 (void)fclose(binaryAppender->FileDescriptor);
101 binaryAppender->FileDescriptor = NULL;
106 static BOOL WLog_BinaryAppender_WriteMessage(wLog* log, wLogAppender* appender,
111 size_t MessageLength = 0;
112 size_t FileNameLength = 0;
113 size_t FunctionNameLength = 0;
114 size_t TextStringLength = 0;
116 wLogBinaryAppender* binaryAppender = NULL;
118 if (!log || !appender || !message)
121 binaryAppender = (wLogBinaryAppender*)appender;
123 fp = binaryAppender->FileDescriptor;
128 FileNameLength = strnlen(message->FileName, INT_MAX);
129 FunctionNameLength = strnlen(message->FunctionName, INT_MAX);
130 TextStringLength = strnlen(message->TextString, INT_MAX);
133 16 + (4 + FileNameLength + 1) + (4 + FunctionNameLength + 1) + (4 + TextStringLength + 1);
135 if ((MessageLength > UINT32_MAX) || (FileNameLength > UINT32_MAX) ||
136 (FunctionNameLength > UINT32_MAX) || (TextStringLength > UINT32_MAX))
139 s = Stream_New(NULL, MessageLength);
143 Stream_Write_UINT32(s, (UINT32)MessageLength);
145 Stream_Write_UINT32(s, message->Type);
146 Stream_Write_UINT32(s, message->Level);
148 WINPR_ASSERT(message->LineNumber <= UINT32_MAX);
149 Stream_Write_UINT32(s, (UINT32)message->LineNumber);
151 Stream_Write_UINT32(s, (UINT32)FileNameLength);
152 Stream_Write(s, message->FileName, FileNameLength + 1);
154 Stream_Write_UINT32(s, (UINT32)FunctionNameLength);
155 Stream_Write(s, message->FunctionName, FunctionNameLength + 1);
157 Stream_Write_UINT32(s, (UINT32)TextStringLength);
158 Stream_Write(s, message->TextString, TextStringLength + 1);
160 Stream_SealLength(s);
162 if (fwrite(Stream_Buffer(s), MessageLength, 1, fp) != 1)
165 Stream_Free(s, TRUE);
170 static BOOL WLog_BinaryAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
176 static BOOL WLog_BinaryAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
182 static BOOL WLog_BinaryAppender_Set(wLogAppender* appender,
const char* setting,
void* value)
184 wLogBinaryAppender* binaryAppender = (wLogBinaryAppender*)appender;
187 if (!value || (strnlen(value, 2) == 0))
190 if (!strcmp(
"outputfilename", setting))
192 binaryAppender->FileName = _strdup((
const char*)value);
193 if (!binaryAppender->FileName)
196 else if (!strcmp(
"outputfilepath", setting))
198 binaryAppender->FilePath = _strdup((
const char*)value);
199 if (!binaryAppender->FilePath)
208 static void WLog_BinaryAppender_Free(wLogAppender* appender)
210 wLogBinaryAppender* binaryAppender = NULL;
213 binaryAppender = (wLogBinaryAppender*)appender;
214 free(binaryAppender->FileName);
215 free(binaryAppender->FilePath);
216 free(binaryAppender->FullFileName);
217 free(binaryAppender);
221 wLogAppender* WLog_BinaryAppender_New(wLog* log)
223 wLogBinaryAppender* BinaryAppender = NULL;
225 BinaryAppender = (wLogBinaryAppender*)calloc(1,
sizeof(wLogBinaryAppender));
229 BinaryAppender->Type = WLOG_APPENDER_BINARY;
230 BinaryAppender->Open = WLog_BinaryAppender_Open;
231 BinaryAppender->Close = WLog_BinaryAppender_Close;
232 BinaryAppender->WriteMessage = WLog_BinaryAppender_WriteMessage;
233 BinaryAppender->WriteDataMessage = WLog_BinaryAppender_WriteDataMessage;
234 BinaryAppender->WriteImageMessage = WLog_BinaryAppender_WriteImageMessage;
235 BinaryAppender->Free = WLog_BinaryAppender_Free;
236 BinaryAppender->Set = WLog_BinaryAppender_Set;
238 return (wLogAppender*)BinaryAppender;