zoukankan      html  css  js  c++  java
  • 一个简单的调试日志功能

    #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博客的好友拍砖,留言。
  • 相关阅读:
    依赖注入(DI)和Ninject
    Dapper.NET——轻量ORM
    优化SQL查询:如何写出高性能SQL语句
    Razor语法
    sublime Text 3 官方版 3114 注册码
    数据库索引,存储过程,视图,事务
    Action向视图传值的6种方式
    C#知识点提要
    算法总结
    c++ 构造函数,拷贝构造函数,析构函数与赋值操作符
  • 原文地址:https://www.cnblogs.com/M4ster/p/logDebug.html
Copyright © 2011-2022 走看看