Changeset 16 in libcf
- Timestamp:
- 02/01/13 09:37:19 (12 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_debug.h
r14 r16 6 6 7 7 #include "cf_base.h" 8 9 #include <stdio.h> 8 10 9 11 #define CF_ERROR_DEBUG_INVALID_CTX CF_ERROR_DEBUG - 1 … … 19 21 # define CF_UPDATE_CTX(__ctx) \ 20 22 CF_Debug_UpdateCtx (__ctx,__FILE__,__func__,__LINE__) 23 # define CF_DEBUG_PRINT(__fp,__fmt,...) \ 24 CF_Debug_Print (__fp,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__) 25 # define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...) \ 26 CF_Debug_PrintBin (__fp,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__) 21 27 # define CF_DEBUG_TRACE(__ctx,__fmt,...) \ 22 28 do { \ … … 63 69 64 70 CF_EXPORT int 71 CF_Debug_Print (FILE * fp, 72 const char * file, 73 const char * func, 74 const int line, 75 const char * fmt, ...); 76 77 int 78 CF_Debug_PrintBin (FILE * fp, 79 const char * file, 80 const char * func, 81 const int line, 82 const unsigned char * bin, 83 const int len, 84 const char * fmt, ...); 85 86 CF_EXPORT int 65 87 CF_Debug_UpdateCtx (CF_Debug_Ctx ctx, 66 88 const char * file, -
trunk/include/cf_file.h
r15 r16 16 16 #define CF_ERROR_FILE_GET_SIZE CF_ERROR_FILE - 7 17 17 18 #define RO O_RDONLY 19 #define WO O_WRONLY 20 #define WR O_RDWR 21 #define CR O_CREAT 22 #define TR O_TRUNC 18 typedef enum{ 19 CF_FILE_RO = O_RDONLY, 20 CF_FILE_WO = O_WRONLY, 21 CF_FILE_WR = O_RDWR, 22 CF_FILE_CR = O_CREAT, 23 CF_FILE_TR = O_TRUNC 24 } E_CF_FILE_FLAG, CF_FILE_FLAG; 23 25 24 26 #ifdef __cplusplus … … 27 29 28 30 CF_EXPORT int 29 CF_File_Open (const char * path,30 const intflag);31 CF_File_Open (const char * path, 32 const CF_FILE_FLAG flag); 31 33 32 34 CF_EXPORT int -
trunk/src/cf_debug.c
r14 r16 5 5 #include "cf_local.h" 6 6 7 #include <stdio.h>8 7 #include <stdlib.h> 9 8 #include <ctype.h> … … 50 49 51 50 int 52 CF_Debug_Local_Print (CF_DEBUG_CTX * ctx, 51 CF_Debug_Local_Print (FILE * fp, 52 const char * file, 53 const char * func, 54 const int line, 53 55 const char * fmt, 54 56 va_list valist) 55 57 { 56 fprintf (GET_CTX_OSTREAM (ctx), "[DEBUG][%s:%d][%s] ", 57 ctx->file, 58 ctx->line, 59 ctx->func); 60 vfprintf (GET_CTX_OSTREAM (ctx), fmt, valist); 61 62 return CF_OK; 63 } 64 65 CF_Debug_Ctx 66 CF_Debug_CreateCtx (void) 67 { 68 CF_DEBUG_CTX * ctx = NULL; 69 70 ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1); 71 72 return (CF_Debug_Ctx) ctx; 73 } 74 75 int 76 CF_Debug_DestroyCtx (CF_Debug_Ctx ctx) 77 { 78 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 79 CF_DEBUG_CALLSTACK * pop = NULL; 80 CF_DEBUG_CALLSTACK * next = NULL; 81 82 CHECK_INVALID_CTX (ctx); 83 84 if (context->fp != NULL) 85 fclose (context->fp); 86 87 for (pop = next = context->callstack.caller ; pop ; pop = next) 88 { 89 next = next->caller; 90 free (pop); 91 } 92 93 free (context); 94 95 return CF_OK; 96 } 97 98 int 99 CF_Debug_SetOutputFD (CF_Debug_Ctx ctx, 100 int fd) 101 { 102 int result = 0; 103 int dupfd = 0; 104 105 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 106 FILE * fp = NULL; 107 108 CHECK_INVALID_CTX (ctx); 109 110 TRY 111 { 112 dupfd = dup (fd); 113 if (dupfd < 0) 114 { 115 result = -1; 116 TRY_BREAK; 117 } 118 119 fp = fdopen (dupfd, "a+"); 120 if (fp == NULL) 121 { 122 close (dupfd); 123 result = -2; 124 TRY_BREAK; 125 } 126 127 context->fp = fp; 128 } 129 CATCH_IF (result < 0) 130 { 131 return CF_ERROR_DEBUG_SET_OUTPUT_FD; 132 } 133 134 return CF_OK; 135 } 136 137 int 138 CF_Debug_UpdateCtx (CF_Debug_Ctx ctx, 139 const char * file, 140 const char * func, 141 const int line) 142 { 143 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 144 145 CHECK_INVALID_CTX (ctx); 146 147 strncpy (context->file, file, strlen (file)); 148 strncpy (context->func, func, strlen (func)); 149 context->line = line; 150 151 return CF_OK; 152 } 153 154 int 155 CF_Debug_Trace (CF_Debug_Ctx ctx, 156 const char * fmt, ...) 157 { 158 va_list valist; 159 160 CHECK_INVALID_CTX (ctx); 161 162 va_start (valist, fmt); 163 CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist); 164 va_end (valist); 165 166 return CF_OK; 167 } 168 169 int 170 CF_Debug_TraceBin (CF_Debug_Ctx ctx, 171 const unsigned char * bin, 172 const int len, 173 const char * fmt, ...) 174 { 175 int i, j; 176 va_list valist; 177 FILE * fp = NULL; 178 179 CHECK_INVALID_CTX (ctx); 180 fp = GET_CTX_OSTREAM (ctx); 181 182 va_start (valist, fmt); 183 CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist); 58 fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func); 59 vfprintf (fp, fmt, valist); 60 61 return CF_OK; 62 } 63 64 int 65 CF_Debug_Local_PrintBin (FILE * fp, 66 const char * file, 67 const char * func, 68 const int line, 69 const unsigned char * bin, 70 const int len) 71 { 72 int i, j; 184 73 185 74 for (i = 0 ; i < len ; i += 16) 186 75 { 187 fprintf (fp, "[DEBUG][%s %s(%d)] ", __FILE__, __func__, __LINE__);76 fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func); 188 77 fprintf (fp, "%06x : ", i); 189 78 … … 208 97 } 209 98 99 CF_Debug_Ctx 100 CF_Debug_CreateCtx (void) 101 { 102 CF_DEBUG_CTX * ctx = NULL; 103 104 ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1); 105 106 return (CF_Debug_Ctx) ctx; 107 } 108 109 int 110 CF_Debug_DestroyCtx (CF_Debug_Ctx ctx) 111 { 112 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 113 CF_DEBUG_CALLSTACK * pop = NULL; 114 CF_DEBUG_CALLSTACK * next = NULL; 115 116 CHECK_INVALID_CTX (ctx); 117 118 if (context->fp != NULL) 119 fclose (context->fp); 120 121 for (pop = next = context->callstack.caller ; pop ; pop = next) 122 { 123 next = next->caller; 124 free (pop); 125 } 126 127 free (context); 128 129 return CF_OK; 130 } 131 132 int 133 CF_Debug_SetOutputFD (CF_Debug_Ctx ctx, 134 int fd) 135 { 136 int result = 0; 137 int dupfd = 0; 138 139 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 140 FILE * fp = NULL; 141 142 CHECK_INVALID_CTX (ctx); 143 144 TRY 145 { 146 dupfd = dup (fd); 147 if (dupfd < 0) 148 { 149 result = -1; 150 TRY_BREAK; 151 } 152 153 fp = fdopen (dupfd, "a+"); 154 if (fp == NULL) 155 { 156 close (dupfd); 157 result = -2; 158 TRY_BREAK; 159 } 160 161 context->fp = fp; 162 } 163 CATCH_IF (result < 0) 164 { 165 return CF_ERROR_DEBUG_SET_OUTPUT_FD; 166 } 167 168 return CF_OK; 169 } 170 171 int 172 CF_Debug_Print (FILE * fp, 173 const char * file, 174 const char * func, 175 const int line, 176 const char * fmt, ...) 177 { 178 va_list valist; 179 180 va_start (valist, fmt); 181 CF_Debug_Local_Print (fp, file, func, line, fmt, valist); 182 va_end (valist); 183 184 return CF_OK; 185 } 186 187 int 188 CF_Debug_PrintBin (FILE * fp, 189 const char * file, 190 const char * func, 191 const int line, 192 const unsigned char * bin, 193 const int len, 194 const char * fmt, ...) 195 { 196 va_list valist; 197 198 va_start (valist, fmt); 199 CF_Debug_Local_Print (fp, file, func, line, fmt, valist); 200 va_end (valist); 201 202 CF_Debug_Local_PrintBin (fp, file, func, line, bin, len); 203 204 return CF_OK; 205 } 206 207 int 208 CF_Debug_UpdateCtx (CF_Debug_Ctx ctx, 209 const char * file, 210 const char * func, 211 const int line) 212 { 213 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 214 215 CHECK_INVALID_CTX (ctx); 216 217 strncpy (context->file, file, strlen (file)); 218 strncpy (context->func, func, strlen (func)); 219 context->line = line; 220 221 return CF_OK; 222 } 223 224 int 225 CF_Debug_Trace (CF_Debug_Ctx ctx, 226 const char * fmt, ...) 227 { 228 va_list valist; 229 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 230 231 CHECK_INVALID_CTX (ctx); 232 233 va_start (valist, fmt); 234 CF_Debug_Local_Print (GET_CTX_OSTREAM (context), 235 context->file, 236 context->func, 237 context->line, 238 fmt, valist); 239 va_end (valist); 240 241 return CF_OK; 242 } 243 244 int 245 CF_Debug_TraceBin (CF_Debug_Ctx ctx, 246 const unsigned char * bin, 247 const int len, 248 const char * fmt, ...) 249 { 250 va_list valist; 251 FILE * fp = NULL; 252 CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx; 253 254 CHECK_INVALID_CTX (ctx); 255 256 va_start (valist, fmt); 257 CF_Debug_Local_Print (GET_CTX_OSTREAM (context), 258 context->file, 259 context->func, 260 context->line, 261 fmt, valist); 262 va_end (valist); 263 264 CF_Debug_Local_PrintBin (context->fp, 265 context->file, 266 context->func, 267 context->line, 268 bin, len); 269 270 return CF_OK; 271 } 272 210 273 int 211 274 CF_Debug_CallStackPush (CF_Debug_Ctx ctx, -
trunk/src/cf_file.c
r15 r16 25 25 26 26 int 27 CF_File_Open (const char * path,28 const intflag)27 CF_File_Open (const char * path, 28 const CF_FILE_FLAG flag) 29 29 { 30 30 int result = open (path, flag); … … 39 39 CF_File_Create (const char * path) 40 40 { 41 int result = open (path, C R|WO|TR, FILE_MODE);41 int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE); 42 42 43 43 if (result < 0) -
trunk/test/test.c
r15 r16 25 25 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); 26 26 if (gDebugCtx == NULL) 27 CF_DEBUG_ TRACE (gDebugCtx, "create debug ctx error\n");27 CF_DEBUG_PRINT (stderr, "create debug ctx error\n"); 28 28 29 29 for (i = 0 ; i < 10000 ; i++) … … 49 49 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 50 50 51 fd = CF_File_Open (file, RO);51 fd = CF_File_Open (file, CF_FILE_RO); 52 52 if (fd < 0) 53 53 CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n"); … … 101 101 102 102 if (gLogCtx == NULL) 103 CF_DEBUG_ TRACE (gDebugCtx, "create log ctx error\n");103 CF_DEBUG_PRINT (stderr, "create log ctx error\n"); 104 104 105 105 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 106 106 107 CF_DEBUG_ TRACE (gDebugCtx, " == LOG TEST ==\n");107 CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n"); 108 108 log_test ("LOG_TEST"); 109 109 110 CF_DEBUG_ TRACE (gDebugCtx, " == FILE READ TEST ==\n");110 CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n"); 111 111 file_test ("FILE_READ_TEST"); 112 112 113 CF_DEBUG_ TRACE (gDebugCtx, " == CALLSTACK TEST ==\n");113 CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n"); 114 114 callee1 (); 115 115 116 116 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); 117 117 118 CF_DEBUG_ TRACE (gDebugCtx, " == END OF TEST ==\n");118 CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n"); 119 119 CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n", 120 120 gDebugCallstack.file,
Note:
See TracChangeset
for help on using the changeset viewer.