Changeset 19 in libcf


Ignore:
Timestamp:
02/01/13 12:48:28 (11 years ago)
Author:
cheese
Message:

#1 add logging function with mapped id

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_log.h

    r12 r19  
    1414#define CF_ERROR_LOG_UNLOCK_CTX         CF_ERROR_LOG - 6
    1515#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
    1620
    1721#define CF_LOG_BUFFER_DEFAULT           -1
    1822#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
    1931
    2032typedef void *  CF_Log_Ctx;
     
    3547
    3648CF_EXPORT int
     49CF_Log_DestroyCtx       (CF_Log_Ctx ctx);
     50
     51CF_EXPORT int
    3752CF_Log_SetMultiThread   (CF_Log_Ctx ctx);
    3853
    3954CF_EXPORT int
    4055CF_Log_UnsetMultiThread (CF_Log_Ctx ctx);
    41 
    42 CF_EXPORT int
    43 CF_Log_DestroyCtx       (CF_Log_Ctx ctx);
    4456
    4557CF_EXPORT int
     
    5163CF_Log_Flush            (CF_Log_Ctx ctx);
    5264
     65CF_EXPORT int
     66CF_Log_MapCtxID         (const int          mapid,
     67                         const CF_Log_Ctx   ctx);
     68
     69CF_EXPORT int
     70CF_Log_UnmapCtxID       (const int mapid);
     71
     72CF_EXPORT CF_Log_Ctx
     73CF_Log_GetMappedCtx     (const int mapid);
     74
    5375#ifdef __cplusplus
    5476}
  • trunk/src/cf_log.c

    r14 r19  
    201201CF_Log_Finalize (void)
    202202{
     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
    203213    memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT));
    204214
     
    257267
    258268int
    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 int
    272 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 int
    285269CF_Log_DestroyCtx (CF_Log_Ctx ctx)
    286270{
     
    313297
    314298int
     299CF_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
     311int
     312CF_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
     324int
    315325CF_Log_Write (CF_Log_Ctx    ctx,
    316326              const char    * prefix,
     
    373383    return CF_OK;
    374384}
     385
     386int
     387CF_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
     401int
     402CF_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
     418CF_Log_Ctx
     419CF_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  
    9191
    9292clean: dummy
    93     rm -rf log.txt
     93    rm -rf *.txt
    9494    rm -rf $(TARGET_PATH) $(OBJ_PATH)
    9595
  • trunk/test/test.c

    r16 r19  
    1313CF_Debug_CallStack  gDebugCallstack;
    1414
    15 CF_Log_Ctx          gLogCtx;
    16 
    1715const char * file = "./log.txt";
    1816
    1917void log_test (const char * message)
    2018{
    21     int i = 0;
     19    int         i, j;
     20    char        idname[16] = {0x00,};
     21    CF_Log_Ctx  gLogCtx;
    2222
    2323    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    2424
     25    CF_Log_Initialize (10);
     26
    2527    gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
    26     if (gDebugCtx == 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");
    2830
    2931    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);
    3133
    3234    CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
     
    3941
    4042    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 ();
    4161}
    4262
     
    100120    gDebugCtx = CF_Debug_CreateCtx ();
    101121
    102     if (gLogCtx == NULL)
    103         CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    104 
    105122    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    106123
Note: See TracChangeset for help on using the changeset viewer.