A simple debug log feature

Source: Internet
Author: User

#ifndef Logdebug_h #define Logdebug_h<windows.h>void  setdebuglogparam (bool Bout2file,bool bout2debugstring, BOOL Bout2console, BOOL bwithdatetime); void 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"#defineOds_log_maxlength 512BOOL Gbout2console=TRUE; BOOL Gbout2file=TRUE; BOOL Gbout2outdebugstr=TRUE; BOOL Gbwithdatetime=FALSE;voidWritedebuglog (DWORD dwlasterror, LPCSTR file,lpcstr function,intCodeLine, 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); intIwritten = _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[ -] = {0}; _stprintf_s (LastError, the, _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")); StaticHANDLE 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); }    StaticHANDLE 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    }voidSetdebuglogparam (bool Bout2file,bool bout2debugstring, BOOL bout2console, bool bwithdatetime) {Gbout2console=Bout2console; Gbout2file=Bout2file; Gbout2outdebugstr=bout2debugstring; Gbwithdatetime=Bwithdatetime;}

A few of the functions are written by their own small tool function, it is not affixed to the changes you look at it

A simple debug log feature

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.