//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "FrmSysLogF.h"
#include "VMSCommLibF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFrmSysLog *FrmSysLog = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmSysLog::TFrmSysLog(TComponent* Owner)
    : TForm(Owner)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFrmSysLog::btnClearClick(TObject *Sender)
{
    try
    {
        reMsg->Lines->Clear();
        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmSysLog::btnCopyClick(TObject *Sender)
{
    try
    {
        reMsg->SelectAll();
        reMsg->CopyToClipboard();
        Application->ProcessMessages();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmSysLog::OnWMLogDisplayMessage(TMessage &Msg)
{
    if (chkLogPause->Checked) return;

    IPC_LOG_MESSAGE *pLog = (IPC_LOG_MESSAGE *)Msg.WParam;
    if (pLog)
    {
#if 0
        IPC_LOG_MESSAGE Log;
        Log.Kind = pLog->Kind;
        Log.Flag = pLog->Flag;
        Log.Len  = pLog->Len;
        Log.Tm   = pLog->Tm;
        memcpy(Log.Msg, pLog->Msg, sizeof(Log.Msg));
        //ReplyMessage(0);
        LogWrite(&Log);
#else
        ReplyMessage(0);
        LogWrite(pLog);
#endif
    }
}
//---------------------------------------------------------------------------

void TFrmSysLog::LogWrite(IPC_LOG_MESSAGE *ALog)
{
    if (!ALog) return;

    try
    {
        //reMsg->Lines->BeginUpdate();

        AnsiString  sLogKind = " [XXX] ";
        switch(ALog->Kind)
        {
        case eLOG_INFO   : sLogKind = " [INF] "; break;
        case eLOG_DATA   : sLogKind = " [DAT] "; break;
        case eLOG_ERROR  : sLogKind = " [ERR] "; break;
        case eLOG_WARNING: sLogKind = " [WAN] "; break;
        case eLOG_DEBUG  : sLogKind = " [DBG] "; break;
        case eLOG_DETAIL : sLogKind = " [DET] "; break;
        }
        try
        {
#if 0
            while (reMsg->Lines->Count >= g_AppCfg.nMaxLogLines)
                reMsg->Lines->Delete(0);
#else
            if (reMsg->Lines->Count >= g_AppCfg.nMaxLogLines)
                reMsg->Lines->Clear();
#endif

            reMsg->Lines->Add(ALog->Tm.FormatString("hh:nn:ss") + "." + Now().FormatString("ss") + sLogKind + String(ALog->Msg));
            //reMsg->Lines->Add(ALog->Tm.FormatString("hh:nn:ss") + "." + Now().FormatString("ss") + String(ALog->Msg));
            //reMsg->Lines->Add(ALog->Tm.FormatString("hh:nn:ss") + "[" + Now().FormatString("ss") +  "]" + String(ALog->Msg));

            Application->ProcessMessages();
        }
        catch(Exception &e)
        {
        }
    }
    __finally
    {
        //reMsg->Lines->EndUpdate();
        //SendMessage(reMsg->Handle, EM_SCROLLCARET, 0, 0);
        //reMsg->Perform(EM_SCROLLCARET, 0, 0);
        //Application->ProcessMessages();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmSysLog::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    reMsg->Lines->Add( Now().FormatString("hh:nn:ss") + "." + Now().FormatString("ss") + " ***** Starting program...");
}
//---------------------------------------------------------------------------

void TFrmSysLog::DisplayMsg(String& AMsg)
{
    if (chkLogPause->Checked) return;
    if (!Showing) return;

    if (reMsg->Lines->Count >= g_AppCfg.nMaxLogLines)
        reMsg->Lines->Clear();

	if (AMsg != "")
	{
		AMsg[AMsg.Length()-1] = 0;	// 마지막줄의 CRLF 삭제. 그래야 메모장에서는 줄간 간격이 떨어지지 않게 찍힌다.
		reMsg->Lines->Add(AMsg);
	}
}
//---------------------------------------------------------------------------


