/*
 * Copyright (c) 1998-2000 by KVA Software
 * All rights reserved.
 *
 * File:      dbg.h
 * Contents:  CDebugPrintf class definition
 * Author:    Vladimir Kirienko
 *
 *            http://www.uran.net/kva
 *            kirienko@geocities.com
 */
#pragma once

// #define USE_STL
#undef USE_STL

// #define USE_MFC
#undef USE_MFC

#undef USE_DEBUG_PRINTF

#if defined(_DEBUG) || defined(DEBUG_PRINTF)
#define USE_DEBUG_PRINTF
#endif

#include "IniFile.h"
#include "HighResPerformanceCounter.h"

#ifdef USE_DEBUG_PRINTF

#include <string>

/*
 *  Macro definitions
 *
 */
#define IFDEBUG(doit) doit

#define PRINTF         debug.printf
#define SHOW_CONSOLE   debug.ShowConsole();
#define HIDE_CONSOLE   debug.HideConsole();
#define SHOW_LASTERROR debug.ShowLastError();
#define CLOSE_LOG      debug.CloseLog();

/*-----------------------------------------------------------------------
 *  Class        : CDebugPrintf
 *  Prototype    : class CDebugPrintf
 *  Description  :
 *  Parent class :
 *  History      :
 */
class CDebugPrintf
{
public:
    bool LogStarted();
    void StopLog();
    void StartLog(char* cFilename = nullptr);
    CDebugPrintf(int nDebugType = 0);
    virtual ~CDebugPrintf();

    void printf(const char* str);

#ifdef USE_STL
    void printf(string& str)
    {
        printf(str.c_str());
    }

    void printf(string* str)
    {
        printf(str->c_str());
    }

#endif

#ifdef USE_MFC
    void printf(CString& str)
    {
        printf(str.GetBuffer(1024));
    }

    void printf(CString* str)
    {
        printf(str->GetBuffer(1024));
    }

#endif

    void CloseLog();

    void ShowConsole();
    void HideConsole();
    void ShowLastError();

private:
    HANDLE m_hFile;           // log-file handle
    HANDLE m_hDebugMutex;     // allow to check that log-file is open by other process
    CRITICAL_SECTION m_hLock; // lock for thread-safe access

    bool m_bUseConsole; // use or not log-console with log-file
    int m_nDebugType;   // to indicate debug type. 0 for debug log (TRACE), 1 for debug timing
    char m_cFileName[256];
    char m_cMutexName[256];

    CHighResPerformanceCounter m_HighResCounter;
    long m_nDebugLogCounter;
    long m_nNoOfDebugLogSetting;
    long m_nDebugLogFileSizSetting;

    long m_nNoOfEventLogSetting;
    long m_nEventLogFileSizSetting;

    // allow to log by day
    bool m_bLogByDay;
    std::string m_strLogByDayPath;
    long m_lLastLogByDateTime;
    CIniFile m_eventLogIni;
};

#else

#define IFDEBUG(doit)

#define SLASH /
#define COMMENT \
    ;           \
    SLASH /

#define PRINTF         COMMENT
#define SHOW_CONSOLE   COMMENT
#define HIDE_CONSOLE   COMMENT
#define SHOW_LASTERROR COMMENT
#define CLOSE_LOG      COMMENT

#endif
