Write a simple logtrace in C ++

Source: Internet
Author: User

# Include <afx. h>
# Include <shellapi. h>

Class log
{
Public:
Log ();
Virtual ~ Log ();
Public:
Static cstring getlogfile ();
Static short setlogfile (lpctstr strpath );
Static short viewlogfile ();

Static short setprefix (lpctstr strprefix );

Static cstring soutv (maid, maid = NULL, va_list valist = NULL );
Static cstring sout0 (maid strtype, maid strformat = NULL ,...);
Static cstring sout (lpctstr strformat = NULL ,...);

Static short outv (maid, maid = NULL, va_list valist = NULL );
Static short out0 (maid strtype, maid strformat = NULL ,...);
Static short out (lpctstr strformat = NULL ,...);

Protected:
Static cstring s_strlogfile;
Static cstring s_strlogprefix;
Static handle s_hwriteevent;
};

 

//////////////////////////////////////// //////////////////////////////////////// //////////

# Include "logtrace. H"

# Ifdef _ debug
# Define new debug_new
# Endif

 

// Obtain the directory of the executable program
// Bool bincludesep -- whether the last separator is included "/"

Cstring getexepath (bool bincludesep)
{
// Obtain the current file name
Cstring strfilename;
Getmodulefilename (getmodulehandle (0), strfilename. getbuffer (_ max_path), _ max_path );
Strfilename. releasebuffer ();
 
// Obtain the current directory
Strfilename = strfilename. Left (strfilename. reversefind (_ T ('//') + 1 );
 
If (bincludesep)
Return strfilename;
Else return strfilename. Left (strfilename. getlength ()-1 );
} //-Get the final file name. If the given file is not a full path, it is relative to the exe-

Cstring getfileforexepath (lpctstr strcurfilename)
{
Cstring strpath = strcurfilename;
If (! Strpath. isempty ()){
//-Relative path-
If (strpath. Find (_ T (":") <= 0)
{
Strpath. Format (_ T ("% S % s"), getexepath (false), strcurfilename );
}
}
Return strpath;
}

# Define log_event _ T ("chylogwrite ")
Cstring log: s_strlogfile = _ T ("");
Cstring log: s_strlogprefix = _ T ("");
Handle log: s_hwriteevent = NULL;

Log: log ()
{

}

Log ::~ Log ()
{

}

Short log: setlogfile (lpctstr strpath)
{
If (strpath = NULL | strpath [0] = 0)
S_strlogfile = getfileforexepath (L "log. log ");
Else s_strlogfile = getfileforexepath (strpath );
Return 1;
}

Cstring log: getlogfile ()
{
Return s_strlogfile;
}

Short log: viewlogfile ()
{
Cstring strlogfile = getlogfile ();
ShellExecute (null, _ T ("open"), strlogfile, null, null, sw_show );
Return strlogfile. isempty ()? ;
}

Short log: setprefix (lpctstr strprefix)
{
If (strprefix & strprefix [0])
{
S_strlogprefix = strprefix;
}
Return 1;
}

Cstring log: soutv (maid, maid)
{
Cstring strpart_prefix;
If (! S_strlogprefix.isempty ())
{
Strpart_prefix.format (_ T ("[% s]"), s_strlogprefix );
}
Cstring strpart_time;
{Systemtime required IME = {0 };
Getlocaltime (& systime );
Strpart_time.format (_ T ("[% 2D-% 2D % 2D: % 2D: % 2D _ % 3d]"),
Invalid ime. wmonth, invalid ime. wday,
Systime. whour, systime. wminute, systime. wsecond,
Systime. wmilliseconds );
}
 
Cstring strpart_type;
If (strtype & strtype [0])
{
Strpart_type.format (_ T ("[% s]"), strtype );
}
 
Cstring strpart_info;
{
Strpart_info.formatv (strformat, valist );
}
 
Cstring STR = strpart_prefix + strpart_time + strpart_type + strpart_info;
 
Return STR;
}

Cstring log: sout0 (maid, maid ,...)
{
Va_list valist;
Va_start (valist, strformat );
Cstring strinfo = soutv (strtype, strformat, valist );
Va_end (valist); Return strinfo;
}

Cstring log: sout (lpctstr strformat ,...)
{
Va_list valist;
Va_start (valist, strformat );
Cstring strinfo = soutv (null, strformat, valist );
Va_end (valist );
 
Return strinfo;
}

Short log: outv (maid, maid)

{
//--
If (s_hwriteevent = NULL)
{
S_hwriteevent = openevent (0, false, log_event );
If (s_hwriteevent = NULL)
S_hwriteevent = createevent (null, false, true, log_event );
}
Waitforsingleobject (s_hwriteevent, infinite );

//-Open and close the file-
If (s_strlogfile.isempty () setlogfile (null );
Cstdiofile file;
If (file. Open (s_strlogfile, cfile: modecreate | cfile: modenotruncate | cfile: modewrite ))
{
Cstring strpart_newline = _ T ("/N ");
Cstring strinfo = soutv (strtype, strformat, valist );
Cstring STR = strpart_newline + strinfo;
File. seektoend ();
File. writestring (STR );
File. Close ();
}
Setevent (s_hwriteevent );
Return 1;
}

Short log: out0 (maid, maid ,...)
{
Va_list valist;
Va_start (valist, strformat );
Short RTN = outv (strtype, strformat, valist );
Va_end (valist );
Return RTN;
}

Short log: Out (lpctstr strformat ,...)
{
Va_list valist;
Va_start (valist, strformat );
Short RTN = outv (null, strformat, valist );
Va_end (valist );
Return RTN;
}

 

//////////////////////////////////////// /////////////////

 

Used, where logs are to be generated

 

# Include "logtrace. H"
# Ifdef _ debug
# Define new debug_new
# Endif

 

Then use:

Log: out0 (L "debug", l "hello ");
Log: out0 (L "warn5", l "Hello % d", 25 );
Log: out0 (L "warn3", l "Hello % s, you have % d apples! ", L" Libai ", 10 );
Log: Out (L "Hello % s, you have % d apples! ", L" Libai ", 10 );

 

You can.

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.