source: libcf/trunk/include/cf_debug.h@ 50

Last change on this file since 50 was 50, checked in by cheese, 11 years ago

#1 fix preprocessor definition for windows

File size: 5.3 KB
Line 
1/**
2 * @file cf_debug.h
3 * @author myusgun <myusgun@gmail.com>
4 * @version 0.1
5 *
6 * @remark
7 * 디버그 함수를 직접 호출하지 않고 정의된 매크로를 사용하면,
8 * Preprocessor에 _DEBUG가 정의되어 있는 경우에 만
9 * 디버그 코드가 동작하도록 할 수 있음 <br />
10 * 디버그 메시지는 stdout 및 stderr를 이용한 파일 포인터 출력을 지원하고,
11 * 환경에 적합하도록 사용자가 구성한 컨텍스트를 이용하여 사용할 수도 있음 <br />
12 * (단, 콜스택의 푸시/팝은 컨텍스트를 이용해야만 사용 가능)
13 *
14 * @example debug.c
15 */
16#ifndef __CF_DEBUG_H__
17#define __CF_DEBUG_H__
18
19#include "cf_base.h"
20
21#include <stdio.h>
22
23/** Windows 함수 이름 매크로 재정의 */
24#if defined(_WIN32) || defined(_WIN64)
25# define __func__ __FUNCTION__
26#endif
27
28#ifdef _DEBUG
29/**
30 * 디버그 컨텍스트 생성
31 *
32 * @see CF_Debug_CreateCtx
33 */
34# define CF_DEBUG_CREATE_CTX() \
35 CF_Debug_CreateCtx ()
36
37/**
38 * 디버그 컨텍스트 해제
39 *
40 * @param __ctx 디버그 컨텍스트
41 *
42 * @see CF_Debug_DestroyCtx
43 */
44# define CF_DEBUG_DESTROY_CTX(__ctx) \
45 CF_Debug_DestroyCtx (__ctx)
46
47/**
48 * 디버그 메시지를 지정된 파일 포인터로 출력
49 *
50 * @param __fp 파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
51 * @param __fmt 포맷 스트링
52 * @param ... 가변 인자
53 *
54 * @see CF_Debug_Print
55 */
56# define CF_DEBUG_PRINT(__fp,__fmt,...) \
57 CF_Debug_Print (__fp,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__)
58
59/**
60 * 바이너리 데이터를 디버그 메시지와 함께 지정된 파일 포인터로 출력
61 *
62 * @param __fp 파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
63 * @param __bin 바이너리 데이터
64 * @param __len 바이너리 길이
65 * @param __fmt 포맷 스트링
66 * @param ... 가변 인자
67 *
68 * @see CF_Debug_PrintBin
69 */
70# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...) \
71 CF_Debug_PrintBin (__fp,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__)
72
73/**
74 * 컨텍스트를 업데이트하고 디버그 메시지를 출력
75 *
76 * @param __ctx 디버그 컨텍스트
77 * @param __fmt 포맷 스트링
78 * @param ... 가변 인자
79 *
80 * @see CF_Debug_Trace
81 */
82# define CF_DEBUG_TRACE(__ctx,__fmt,...) \
83 CF_Debug_Trace (__ctx,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__)
84
85/**
86 * 컨텍스트를 업데이트하고 바이너리 데이터를 디버그 메시지와 함께 출력
87 *
88 * @param __ctx 디버그 컨텍스트
89 * @param __bin 바이너리 데이터
90 * @param __len 바이너리 길이
91 * @param __fmt 포맷 스트링
92 * @param ... 가변 인자
93 *
94 * @see CF_Debug_TraceBin
95 */
96# define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...) \
97 CF_Debug_TraceBin (__ctx,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__)
98
99/**
100 * 컨텍스트에 콜스택 푸시
101 *
102 * @param __ctx 디버그 컨텍스트
103 *
104 * @see CF_Debug_CallStackPush
105 */
106# define CF_DEBUG_CALLSTACK_PUSH(__ctx) \
107 CF_Debug_CallStackPush (__ctx,__FILE__,__func__,__LINE__)
108
109/**
110 * 컨텍스트에서 콜스택 팝
111 *
112 * @param __ctx 디버그 컨텍스트
113 * @param __callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
114 *
115 * @see CF_Debug_CallStackPop, CF_Debug_CallStack
116 */
117# define CF_DEBUG_CALLSTACK_POP(__ctx,__callstack) \
118 CF_Debug_CallStackPop (__ctx,__callstack)
119#else // #ifdef _DEBUG
120# define CF_DEBUG_CREATE_CTX() NULL
121# define CF_DEBUG_DESTROY_CTX(__ctx)
122# define CF_DEBUG_PRINT(__fp,__fmt,...)
123# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)
124# define CF_DEBUG_TRACE(__ctx,__fmt,...)
125# define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...)
126# define CF_DEBUG_CALLSTACK_PUSH(__ctx)
127# define CF_DEBUG_CALLSTACK_POP(__ctx,__callstack) 1
128#endif // #ifdef _DEBUG
129
130/** 디버그 컨텍스트 */
131typedef void * CF_Debug_Ctx;
132
133/** 콜스택 데이터 */
134typedef struct cf_debug_callStack {
135 char file[NAME_LENGTH + 1]; /**< 파일 이름 */
136 char function[NAME_LENGTH + 1]; /**< 함수 이름 */
137 int line; /**< 라인 넘버 */
138} CF_Debug_CallStack;
139
140#ifdef __cplusplus
141extern "C" {
142#endif
143
144CF_EXPORT CF_Debug_Ctx
145CF_Debug_CreateCtx (void);
146
147CF_EXPORT int
148CF_Debug_DestroyCtx (CF_Debug_Ctx ctx);
149
150CF_EXPORT int
151CF_Debug_SetOutputFD (CF_Debug_Ctx ctx,
152 int fd);
153
154CF_EXPORT int
155CF_Debug_Print (FILE * fp,
156 const char * file,
157 const char * func,
158 const int line,
159 const char * fmt, ...);
160
161CF_EXPORT int
162CF_Debug_PrintBin (FILE * fp,
163 const char * file,
164 const char * func,
165 const int line,
166 const unsigned char * bin,
167 const int len,
168 const char * fmt, ...);
169
170CF_EXPORT int
171CF_Debug_Trace (CF_Debug_Ctx ctx,
172 const char * file,
173 const char * func,
174 const int line,
175 const char * fmt, ...);
176
177CF_EXPORT int
178CF_Debug_TraceBin (CF_Debug_Ctx ctx,
179 const char * file,
180 const char * func,
181 const int line,
182 const unsigned char * bin,
183 const int len,
184 const char * fmt, ...);
185
186CF_EXPORT int
187CF_Debug_CallStackPush (CF_Debug_Ctx ctx,
188 const char * file,
189 const char * func,
190 const int line);
191
192CF_EXPORT int
193CF_Debug_CallStackPop (CF_Debug_Ctx ctx,
194 CF_Debug_CallStack * callstack);
195
196#ifdef __cplusplus
197}
198#endif
199
200#endif // #ifndef __CF_DEBUG_H__
Note: See TracBrowser for help on using the repository browser.