Changeset 25 in libcf for trunk/src/cf_log.c


Ignore:
Timestamp:
02/02/13 17:27:06 (11 years ago)
Author:
cheese
Message:

#1 fix logging bug and debugging macros

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_log.c

    r23 r25  
    2424#endif // #ifdef _WIN32
    2525
    26 #define CHECK_INVALID_CTX(__ctx)                \
    27     if (__ctx == NULL)                          \
    28         return CF_ERROR_LOG_INVALID_CTX
    29 
    30 #define LOCK_LOG_CTX(__ctx)                     \
    31     CF_Mutex_Lock (&__ctx->mutex)
    32 
    33 #define UNLOCK_LOG_CTX(__ctx)                   \
    34     CF_Mutex_Unlock (&__ctx->mutex)
     26#define CHECK_INVALID_CTX(__ctx)        (__ctx == NULL)
     27#define LOCK_LOG_CTX(__ctx)             CF_Mutex_Lock (&__ctx->mutex)
     28#define UNLOCK_LOG_CTX(__ctx)           CF_Mutex_Unlock (&__ctx->mutex)
     29#define CHECK_INITIALIZED()             (gLogEnvironment.ctxPool == NULL||  \
     30                                         gLogEnvironment.ctxSize <= 0   )
     31#define CHECK_INVALID_MAPID(__mapid)    (gLogEnvironment.ctxSize <= __mapid)
     32#define CHECK_MAPPED_ID(__mapid)        (gLogEnvironment.ctxPool[__mapid] != NULL)  \
     33
    3534
    3635#define CF_LOG_BUFFER_DEFAULT_SIZE      128 * 1024
     
    271270    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    272271
    273     CHECK_INVALID_CTX (ctx);
     272    if (CHECK_INVALID_CTX (ctx))
     273        return CF_ERROR_LOG_INVALID_CTX;
    274274
    275275    memset (context->path, 0x00, sizeof (context->path));
     
    301301    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    302302
    303     CHECK_INVALID_CTX (ctx);
     303    if (CHECK_INVALID_CTX (ctx))
     304        return CF_ERROR_LOG_INVALID_CTX;
    304305
    305306    if (CF_Mutex_Create (&context->mutex) < 0)
     
    314315    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    315316
    316     CHECK_INVALID_CTX (ctx);
     317    if (CHECK_INVALID_CTX (ctx))
     318        return CF_ERROR_LOG_INVALID_CTX;
    317319
    318320    if (CF_Mutex_Destory (&context->mutex) < 0)
     
    332334    size_t      length = 0;
    333335
    334     CHECK_INVALID_CTX (ctx);
     336    if (CHECK_INVALID_CTX (ctx))
     337        return CF_ERROR_LOG_INVALID_CTX;
    335338
    336339    LOCK_LOG_CTX (context);
     
    375378    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    376379
    377     CHECK_INVALID_CTX (ctx);
     380    if (CHECK_INVALID_CTX (ctx))
     381        return CF_ERROR_LOG_INVALID_CTX;
    378382
    379383    LOCK_LOG_CTX (context);
     
    388392                 const CF_Log_Ctx   ctx)
    389393{
    390     if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0)
     394    int result = 0;
     395
     396    TRY
     397    {
     398        if (CHECK_INITIALIZED())
     399        {
     400            result = CF_ERROR_LOG_NOT_INITIALIZE;
     401            TRY_BREAK;
     402        }
     403        if (CHECK_INVALID_MAPID(mapid))
     404        {
     405            result = CF_ERROR_LOG_INVALID_MAPID;
     406            TRY_BREAK;
     407        }
     408        if (CHECK_MAPPED_ID(mapid))
     409        {
     410            result = CF_ERROR_LOG_ALREADY_MAPPED_ID;
     411            TRY_BREAK;
     412        }
     413    }
     414    CATCH_IF (result < 0)
     415    {
     416        CF_Log_DestroyCtx (ctx);
     417        return result;
     418    }
     419
     420    gLogEnvironment.ctxPool[mapid] = ctx;
     421
     422    return CF_OK;
     423}
     424
     425int
     426CF_Log_UnmapCtxID (const int mapid)
     427{
     428    if (CHECK_INITIALIZED())
    391429        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)
     430    if (CHECK_INVALID_MAPID(mapid))
     431        return CF_ERROR_LOG_INVALID_MAPID;
     432    if (!CHECK_MAPPED_ID(mapid))
    408433        return CF_ERROR_LOG_NOT_MAPPED_ID;
    409434
     
    419444CF_Log_GetMappedCtx (const int mapid)
    420445{
    421     if (gLogEnvironment.ctxPool == NULL || gLogEnvironment.ctxSize <= 0)
     446    if (CHECK_INITIALIZED())
    422447        return NULL;
     448    if (CHECK_INVALID_MAPID(mapid))
     449        return NULL;
     450    if (!CHECK_MAPPED_ID(mapid))
     451        return NULL;
    423452
    424453    return gLogEnvironment.ctxPool[mapid];
Note: See TracChangeset for help on using the changeset viewer.