#if !defined(_LOGFILE_H)
#define _LOGFILE_H

#include "PubHeader.h"
#include "otstr.h"

//////////////////////////////////////
typedef char  *LPSTR, *PSTR;
typedef const  char *LPCSTR;
typedef LPCSTR LPCTSTR;

///////////////////////////////////////////////////////////////////////////

#ifdef _DLL_PROJECT
class CLASS_EXPORT COneLogFile
#else
class COneLogFile
#endif
{
public:
    enum { ONELOG_SAVBUFSIZE=1024*3 };
    enum { DIRECT_LOG=0x00, BUF_LOG=0x01 };

private:
	char    m_logfilename[64];
    int     m_LogMode;
    bool    m_bOverWrite;

	char    m_pSavbuf[ONELOG_SAVBUFSIZE+50];
	DWORD   m_Savbuflen;

public:
	COneLogFile(char* pFileName,int LogMode, bool bOverWrite=false);
   ~COneLogFile();

    void Append (LPCTSTR lpstrLog);
    void Appends(const char* pszFormat, ...);

    void Flush();

private:
	void NewLogFile(bool bOverWrite);
    void CloseLogFile();
    
    void SaveDataToFile(LPCTSTR pbuf,bool bOverWrite=false);
}; 

//////////////////////////////////////
#ifdef _DLL_PROJECT
class CLASS_EXPORT CLogFileEx
#else
class CLogFileEx
#endif
{
public:
    enum { SAV_BUFSIZE=1024*16 };
    enum { DIRECT_LOG =0x00, BUF_LOG=0x01 };

    enum { 
            FILEATTR_NO    =0x00,
            FILEATTR_NORMAL=0x01,   // FileName Create by SystemTime
            FILEATTR_CYC   =0x02    // FileName Create by Cycle
    };
    
private:
    OTSTR  m_szFileTitle;
    OTSTR  m_szFilePath;
    OTSTR  m_szExt;

    unsigned char m_FirstCh;
    unsigned char m_LastCh;
    unsigned char m_NextCh;
    
    int     m_LogMode;
    int     m_FileMode;
    
    DWORD   m_MaxfileSize;

    OTSTR   m_szCurFileName;
    DWORD   m_CurfileSize;
    
    char    m_buf[SAV_BUFSIZE+50];

public:
    CLogFileEx();
   ~CLogFileEx();
    
    bool SetFileAttr_Cycle (char* szFileTitle,char* szExt,unsigned char FirstCh,unsigned char LastCh);
    bool SetFileAttr_Normal(char* szFilePath,char* szExt);

    bool SetLogAttr(int LogMode,DWORD MaxfileSize=0xFFFFFFFF);  // 0xFFFFFFFF, mean no size limit
    
    bool Begin();
    void Finish();
    
    void Flush(); // SAVE DATA IN BUFFER TO LOG FILE

    void AddLog (const char* szbuf);
    void AddLog_(const char* pszFormat, ...);
    
private:
    void NewLogFile();
    void CloseLogFile();

    bool SaveDataToFile(LPCTSTR pbuf,bool bEraseFirst);
}; 


#endif


