Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
jlib
Commits
c180f138
Commit
c180f138
authored
May 04, 2015
by
captainwong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
log dump
parent
307bf11d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
486 additions
and
455 deletions
+486
-455
Log.h
Log.h
+485
-455
global.h
global.h
+1
-0
No files found.
Log.h
View file @
c180f138
// Log.h: interface for the CLog class.
// Log.h: interface for the CLog class.
//
//
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/*
/*
日志类CLog
日志类CLog
简述:单例的日志输出类,使用时会自动构造,但因为要保证日志类最后一个析构,需要手动释放
简述:单例的日志输出类,使用时会自动构造,但因为要保证日志类最后一个析构,需要手动释放
功能:写日志,输出TRACE内容到dbgview,console。
功能:写日志,输出TRACE内容到dbgview,console。
使用:
使用:
使用函数
使用函数
static void SetOutputLogFileName(LPCTSTR szFileName);
static void SetOutputLogFileName(LPCTSTR szFileName);
static void SetOutputConsole(BOOL bFlag);
static void SetOutputConsole(BOOL bFlag);
static void SetOutputDbgView(BOOL bFlag);
static void SetOutputDbgView(BOOL bFlag);
static void SetOutputLogFile(BOOL bFlag);
static void SetOutputLogFile(BOOL bFlag);
来设置输出日志的方式
来设置输出日志的方式
使用TRACE或使用函数static void WriteLog(const char* format, ...);写日志,可接受可变参数
使用TRACE或使用函数static void WriteLog(const char* format, ...);写日志,可接受可变参数
释放:在程序退出时(ExitInstance()函数的最后)调用CLog::Release()释放引用计数
释放:在程序退出时(ExitInstance()函数的最后)调用CLog::Release()释放引用计数
*/
*/
#if !defined(AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_)
#if !defined(AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_)
#define AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_
#define AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_
#if _MSC_VER > 1000
#if _MSC_VER > 1000
#pragma once
#pragma once
#endif // _MSC_VER > 1000
#endif // _MSC_VER > 1000
#include <assert.h>
#include <assert.h>
//#include "dbghlpapi.h"
//#include "dbghlpapi.h"
//#ifdef TRACE
//#ifdef TRACE
//#undef TRACE
//#undef TRACE
//#define TRACE CLog::WriteLog
//#define TRACE CLog::WriteLog
//#endif
//#endif
#define IMPLEMENT_CLASS_LOG_STATIC_MEMBER \
#define IMPLEMENT_CLASS_LOG_STATIC_MEMBER \
CRITICAL_SECTION CLog::m_cs;\
CRITICAL_SECTION
CLog
::
m_cs
;
\
CLock CLog::m_lockForSingleton;\
CLock
CLog
::
m_lockForSingleton
;
\
char CLog::m_ansiBuf[MAX_OUTPUT_LEN];\
char
CLog
::
m_ansiBuf
[
MAX_OUTPUT_LEN
];
\
wchar_t CLog::m_utf16Buf[MAX_OUTPUT_LEN];\
wchar_t
CLog
::
m_utf16Buf
[
MAX_OUTPUT_LEN
];
\
CLog* CLog::m_pInstance = NULL;
CLog
*
CLog
::
m_pInstance
=
NULL
;
/*
/*
#define TRACEFUNCNAME \
#define TRACEFUNCNAME \
CString str = _T("");\
CString str = _T("");\
INIT_SYM(TRUE);\
INIT_SYM(TRUE);\
str.Format("File %s, line %d, funcname %s\n", __FILE__, __LINE__, __FUNCNAME__);\
str.Format("File %s, line %d, funcname %s\n", __FILE__, __LINE__, __FUNCNAME__);\
OutputDebugString(str);\
OutputDebugString(str);\
UNINIT_SYM();\
UNINIT_SYM();\
*/
*/
#define MAX_OUTPUT_LEN 4096
#define MAX_OUTPUT_LEN 4096
#define MAX_FILE_LEN 1024 * 1024 * 10
#define MAX_FILE_LEN 1024 * 1024 * 10
static
TCHAR
g_szFileName
[
1024
]
=
{
0
};
static
TCHAR
g_szFileName
[
1024
]
=
{
0
};
class
CLog
class
CLog
{
{
private
:
private
:
double
exe_time
;
double
exe_time
;
LARGE_INTEGER
freq
;
LARGE_INTEGER
freq
;
LARGE_INTEGER
start_t
,
stop_t
;
LARGE_INTEGER
start_t
,
stop_t
;
BOOL
m_bOutputLogFile
;
BOOL
m_bOutputLogFile
;
BOOL
m_bOutputDbgView
;
BOOL
m_bOutputDbgView
;
BOOL
m_bOutputConsole
;
BOOL
m_bOutputConsole
;
BOOL
m_bConsoleOpened
;
BOOL
m_bConsoleOpened
;
BOOL
m_bDbgviewOpened
;
BOOL
m_bDbgviewOpened
;
BOOL
m_bLogFileOpened
;
BOOL
m_bLogFileOpened
;
BOOL
m_bRunning
;
BOOL
m_bRunning
;
FILE
*
m_pLogFile
;
FILE
*
m_pLogFile
;
PROCESS_INFORMATION
pi
;
PROCESS_INFORMATION
pi
;
static
CRITICAL_SECTION
m_cs
;
static
CRITICAL_SECTION
m_cs
;
static
CLock
m_lockForSingleton
;
static
CLock
m_lockForSingleton
;
static
char
m_ansiBuf
[
MAX_OUTPUT_LEN
];
static
char
m_ansiBuf
[
MAX_OUTPUT_LEN
];
static
wchar_t
m_utf16Buf
[
MAX_OUTPUT_LEN
];
static
wchar_t
m_utf16Buf
[
MAX_OUTPUT_LEN
];
static
CLog
*
m_pInstance
;
static
CLog
*
m_pInstance
;
public
:
public
:
static
const
wchar_t
*
GetPrivateLogPath
(){
static
const
wchar_t
*
GetPrivateLogPath
(){
static
wchar_t
strPrivateMapFolder
[
1024
]
=
{
0
};
static
wchar_t
strPrivateMapFolder
[
1024
]
=
{
0
};
static
BOOL
b
=
TRUE
;
static
BOOL
b
=
TRUE
;
if
(
b
){
if
(
b
){
wsprintf
(
strPrivateMapFolder
,
_T
(
"%s
\\
Log"
),
GetModuleFilePath
());
wsprintf
(
strPrivateMapFolder
,
_T
(
"%s
\\
Log"
),
GetModuleFilePath
());
b
=
FALSE
;
b
=
FALSE
;
}
}
return
strPrivateMapFolder
;
return
strPrivateMapFolder
;
}
}
static
CLog
*
GetInstance
(){
static
CLog
*
GetInstance
(){
m_lockForSingleton
.
Lock
();
m_lockForSingleton
.
Lock
();
if
(
CLog
::
m_pInstance
==
NULL
)
{
if
(
CLog
::
m_pInstance
==
NULL
)
{
static
CLog
log
;
static
CLog
log
;
CLog
::
m_pInstance
=
&
log
;
CLog
::
m_pInstance
=
&
log
;
}
}
m_lockForSingleton
.
UnLock
();
m_lockForSingleton
.
UnLock
();
return
CLog
::
m_pInstance
;
return
CLog
::
m_pInstance
;
}
}
~
CLog
(){
~
CLog
(){
m_bRunning
=
FALSE
;
m_bRunning
=
FALSE
;
//WaitTillThreadExited(m_handle);
//WaitTillThreadExited(m_handle);
if
(
m_bConsoleOpened
)
if
(
m_bConsoleOpened
)
OpenConsole
(
FALSE
);
OpenConsole
(
FALSE
);
if
(
m_bDbgviewOpened
)
if
(
m_bDbgviewOpened
)
OpenDbgview
(
FALSE
);
OpenDbgview
(
FALSE
);
if
(
m_bLogFileOpened
)
if
(
m_bLogFileOpened
)
CloseLogFile
();
CloseLogFile
();
DeleteCriticalSection
(
&
m_cs
);
DeleteCriticalSection
(
&
m_cs
);
#ifdef _DEBUG
#ifdef _DEBUG
TCHAR
buf
[
1024
],
out
[
1024
];
TCHAR
buf
[
1024
],
out
[
1024
];
wsprintf
(
buf
,
_T
(
"%s
\n
"
),
_T
(
"CLog::~CLog() destruction"
));
wsprintf
(
buf
,
_T
(
"%s
\n
"
),
_T
(
"CLog::~CLog() destruction"
));
FormatBuf
(
buf
,
out
,
1024
);
FormatBuf
(
buf
,
out
,
1024
);
OutputDebugString
(
out
);
OutputDebugString
(
out
);
#endif
#endif
}
}
static
void
SetOutputLogFileName
(
LPCTSTR
szLastName
){
static
void
SetOutputLogFileName
(
LPCTSTR
szLastName
){
lstrcpy
(
g_szFileName
,
szLastName
);
lstrcpy
(
g_szFileName
,
szLastName
);
}
}
static
void
SetOutputConsole
(
BOOL
bFlag
){
static
void
SetOutputConsole
(
BOOL
bFlag
){
CLog
*
plog
=
CLog
::
GetInstance
();
CLog
*
plog
=
CLog
::
GetInstance
();
plog
->
m_bOutputConsole
=
bFlag
;
plog
->
m_bOutputConsole
=
bFlag
;
plog
->
OpenConsole
(
bFlag
);
plog
->
OpenConsole
(
bFlag
);
}
}
static
void
SetOutputDbgView
(
BOOL
bFlag
){
static
void
SetOutputDbgView
(
BOOL
bFlag
){
CLog
*
plog
=
CLog
::
GetInstance
();
CLog
*
plog
=
CLog
::
GetInstance
();
plog
->
m_bOutputDbgView
=
bFlag
;
plog
->
m_bOutputDbgView
=
bFlag
;
//plog->OpenDbgview(bFlag);
//plog->OpenDbgview(bFlag);
}
}
static
void
SetOutputLogFile
(
BOOL
bFlag
){
static
void
SetOutputLogFile
(
BOOL
bFlag
){
CLog
*
plog
=
CLog
::
GetInstance
();
CLog
*
plog
=
CLog
::
GetInstance
();
plog
->
m_bOutputLogFile
=
bFlag
;
plog
->
m_bOutputLogFile
=
bFlag
;
if
(
plog
->
m_bOutputLogFile
&&
lstrlen
(
g_szFileName
)
==
0
)
if
(
plog
->
m_bOutputLogFile
&&
lstrlen
(
g_szFileName
)
==
0
)
plog
->
CreateFileName
();
plog
->
CreateFileName
();
}
}
static
void
WriteLog
(
const
TCHAR
*
format
,
...)
{
static
void
Dump
(
const
char
*
buff
,
size_t
buff_len
)
try
{
{
CLog
*
plog
=
CLog
::
GetInstance
();
try
{
if
(
plog
==
NULL
)
CLog
*
plog
=
CLog
::
GetInstance
();
return
;
if
(
plog
==
NULL
)
CLocalLock
lock
(
&
m_cs
);
return
;
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
CLocalLock
lock
(
&
m_cs
);
static
TCHAR
buf
[
MAX_OUTPUT_LEN
],
output
[
MAX_OUTPUT_LEN
],
*
p
;
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
p
=
buf
;
size_t
output_len
=
buff_len
*
6
+
64
;
va_list
args
;
wchar_t
*
output
=
new
wchar_t
[
output_len
];
va_start
(
args
,
format
);
output
[
0
]
=
0
;
p
+=
_vsntprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
wchar_t
c
[
64
]
=
{
0
};
va_end
(
args
);
swprintf_s
(
c
,
L"len %d
\n
"
,
buff_len
);
while
(
(
p
>
buf
)
&&
_istspace
(
*
(
p
-
1
))
)
wcscat_s
(
output
,
output_len
,
c
);
*--
p
=
_T
(
'\0'
);
for
(
size_t
i
=
0
;
i
<
buff_len
;
i
++
)
{
*
p
++
=
_T
(
'\r'
);
swprintf_s
(
c
,
L"%02X "
,
buff
[
i
]);
*
p
++
=
_T
(
'\n'
);
wcscat_s
(
output
,
output_len
,
c
);
*
p
=
_T
(
'\0'
);
if
(
i
>
0
&&
(
i
+
1
)
%
16
==
0
)
{
plog
->
FormatBuf
(
buf
,
output
);
wcscat_s
(
output
,
output_len
,
L"
\n
"
);
plog
->
Output
(
output
);
}
}
}
}
wcscat_s
(
output
,
output_len
,
L"
\n
"
);
catch
(...){
plog
->
Output
(
output
);
assert
(
0
);
delete
[]
output
;
}
}
}
}
catch
(...)
{
assert
(
0
);
static
void
WriteLogW
(
const
wchar_t
*
format
,
...)
{
}
try
{
}
CLog
*
plog
=
CLog
::
GetInstance
();
if
(
plog
==
NULL
)
static
void
WriteLog
(
const
TCHAR
*
format
,
...)
{
return
;
try
{
CLocalLock
lock
(
&
m_cs
);
CLog
*
plog
=
CLog
::
GetInstance
();
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
if
(
plog
==
NULL
)
static
wchar_t
buf
[
MAX_OUTPUT_LEN
],
*
p
;
return
;
p
=
buf
;
CLocalLock
lock
(
&
m_cs
);
va_list
args
;
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
va_start
(
args
,
format
);
static
TCHAR
buf
[
MAX_OUTPUT_LEN
],
output
[
MAX_OUTPUT_LEN
],
*
p
;
p
+=
_vsnwprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
p
=
buf
;
va_end
(
args
);
va_list
args
;
while
(
(
p
>
buf
)
&&
iswspace
(
*
(
p
-
1
))
)
va_start
(
args
,
format
);
*--
p
=
'\0'
;
p
+=
_vsntprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
*
p
++
=
'\r'
;
va_end
(
args
);
*
p
++
=
'\n'
;
while
(
(
p
>
buf
)
&&
_istspace
(
*
(
p
-
1
))
)
*
p
=
'\0'
;
*--
p
=
_T
(
'\0'
);
#if defined(_UNICODE) || defined(UNICODE)
*
p
++
=
_T
(
'\r'
);
wchar_t
output
[
MAX_OUTPUT_LEN
];
*
p
++
=
_T
(
'\n'
);
plog
->
FormatBuf
(
buf
,
output
);
*
p
=
_T
(
'\0'
);
plog
->
Output
(
output
);
plog
->
FormatBuf
(
buf
,
output
);
#else
plog
->
Output
(
output
);
char
output
[
MAX_OUTPUT_LEN
];
}
if
(
Utf16ToAnsiUseCharArray
(
buf
,
m_ansiBuf
,
MAX_OUTPUT_LEN
)){
}
plog
->
FormatBuf
(
m_ansiBuf
,
output
);
catch
(...){
plog
->
Output
(
output
);
assert
(
0
);
}
else
{
}
char
*
ansiOut
=
Utf16ToAnsi
(
buf
);
}
plog
->
FormatBuf
(
ansiOut
,
output
);
plog
->
Output
(
output
);
static
void
WriteLogW
(
const
wchar_t
*
format
,
...)
{
SAFEDELETEARR
(
ansiOut
);
try
{
}
CLog
*
plog
=
CLog
::
GetInstance
();
#endif
if
(
plog
==
NULL
)
}
return
;
}
CLocalLock
lock
(
&
m_cs
);
catch
(...){
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
assert
(
0
);
static
wchar_t
buf
[
MAX_OUTPUT_LEN
],
*
p
;
}
p
=
buf
;
}
va_list
args
;
va_start
(
args
,
format
);
static
void
WriteLogA
(
const
char
*
format
,
...)
{
p
+=
_vsnwprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
try
{
va_end
(
args
);
CLog
*
plog
=
CLog
::
GetInstance
();
while
(
(
p
>
buf
)
&&
iswspace
(
*
(
p
-
1
))
)
if
(
plog
==
NULL
)
*--
p
=
'\0'
;
return
;
*
p
++
=
'\r'
;
CLocalLock
lock
(
&
m_cs
);
*
p
++
=
'\n'
;
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
*
p
=
'\0'
;
static
char
buf
[
MAX_OUTPUT_LEN
],
*
p
;
#if defined(_UNICODE) || defined(UNICODE)
p
=
buf
;
wchar_t
output
[
MAX_OUTPUT_LEN
];
va_list
args
;
plog
->
FormatBuf
(
buf
,
output
);
va_start
(
args
,
format
);
plog
->
Output
(
output
);
p
+=
_vsnprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
#else
va_end
(
args
);
char
output
[
MAX_OUTPUT_LEN
];
while
(
(
p
>
buf
)
&&
isspace
(
*
(
p
-
1
))
)
if
(
Utf16ToAnsiUseCharArray
(
buf
,
m_ansiBuf
,
MAX_OUTPUT_LEN
)){
*--
p
=
'\0'
;
plog
->
FormatBuf
(
m_ansiBuf
,
output
);
*
p
++
=
'\r'
;
plog
->
Output
(
output
);
*
p
++
=
'\n'
;
}
else
{
*
p
=
'\0'
;
char
*
ansiOut
=
Utf16ToAnsi
(
buf
);
#if defined(_UNICODE) || defined(UNICODE)
plog
->
FormatBuf
(
ansiOut
,
output
);
wchar_t
output
[
MAX_OUTPUT_LEN
];
plog
->
Output
(
output
);
if
(
AnsiToUtf16Array
(
buf
,
m_utf16Buf
,
MAX_OUTPUT_LEN
))
SAFEDELETEARR
(
ansiOut
);
{
}
plog
->
FormatBuf
(
m_utf16Buf
,
output
);
#endif
plog
->
Output
(
output
);
}
}
}
else
catch
(...){
{
assert
(
0
);
wchar_t
*
wBuf
=
AnsiToUtf16
(
buf
);
}
plog
->
FormatBuf
(
wBuf
,
output
);
}
plog
->
Output
(
output
);
SAFEDELETEARR
(
wBuf
);
static
void
WriteLogA
(
const
char
*
format
,
...)
{
}
try
{
#else
CLog
*
plog
=
CLog
::
GetInstance
();
char
output
[
MAX_OUTPUT_LEN
];
if
(
plog
==
NULL
)
plog
->
FormatBuf
(
buf
,
output
);
return
;
plog
->
Output
(
output
);
CLocalLock
lock
(
&
m_cs
);
#endif
if
(
plog
->
m_bOutputLogFile
||
plog
->
m_bOutputDbgView
||
plog
->
m_bOutputConsole
)
{
}
static
char
buf
[
MAX_OUTPUT_LEN
],
*
p
;
}
p
=
buf
;
catch
(...){
va_list
args
;
assert
(
0
);
va_start
(
args
,
format
);
}
p
+=
_vsnprintf_s
(
p
,
sizeof
(
buf
)
-
1
,
sizeof
(
buf
)
-
1
,
format
,
args
);
}
va_end
(
args
);
while
(
(
p
>
buf
)
&&
isspace
(
*
(
p
-
1
))
)
protected
:
*--
p
=
'\0'
;
CLog
()
:
m_pLogFile
(
NULL
),
m_bOutputLogFile
(
FALSE
),
m_bOutputDbgView
(
FALSE
),
*
p
++
=
'\r'
;
m_bOutputConsole
(
FALSE
),
m_bConsoleOpened
(
FALSE
),
m_bDbgviewOpened
(
FALSE
),
*
p
++
=
'\n'
;
m_bLogFileOpened
(
FALSE
),
m_bRunning
(
TRUE
){
*
p
=
'\0'
;
QueryPerformanceFrequency
(
&
freq
);
#if defined(_UNICODE) || defined(UNICODE)
//OutputDebugString(_T("The frequency of your pc is 0x%X.\n"), freq.QuadPart);
wchar_t
output
[
MAX_OUTPUT_LEN
];
QueryPerformanceCounter
(
&
start_t
);
if
(
AnsiToUtf16Array
(
buf
,
m_utf16Buf
,
MAX_OUTPUT_LEN
))
memset
(
g_szFileName
,
0
,
sizeof
(
g_szFileName
));
{
TCHAR
out
[
128
]
=
{
0
};
plog
->
FormatBuf
(
m_utf16Buf
,
output
);
wsprintf
(
out
,
_T
(
"CLog construction addr: %p
\n
"
),
this
);
plog
->
Output
(
output
);
OutputDebugString
(
out
);
}
memset
(
&
pi
,
0
,
sizeof
(
pi
));
else
InitializeCriticalSection
(
&
m_cs
);
{
}
wchar_t
*
wBuf
=
AnsiToUtf16
(
buf
);
plog
->
FormatBuf
(
wBuf
,
output
);
LPCTSTR
GetAppRunTime
()
{
plog
->
Output
(
output
);
static
TCHAR
szTime
[
128
];
SAFEDELETEARR
(
wBuf
);
memset
(
szTime
,
0
,
sizeof
szTime
);
}
#else
QueryPerformanceCounter
(
&
stop_t
);
char
output
[
MAX_OUTPUT_LEN
];
exe_time
=
1e3
*
(
stop_t
.
QuadPart
-
start_t
.
QuadPart
)
/
freq
.
QuadPart
;
plog
->
FormatBuf
(
buf
,
output
);
WORD
day
,
hour
,
min
,
sec
;
plog
->
Output
(
output
);
#endif
sec
=
static_cast
<
unsigned
short
>
(
static_cast
<
unsigned
long
>
(
exe_time
/
1000
)
%
60
);
}
min
=
static_cast
<
unsigned
short
>
(
static_cast
<
unsigned
long
>
(
exe_time
/
1000
/
60
)
%
60
);
}
hour
=
static_cast
<
unsigned
short
>
((
exe_time
/
1000
/
60
)
/
60
);
catch
(...){
day
=
static_cast
<
unsigned
short
>
(
exe_time
/
1000
.
0
/
60
.
0
/
60
.
0
/
24
);
assert
(
0
);
}
double
ms
=
exe_time
-
(
int
)(
exe_time
)
+
((
int
)(
exe_time
)
%
1000
);
}
wsprintf
(
szTime
,
_T
(
"%dday%02d:%02d:%02ds.%3.6fms"
),
day
,
hour
,
min
,
sec
,
ms
);
return
szTime
;
protected
:
}
CLog
()
:
m_pLogFile
(
NULL
),
m_bOutputLogFile
(
FALSE
),
m_bOutputDbgView
(
FALSE
),
m_bOutputConsole
(
FALSE
),
m_bConsoleOpened
(
FALSE
),
m_bDbgviewOpened
(
FALSE
),
void
Output
(
const
TCHAR
*
out
)
{
m_bLogFileOpened
(
FALSE
),
m_bRunning
(
TRUE
){
if
(
m_bOutputLogFile
){
QueryPerformanceFrequency
(
&
freq
);
OutputFile
(
out
);
//OutputDebugString(_T("The frequency of your pc is 0x%X.\n"), freq.QuadPart);
}
QueryPerformanceCounter
(
&
start_t
);
#ifdef _DEBUG
memset
(
g_szFileName
,
0
,
sizeof
(
g_szFileName
));
OutputDebugString
(
out
);
TCHAR
out
[
128
]
=
{
0
};
#else
wsprintf
(
out
,
_T
(
"CLog construction addr: %p
\n
"
),
this
);
if
(
m_bOutputDbgView
){
OutputDebugString
(
out
);
OutputDebugString
(
out
);
memset
(
&
pi
,
0
,
sizeof
(
pi
));
}
InitializeCriticalSection
(
&
m_cs
);
#endif
}
if
(
m_bOutputConsole
){
_tprintf
(
out
);
LPCTSTR
GetAppRunTime
()
{
}
static
TCHAR
szTime
[
128
];
}
memset
(
szTime
,
0
,
sizeof
szTime
);
WORD
FormatBuf
(
const
TCHAR
*
oldBuf
,
TCHAR
*
newBuf
,
WORD
max_new_buff_len
=
MAX_OUTPUT_LEN
){
QueryPerformanceCounter
(
&
stop_t
);
static
SYSTEMTIME
st
;
exe_time
=
1e3
*
(
stop_t
.
QuadPart
-
start_t
.
QuadPart
)
/
freq
.
QuadPart
;
static
TCHAR
sztime
[
128
];
WORD
day
,
hour
,
min
,
sec
;
memset
(
sztime
,
0
,
sizeof
sztime
);
GetLocalTime
(
&
st
);
sec
=
static_cast
<
unsigned
short
>
(
static_cast
<
unsigned
long
>
(
exe_time
/
1000
)
%
60
);
wsprintf
(
sztime
,
_T
(
"HB %04d-%02d-%02d-w%d %02d:%02d:%02d.%03d ---- "
),
min
=
static_cast
<
unsigned
short
>
(
static_cast
<
unsigned
long
>
(
exe_time
/
1000
/
60
)
%
60
);
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
hour
=
static_cast
<
unsigned
short
>
((
exe_time
/
1000
/
60
)
/
60
);
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
,
st
.
wMilliseconds
);
day
=
static_cast
<
unsigned
short
>
(
exe_time
/
1000
.
0
/
60
.
0
/
60
.
0
/
24
);
lstrcpy
(
newBuf
,
sztime
);
if
(
lstrlen
(
sztime
)
+
lstrlen
(
oldBuf
)
<
max_new_buff_len
){
double
ms
=
exe_time
-
(
int
)(
exe_time
)
+
((
int
)(
exe_time
)
%
1000
);
lstrcat
(
newBuf
,
oldBuf
);
wsprintf
(
szTime
,
_T
(
"%dday%02d:%02d:%02ds.%3.6fms"
),
day
,
hour
,
min
,
sec
,
ms
);
}
return
szTime
;
return
static_cast
<
unsigned
short
>
(
lstrlen
(
newBuf
));
}
}
void
Output
(
const
TCHAR
*
out
)
{
void
OutputFile
(
const
TCHAR
*
buf
){
if
(
m_bOutputLogFile
){
if
(
m_pLogFile
==
NULL
)
OutputFile
(
out
);
m_pLogFile
=
OpenLogFile
();
}
if
(
m_pLogFile
!=
NULL
){
#ifdef _DEBUG
fseek
(
m_pLogFile
,
0
,
SEEK_END
);
OutputDebugString
(
out
);
long
filelen
=
ftell
(
m_pLogFile
);
#else
if
(
filelen
>=
MAX_FILE_LEN
){
if
(
m_bOutputDbgView
){
fclose
(
m_pLogFile
);
OutputDebugString
(
out
);
CreateFileName
();
}
m_pLogFile
=
OpenLogFile
();
#endif
}
if
(
m_bOutputConsole
){
}
_tprintf
(
out
);
if
(
m_pLogFile
!=
NULL
){
}
fseek
(
m_pLogFile
,
0
,
SEEK_END
);
}
#if defined(_UNICODE) || defined(UNICODE)
if
(
Utf16ToAnsiUseCharArray
(
buf
,
m_ansiBuf
,
MAX_OUTPUT_LEN
)){
WORD
FormatBuf
(
const
TCHAR
*
oldBuf
,
TCHAR
*
newBuf
,
WORD
max_new_buff_len
=
MAX_OUTPUT_LEN
){
fwrite
(
m_ansiBuf
,
1
,
strlen
(
m_ansiBuf
),
m_pLogFile
);
static
SYSTEMTIME
st
;
}
else
{
static
TCHAR
sztime
[
128
];
char
*
ansiOut
=
Utf16ToAnsi
(
buf
);
memset
(
sztime
,
0
,
sizeof
sztime
);
fwrite
(
ansiOut
,
1
,
strlen
(
ansiOut
),
m_pLogFile
);
GetLocalTime
(
&
st
);
SAFEDELETEARR
(
ansiOut
);
wsprintf
(
sztime
,
_T
(
"HB %04d-%02d-%02d-w%d %02d:%02d:%02d.%03d ---- "
),
}
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
#else
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
,
st
.
wMilliseconds
);
fwrite
(
buf
,
1
,
strlen
(
buf
),
m_pLogFile
);
lstrcpy
(
newBuf
,
sztime
);
#endif
if
(
lstrlen
(
sztime
)
+
lstrlen
(
oldBuf
)
<
max_new_buff_len
){
fflush
(
m_pLogFile
);
lstrcat
(
newBuf
,
oldBuf
);
}
}
}
return
static_cast
<
unsigned
short
>
(
lstrlen
(
newBuf
));
}
BOOL
OpenDbgview
(
BOOL
bOpen
){
static
STARTUPINFO
si
;
void
OutputFile
(
const
TCHAR
*
buf
){
static
LPCTSTR
lpszAppName
=
_T
(
"dbgview.exe"
);
if
(
m_pLogFile
==
NULL
)
if
(
bOpen
){
m_pLogFile
=
OpenLogFile
();
if
(
!
m_bDbgviewOpened
){
if
(
m_pLogFile
!=
NULL
){
memset
(
&
si
,
0
,
sizeof
(
STARTUPINFO
));
fseek
(
m_pLogFile
,
0
,
SEEK_END
);
si
.
cb
=
sizeof
(
STARTUPINFO
);
long
filelen
=
ftell
(
m_pLogFile
);
si
.
dwFlags
=
STARTF_USESHOWWINDOW
;
if
(
filelen
>=
MAX_FILE_LEN
){
si
.
wShowWindow
=
SW_SHOW
;
fclose
(
m_pLogFile
);
if
(
CreateProcess
(
lpszAppName
,
NULL
,
NULL
,
NULL
,
FALSE
,
0
,
NULL
,
NULL
,
&
si
,
&
pi
))
{
CreateFileName
();
m_bDbgviewOpened
=
TRUE
;
m_pLogFile
=
OpenLogFile
();
}
}
}
}
}
else
if
(
m_bDbgviewOpened
){
if
(
m_pLogFile
!=
NULL
){
CloseHandle
(
pi
.
hThread
);
fseek
(
m_pLogFile
,
0
,
SEEK_END
);
CloseHandle
(
pi
.
hProcess
);
#if defined(_UNICODE) || defined(UNICODE)
m_bDbgviewOpened
=
FALSE
;
if
(
Utf16ToAnsiUseCharArray
(
buf
,
m_ansiBuf
,
MAX_OUTPUT_LEN
)){
}
fwrite
(
m_ansiBuf
,
1
,
strlen
(
m_ansiBuf
),
m_pLogFile
);
}
else
{
return
bOpen
?
m_bDbgviewOpened
:
!
m_bDbgviewOpened
;
char
*
ansiOut
=
Utf16ToAnsi
(
buf
);
}
fwrite
(
ansiOut
,
1
,
strlen
(
ansiOut
),
m_pLogFile
);
SAFEDELETEARR
(
ansiOut
);
FILE
*
OpenLogFile
(){
}
FILE
*
pfile
=
NULL
;
#else
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"r"
));
fwrite
(
buf
,
1
,
strlen
(
buf
),
m_pLogFile
);
if
(
pfile
==
NULL
){
#endif
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"wb"
));
fflush
(
m_pLogFile
);
if
(
pfile
==
NULL
){
}
MessageBox
(
NULL
,
_T
(
"Create log file failed."
),
NULL
,
0
);
}
ASSERT
(
0
);
return
NULL
;
BOOL
OpenDbgview
(
BOOL
bOpen
){
}
static
STARTUPINFO
si
;
}
else
{
static
LPCTSTR
lpszAppName
=
_T
(
"dbgview.exe"
);
fclose
(
pfile
);
if
(
bOpen
){
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"ab"
));
if
(
!
m_bDbgviewOpened
){
if
(
pfile
==
NULL
){
memset
(
&
si
,
0
,
sizeof
(
STARTUPINFO
));
MessageBox
(
NULL
,
_T
(
"Open log file failed."
),
NULL
,
0
);
si
.
cb
=
sizeof
(
STARTUPINFO
);
ASSERT
(
0
);
si
.
dwFlags
=
STARTF_USESHOWWINDOW
;
return
NULL
;
si
.
wShowWindow
=
SW_SHOW
;
}
if
(
CreateProcess
(
lpszAppName
,
NULL
,
NULL
,
NULL
,
FALSE
,
0
,
NULL
,
NULL
,
&
si
,
&
pi
))
{
}
m_bDbgviewOpened
=
TRUE
;
if
(
pfile
!=
NULL
)
}
m_bLogFileOpened
=
TRUE
;
}
return
pfile
;
}
else
if
(
m_bDbgviewOpened
){
}
CloseHandle
(
pi
.
hThread
);
CloseHandle
(
pi
.
hProcess
);
void
CreateFileName
(){
m_bDbgviewOpened
=
FALSE
;
SYSTEMTIME
st
;
}
GetLocalTime
(
&
st
);
const
wchar_t
*
path
=
GetPrivateLogPath
();
return
bOpen
?
m_bDbgviewOpened
:
!
m_bDbgviewOpened
;
CreateDirectory
(
path
,
NULL
);
}
wchar_t
exe
[
1024
]
=
{
0
};
GetModuleFileName
(
NULL
,
exe
,
1023
);
FILE
*
OpenLogFile
(){
wsprintf
(
g_szFileName
,
_T
(
"%s
\\
%s-%04d-%02d-%02d-w%d-%02d-%02d-%02d"
),
FILE
*
pfile
=
NULL
;
path
,
CFileOper
::
GetFileNameFromPathName
(
exe
),
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"r"
));
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
if
(
pfile
==
NULL
){
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"wb"
));
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
);
if
(
pfile
==
NULL
){
lstrcat
(
g_szFileName
,
_T
(
".log"
));
MessageBox
(
NULL
,
_T
(
"Create log file failed."
),
NULL
,
0
);
}
ASSERT
(
0
);
return
NULL
;
void
CloseLogFile
(){
}
if
(
m_pLogFile
!=
NULL
){
}
else
{
fflush
(
m_pLogFile
);
fclose
(
pfile
);
fclose
(
m_pLogFile
);
_tfopen_s
(
&
pfile
,
g_szFileName
,
_T
(
"ab"
));
m_pLogFile
=
NULL
;
if
(
pfile
==
NULL
){
}
MessageBox
(
NULL
,
_T
(
"Open log file failed."
),
NULL
,
0
);
}
ASSERT
(
0
);
return
NULL
;
BOOL
OpenConsole
(
BOOL
bOpen
){
}
static
FILE
*
pConsole
=
NULL
;
}
if
(
bOpen
){
if
(
pfile
!=
NULL
)
if
(
!
m_bConsoleOpened
){
m_bLogFileOpened
=
TRUE
;
if
(
AllocConsole
()){
return
pfile
;
SetConsoleTitle
(
_T
(
"output"
));
}
/*COORD coord;
coord.X = 120;
void
CreateFileName
(){
coord.Y = 60;
SYSTEMTIME
st
;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetLocalTime
(
&
st
);
FillConsoleOutputAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
const
wchar_t
*
path
=
GetPrivateLogPath
();
120, coord, NULL);
CreateDirectory
(
path
,
NULL
);
SetConsoleScreenBufferSize(hStdOut, coord);
wchar_t
exe
[
1024
]
=
{
0
};
SMALL_RECT se;
GetModuleFileName
(
NULL
,
exe
,
1023
);
se.Left = 0;
wsprintf
(
g_szFileName
,
_T
(
"%s
\\
%s-%04d-%02d-%02d-w%d-%02d-%02d-%02d"
),
se.Top = 0;
path
,
CFileOper
::
GetFileNameFromPathName
(
exe
),
se.Right = 100;
st
.
wYear
,
st
.
wMonth
,
st
.
wDay
,
se.Bottom = 100;
(
st
.
wDayOfWeek
!=
0
)
?
st
.
wDayOfWeek
:
7
,
SetConsoleWindowInfo(hStdOut, FALSE, &se);
st
.
wHour
,
st
.
wMinute
,
st
.
wSecond
);
*/
lstrcat
(
g_szFileName
,
_T
(
".log"
));
_tfreopen_s
(
&
pConsole
,
_T
(
"CONOUT$"
),
_T
(
"w"
),
stdout
);
}
m_bConsoleOpened
=
TRUE
;
}
void
CloseLogFile
(){
}
if
(
m_pLogFile
!=
NULL
){
return
m_bConsoleOpened
;
fflush
(
m_pLogFile
);
}
else
{
fclose
(
m_pLogFile
);
if
(
m_bConsoleOpened
){
m_pLogFile
=
NULL
;
fclose
(
pConsole
);
}
if
(
FreeConsole
()){
}
m_bConsoleOpened
=
FALSE
;
}
BOOL
OpenConsole
(
BOOL
bOpen
){
}
static
FILE
*
pConsole
=
NULL
;
return
!
m_bConsoleOpened
;
if
(
bOpen
){
}
if
(
!
m_bConsoleOpened
){
}
if
(
AllocConsole
()){
SetConsoleTitle
(
_T
(
"output"
));
LPCTSTR
GetLastTimeLogFileName
(){
/*COORD coord;
return
g_szFileName
;
coord.X = 120;
}
coord.Y = 60;
};
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
FillConsoleOutputAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
//CRITICAL_SECTION CLog::m_cs;
120, coord, NULL);
//int CLog::m_nRef = 0;
SetConsoleScreenBufferSize(hStdOut, coord);
//char CLog::m_ansiBuf[MAX_OUTPUT_LEN] = {0};
SMALL_RECT se;
//CLock CLog::m_lockForSingleton;
se.Left = 0;
se.Top = 0;
#endif // !defined(AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_)
se.Right = 100;
se.Bottom = 100;
SetConsoleWindowInfo(hStdOut, FALSE, &se);
*/
_tfreopen_s
(
&
pConsole
,
_T
(
"CONOUT$"
),
_T
(
"w"
),
stdout
);
m_bConsoleOpened
=
TRUE
;
}
}
return
m_bConsoleOpened
;
}
else
{
if
(
m_bConsoleOpened
){
fclose
(
pConsole
);
if
(
FreeConsole
()){
m_bConsoleOpened
=
FALSE
;
}
}
return
!
m_bConsoleOpened
;
}
}
LPCTSTR
GetLastTimeLogFileName
(){
return
g_szFileName
;
}
};
//CRITICAL_SECTION CLog::m_cs;
//int CLog::m_nRef = 0;
//char CLog::m_ansiBuf[MAX_OUTPUT_LEN] = {0};
//CLock CLog::m_lockForSingleton;
#endif // !defined(AFX_ERRORLOG_H__46D664F1_E737_46B7_9813_2EF1415FF884__INCLUDED_)
global.h
View file @
c180f138
...
@@ -9,6 +9,7 @@
...
@@ -9,6 +9,7 @@
#define LOG CLog::WriteLog
#define LOG CLog::WriteLog
#define LOGA CLog::WriteLogA
#define LOGA CLog::WriteLogA
#define LOGW CLog::WriteLogW
#define LOGW CLog::WriteLogW
#define LOGB(b, l) CLog::Dump(b, l)
class
LogFunction
{
class
LogFunction
{
private
:
private
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment