/*
 * ************************************************************************************************************
 * Author: Marcin Załęczny
 * Copyright (C)2010 Marcin Załęczny <mzaleczny@hotmail.com>
 * License: The GNU Lesser General Public License, version 3.0 (LGPLv3)
 * Library Name: mzstd
 * Version: 1.07
 * First release date:   2010-11-08
 * Current release date: 2011-03-04
 *
 *
 * This file is part of mzstd library
 * 
 * mzstd library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * mzstd library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************************************************
 */

#ifndef __MZSTD_LOGGER_H__
#define __MZSTD_LOGGER_H__

#include "mzstdCommon.h"
#include "mzObject.h"
#include "mzString.h"
#include "mzFileUtils.h"

#define DEFAULT_LOGGER_FILENAME                         L"trace.log"

#define LOG(MESSAGE)                                    WriteLogMessage((DWORD)__LINE__, (CHAR*)__FILE__, ENC_UTF8, (MESSAGE));
#define LOGFMT1(FORMAT, PARAM1)                         WriteLogMessage((DWORD)__LINE__, (CHAR*)__FILE__, ENC_UTF8, FormatMessage((WCHAR*)(FORMAT), (PARAM1)).toWCHAR());
#define LOGFMT2(FORMAT, PARAM1, PARAM2)                 WriteLogMessage((DWORD)__LINE__, (CHAR*)__FILE__, ENC_UTF8, FormatMessage((WCHAR*)(FORMAT), (PARAM1), (PARAM2)).toWCHAR());
#define LOGFMT3(FORMAT, PARAM1, PARAM2, PARAM3)         WriteLogMessage((DWORD)__LINE__, (CHAR*)__FILE__, ENC_UTF8, FormatMessage((WCHAR*)(FORMAT), (PARAM1), (PARAM2), (PARAM3)).toWCHAR());
#define LOGMEM(MEMORY, SIZE)                            WriteMemoryLogMessage((DWORD)__LINE__, (CHAR*)__FILE__, ENC_UTF8, (MEMORY), (SIZE));


class DECLSPEC_MZSTD CLogger : public CObject {
public:
	WINAPI CLogger(CString filename);
    virtual WINAPI ~CLogger();

    VOID __cdecl log(WCHAR* format, va_list paramList);
	VOID __cdecl log(WCHAR* format, ...);
	VOID __cdecl log(WCHAR* message, BOOL appendEOL = FALSE);
    VOID WINAPI close();
    VOID WINAPI reopen();
    VOID WINAPI flush();
    BOOL WINAPI isReady() { return this->_fh != INVALID_HANDLE_VALUE; }
    
    inline CString WINAPI getFilename() { return this->_filename; }

protected:
    HANDLE _fh;
    CString _filename;
    
private:
    ULONG _writeOperations;
};
typedef CLogger* PLogger;

DECLSPEC_MZSTD BOOL WINAPI InitLogger(CString fname = DEFAULT_LOGGER_FILENAME);
DECLSPEC_MZSTD VOID WINAPI CleanupLogger();
DECLSPEC_MZSTD BOOL __cdecl WriteLogMessage(DWORD lineNumber, CHAR* sourceFilename, LONG encoding, WCHAR *message, ...);
DECLSPEC_MZSTD BOOL __cdecl WriteLogMessage(WCHAR *message, ...);
DECLSPEC_MZSTD BOOL WINAPI WriteMemoryLogMessage(DWORD lineNumber, CHAR* sourceFilename, LONG encoding, LPVOID memory, ULONG size);
DECLSPEC_MZSTD VOID WINAPI FlushLogger();

#endif
