一个简单的调试日志功能

Posted m4sterx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个简单的调试日志功能相关的知识,希望对你有一定的参考价值。

#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("\r\n"));
    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 ;
}

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

以上是关于一个简单的调试日志功能的主要内容,如果未能解决你的问题,请参考以下文章

argparse 代码片段只打印部分日志

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

调试利器!一款轻量级日志库 log.c

调试利器!一款轻量级日志库 log.c

IntelliJ:求值lambda表达式在调试时引发编译错误

IOS开发-OC学习-常用功能代码片段整理