一个简单的调试日志功能

#ifndef LOGDEBUG_H
#define LOGDEBUG_H

#include <windows.h>

void SetDebugLogParam(BOOL bOut2File,BOOL bOut2Debugstring ,BOOL bOut2Console, BOOL bWithDateTime );
void WriteDebugLog(DWORD dwLastError, LPCSTR file, LPCSTR function, int codeLine, LPCTSTR content, ...);
#define Debug(fmt, ...) WriteDebugLog(::GetLastError(), __FILE__,__FUNCTION__, __LINE__, fmt, __VA_ARGS__)

#endif
#include "LogDebug.h"
#include <tchar.h>
#include <stdio.h>
#include "Unitls.h"

#define ODS_LOG_MAXLENGTH 512

BOOL gbOut2Console = TRUE ;
BOOL gbOut2File = TRUE ;
BOOL gbOut2OutDebugstr = TRUE ;
BOOL gbWithDateTime = FALSE ;

void WriteDebugLog( DWORD dwLastError, LPCSTR file,LPCSTR function, int codeLine, LPCTSTR content, ... )
{
    TCHAR logContent[ODS_LOG_MAXLENGTH + 1] = {0};

    DWORD dwThreadID = ::GetCurrentThreadId();
    
    WCHAR wszTm[MAX_PATH];
    SYSTEMTIME sytm;
    GetLocalTime(&sytm);
    _stprintf_s(wszTm,L" %04d%02d%02d-%02d%02d%02d",sytm.wYear,sytm.wMonth,sytm.wDay,sytm.wHour,sytm.wMinute,sytm.wSecond);

    
    LPWSTR lpwszFile = UTF8_2_UTF16((LPSTR)file);
    LPWSTR lpwszFunction = UTF8_2_UTF16((LPSTR)function);
    int iWritten = _stprintf_s(logContent, _T("Debug [%s %s:%d] %u "), lpwszFile,lpwszFunction, codeLine, dwThreadID);
    zFree((LPBYTE)lpwszFile);
    zFree((LPBYTE)lpwszFunction);
    va_list ap;
    va_start(ap, content);
    _vsntprintf_s(logContent + iWritten, ODS_LOG_MAXLENGTH - iWritten, _TRUNCATE, content, ap);
    va_end(ap);

    if (dwLastError != 0)
    {
        TCHAR lastError[16] = {0};
        _stprintf_s(lastError, 15, _T(" E%u"), dwLastError);

        size_t len = _tcslen(logContent);
        if (len + _tcslen(lastError) < ODS_LOG_MAXLENGTH)
        {
            _tcscat_s(logContent, lastError);
        }
    }

    if (gbWithDateTime )
    {
        

        size_t len = _tcslen(logContent);
        if (len + _tcslen(wszTm) < ODS_LOG_MAXLENGTH)
        {
            _tcscat_s(logContent, wszTm);
        }
    }

    _tcscat_s(logContent, _T("
"));
    static HANDLE hStandOut = INVALID_HANDLE_VALUE ;
    if (gbOut2Console)
    {
        
        if (hStandOut == INVALID_HANDLE_VALUE)
        {
            AllocConsole();
            hStandOut = GetStdHandle(STD_OUTPUT_HANDLE);
            
            
        }
        
        if (hStandOut != INVALID_HANDLE_VALUE)
        {
            DWORD dwWriten = 0;
            

            WriteConsole(hStandOut,logContent,wcslen(logContent),&dwWriten,0);
            
        }
    }
    
    if (gbOut2OutDebugstr)
    {
        OutputDebugString(logContent);
    }
    static HANDLE hOutFIle = INVALID_HANDLE_VALUE ;
    if (gbOut2File)
    {
        

        if (hOutFIle == INVALID_HANDLE_VALUE)
        {
            WCHAR wszFileName[MAX_PATH];
            
            GetModuleFileName(NULL,wszFileName,MAX_PATH);
            _tcscat_s(wszFileName,wszTm);

            _tcscat_s(wszFileName,L".debuglogs");

            hOutFIle = CreateFile(wszFileName,GENERIC_WRITE, FILE_SHARE_READ,NULL,OPEN_ALWAYS,NULL,NULL);
        }
        DWORD dwWriten = 0;
        WriteFile(hOutFIle,logContent,wcslen(logContent)*2,&dwWriten,NULL);


    }
    // #endif

    
}

void SetDebugLogParam(BOOL bOut2File,BOOL bOut2Debugstring ,BOOL bOut2Console, BOOL bWithDateTime)
{
    gbOut2Console = bOut2Console ;
    gbOut2File = bOut2File;
    gbOut2OutDebugstr = bOut2Debugstring ;
    gbWithDateTime = bWithDateTime ;
}

有几个函数是自己写的小工具函数,就不贴了,自己看着改改吧

签名档: 从事网络安全和编程的我,很希望能找到志同道合的朋友交流。 欢迎cn博客的好友拍砖,留言。
原文地址:https://www.cnblogs.com/M4ster/p/logDebug.html