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

#include <vcl.h>
#pragma hdrstop

#include "VMSCommLibF.h"

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

#pragma package(smart_init)

SYSTEM_INFORMATION *g_SysInfo;
bool                LOAD_DB = true;

APP_CONFIG  g_AppCfg;
LOG_INFO    g_LogCfg;
TITSLog    *ITSLog = NULL;

TItsNetQ    g_logQ;
TItsNetQ    g_dbmQ;
TItsNetQ    g_jobQ;

TItsQBuff<IPC_LOG_MESSAGE, MAX_LOGQ_BUFF> g_logBuff;
TItsQBuff<IPC_DBM_MESSAGE, MAX_DBMQ_BUFF> g_dbmBuff;
TItsQBuff<IPC_JOB_MESSAGE, MAX_JOBQ_BUFF> g_jobBuff;

String          g_sAppDir  = "";       // Application Directory
String          g_sAppName = "";       // Program name
String          g_sCfgDir  = "";       // Program Config Directory
String          g_sLogDir  = "";       // Program Log Directory
String          g_sTempDir = "";       // Program Temp Directory
String          g_sImgDir = "";        // Program Image Directory

String          g_sFtpHomeDir   = "";  // FTP Home Directory
String          g_sFtpFormDir   = "";  // FTP Form Directory
String          g_sFtpVideoDir  = "";  // FTP Video Directory
String          g_sFtpStaticDir = "";  // FTP 정적이미지 Directory
String          g_sFtpImageDir  = "";  // FTP 이미지 Directory

GRADE_TEXT      FIGURE;
GRADE_TEXT      TEXT;


int APP_LogWrite(void* ALogObj, int ALogKind, int ATarget, int AFrom, char *AFmt, ...)
{
    va_list ap;
    int     cnt = 0;

    String  sLogKind = " [XXX] ";
    bool bLog = false;
    switch(ALogKind)
    {
    case eLOG_INFO   : bLog = g_LogCfg.Info;    sLogKind = " [INF] "; break;
    case eLOG_DATA   : bLog = g_LogCfg.Data;    sLogKind = " [DAT] "; break;
    case eLOG_ERROR  : bLog = g_LogCfg.Error;   sLogKind = " [ERR] "; break;
    case eLOG_WARNING: bLog = g_LogCfg.Warning; sLogKind = " [WAN] "; break;
    case eLOG_DEBUG  : bLog = g_LogCfg.Debug;   sLogKind = " [DBG] "; break;
    case eLOG_DETAIL : bLog = g_LogCfg.Detail;  sLogKind = " [DET] "; break;
    }
    if (!bLog) return -1;

    try
    {
        IPC_LOG_MESSAGE *pLog = g_logBuff.GetBuff();
        if (pLog)
        {
            memset(pLog->Msg, 0x00, sizeof(pLog->Msg));

            va_start(ap, AFmt);
            cnt = vsprintf(pLog->Msg, AFmt, ap);
            va_end(ap);

            pLog->Target = AFrom;
            pLog->From   = ATarget;
            pLog->Kind   = ALogKind;
            pLog->Flag   = 0;
            pLog->Write  = true;
            pLog->Tm     = Now();
            pLog->Type   = 0;
            pLog->Obj    = ALogObj;
            pLog->Ctlr   = NULL;
            pLog->Len    = strlen(pLog->Msg);

            g_logQ.PushTimeout((DWORD)pLog, 2000);
#if 0
            SingleLogThread->AddSysLogd(Now().FormatString("hh:nn:ss") + sLogKind + String(pLog->Msg));
#endif
        }
    }
    catch(Exception &e)
    {
    }
    return cnt;
}
//---------------------------------------------------------------------------

int APP_PostDbThreadMessage(e_dbm_command ACommand, int ASize, void *AData)
{
    IPC_DBM_MESSAGE *pDbm = g_dbmBuff.GetBuff();
    if (pDbm)
    {
        pDbm->Command = ACommand;
        pDbm->Size    = ASize;
        if (ASize > 0)
        {
            memcpy(pDbm->U.Data, AData, ASize);
        }
        else
        {
            pDbm->U.Data[0] = 0x00;
        }

        g_dbmQ.PushTimeout((DWORD)pDbm, 2000);
    }
    return 0;
}
//---------------------------------------------------------------------------


