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


Ignore:
Timestamp:
02/05/13 18:18:37 (11 years ago)
Author:
cheese
Message:

#1 separate example code and doxygen comment and fix logging push logic by vfire

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/cf_log.c

    r34 r35  
    161161
    162162static int
    163 CF_Log_Local_Push (CF_LOG_CTX   * ctx,
    164                    const char   * buffer)
    165 {
    166     strncat (ctx->buffer + ctx->length,
    167              buffer,
    168              ctx->size - ctx->length);
    169     ctx->length = strlen (ctx->buffer);
    170 
    171     return CF_OK;
    172 }
    173 
    174 static int
    175163CF_Log_Local_Flush (CF_LOG_CTX * ctx)
    176164{
     
    184172}
    185173
     174/**
     175 * 로그 데이터 처리
     176 *
     177 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     178 *
     179 * @param ctx           로그 컨텍스트
     180 * @param buffer        로그 데이터
     181 * @param demandSize    로그 데이터 길이
     182 *
     183 * @author vfire
     184 */
     185/* static */int
     186CF_Log_Local_Push (CF_LOG_CTX   * ctx,
     187                   const char   * buffer,
     188                   const size_t demandSize)
     189{
     190    if( ctx->size > 0 ) /* 버퍼단위 버퍼링.... */
     191    {
     192        size_t writeSize;
     193        size_t remainSize;
     194
     195        remainSize = demandSize;
     196        while (remainSize)
     197        {
     198            writeSize = (ctx->size - ctx->length) < remainSize ? (ctx->size - ctx->length) : remainSize;
     199
     200            memcpy (ctx->buffer + ctx->length, buffer + demandSize - remainSize, writeSize);
     201            ctx->length += writeSize;
     202
     203            if (ctx->length == ctx->size)
     204                CF_Log_Local_Flush (ctx);
     205
     206            remainSize -= writeSize;
     207        }
     208    }
     209    else /* flush되어야 함. */
     210    {
     211        if (CF_File_Write (ctx->fd, buffer, demandSize) < 0)
     212            return CF_ERROR_LOG_FLUSH;
     213    }
     214
     215    return CF_OK;
     216}
     217
     218/**
     219 * 로그를 사용하기 위해 초기화
     220 *
     221 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     222 *
     223 * @param logPool 아이디 넘버 최대 값
     224 */
    186225int
    187226CF_Log_Initialize (const int logPool)
     
    201240}
    202241
     242/**
     243 * 로그가 모두 사용된 후 자원 해제
     244 *
     245 * @return CF_OK 반환
     246 */
    203247int
    204248CF_Log_Finalize (void)
     
    219263}
    220264
     265/**
     266 * 로그 컨텍스트 생성
     267 *
     268 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
     269 *
     270 * @param path      로그 파일 경로
     271 * @param memsize   로그 버퍼 크기
     272 *
     273 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO
     274 */
    221275CF_Log_Ctx
    222276CF_Log_CreateCtx (const char    * path,
     
    269323}
    270324
     325/**
     326 * 로그 컨텍스트 해제
     327 *
     328 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     329 *
     330 * @param ctx 로그 컨텍스트
     331 */
    271332int
    272333CF_Log_DestroyCtx (CF_Log_Ctx ctx)
     
    300361}
    301362
     363/**
     364 * 로그 컨텍스트에 멀티쓰레드 모드 설정
     365 *
     366 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     367 *
     368 * @param ctx 로그 컨텍스트
     369 */
    302370int
    303371CF_Log_SetMultiThread (CF_Log_Ctx ctx)
     
    314382}
    315383
     384/**
     385 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
     386 *
     387 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     388 *
     389 * @param ctx 로그 컨텍스트
     390 */
    316391int
    317392CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
     
    328403}
    329404
     405/**
     406 * 로그 컨텍스트에 따라 로그 쓰기
     407 *
     408 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     409 *
     410 * @param ctx       로그 컨텍스트
     411 * @param prefix    로그의 프리픽스 문자열
     412 * @param fmt       포맷 스트링
     413 * @param ...       가변 인자
     414 */
    330415int
    331416CF_Log_Write (CF_Log_Ctx    ctx,
     
    335420    CF_LOG_CTX  * context = (CF_LOG_CTX *) ctx;
    336421    va_list     valist;
    337     char        buffer[4096] = {0x00,};
     422    char        buffer[16 * 1024] = {0x00,};
    338423    char        datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,};
    339     size_t      length = 0;
    340424
    341425    if (CHECK_INVALID_CTX (ctx))
     
    349433    vsprintf (buffer + strlen (buffer), fmt, valist);
    350434
    351     if (context->size == CF_LOG_BUFFER_NO)
    352     {
    353         context->buffer = buffer;
    354         context->length = strlen (buffer);
    355 
    356         CF_Log_Local_Flush (context);
    357 
    358         context->buffer = NULL;
    359         /* context->length = 0; // already did this in flush */
    360     }
    361     else /* (context->size > 0) */
    362     {
    363         length = context->size - context->length;
    364         CF_Log_Local_Push (context, buffer);
    365 
    366         if (context->length == context->size)
    367         {
    368             CF_Log_Local_Flush (context);
    369             CF_Log_Local_Push (context, buffer + length);
    370         }
    371     }
     435    CF_Log_Local_Push (context, buffer, strlen (buffer));
    372436
    373437    va_end (valist);
     
    377441}
    378442
     443/**
     444 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
     445 *
     446 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     447 *
     448 * @param ctx 로그 컨텍스트
     449 */
    379450int
    380451CF_Log_Flush (CF_Log_Ctx ctx)
     
    392463}
    393464
     465/**
     466 * 로그 컨텍스트에 아이디 넘버 할당<br />
     467 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
     468 *
     469 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     470 *
     471 * @param mapid 부여할 아이디 넘버
     472 * @param ctx   로그 컨텍스트
     473 *
     474 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함
     475 *
     476 * @see CF_LOG_OPEN, CF_Log_CreateCtx
     477 */
    394478int
    395479CF_Log_MapCtxID (const int          mapid,
     
    427511}
    428512
     513/**
     514 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
     515 *
     516 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     517 *
     518 * @param mapid 로그의 아이디 넘버
     519 *
     520 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
     521 *
     522 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
     523 */
    429524int
    430525CF_Log_UnmapCtxID (const int mapid)
     
    445540}
    446541
     542/**
     543 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
     544 *
     545 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
     546 *
     547 * @param mapid 로그의 아이디 넘버
     548 */
    447549CF_Log_Ctx
    448550CF_Log_GetMappedCtx (const int mapid)
Note: See TracChangeset for help on using the changeset viewer.