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 <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>
|
|
|
|
#include <winpr/thread.h>
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
#include <winpr/wlog.h>
|
|
|
|
|
2013-10-09 03:39:03 +04:00
|
|
|
#include "wlog/Message.h"
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
#include "wlog/FileAppender.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* File Appender
|
|
|
|
*/
|
|
|
|
|
2015-06-17 23:08:02 +03:00
|
|
|
BOOL WLog_FileAppender_SetOutputFileName(wLog* log, wLogFileAppender* appender, const char* filename)
|
2013-10-02 00:08:26 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!appender || !filename)
|
|
|
|
return FALSE;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
if (appender->Type != WLOG_APPENDER_FILE)
|
2015-06-17 23:08:02 +03:00
|
|
|
return FALSE;
|
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-06-17 23:08:02 +03:00
|
|
|
BOOL WLog_FileAppender_SetOutputFilePath(wLog* log, wLogFileAppender* appender, const char* filepath)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
2015-06-17 23:08:02 +03:00
|
|
|
if (!appender || !filepath)
|
|
|
|
return FALSE;
|
2013-10-02 00:50:47 +04:00
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
if (appender->Type != WLOG_APPENDER_FILE)
|
2015-06-17 23:08:02 +03:00
|
|
|
return FALSE;
|
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
|
|
|
}
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
int WLog_FileAppender_Open(wLog* log, wLogFileAppender* appender)
|
|
|
|
{
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender)
|
|
|
|
return -1;
|
|
|
|
|
2013-10-02 00:50:47 +04:00
|
|
|
if (!appender->FilePath)
|
|
|
|
{
|
|
|
|
appender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!appender->FilePath)
|
|
|
|
return -1;
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
if (!appender->FileName)
|
2013-10-02 00:50:47 +04:00
|
|
|
{
|
2015-04-03 17:21:01 +03:00
|
|
|
appender->FileName = (char*) malloc(MAX_PATH);
|
|
|
|
if (!appender->FileName)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sprintf_s(appender->FileName, MAX_PATH, "%u.log", (unsigned int) GetCurrentProcessId());
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!appender->FullFileName)
|
|
|
|
{
|
|
|
|
appender->FullFileName = GetCombinedPath(appender->FilePath, appender->FileName);
|
2015-04-03 17:21:01 +03:00
|
|
|
if (!appender->FullFileName)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PathFileExistsA(appender->FilePath))
|
|
|
|
{
|
2015-06-03 12:47:40 +03:00
|
|
|
if (!PathMakePathA(appender->FilePath, 0))
|
2015-04-03 17:21:01 +03:00
|
|
|
return -1;
|
|
|
|
UnixChangeFileMode(appender->FilePath, 0xFFFF);
|
2013-10-02 00:50:47 +04:00
|
|
|
}
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2013-10-02 00:50:47 +04:00
|
|
|
appender->FileDescriptor = fopen(appender->FullFileName, "a+");
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
if (!appender->FileDescriptor)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WLog_FileAppender_Close(wLog* log, wLogFileAppender* appender)
|
|
|
|
{
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender)
|
|
|
|
return -1;
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
if (!appender->FileDescriptor)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fclose(appender->FileDescriptor);
|
|
|
|
|
|
|
|
appender->FileDescriptor = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WLog_FileAppender_WriteMessage(wLog* log, wLogFileAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
char prefix[WLOG_MAX_PREFIX_SIZE];
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
|
|
|
return -1;
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
fp = appender->FileDescriptor;
|
|
|
|
|
|
|
|
if (!fp)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
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! */
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
static int g_DataId = 0;
|
|
|
|
|
|
|
|
int WLog_FileAppender_WriteDataMessage(wLog* log, wLogFileAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
int DataId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
|
|
|
return -1;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return DataId;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int g_ImageId = 0;
|
|
|
|
|
|
|
|
int WLog_FileAppender_WriteImageMessage(wLog* log, wLogFileAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
int ImageId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (!log || !appender || !message)
|
|
|
|
return -1;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return ImageId;
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
wLogFileAppender* WLog_FileAppender_New(wLog* log)
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
FileAppender->Open = (WLOG_APPENDER_OPEN_FN) WLog_FileAppender_Open;
|
|
|
|
FileAppender->Close = (WLOG_APPENDER_OPEN_FN) WLog_FileAppender_Close;
|
|
|
|
FileAppender->WriteMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_MESSAGE_FN) WLog_FileAppender_WriteMessage;
|
|
|
|
FileAppender->WriteDataMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_DATA_MESSAGE_FN) WLog_FileAppender_WriteDataMessage;
|
|
|
|
FileAppender->WriteImageMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN) WLog_FileAppender_WriteImageMessage;
|
|
|
|
|
|
|
|
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);
|
|
|
|
status = WLog_FileAppender_SetOutputFilePath(log, FileAppender, env);
|
|
|
|
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;
|
|
|
|
|
|
|
|
nSize = GetEnvironmentVariableA(name, env, nSize);
|
|
|
|
status = WLog_FileAppender_SetOutputFileName(log, FileAppender, env);
|
|
|
|
free(env);
|
|
|
|
|
|
|
|
if (!status)
|
|
|
|
goto error_output_file_name;
|
|
|
|
}
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
|
|
|
void WLog_FileAppender_Free(wLog* log, wLogFileAppender* appender)
|
|
|
|
{
|
|
|
|
if (appender)
|
|
|
|
{
|
2015-05-11 10:07:39 +03:00
|
|
|
free(appender->FileName);
|
|
|
|
free(appender->FilePath);
|
|
|
|
free(appender->FullFileName);
|
2013-10-02 00:08:26 +04:00
|
|
|
free(appender);
|
|
|
|
}
|
|
|
|
}
|