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>
|
2013-10-02 23:41:46 +04:00
|
|
|
#include <winpr/path.h>
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
#include <winpr/wlog.h>
|
|
|
|
|
2013-10-02 23:41:46 +04:00
|
|
|
#include "wlog/Message.h"
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
#include "wlog/ConsoleAppender.h"
|
|
|
|
|
2014-08-07 19:47:59 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
/**
|
|
|
|
* Console Appender
|
|
|
|
*/
|
|
|
|
|
|
|
|
void WLog_ConsoleAppender_SetOutputStream(wLog* log, wLogConsoleAppender* appender, int outputStream)
|
|
|
|
{
|
|
|
|
if (!appender)
|
|
|
|
return;
|
|
|
|
|
2013-10-02 02:19:50 +04:00
|
|
|
if (appender->Type != WLOG_APPENDER_CONSOLE)
|
|
|
|
return;
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
if (outputStream < 0)
|
2015-05-06 11:56:30 +03:00
|
|
|
outputStream = WLOG_CONSOLE_DEFAULT;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
if (outputStream == WLOG_CONSOLE_STDOUT)
|
|
|
|
appender->outputStream = WLOG_CONSOLE_STDOUT;
|
|
|
|
else if (outputStream == WLOG_CONSOLE_STDERR)
|
|
|
|
appender->outputStream = WLOG_CONSOLE_STDERR;
|
|
|
|
else
|
2015-05-06 11:56:30 +03:00
|
|
|
appender->outputStream = WLOG_CONSOLE_DEFAULT;
|
2013-10-02 00:08:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int WLog_ConsoleAppender_Open(wLog* log, wLogConsoleAppender* appender)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WLog_ConsoleAppender_Close(wLog* log, wLogConsoleAppender* appender)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WLog_ConsoleAppender_WriteMessage(wLog* log, wLogConsoleAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
char prefix[WLOG_MAX_PREFIX_SIZE];
|
|
|
|
|
|
|
|
message->PrefixString = prefix;
|
|
|
|
WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
|
|
|
|
|
2014-05-31 18:10:19 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
if (appender->outputStream == WLOG_CONSOLE_DEBUG)
|
|
|
|
{
|
|
|
|
char MessageString[4096];
|
|
|
|
|
|
|
|
sprintf_s(MessageString, sizeof(MessageString), "%s%s\n",
|
|
|
|
message->PrefixString, message->TextString);
|
|
|
|
|
|
|
|
OutputDebugStringA(MessageString);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-07 19:47:59 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
(void)fp;
|
|
|
|
android_LogPriority level;
|
2014-08-14 12:38:02 +04:00
|
|
|
switch(message->Level)
|
2014-08-07 19:47:59 +04:00
|
|
|
{
|
|
|
|
case WLOG_TRACE:
|
|
|
|
level = ANDROID_LOG_VERBOSE;
|
|
|
|
break;
|
|
|
|
case WLOG_DEBUG:
|
|
|
|
level = ANDROID_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
case WLOG_INFO:
|
|
|
|
level = ANDROID_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case WLOG_WARN:
|
|
|
|
level = ANDROID_LOG_WARN;
|
|
|
|
break;
|
|
|
|
case WLOG_ERROR:
|
|
|
|
level = ANDROID_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
case WLOG_FATAL:
|
|
|
|
level = ANDROID_LOG_FATAL;
|
|
|
|
break;
|
|
|
|
case WLOG_OFF:
|
|
|
|
level = ANDROID_LOG_SILENT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
level = ANDROID_LOG_FATAL;
|
|
|
|
break;
|
|
|
|
}
|
2014-05-31 18:10:19 +04:00
|
|
|
|
2014-08-07 19:47:59 +04:00
|
|
|
if (level != ANDROID_LOG_SILENT)
|
2014-08-07 23:51:52 +04:00
|
|
|
__android_log_print(level, log->Name, "%s%s", message->PrefixString, message->TextString);
|
2014-08-07 19:47:59 +04:00
|
|
|
|
|
|
|
#else
|
2015-05-06 11:56:30 +03:00
|
|
|
switch(appender->outputStream)
|
|
|
|
{
|
|
|
|
case WLOG_CONSOLE_STDOUT:
|
|
|
|
fp = stdout;
|
|
|
|
break;
|
|
|
|
case WLOG_CONSOLE_STDERR:
|
|
|
|
fp = stderr;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
switch(message->Level)
|
|
|
|
{
|
|
|
|
case WLOG_TRACE:
|
|
|
|
case WLOG_DEBUG:
|
|
|
|
case WLOG_INFO:
|
|
|
|
fp = stdout;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fp = stderr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-05-31 18:10:19 +04:00
|
|
|
|
2015-05-06 11:58:57 +03:00
|
|
|
if (message->Level != WLOG_OFF)
|
2015-05-06 11:56:30 +03:00
|
|
|
fprintf(fp, "%s%s\n", message->PrefixString, message->TextString);
|
2014-08-07 19:47:59 +04:00
|
|
|
#endif
|
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_ConsoleAppender_WriteDataMessage(wLog* log, wLogConsoleAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
int DataId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
|
|
|
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_ConsoleAppender_WriteImageMessage(wLog* log, wLogConsoleAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
int ImageId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
|
|
|
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-09 07:18:59 +04:00
|
|
|
static int g_PacketId = 0;
|
|
|
|
|
|
|
|
int WLog_ConsoleAppender_WritePacketMessage(wLog* log, wLogConsoleAppender* appender, wLogMessage* message)
|
|
|
|
{
|
|
|
|
int PacketId;
|
|
|
|
char* FullFileName;
|
|
|
|
|
|
|
|
PacketId = g_PacketId++;
|
|
|
|
|
|
|
|
if (!appender->PacketMessageContext)
|
|
|
|
{
|
2013-10-09 07:43:57 +04:00
|
|
|
FullFileName = WLog_Message_GetOutputFileName(-1, "pcap");
|
2013-10-09 07:18:59 +04:00
|
|
|
appender->PacketMessageContext = (void*) Pcap_Open(FullFileName, TRUE);
|
|
|
|
free(FullFileName);
|
|
|
|
}
|
|
|
|
|
2014-08-14 12:38:02 +04:00
|
|
|
if (appender->PacketMessageContext)
|
|
|
|
WLog_PacketMessage_Write((wPcap*) appender->PacketMessageContext,
|
|
|
|
message->PacketData, message->PacketLength, message->PacketFlags);
|
2013-10-09 07:18:59 +04:00
|
|
|
|
|
|
|
return PacketId;
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
wLogConsoleAppender* WLog_ConsoleAppender_New(wLog* log)
|
|
|
|
{
|
|
|
|
wLogConsoleAppender* ConsoleAppender;
|
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
ConsoleAppender = (wLogConsoleAppender*) calloc(1, sizeof(wLogConsoleAppender));
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
if (!ConsoleAppender)
|
|
|
|
return NULL;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
ConsoleAppender->Type = WLOG_APPENDER_CONSOLE;
|
2013-10-04 19:33:49 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
ConsoleAppender->Open = (WLOG_APPENDER_OPEN_FN) WLog_ConsoleAppender_Open;
|
|
|
|
ConsoleAppender->Close = (WLOG_APPENDER_OPEN_FN) WLog_ConsoleAppender_Close;
|
2013-10-02 23:41:46 +04:00
|
|
|
|
2015-03-23 19:25:23 +03:00
|
|
|
ConsoleAppender->WriteMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_MESSAGE_FN) WLog_ConsoleAppender_WriteMessage;
|
|
|
|
ConsoleAppender->WriteDataMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_DATA_MESSAGE_FN) WLog_ConsoleAppender_WriteDataMessage;
|
|
|
|
ConsoleAppender->WriteImageMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_IMAGE_MESSAGE_FN) WLog_ConsoleAppender_WriteImageMessage;
|
|
|
|
ConsoleAppender->WritePacketMessage =
|
|
|
|
(WLOG_APPENDER_WRITE_PACKET_MESSAGE_FN) WLog_ConsoleAppender_WritePacketMessage;
|
2013-10-02 00:08:26 +04:00
|
|
|
|
2015-05-06 11:56:30 +03:00
|
|
|
ConsoleAppender->outputStream = WLOG_CONSOLE_DEFAULT;
|
2014-05-31 18:10:19 +04:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2015-05-06 16:36:54 +03:00
|
|
|
if (IsDebuggerPresent())
|
|
|
|
ConsoleAppender->outputStream = WLOG_CONSOLE_DEBUG;
|
2014-05-31 18:10:19 +04:00
|
|
|
#endif
|
2013-10-02 00:08:26 +04:00
|
|
|
|
|
|
|
return ConsoleAppender;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WLog_ConsoleAppender_Free(wLog* log, wLogConsoleAppender* appender)
|
|
|
|
{
|
|
|
|
if (appender)
|
|
|
|
{
|
2013-10-09 07:18:59 +04:00
|
|
|
if (appender->PacketMessageContext)
|
|
|
|
{
|
|
|
|
Pcap_Close((wPcap*) appender->PacketMessageContext);
|
|
|
|
}
|
|
|
|
|
2013-10-02 00:08:26 +04:00
|
|
|
free(appender);
|
|
|
|
}
|
|
|
|
}
|