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


Ignore:
Timestamp:
07/17/13 12:13:28 (11 years ago)
Author:
cheese
Message:

#1 refactoring log writing functions

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_log.c

    r128 r136  
    3737
    3838#define ASSERT_INIT()                   \
    39     if (gLogArray.ctxPool == NULL   ||  \
    40         gLogArray.ctxSize <= 0      )   \
     39    if (gLogPool.ctxPool == NULL    ||  \
     40        gLogPool.poolSize <= 0      )   \
    4141        return CF_ERROR_LOG_NOT_INITIALIZE
    4242
    4343#define ASSERT_MAPID(__mapid)           \
    44     if (gLogArray.ctxSize <= __mapid)   \
     44    if (gLogPool.poolSize <= __mapid)   \
    4545        return CF_ERROR_LOG_INVALID_MAPID
    4646
    4747#define ASSERT_MAPPED_CTX(__mapid)          \
    48     if (gLogArray.ctxPool[__mapid] != NULL) \
     48    if (gLogPool.ctxPool[__mapid] != NULL)  \
    4949        return CF_ERROR_LOG_ALREADY_MAPPED_ID
    5050
    5151#define ASSERT_NOT_MAPPED_CTX(__mapid)      \
    52     if (gLogArray.ctxPool[__mapid] == NULL) \
     52    if (gLogPool.ctxPool[__mapid] == NULL)  \
    5353        return CF_ERROR_LOG_NOT_MAPPED_ID
    5454
     
    8787{
    8888    CF_Log_Ctx  * ctxPool;
    89     int         ctxSize;
    90 } CF_LOG_ARRAY;
    91 
    92 static CF_LOG_ARRAY gLogArray;
     89    int         poolSize;
     90} CF_LOG_POOL;
     91
     92static CF_LOG_POOL  gLogPool;
    9393
    9494#if defined(_WIN32) || defined(_WIN64)
     
    294294 * \param prefix    로그의 프리픽스 문자열
    295295 * \param fmt       포맷 스트링
    296  * \param ...       가변 인자
    297  */
    298 static int
    299 CF_Log_WriteCtx (CF_Log_Ctx ctx,
    300                  const char * prefix,
    301                  const char * fmt,
    302 //               ...)
    303                  va_list    valist)
     296 * \param valist    가변 인자 리스트
     297 */
     298static int
     299CF_Log_WriteVA (CF_Log_Ctx  ctx,
     300                const char  * prefix,
     301                const char  * fmt,
     302                va_list     valist)
    304303{
    305304#define BUF_LEN 16 * 1024
    306305    CF_LOG_CTX  * context = (CF_LOG_CTX *) ctx;
    307 //  va_list     valist;
    308306    char        buffer[BUF_LEN + 1] = {0x00,};
    309307    char        datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
    310308    int         length = 0;
     309    int         result = 0;
    311310
    312311    ASSERT_CTX (ctx);
    313 
    314     CF_Mutex_Lock (context->mutex);
    315 //  va_start (valist, fmt);
    316312
    317313    CF_Log_Local_GetTimeString (datetime);
     
    319315    vsnprintf (&buffer[length], BUF_LEN - (size_t)length, fmt, valist);
    320316
    321     CF_Log_Local_Push (context, buffer, strlen (buffer));
    322 
    323 //  va_end (valist);
     317    CF_Mutex_Lock (context->mutex);
     318    result = CF_Log_Local_Push (context, buffer, strlen (buffer));
    324319    CF_Mutex_Unlock (context->mutex);
    325320
    326     return CF_OK;
     321    return result;
     322}
     323
     324/**
     325 * 로그 컨텍스트에 따라 로그 쓰기
     326 *
     327 * \return 성공 시, CF_OK; 실패 시, 오류 코드
     328 *
     329 * \param ctx       로그 컨텍스트
     330 * \param prefix    로그의 프리픽스 문자열
     331 * \param fmt       포맷 스트링
     332 * \param ...       가변 인자
     333 */
     334int
     335CF_Log_WriteCtx (CF_Log_Ctx ctx,
     336                 const char * prefix,
     337                 const char * fmt, ...)
     338{
     339    int     result = 0;
     340    va_list valist;
     341
     342    va_start (valist, fmt);
     343    result = CF_Log_WriteVA (ctx, prefix, fmt, valist);
     344    va_end (valist);
     345
     346    return result;
    327347}
    328348
     
    337357CF_Log_FlushCtx (CF_Log_Ctx ctx)
    338358{
    339     CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
     359    CF_LOG_CTX  * context = (CF_LOG_CTX *) ctx;
     360    int         result = 0;
    340361
    341362    ASSERT_CTX (ctx);
    342363
    343     CF_Mutex_Lock (context->mutex);
    344364    CF_Log_Local_Flush (context);
     365    result = CF_Mutex_Lock (context->mutex);
    345366    CF_Mutex_Unlock (context->mutex);
    346367
    347     return CF_OK;
     368    return result;
    348369}
    349370
     
    464485    ASSERT_MAPPED_CTX (mapid);
    465486
    466     gLogArray.ctxPool[mapid] = ctx;
     487    gLogPool.ctxPool[mapid] = ctx;
    467488
    468489    return CF_OK;
     
    487508    ASSERT_NOT_MAPPED_CTX (mapid);
    488509
    489     CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
    490 
    491     free (gLogArray.ctxPool[mapid]);
    492     gLogArray.ctxPool[mapid] = NULL;
     510    CF_Log_DestroyCtx (gLogPool.ctxPool[mapid]);
     511
     512    free (gLogPool.ctxPool[mapid]);
     513    gLogPool.ctxPool[mapid] = NULL;
    493514
    494515    return CF_OK;
     
    511532    ASSERT_NOT_MAPPED_CTX (mapid);
    512533
    513     *ctx = gLogArray.ctxPool[mapid];
     534    *ctx = gLogPool.ctxPool[mapid];
    514535
    515536    return CF_OK;
     
    526547CF_Log_Initialize (const int poolSize)
    527548{
    528     memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
     549    memset (&gLogPool, 0x00, sizeof (CF_LOG_POOL));
    529550
    530551    if (poolSize > 0)
    531552    {
    532         gLogArray.ctxPool =
     553        gLogPool.ctxPool =
    533554            (CF_Log_Ctx *) calloc ((size_t) poolSize, sizeof (CF_Log_Ctx));
    534         if (gLogArray.ctxPool == NULL)
     555        if (gLogPool.ctxPool == NULL)
    535556            return CF_ERROR_LOG_INITIALIZE;
    536         gLogArray.ctxSize = poolSize;
     557        gLogPool.poolSize = poolSize;
    537558    }
    538559
     
    550571    int mapid = 0;
    551572
    552     for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
     573    for (mapid = 0 ; mapid < gLogPool.poolSize ; mapid++)
    553574    {
    554575        CF_Log_UnmapCtxID (mapid);
    555576    }
    556577
    557     if (gLogArray.ctxPool != NULL)
    558         free (gLogArray.ctxPool);
    559 
    560     memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
     578    if (gLogPool.ctxPool != NULL)
     579        free (gLogPool.ctxPool);
     580
     581    memset (&gLogPool, 0x00, sizeof (CF_LOG_POOL));
    561582
    562583    return CF_OK;
     
    679700
    680701    va_start (valist, fmt);
    681     result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
     702    result = CF_Log_WriteVA (ctx, prefix, fmt, valist);
    682703    va_end (valist);
    683704
Note: See TracChangeset for help on using the changeset viewer.