-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugMessage.cpp
More file actions
153 lines (135 loc) · 3.06 KB
/
DebugMessage.cpp
File metadata and controls
153 lines (135 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*-----------------------------------------------------------------------------
* DebugMessage.h
*-----------------------------------------------------------------------------
*
*-----------------------------------------------------------------------------
* All rights reserved by somma (fixbrain@gmail.com, unsorted@msn.com)
*-----------------------------------------------------------------------------
* - 06.06.2010 created
**---------------------------------------------------------------------------*/
#include "stdafx.h"
#include "DebugMessage.h"
#include <crtdbg.h>
#include <strsafe.h>
PCH get_filename_part(IN PCH full_path, IN DWORD length);
/**----------------------------------------------------------------------------
\brief
\param
\return
\code
\endcode
-----------------------------------------------------------------------------*/
void
DebugMessage(
const char* Function,
int Line,
const char* LogPrefix,
const char* fmt,
...
)
{
_ASSERTE(NULL != LogPrefix); if (NULL == LogPrefix) return;
_ASSERTE(NULL != fmt); if (NULL == fmt) return;
char log_buffer[4096] = { 0 };
size_t remain = sizeof(log_buffer);
char* pos = log_buffer;
va_list args;
// 프로세스 정보
//
char name_buffer[MAX_PATH + 1] = { 0 };
char *name = name_buffer;
name_buffer[0] = '\0';
DWORD len = GetModuleFileNameA(NULL, name_buffer, sizeof(name_buffer));
if (len>0) name = get_filename_part(name_buffer, len);
// format string 파라미터를 Multibyte / WideChar 가 잘 못 들어온 경우등
// 에러가 발생할 수 있어서 해당 함수를 찾기위한 디버그 코드
// e.g. %s => L"debug string...." or %S => "debug string"
//
size_t remain_b = remain;
char* pos_b = pos;
HRESULT hRes = StringCbPrintfExA(
pos,
remain,
&pos,
&remain,
0,
"%-18s(%04u:%04u), %s %s() ",
name,
GetCurrentProcessId(),
GetCurrentThreadId(),
LogPrefix,
Function
);
if (S_OK != hRes)
{
OutputDebugStringA("StringCbPrintfExA() failed, more log buffer needed...?");
return;
}
remain_b = remain;
pos_b = pos;
va_start(args, fmt);
hRes = StringCbVPrintfExA(
pos,
remain,
&pos,
&remain,
0,
fmt,
args
);
if (S_OK != hRes)
{
// 한글 같은 unicode 문자열이 있는 경우 발생할 수 있음
//
StringCbPrintfExA(
pos_b,
remain_b,
&pos_b,
&remain_b,
0,
"invalid function call parameters, line=%d",
Line
);
remain = remain_b;
pos = pos_b;
}
va_end(args);
hRes = StringCbPrintfExA(
pos,
remain,
&pos,
&remain,
0,
"%s",
"\r\n"
);
if (!SUCCEEDED(hRes))
{
OutputDebugStringA("StringCbPrintfExA() failed, more log buffer needed...?");
return;
}
OutputDebugStringA(log_buffer);
}
/**----------------------------------------------------------------------------
\brief
\param
\return
\code
\endcode
-----------------------------------------------------------------------------*/
PCH
get_filename_part(
IN PCH full_path,
IN DWORD length
)
{
PCH p = NULL;
if (length == 0) return NULL;
p = &full_path[length];
while (p >= full_path)
{
if (*p == '\\') return p + 1;
--p;
}
return NULL;
}