- Timestamp:
- 02/01/13 12:48:28 (11 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_log.h
r12 r19 14 14 #define CF_ERROR_LOG_UNLOCK_CTX CF_ERROR_LOG - 6 15 15 #define CF_ERROR_LOG_FLUSH CF_ERROR_LOG - 7 16 #define CF_ERROR_LOG_INVALID_MAPID CF_ERROR_LOG - 8 17 #define CF_ERROR_LOG_NOT_MAPPED_ID CF_ERROR_LOG - 9 18 #define CF_ERROR_LOG_ALREADY_MAPPED_ID CF_ERROR_LOG - 10 19 #define CF_ERROR_LOG_NOT_INITIALIZE CF_ERROR_LOG - 11 16 20 17 21 #define CF_LOG_BUFFER_DEFAULT -1 18 22 #define CF_LOG_BUFFER_NO 0 23 24 #define CF_LOG_OPEN(__id,__file,__memsize) \ 25 CF_Log_MapCtxID (__id, CF_Log_CreateCtx (__file, __memsize)) 26 27 #define CF_LOG_WRITE(__id,__pf,__fmt,...) \ 28 CF_Log_Write (CF_Log_GetMappedCtx (__id),__pf,__fmt,##__VA_ARGS__) 29 30 #define CF_LOG_CLOSE CF_Log_UnmapCtxID 19 31 20 32 typedef void * CF_Log_Ctx; … … 35 47 36 48 CF_EXPORT int 49 CF_Log_DestroyCtx (CF_Log_Ctx ctx); 50 51 CF_EXPORT int 37 52 CF_Log_SetMultiThread (CF_Log_Ctx ctx); 38 53 39 54 CF_EXPORT int 40 55 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx); 41 42 CF_EXPORT int43 CF_Log_DestroyCtx (CF_Log_Ctx ctx);44 56 45 57 CF_EXPORT int … … 51 63 CF_Log_Flush (CF_Log_Ctx ctx); 52 64 65 CF_EXPORT int 66 CF_Log_MapCtxID (const int mapid, 67 const CF_Log_Ctx ctx); 68 69 CF_EXPORT int 70 CF_Log_UnmapCtxID (const int mapid); 71 72 CF_EXPORT CF_Log_Ctx 73 CF_Log_GetMappedCtx (const int mapid); 74 53 75 #ifdef __cplusplus 54 76 } -
trunk/src/cf_log.c
r14 r19 201 201 CF_Log_Finalize (void) 202 202 { 203 int mapid = 0; 204 205 for (mapid = 0 ; mapid < gLogEnvironment.ctxSize ; mapid++) 206 { 207 CF_Log_UnmapCtxID (mapid); 208 } 209 210 if (gLogEnvironment.ctxPool != NULL) 211 free (gLogEnvironment.ctxPool); 212 203 213 memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT)); 204 214 … … 257 267 258 268 int 259 CF_Log_SetMultiThread (CF_Log_Ctx ctx)260 {261 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;262 263 CHECK_INVALID_CTX (ctx);264 265 if (CF_Mutex_Create (&context->mutex) < 0)266 return CF_ERROR_LOG_SET_MULTITHREAD;267 268 return CF_OK;269 }270 271 int272 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)273 {274 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;275 276 CHECK_INVALID_CTX (ctx);277 278 if (CF_Mutex_Destory (&context->mutex) < 0)279 return CF_ERROR_LOG_UNSET_MULTITHREAD;280 281 return CF_OK;282 }283 284 int285 269 CF_Log_DestroyCtx (CF_Log_Ctx ctx) 286 270 { … … 313 297 314 298 int 299 CF_Log_SetMultiThread (CF_Log_Ctx ctx) 300 { 301 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 302 303 CHECK_INVALID_CTX (ctx); 304 305 if (CF_Mutex_Create (&context->mutex) < 0) 306 return CF_ERROR_LOG_SET_MULTITHREAD; 307 308 return CF_OK; 309 } 310 311 int 312 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx) 313 { 314 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 315 316 CHECK_INVALID_CTX (ctx); 317 318 if (CF_Mutex_Destory (&context->mutex) < 0) 319 return CF_ERROR_LOG_UNSET_MULTITHREAD; 320 321 return CF_OK; 322 } 323 324 int 315 325 CF_Log_Write (CF_Log_Ctx ctx, 316 326 const char * prefix, … … 373 383 return CF_OK; 374 384 } 385 386 int 387 CF_Log_MapCtxID (const int mapid, 388 const CF_Log_Ctx ctx) 389 { 390 if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0) 391 return CF_ERROR_LOG_NOT_INITIALIZE; 392 393 if (gLogEnvironment.ctxPool[mapid] != NULL) 394 return CF_ERROR_LOG_ALREADY_MAPPED_ID; 395 396 gLogEnvironment.ctxPool[mapid] = ctx; 397 398 return CF_OK; 399 } 400 401 int 402 CF_Log_UnmapCtxID (const int mapid) 403 { 404 if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0) 405 return CF_ERROR_LOG_NOT_INITIALIZE; 406 407 if (gLogEnvironment.ctxPool[mapid] == NULL) 408 return CF_ERROR_LOG_NOT_MAPPED_ID; 409 410 CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]); 411 412 free (gLogEnvironment.ctxPool[mapid]); 413 gLogEnvironment.ctxPool[mapid] = NULL; 414 415 return CF_OK; 416 } 417 418 CF_Log_Ctx 419 CF_Log_GetMappedCtx (const int mapid) 420 { 421 if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0) 422 return NULL; 423 424 return gLogEnvironment.ctxPool[mapid]; 425 } -
trunk/test/makefile
r14 r19 91 91 92 92 clean: dummy 93 rm -rf log.txt93 rm -rf *.txt 94 94 rm -rf $(TARGET_PATH) $(OBJ_PATH) 95 95 -
trunk/test/test.c
r16 r19 13 13 CF_Debug_CallStack gDebugCallstack; 14 14 15 CF_Log_Ctx gLogCtx;16 17 15 const char * file = "./log.txt"; 18 16 19 17 void log_test (const char * message) 20 18 { 21 int i = 0; 19 int i, j; 20 char idname[16] = {0x00,}; 21 CF_Log_Ctx gLogCtx; 22 22 23 23 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 24 24 25 CF_Log_Initialize (10); 26 25 27 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); 26 if (g DebugCtx == NULL)27 CF_DEBUG_PRINT (stderr, "create debug ctx error\n");28 if (gLogCtx == NULL) 29 CF_DEBUG_PRINT (stderr, "create log ctx error\n"); 28 30 29 31 for (i = 0 ; i < 10000 ; i++) 30 CF_Log_Write (gLogCtx, " TEST", "turn %d\n", i);32 CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i); 31 33 32 34 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); … … 39 41 40 42 CF_Log_DestroyCtx (gLogCtx); 43 44 /////////////////// 45 46 for (i = 0 ; i < 10 ; i++) 47 { 48 sprintf (idname, "logid%d.txt", i); 49 CF_LOG_OPEN (i, idname, CF_LOG_BUFFER_NO); 50 } 51 52 for (i = 0 ; i < 10 ; i++) 53 { 54 for (j = 0 ; j < 10000 ; j++) 55 CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j); 56 57 CF_LOG_CLOSE (i); 58 } 59 60 CF_Log_Finalize (); 41 61 } 42 62 … … 100 120 gDebugCtx = CF_Debug_CreateCtx (); 101 121 102 if (gLogCtx == NULL)103 CF_DEBUG_PRINT (stderr, "create log ctx error\n");104 105 122 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 106 123
Note:
See TracChangeset
for help on using the changeset viewer.