20 #include <winpr/config.h>
22 #include "FileAppender.h"
25 #include <winpr/crt.h>
26 #include <winpr/environment.h>
27 #include <winpr/file.h>
28 #include <winpr/path.h>
32 WLOG_APPENDER_COMMON();
40 static BOOL WLog_FileAppender_SetOutputFileName(wLogFileAppender* appender,
const char* filename)
42 appender->FileName = _strdup(filename);
44 if (!appender->FileName)
50 static BOOL WLog_FileAppender_SetOutputFilePath(wLogFileAppender* appender,
const char* filepath)
52 appender->FilePath = _strdup(filepath);
54 if (!appender->FilePath)
60 static BOOL WLog_FileAppender_Open(wLog* log, wLogAppender* appender)
62 wLogFileAppender* fileAppender = NULL;
64 if (!log || !appender)
67 fileAppender = (wLogFileAppender*)appender;
69 if (!fileAppender->FilePath)
71 fileAppender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP,
"wlog");
73 if (!fileAppender->FilePath)
77 if (!fileAppender->FileName)
79 fileAppender->FileName = (
char*)malloc(MAX_PATH);
81 if (!fileAppender->FileName)
84 (void)sprintf_s(fileAppender->FileName, MAX_PATH,
"%" PRIu32
".log", GetCurrentProcessId());
87 if (!fileAppender->FullFileName)
89 fileAppender->FullFileName =
90 GetCombinedPath(fileAppender->FilePath, fileAppender->FileName);
92 if (!fileAppender->FullFileName)
96 if (!winpr_PathFileExists(fileAppender->FilePath))
98 if (!winpr_PathMakePath(fileAppender->FilePath, 0))
101 UnixChangeFileMode(fileAppender->FilePath, 0xFFFF);
104 fileAppender->FileDescriptor = winpr_fopen(fileAppender->FullFileName,
"a+");
106 if (!fileAppender->FileDescriptor)
112 static BOOL WLog_FileAppender_Close(wLog* log, wLogAppender* appender)
114 wLogFileAppender* fileAppender = NULL;
116 if (!log || !appender)
119 fileAppender = (wLogFileAppender*)appender;
121 if (!fileAppender->FileDescriptor)
124 (void)fclose(fileAppender->FileDescriptor);
125 fileAppender->FileDescriptor = NULL;
129 static BOOL WLog_FileAppender_WriteMessage(wLog* log, wLogAppender* appender,
wLogMessage* message)
132 char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
133 wLogFileAppender* fileAppender = NULL;
135 if (!log || !appender || !message)
138 fileAppender = (wLogFileAppender*)appender;
139 fp = fileAppender->FileDescriptor;
144 message->PrefixString = prefix;
145 WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
146 (void)fprintf(fp,
"%s%s\n", message->PrefixString, message->TextString);
151 static int g_DataId = 0;
153 static BOOL WLog_FileAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
157 char* FullFileName = NULL;
159 if (!log || !appender || !message)
163 FullFileName = WLog_Message_GetOutputFileName(DataId,
"dat");
164 WLog_DataMessage_Write(FullFileName, message->Data, message->Length);
169 static int g_ImageId = 0;
171 static BOOL WLog_FileAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
175 char* FullFileName = NULL;
177 if (!log || !appender || !message)
180 ImageId = g_ImageId++;
181 FullFileName = WLog_Message_GetOutputFileName(ImageId,
"bmp");
182 WLog_ImageMessage_Write(FullFileName, message->ImageData, message->ImageWidth,
183 message->ImageHeight, message->ImageBpp);
188 static BOOL WLog_FileAppender_Set(wLogAppender* appender,
const char* setting,
void* value)
190 wLogFileAppender* fileAppender = (wLogFileAppender*)appender;
193 if (!value || (strnlen(value, 2) == 0))
196 if (!strcmp(
"outputfilename", setting))
197 return WLog_FileAppender_SetOutputFileName(fileAppender, (
const char*)value);
199 if (!strcmp(
"outputfilepath", setting))
200 return WLog_FileAppender_SetOutputFilePath(fileAppender, (
const char*)value);
205 static void WLog_FileAppender_Free(wLogAppender* appender)
207 wLogFileAppender* fileAppender = NULL;
211 fileAppender = (wLogFileAppender*)appender;
212 free(fileAppender->FileName);
213 free(fileAppender->FilePath);
214 free(fileAppender->FullFileName);
219 wLogAppender* WLog_FileAppender_New(wLog* log)
224 wLogFileAppender* FileAppender = NULL;
225 FileAppender = (wLogFileAppender*)calloc(1,
sizeof(wLogFileAppender));
230 FileAppender->Type = WLOG_APPENDER_FILE;
231 FileAppender->Open = WLog_FileAppender_Open;
232 FileAppender->Close = WLog_FileAppender_Close;
233 FileAppender->WriteMessage = WLog_FileAppender_WriteMessage;
234 FileAppender->WriteDataMessage = WLog_FileAppender_WriteDataMessage;
235 FileAppender->WriteImageMessage = WLog_FileAppender_WriteImageMessage;
236 FileAppender->Free = WLog_FileAppender_Free;
237 FileAppender->Set = WLog_FileAppender_Set;
238 name =
"WLOG_FILEAPPENDER_OUTPUT_FILE_PATH";
239 nSize = GetEnvironmentVariableA(name, NULL, 0);
244 env = (LPSTR)malloc(nSize);
249 if (GetEnvironmentVariableA(name, env, nSize) != nSize - 1)
255 status = WLog_FileAppender_SetOutputFilePath(FileAppender, env);
262 name =
"WLOG_FILEAPPENDER_OUTPUT_FILE_NAME";
263 nSize = GetEnvironmentVariableA(name, NULL, 0);
268 env = (LPSTR)malloc(nSize);
271 goto error_output_file_name;
273 if (GetEnvironmentVariableA(name, env, nSize) == nSize - 1)
274 status = WLog_FileAppender_SetOutputFileName(FileAppender, env);
278 goto error_output_file_name;
281 return (wLogAppender*)FileAppender;
282 error_output_file_name:
283 free(FileAppender->FilePath);