FreeRDP/winpr/libwinpr/utils/wlog/FileAppender.c

292 lines
6.8 KiB
C
Raw Normal View History

2013-10-02 00:08:26 +04:00
/**
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "FileAppender.h"
#include "Message.h"
2013-10-02 00:08:26 +04:00
#include <winpr/crt.h>
#include <winpr/environment.h>
#include <winpr/file.h>
#include <winpr/path.h>
2013-10-02 00:08:26 +04:00
struct _wLogFileAppender
{
2017-02-20 15:38:20 +03:00
WLOG_APPENDER_COMMON();
2017-02-20 15:38:20 +03:00
char* FileName;
char* FilePath;
char* FullFileName;
FILE* FileDescriptor;
};
typedef struct _wLogFileAppender wLogFileAppender;
2013-10-02 00:08:26 +04:00
static BOOL WLog_FileAppender_SetOutputFileName(wLogFileAppender* appender, const char* filename)
2013-10-02 00:08:26 +04:00
{
appender->FileName = _strdup(filename);
2017-02-20 15:38:20 +03:00
if (!appender->FileName)
return FALSE;
return TRUE;
2013-10-02 00:08:26 +04:00
}
static BOOL WLog_FileAppender_SetOutputFilePath(wLogFileAppender* appender, const char* filepath)
{
appender->FilePath = _strdup(filepath);
2017-02-20 15:38:20 +03:00
if (!appender->FilePath)
return FALSE;
return TRUE;
}
static BOOL WLog_FileAppender_Open(wLog* log, wLogAppender* appender)
2013-10-02 00:08:26 +04:00
{
2017-02-20 15:38:20 +03:00
wLogFileAppender* fileAppender;
if (!log || !appender)
return FALSE;
2017-02-20 15:38:20 +03:00
fileAppender = (wLogFileAppender*)appender;
if (!fileAppender->FilePath)
{
fileAppender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
2017-02-20 15:38:20 +03:00
if (!fileAppender->FilePath)
return FALSE;
}
if (!fileAppender->FileName)
{
2019-11-06 17:24:51 +03:00
fileAppender->FileName = (char*)malloc(MAX_PATH);
2017-02-20 15:38:20 +03:00
if (!fileAppender->FileName)
return FALSE;
2019-11-06 17:24:51 +03:00
sprintf_s(fileAppender->FileName, MAX_PATH, "%" PRIu32 ".log", GetCurrentProcessId());
}
if (!fileAppender->FullFileName)
{
2019-11-06 17:24:51 +03:00
fileAppender->FullFileName =
GetCombinedPath(fileAppender->FilePath, fileAppender->FileName);
2017-02-20 15:38:20 +03:00
if (!fileAppender->FullFileName)
return FALSE;
}
if (!PathFileExistsA(fileAppender->FilePath))
{
if (!PathMakePathA(fileAppender->FilePath, 0))
return FALSE;
2017-02-20 15:38:20 +03:00
UnixChangeFileMode(fileAppender->FilePath, 0xFFFF);
}
2013-10-02 00:08:26 +04:00
fileAppender->FileDescriptor = fopen(fileAppender->FullFileName, "a+");
2013-10-02 00:08:26 +04:00
if (!fileAppender->FileDescriptor)
return FALSE;
2013-10-02 00:08:26 +04:00
return TRUE;
2013-10-02 00:08:26 +04:00
}
static BOOL WLog_FileAppender_Close(wLog* log, wLogAppender* appender)
2013-10-02 00:08:26 +04:00
{
2017-02-20 15:38:20 +03:00
wLogFileAppender* fileAppender;
if (!log || !appender)
return FALSE;
2017-02-20 15:38:20 +03:00
fileAppender = (wLogFileAppender*)appender;
if (!fileAppender->FileDescriptor)
return TRUE;
2013-10-02 00:08:26 +04:00
fclose(fileAppender->FileDescriptor);
fileAppender->FileDescriptor = NULL;
return TRUE;
2013-10-02 00:08:26 +04:00
}
static BOOL WLog_FileAppender_WriteMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
2013-10-02 00:08:26 +04:00
{
FILE* fp;
char prefix[WLOG_MAX_PREFIX_SIZE];
2017-02-20 15:38:20 +03:00
wLogFileAppender* fileAppender;
2013-10-02 00:08:26 +04:00
if (!log || !appender || !message)
return FALSE;
2017-02-20 15:38:20 +03:00
fileAppender = (wLogFileAppender*)appender;
fp = fileAppender->FileDescriptor;
2013-10-02 00:08:26 +04:00
if (!fp)
return FALSE;
2013-10-02 00:08:26 +04:00
message->PrefixString = prefix;
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
2014-02-26 22:35:49 +04:00
fflush(fp); /* slow! */
return TRUE;
2013-10-02 00:08:26 +04:00
}
static int g_DataId = 0;
2017-02-20 15:38:20 +03:00
static BOOL WLog_FileAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
2019-11-06 17:24:51 +03:00
wLogMessage* message)
{
int DataId;
char* FullFileName;
if (!log || !appender || !message)
return FALSE;
DataId = g_DataId++;
FullFileName = WLog_Message_GetOutputFileName(DataId, "dat");
WLog_DataMessage_Write(FullFileName, message->Data, message->Length);
free(FullFileName);
return TRUE;
}
static int g_ImageId = 0;
2017-02-20 15:38:20 +03:00
static BOOL WLog_FileAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
2019-11-06 17:24:51 +03:00
wLogMessage* message)
{
int ImageId;
char* FullFileName;
if (!log || !appender || !message)
return FALSE;
ImageId = g_ImageId++;
FullFileName = WLog_Message_GetOutputFileName(ImageId, "bmp");
2019-11-06 17:24:51 +03:00
WLog_ImageMessage_Write(FullFileName, message->ImageData, message->ImageWidth,
message->ImageHeight, message->ImageBpp);
free(FullFileName);
return TRUE;
}
2017-02-20 15:38:20 +03:00
static BOOL WLog_FileAppender_Set(wLogAppender* appender, const char* setting, void* value)
{
2019-11-06 17:24:51 +03:00
wLogFileAppender* fileAppender = (wLogFileAppender*)appender;
/* Just check the value string is not empty */
if (!value || (strnlen(value, 2) == 0))
return FALSE;
if (!strcmp("outputfilename", setting))
2017-02-20 15:38:20 +03:00
return WLog_FileAppender_SetOutputFileName(fileAppender, (const char*)value);
else if (!strcmp("outputfilepath", setting))
2017-02-20 15:38:20 +03:00
return WLog_FileAppender_SetOutputFilePath(fileAppender, (const char*)value);
else
return FALSE;
return TRUE;
}
static void WLog_FileAppender_Free(wLogAppender* appender)
{
wLogFileAppender* fileAppender = NULL;
if (appender)
{
2017-02-20 15:38:20 +03:00
fileAppender = (wLogFileAppender*)appender;
free(fileAppender->FileName);
free(fileAppender->FilePath);
free(fileAppender->FullFileName);
free(fileAppender);
}
}
wLogAppender* WLog_FileAppender_New(wLog* log)
2013-10-02 00:08:26 +04:00
{
LPSTR env;
LPCSTR name;
DWORD nSize;
2013-10-02 00:08:26 +04:00
wLogFileAppender* FileAppender;
2019-11-06 17:24:51 +03:00
FileAppender = (wLogFileAppender*)calloc(1, sizeof(wLogFileAppender));
2017-02-20 15:38:20 +03:00
if (!FileAppender)
return NULL;
FileAppender->Type = WLOG_APPENDER_FILE;
FileAppender->Open = WLog_FileAppender_Open;
FileAppender->Close = WLog_FileAppender_Close;
FileAppender->WriteMessage = WLog_FileAppender_WriteMessage;
FileAppender->WriteDataMessage = WLog_FileAppender_WriteDataMessage;
FileAppender->WriteImageMessage = WLog_FileAppender_WriteImageMessage;
FileAppender->Free = WLog_FileAppender_Free;
FileAppender->Set = WLog_FileAppender_Set;
name = "WLOG_FILEAPPENDER_OUTPUT_FILE_PATH";
nSize = GetEnvironmentVariableA(name, NULL, 0);
2017-02-20 15:38:20 +03:00
if (nSize)
{
2017-03-03 14:37:27 +03:00
BOOL status;
2019-11-06 17:24:51 +03:00
env = (LPSTR)malloc(nSize);
2017-02-20 15:38:20 +03:00
if (!env)
goto error_free;
2017-03-03 14:37:27 +03:00
if (GetEnvironmentVariableA(name, env, nSize) != nSize - 1)
2017-02-20 15:38:20 +03:00
{
free(env);
goto error_free;
}
status = WLog_FileAppender_SetOutputFilePath(FileAppender, env);
free(env);
2013-10-02 00:08:26 +04:00
if (!status)
goto error_free;
}
2013-10-02 00:08:26 +04:00
name = "WLOG_FILEAPPENDER_OUTPUT_FILE_NAME";
nSize = GetEnvironmentVariableA(name, NULL, 0);
2017-02-20 15:38:20 +03:00
if (nSize)
2013-10-02 00:08:26 +04:00
{
2017-03-03 14:37:27 +03:00
BOOL status = FALSE;
2019-11-06 17:24:51 +03:00
env = (LPSTR)malloc(nSize);
2017-02-20 15:38:20 +03:00
if (!env)
goto error_output_file_name;
2017-03-03 14:37:27 +03:00
if (GetEnvironmentVariableA(name, env, nSize) == nSize - 1)
status = WLog_FileAppender_SetOutputFileName(FileAppender, env);
free(env);
if (!status)
goto error_output_file_name;
}
2013-10-02 00:08:26 +04:00
2017-02-20 15:38:20 +03:00
return (wLogAppender*)FileAppender;
error_output_file_name:
free(FileAppender->FilePath);
error_free:
free(FileAppender);
return NULL;
2013-10-02 00:08:26 +04:00
}