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
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
#include "FileAppender.h"
|
|
|
|
#include "Message.h"
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
#include <winpr/crt.h>
|
2015-03-12 15:57:01 +03:00
|
|
|
#include <winpr/environment.h>
|
2013-10-02 00:50:47 +04:00
|
|
|
#include <winpr/file.h>
|
|
|
|
#include <winpr/path.h>
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
struct _wLogFileAppender
|
|
|
|
{
|
|
|
|
WLOG_APPENDER_COMMON();
|
2013-10-09 03:39:03 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
char* FileName;
|
|
|
|
char* FilePath;
|
|
|
|
char* FullFileName;
|
|
|
|
FILE* FileDescriptor;
|
|
|
|
};
|
|
|
|
typedef struct _wLogFileAppender wLogFileAppender;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_SetOutputFileName(wLogFileAppender* appender, const char* filename)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
|
|
|
appender->FileName = _strdup(filename);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!appender->FileName)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_SetOutputFilePath(wLogFileAppender* appender, const char* filepath)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
|
|
|
appender->FilePath = _strdup(filepath);
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!appender->FilePath)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_Open(wLog* log, wLogAppender* appender)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
wLogFileAppender *fileAppender;
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-08-14 12:38:02 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender = (wLogFileAppender *)appender;
|
|
|
|
|
|
|
|
if (!fileAppender->FilePath)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
|
|
|
|
if (!fileAppender->FilePath)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!fileAppender->FileName)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender->FileName = (char*) malloc(MAX_PATH);
|
|
|
|
if (!fileAppender->FileName)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-04-03 17:21:01 +03:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
sprintf_s(fileAppender->FileName, MAX_PATH, "%u.log", (unsigned int) GetCurrentProcessId());
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!fileAppender->FullFileName)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender->FullFileName = GetCombinedPath(fileAppender->FilePath, fileAppender->FileName);
|
|
|
|
if (!fileAppender->FullFileName)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-04-03 17:21:01 +03:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!PathFileExistsA(fileAppender->FilePath))
|
2015-04-03 17:21:01 +03:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!PathMakePathA(fileAppender->FilePath, 0))
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2015-11-09 19:56:44 +03:00
|
|
|
UnixChangeFileMode(fileAppender->FilePath, 0xFFFF);
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender->FileDescriptor = fopen(fileAppender->FullFileName, "a+");
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
if (!fileAppender->FileDescriptor)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_Close(wLog* log, wLogAppender* appender)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
2015-11-09 19:56:44 +03:00
|
|
|
wLogFileAppender *fileAppender;
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-08-14 12:38:02 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender = (wLogFileAppender *)appender;
|
|
|
|
|
|
|
|
if (!fileAppender->FileDescriptor)
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fclose(fileAppender->FileDescriptor);
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender->FileDescriptor = NULL;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03: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];
|
2015-11-09 19:56:44 +03:00
|
|
|
wLogFileAppender *fileAppender;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-08-14 12:38:02 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
fileAppender = (wLogFileAppender *)appender;
|
|
|
|
|
|
|
|
fp = fileAppender->FileDescriptor;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
if (!fp)
|
2015-10-21 11:11:06 +03:00
|
|
|
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! */
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
static int g_DataId = 0;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_WriteDataMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
|
2013-10-02 23:41:46 +04:00
|
|
|
{
|
|
|
|
int DataId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-08-14 12:38:02 +04:00
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
DataId = g_DataId++;
|
|
|
|
FullFileName = WLog_Message_GetOutputFileName(DataId, "dat");
|
|
|
|
|
|
|
|
WLog_DataMessage_Write(FullFileName, message->Data, message->Length);
|
|
|
|
|
|
|
|
free(FullFileName);
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 23:41:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static int g_ImageId = 0;
|
|
|
|
|
2016-01-28 14:08:57 +03:00
|
|
|
static BOOL WLog_FileAppender_WriteImageMessage(wLog* log, wLogAppender* appender, wLogMessage* message)
|
2013-10-02 23:41:46 +04:00
|
|
|
{
|
|
|
|
int ImageId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
2015-10-21 11:11:06 +03:00
|
|
|
return FALSE;
|
2014-08-14 12:38:02 +04:00
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
ImageId = g_ImageId++;
|
|
|
|
FullFileName = WLog_Message_GetOutputFileName(ImageId, "bmp");
|
|
|
|
|
|
|
|
WLog_ImageMessage_Write(FullFileName, message->ImageData,
|
|
|
|
message->ImageWidth, message->ImageHeight, message->ImageBpp);
|
|
|
|
|
|
|
|
free(FullFileName);
|
|
|
|
|
2015-10-21 11:11:06 +03:00
|
|
|
return TRUE;
|
2013-10-02 23:41:46 +04:00
|
|
|
}
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
static BOOL WLog_FileAppender_Set(wLogAppender* appender, const char *setting, void *value)
|
|
|
|
{
|
|
|
|
wLogFileAppender *fileAppender = (wLogFileAppender *) appender;
|
|
|
|
|
|
|
|
if (!value || !strlen(value))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!strcmp("outputfilename", setting))
|
|
|
|
return WLog_FileAppender_SetOutputFileName(fileAppender, (const char *)value);
|
|
|
|
else if (!strcmp("outputfilepath", setting))
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2015-03-12 15:57:01 +03:00
|
|
|
LPSTR env;
|
|
|
|
LPCSTR name;
|
|
|
|
DWORD nSize;
|
2013-10-02 00:08:26 +04:00
|
|
|
wLogFileAppender* FileAppender;
|
2015-06-14 01:55:09 +03:00
|
|
|
BOOL status;
|
|
|
|
|
|
|
|
FileAppender = (wLogFileAppender*) calloc(1, sizeof(wLogFileAppender));
|
|
|
|
if (!FileAppender)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
FileAppender->Type = WLOG_APPENDER_FILE;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
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;
|
2015-06-14 01:55:09 +03:00
|
|
|
|
|
|
|
name = "WLOG_FILEAPPENDER_OUTPUT_FILE_PATH";
|
|
|
|
nSize = GetEnvironmentVariableA(name, NULL, 0);
|
|
|
|
if (nSize)
|
|
|
|
{
|
|
|
|
env = (LPSTR) malloc(nSize);
|
|
|
|
if (!env)
|
|
|
|
goto error_free;
|
|
|
|
|
|
|
|
nSize = GetEnvironmentVariableA(name, env, nSize);
|
2015-11-09 19:56:44 +03:00
|
|
|
status = WLog_FileAppender_SetOutputFilePath(FileAppender, env);
|
2015-06-14 01:55:09 +03:00
|
|
|
free(env);
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-06-14 01:55:09 +03:00
|
|
|
if (!status)
|
|
|
|
goto error_free;
|
|
|
|
}
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-06-14 01:55:09 +03:00
|
|
|
name = "WLOG_FILEAPPENDER_OUTPUT_FILE_NAME";
|
|
|
|
nSize = GetEnvironmentVariableA(name, NULL, 0);
|
|
|
|
if (nSize)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
2015-06-14 01:55:09 +03:00
|
|
|
env = (LPSTR) malloc(nSize);
|
|
|
|
if (!env)
|
|
|
|
goto error_output_file_name;
|
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
GetEnvironmentVariableA(name, env, nSize);
|
|
|
|
status = WLog_FileAppender_SetOutputFileName(FileAppender, env);
|
2015-06-14 01:55:09 +03:00
|
|
|
free(env);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
goto error_output_file_name;
|
|
|
|
}
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-11-09 19:56:44 +03:00
|
|
|
return (wLogAppender *)FileAppender;
|
2015-06-14 01:55:09 +03:00
|
|
|
|
|
|
|
error_output_file_name:
|
|
|
|
free(FileAppender->FilePath);
|
|
|
|
error_free:
|
|
|
|
free(FileAppender);
|
|
|
|
return NULL;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|