Changeset 40 in libcf for trunk


Ignore:
Timestamp:
02/06/13 16:11:24 (11 years ago)
Author:
cheese
Message:

#1 change interface of log from context to id-number

Location:
trunk
Files:
1 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_base.h

    r37 r40  
    4848#define NAME_LENGTH                     128
    4949
    50 /** 오류 코드 베이스 넘버 */
    51 #define CF_ERROR_BASE                   CF_ERROR * 1000
    52 /** @see cf_file.h */
    53 #define CF_ERROR_FILE                   CF_ERROR_BASE * 2
    54 /** @see cf_socket.h */
    55 #define CF_ERROR_SOCKET                 CF_ERROR_BASE * 3
    56 /** @see cf_thread.h */
    57 #define CF_ERROR_THREAD                 CF_ERROR_BASE * 4
    58 /** @see cf_thread.h */
    59 #define CF_ERROR_MUTEX                  CF_ERROR_BASE * 5
    60 /** @see cf_debug.h */
    61 #define CF_ERROR_DEBUG                  CF_ERROR_BASE * 6
    62 /** @see cf_log.h */
    63 #define CF_ERROR_LOG                    CF_ERROR_BASE * 7
    64 
    6550/** Windows 에서의 함수 EXPORT 구문 정의 */
    6651#ifdef _WIN32
  • trunk/include/cf_debug.h

    r36 r40  
    2020
    2121#include <stdio.h>
    22 
    23 #define CF_ERROR_DEBUG_INVALID_CTX          CF_ERROR_DEBUG - 1
    24 #define CF_ERROR_DEBUG_SET_OUTPUT_FD        CF_ERROR_DEBUG - 2
    25 #define CF_ERROR_DEBUG_PUSH_CALLSTACK       CF_ERROR_DEBUG - 3
    26 #define CF_ERROR_DEBUG_POP_CALLSTACK        CF_ERROR_DEBUG - 4
    2722
    2823/** Windows 함수 이름 매크로 재정의 */
  • trunk/include/cf_file.h

    r35 r40  
    1111#include "cf_base.h"
    1212#include <fcntl.h>
    13 
    14 #define CF_ERROR_FILE_OPEN              CF_ERROR_FILE - 1
    15 #define CF_ERROR_FILE_INVALID_ARGS      CF_ERROR_FILE - 2
    16 #define CF_ERROR_FILE_READ              CF_ERROR_FILE - 3
    17 #define CF_ERROR_FILE_WRITE             CF_ERROR_FILE - 4
    18 #define CF_ERROR_FILE_CREATE            CF_ERROR_FILE - 5
    19 #define CF_ERROR_FILE_CLOSE             CF_ERROR_FILE - 6
    20 #define CF_ERROR_FILE_GET_SIZE          CF_ERROR_FILE - 7
    2113
    2214/** 파일 열기 옵션 플래그 */
  • trunk/include/cf_log.h

    r38 r40  
    1111#include "cf_base.h"
    1212
    13 #define CF_ERROR_LOG_INITIALIZE         CF_ERROR_LOG - 1
    14 #define CF_ERROR_LOG_INVALID_CTX        CF_ERROR_LOG - 2
    15 #define CF_ERROR_LOG_SET_MULTITHREAD    CF_ERROR_LOG - 3
    16 #define CF_ERROR_LOG_UNSET_MULTITHREAD  CF_ERROR_LOG - 4
    17 #define CF_ERROR_LOG_LOCK_CTX           CF_ERROR_LOG - 5
    18 #define CF_ERROR_LOG_UNLOCK_CTX         CF_ERROR_LOG - 6
    19 #define CF_ERROR_LOG_FLUSH              CF_ERROR_LOG - 7
    20 #define CF_ERROR_LOG_INVALID_MAPID      CF_ERROR_LOG - 8
    21 #define CF_ERROR_LOG_NOT_MAPPED_ID      CF_ERROR_LOG - 9
    22 #define CF_ERROR_LOG_ALREADY_MAPPED_ID  CF_ERROR_LOG - 10
    23 #define CF_ERROR_LOG_NOT_INITIALIZE     CF_ERROR_LOG - 11
    24 
    2513/**
    2614 * 로그의 버퍼 크기를 기본 값으로 설정
    2715 *
    28  * @see CF_Log_CreateCtx, CF_LOG_OPEN
     16 * @see CF_Log_Open
    2917 */
    3018#define CF_LOG_BUFFER_DEFAULT           -1
     
    3321 * 로그 기록 시 버퍼를 사용하지 않고 즉시 기록
    3422 *
    35  * @see CF_Log_CreateCtx, CF_LOG_OPEN, CF_Log_Initialize
     23 * @see CF_Log_Open
    3624 */
    3725#define CF_LOG_BUFFER_NO                0
    38 
    39 /**
    40  * 로그에 아이디 넘버를 부여하여 생성 <br />
    41  * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
    42  *
    43  * @param __id      부여할 아이디 넘버
    44  * @param __file    로그 파일 경로
    45  * @param __memsize 로그 버퍼 크기
    46  *
    47  * @see CF_Log_MapCtxID
    48  */
    49 #define CF_LOG_OPEN(__id,__file,__memsize)              \
    50     CF_Log_MapCtxID (__id, CF_Log_CreateCtx (__file, __memsize))
    51 
    52 /**
    53  * 아이디 넘버에 해당하는 로그에 쓰기
    54  *
    55  * @param __id  로그의 아이디 넘버
    56  * @param __pf  로그의 프리픽스 문자열
    57  * @param __fmt 포맷 스트링
    58  * @param ...   가변 인자
    59  *
    60  * @see CF_Log_GetMappedCtx
    61  */
    62 #define CF_LOG_WRITE(__id,__pf,__fmt,...)               \
    63     CF_Log_Write (CF_Log_GetMappedCtx (__id),__pf,__fmt,##__VA_ARGS__)
    64 
    65 /**
    66  * 아이디 넘버에 해당하는 로그를 닫고 해제
    67  *
    68  * @param __id 로그의 아이디 넘버
    69  *
    70  * @see CF_Log_UnmapCtxID
    71  */
    72 #define CF_LOG_CLOSE(__id)      CF_Log_UnmapCtxID (__id)
    73 
    74 /** 로그 컨텍스트 */
    75 typedef void *  CF_Log_Ctx;
    7626
    7727#ifdef __cplusplus
     
    8535CF_Log_Finalize         (void);
    8636
    87 CF_EXPORT CF_Log_Ctx
    88 CF_Log_CreateCtx        (const char * path,
     37CF_EXPORT int
     38CF_Log_Open             (const int  mapid,
     39                         const char * path,
    8940                         const int  memsize);
    9041
    9142CF_EXPORT int
    92 CF_Log_DestroyCtx       (CF_Log_Ctx ctx);
     43CF_Log_Close            (const int mapid);
    9344
    9445CF_EXPORT int
    95 CF_Log_SetMultiThread   (CF_Log_Ctx ctx);
     46CF_Log_SetMT            (const int      mapid,
     47                         const CF_BOOL  flag);
    9648
    9749CF_EXPORT int
    98 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx);
    99 
    100 CF_EXPORT int
    101 CF_Log_Write            (CF_Log_Ctx ctx,
     50CF_Log_Write            (const int  mapid,
    10251                         const char * prefix,
    10352                         const char * fmt, ...);
    10453
    10554CF_EXPORT int
    106 CF_Log_Flush            (CF_Log_Ctx ctx);
    107 
    108 CF_EXPORT int
    109 CF_Log_MapCtxID         (const int          mapid,
    110                          const CF_Log_Ctx   ctx);
    111 
    112 CF_EXPORT int
    113 CF_Log_UnmapCtxID       (const int mapid);
    114 
    115 CF_EXPORT CF_Log_Ctx
    116 CF_Log_GetMappedCtx     (const int mapid);
     55CF_Log_Flush            (const int mapid);
    11756
    11857#ifdef __cplusplus
  • trunk/include/cf_socket.h

    r35 r40  
    2424# include <unistd.h>
    2525#endif
    26 
    27 #define CF_ERROR_SOCKET_INITIALIZE      CF_ERROR_SOCKET - 1
    28 #define CF_ERROR_SOCKET_NOT_INITIALIZED CF_ERROR_SOCKET - 2
    29 #define CF_ERROR_SOCKET_FINALIZE        CF_ERROR_SOCKET - 3
    30 #define CF_ERROR_SOCKET_CLOSE           CF_ERROR_SOCKET - 4
    31 #define CF_ERROR_SOCKET_CREATE          CF_ERROR_SOCKET - 5
    32 #define CF_ERROR_SOCKET_CONNECT         CF_ERROR_SOCKET - 6
    33 #define CF_ERROR_SOCKET_SET_OPTION      CF_ERROR_SOCKET - 7
    34 #define CF_ERROR_SOCKET_GET_OPTION      CF_ERROR_SOCKET - 8
    35 #define CF_ERROR_SOCKET_SET_TIMEOUT     CF_ERROR_SOCKET - 9
    36 #define CF_ERROR_SOCKET_INVALID_ARGS    CF_ERROR_SOCKET - 10
    37 #define CF_ERROR_SOCKET_INVALID_SOCKET  CF_ERROR_SOCKET - 11
    38 #define CF_ERROR_SOCKET_GET_HOST        CF_ERROR_SOCKET - 12
    39 #define CF_ERROR_SOCKET_BIND            CF_ERROR_SOCKET - 13
    40 #define CF_ERROR_SOCKET_LISTEN          CF_ERROR_SOCKET - 14
    41 #define CF_ERROR_SOCKET_ACCEPT          CF_ERROR_SOCKET - 15
    42 #define CF_ERROR_SOCKET_SEND            CF_ERROR_SOCKET - 16
    43 #define CF_ERROR_SOCKET_RECV            CF_ERROR_SOCKET - 17
    4426
    4527#ifdef _WIN32
  • trunk/include/cf_thread.h

    r35 r40  
    1212
    1313#include "cf_base.h"
    14 
    15 #define CF_ERROR_THREAD_CREATE          CF_ERROR_THREAD - 1
    16 #define CF_ERROR_THREAD_INVALID_ARGS    CF_ERROR_THREAD - 2
    17 
    18 #define CF_ERROR_MUTEX_CREATE           CF_ERROR_MUTEX - 1
    19 #define CF_ERROR_MUTEX_INVALID_ARGS     CF_ERROR_MUTEX - 2
    2014
    2115#ifdef _WIN32
  • trunk/src/cf_debug.c

    r36 r40  
    66#include "cf_debug.h"
    77#include "cf_local.h"
     8#include "cf_error.h"
    89
    910#include <stdlib.h>
  • trunk/src/cf_file.c

    r35 r40  
    55 */
    66#include "cf_file.h"
     7#include "cf_error.h"
    78
    89#ifdef _WIN32
  • trunk/src/cf_log.c

    r38 r40  
    1212#include "cf_thread.h"
    1313#include "cf_local.h"
     14#include "cf_error.h"
    1415
    1516#include <stdio.h>
     
    3839
    3940#define CF_LOG_DATETIME_LENGTH          sizeof ("0000-00-00 00:00:00.000") - 1
     41
     42/**
     43 * 로그 컨텍스트
     44 *
     45 * @remark change from public to private
     46 */
     47typedef void *  CF_Log_Ctx;
    4048
    4149typedef struct __cf_util_datetime__
     
    189197                   const size_t demandSize)
    190198{
     199    int result = CF_OK;
     200
    191201    if( ctx->size > 0 ) /* 버퍼단위 버퍼링.... */
    192202    {
     
    213223        ctx->length = demandSize;
    214224
    215         if (CF_Log_Local_Flush (ctx) < 0)
    216             return CF_ERROR_LOG_FLUSH;
    217     }
     225        result = CF_Log_Local_Flush (ctx);
     226    }
     227
     228    return result;
     229}
     230
     231/**
     232 * 로그 컨텍스트에 멀티쓰레드 모드 설정
     233 *
     234 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     235 *
     236 * @param ctx 로그 컨텍스트
     237 *
     238 * @see CF_Log_UnsetMultiThread
     239 */
     240static int
     241CF_Log_SetMultiThread (CF_Log_Ctx ctx)
     242{
     243    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
     244
     245    if (CHECK_INVALID_CTX (ctx))
     246        return CF_ERROR_LOG_INVALID_CTX;
     247
     248    if (CF_Mutex_Create (&context->mutex) < 0)
     249        return CF_ERROR_LOG_SET_MULTITHREAD;
     250
     251    return CF_OK;
     252}
     253
     254/**
     255 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
     256 *
     257 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     258 *
     259 * @param ctx 로그 컨텍스트
     260 *
     261 * @see CF_Log_SetMultiThread
     262 */
     263static int
     264CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
     265{
     266    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
     267
     268    if (CHECK_INVALID_CTX (ctx))
     269        return CF_ERROR_LOG_INVALID_CTX;
     270
     271    if (CF_Mutex_Destory (&context->mutex) < 0)
     272        return CF_ERROR_LOG_UNSET_MULTITHREAD;
     273
     274    return CF_OK;
     275}
     276
     277/**
     278 * 로그 컨텍스트에 따라 로그 쓰기
     279 *
     280 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     281 *
     282 * @param ctx       로그 컨텍스트
     283 * @param prefix    로그의 프리픽스 문자열
     284 * @param fmt       포맷 스트링
     285 * @param ...       가변 인자
     286 */
     287static int
     288CF_Log_WriteCtx (CF_Log_Ctx ctx,
     289                 const char * prefix,
     290                 const char * fmt,
     291//               ...)
     292                 va_list    valist)
     293{
     294    CF_LOG_CTX  * context = (CF_LOG_CTX *) ctx;
     295//  va_list     valist;
     296    char        buffer[16 * 1024] = {0x00,};
     297    char        datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,};
     298
     299    if (CHECK_INVALID_CTX (ctx))
     300        return CF_ERROR_LOG_INVALID_CTX;
     301
     302    LOCK_LOG_CTX (context);
     303//  va_start (valist, fmt);
     304
     305    CF_Log_Local_GetTimeString (datetime);
     306    sprintf (buffer, "[%s][%s] ", datetime, prefix);
     307    vsprintf (buffer + strlen (buffer), fmt, valist);
     308
     309    CF_Log_Local_Push (context, buffer, strlen (buffer));
     310
     311//  va_end (valist);
     312    UNLOCK_LOG_CTX (context);
     313
     314    return CF_OK;
     315}
     316
     317/**
     318 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
     319 *
     320 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     321 *
     322 * @param ctx 로그 컨텍스트
     323 */
     324static int
     325CF_Log_FlushCtx (CF_Log_Ctx ctx)
     326{
     327    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
     328
     329    if (CHECK_INVALID_CTX (ctx))
     330        return CF_ERROR_LOG_INVALID_CTX;
     331
     332    LOCK_LOG_CTX (context);
     333    CF_Log_Local_Flush (context);
     334    UNLOCK_LOG_CTX (context);
     335
     336    return CF_OK;
     337}
     338
     339/**
     340 * 로그 컨텍스트 해제
     341 *
     342 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     343 *
     344 * @param ctx 로그 컨텍스트
     345 */
     346static int
     347CF_Log_DestroyCtx (CF_Log_Ctx ctx)
     348{
     349    CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
     350
     351    if (CHECK_INVALID_CTX (ctx))
     352        return CF_ERROR_LOG_INVALID_CTX;
     353
     354    if (context->size > 0)
     355    {
     356        CF_Log_FlushCtx (ctx);
     357        free (context->buffer);
     358        context->buffer = NULL;
     359        context->size = 0;
     360    }
     361
     362    CF_File_Close (context->fd);
     363
     364    if (context->mutex != NULL)
     365    {
     366        if (CF_Mutex_Destory (&context->mutex) < 0)
     367            /* do something ? */;
     368
     369        context->mutex = NULL;
     370    }
     371
     372    return CF_OK;
     373}
     374
     375/**
     376 * 로그 컨텍스트 생성
     377 *
     378 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
     379 *
     380 * @param path      로그 파일 경로
     381 * @param memsize   로그 버퍼 크기
     382 * @param ctx       로그 컨텍스트 받을 주소
     383 *
     384 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO
     385 */
     386static int
     387CF_Log_CreateCtx (const char    * path,
     388                  const int     memsize,
     389                  CF_Log_Ctx    * ctx)
     390{
     391    int result = 0;
     392    int size = (memsize == CF_LOG_BUFFER_DEFAULT)
     393             ? CF_LOG_BUFFER_DEFAULT_SIZE
     394             : memsize;
     395    CF_LOG_CTX  * context = NULL;
     396
     397    if (path == NULL)
     398        return CF_ERROR_LOG_INVALID_ARGS;
     399
     400    TRY
     401    {
     402        context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
     403        if (context == NULL)
     404        {
     405            result = CF_ERROR_LOG_ALLOCATE_CTX;
     406            TRY_BREAK;
     407        }
     408
     409        context->fd = CF_File_Create (path);
     410        if (context->fd < 0)
     411        {
     412            result = CF_ERROR_LOG_CREATE_FILE;
     413            TRY_BREAK;
     414        }
     415        sprintf (context->path, "%s", path);
     416
     417        if (size > 0)
     418        {
     419            context->buffer = (char *) calloc ((size_t) size + 1, 1);
     420            if (context->buffer == NULL)
     421            {
     422                result = CF_ERROR_LOG_ALLOCATE_BUFFER;
     423                TRY_BREAK;
     424            }
     425            context->size = (size_t) size;
     426        }
     427    }
     428    CATCH_IF (result < 0)
     429    {
     430        CF_Log_DestroyCtx ((CF_Log_Ctx) context);
     431        return result;
     432    }
     433
     434    *ctx = (CF_Log_Ctx) context;
     435
     436    return CF_OK;
     437}
     438
     439/**
     440 * 로그 컨텍스트에 아이디 넘버 할당<br />
     441 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
     442 *
     443 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     444 *
     445 * @param mapid 부여할 아이디 넘버
     446 * @param ctx   로그 컨텍스트
     447 *
     448 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함
     449 *
     450 * @see CF_LOG_OPEN, CF_Log_CreateCtx
     451 */
     452static int
     453CF_Log_MapCtxID (const int          mapid,
     454                 const CF_Log_Ctx   ctx)
     455{
     456    int result = 0;
     457
     458    TRY
     459    {
     460        if (CHECK_INITIALIZED ())
     461        {
     462            result = CF_ERROR_LOG_NOT_INITIALIZE;
     463            TRY_BREAK;
     464        }
     465        if (CHECK_INVALID_MAPID (mapid))
     466        {
     467            result = CF_ERROR_LOG_INVALID_MAPID;
     468            TRY_BREAK;
     469        }
     470        if (CHECK_MAPPED_ID (mapid))
     471        {
     472            result = CF_ERROR_LOG_ALREADY_MAPPED_ID;
     473            TRY_BREAK;
     474        }
     475    }
     476    CATCH_IF (result < 0)
     477    {
     478        CF_Log_DestroyCtx (ctx);
     479        return result;
     480    }
     481
     482    gLogEnvironment.ctxPool[mapid] = ctx;
     483
     484    return CF_OK;
     485}
     486
     487/**
     488 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
     489 *
     490 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     491 *
     492 * @param mapid 로그의 아이디 넘버
     493 *
     494 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
     495 *
     496 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
     497 */
     498static int
     499CF_Log_UnmapCtxID (const int mapid)
     500{
     501    if (CHECK_INITIALIZED ())
     502        return CF_ERROR_LOG_NOT_INITIALIZE;
     503    if (CHECK_INVALID_MAPID (mapid))
     504        return CF_ERROR_LOG_INVALID_MAPID;
     505    if (!CHECK_MAPPED_ID (mapid))
     506        return CF_ERROR_LOG_NOT_MAPPED_ID;
     507
     508    CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]);
     509
     510    free (gLogEnvironment.ctxPool[mapid]);
     511    gLogEnvironment.ctxPool[mapid] = NULL;
     512
     513    return CF_OK;
     514}
     515
     516/**
     517 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
     518 *
     519 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     520 *
     521 * @param mapid 로그의 아이디 넘버
     522 * @param ctx   로그 컨텍스트 받을 주소
     523 */
     524static int
     525CF_Log_GetMappedCtx (const int  mapid,
     526                     CF_Log_Ctx * ctx)
     527{
     528    if (CHECK_INITIALIZED ())
     529        return CF_ERROR_LOG_NOT_INITIALIZE;
     530    if (CHECK_INVALID_MAPID (mapid))
     531        return CF_ERROR_LOG_INVALID_MAPID;
     532    if (!CHECK_MAPPED_ID (mapid))
     533        return CF_ERROR_LOG_NOT_MAPPED_ID;
     534
     535    *ctx = gLogEnvironment.ctxPool[mapid];
    218536
    219537    return CF_OK;
     
    268586
    269587/**
    270  * 로그 컨텍스트 생성
    271  *
    272  * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
    273  *
     588 * 로그 열기
     589 *
     590 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     591 *
     592 * @param mapid 로그의 아이디 넘버
    274593 * @param path      로그 파일 경로
    275594 * @param memsize   로그 버퍼 크기
     
    277596 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO
    278597 */
    279 CF_Log_Ctx
    280 CF_Log_CreateCtx (const char    * path,
    281                   const int     memsize)
    282 {
    283     int result = 0;
    284     int size = (memsize == CF_LOG_BUFFER_DEFAULT)
    285              ? CF_LOG_BUFFER_DEFAULT_SIZE
    286              : memsize;
    287     CF_LOG_CTX  * context = NULL;
    288 
    289     if (path == NULL)
    290         return NULL;
    291 
    292     TRY
    293     {
    294         context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
    295         if (context == NULL)
    296         {
    297             result = -1;
    298             TRY_BREAK;
    299         }
    300 
    301         context->fd = CF_File_Create (path);
    302         if (context->fd < 0)
    303         {
    304             result = -2;
    305             TRY_BREAK;
    306         }
    307         sprintf (context->path, "%s", path);
    308 
    309         if (size > 0)
    310         {
    311             context->buffer = (char *) calloc ((size_t) size + 1, 1);
    312             if (context->buffer == NULL)
    313             {
    314                 result = -3;
    315                 TRY_BREAK;
    316             }
    317             context->size = (size_t) size;
    318         }
    319     }
    320     CATCH_IF (result < 0)
    321     {
    322         CF_Log_DestroyCtx ((CF_Log_Ctx) context);
    323         return NULL;
    324     }
    325 
    326     return (CF_Log_Ctx) context;
    327 }
    328 
    329 /**
    330  * 로그 컨텍스트 해제
    331  *
    332  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    333  *
    334  * @param ctx 로그 컨텍스트
    335  */
    336598int
    337 CF_Log_DestroyCtx (CF_Log_Ctx ctx)
    338 {
    339     CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    340 
    341     if (CHECK_INVALID_CTX (ctx))
    342         return CF_ERROR_LOG_INVALID_CTX;
    343 
    344     if (context->size > 0)
    345     {
    346         CF_Log_Flush (ctx);
    347         free (context->buffer);
    348         context->buffer = NULL;
    349         context->size = 0;
    350     }
    351 
    352     CF_File_Close (context->fd);
    353 
    354     if (context->mutex != NULL)
    355     {
    356         if (CF_Mutex_Destory (&context->mutex) < 0)
    357             /* do something ? */;
    358 
    359         context->mutex = NULL;
    360     }
    361 
    362     return CF_OK;
    363 }
    364 
    365 /**
    366  * 로그 컨텍스트에 멀티쓰레드 모드 설정
    367  *
    368  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    369  *
    370  * @param ctx 로그 컨텍스트
     599CF_Log_Open (const int  mapid,
     600             const char * path,
     601             const int  memsize)
     602{
     603    int         result = 0;
     604    CF_Log_Ctx  ctx = NULL;
     605
     606    result = CF_Log_CreateCtx (path, memsize, &ctx);
     607    if (result < 0)
     608        return result;
     609
     610    result = CF_Log_MapCtxID (mapid, ctx);
     611
     612    return result;
     613}
     614
     615/**
     616 * 로그 닫기
     617 *
     618 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     619 *
     620 * @param mapid 로그의 아이디 넘버
    371621 */
    372622int
    373 CF_Log_SetMultiThread (CF_Log_Ctx ctx)
    374 {
    375     CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    376 
    377     if (CHECK_INVALID_CTX (ctx))
    378         return CF_ERROR_LOG_INVALID_CTX;
    379 
    380     if (CF_Mutex_Create (&context->mutex) < 0)
    381         return CF_ERROR_LOG_SET_MULTITHREAD;
    382 
    383     return CF_OK;
    384 }
    385 
    386 /**
    387  * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
    388  *
    389  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    390  *
    391  * @param ctx 로그 컨텍스트
    392  */
    393 int
    394 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
    395 {
    396     CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    397 
    398     if (CHECK_INVALID_CTX (ctx))
    399         return CF_ERROR_LOG_INVALID_CTX;
    400 
    401     if (CF_Mutex_Destory (&context->mutex) < 0)
    402         return CF_ERROR_LOG_UNSET_MULTITHREAD;
    403 
    404     return CF_OK;
    405 }
    406 
    407 /**
    408  * 로그 컨텍스트에 따라 로그 쓰기
    409  *
    410  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    411  *
    412  * @param ctx       로그 컨텍스트
     623CF_Log_Close (const int mapid)
     624{
     625    return CF_Log_UnmapCtxID (mapid);
     626}
     627
     628/**
     629 * 로그 쓰기
     630 *
     631 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     632 *
     633 * @param mapid     로그의 아이디 넘버
    413634 * @param prefix    로그의 프리픽스 문자열
    414635 * @param fmt       포맷 스트링
     
    416637 */
    417638int
    418 CF_Log_Write (CF_Log_Ctx    ctx,
     639CF_Log_Write (const int     mapid,
    419640              const char    * prefix,
    420641              const char    * fmt, ...)
    421642{
    422     CF_LOG_CTX  * context = (CF_LOG_CTX *) ctx;
     643    int         result = 0;
     644    CF_Log_Ctx  ctx = NULL;
    423645    va_list     valist;
    424     char        buffer[16 * 1024] = {0x00,};
    425     char        datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,};
    426 
    427     if (CHECK_INVALID_CTX (ctx))
    428         return CF_ERROR_LOG_INVALID_CTX;
    429 
    430     LOCK_LOG_CTX (context);
     646
     647    result = CF_Log_GetMappedCtx (mapid, &ctx);
     648    if (result < 0)
     649        return result;
     650
    431651    va_start (valist, fmt);
    432 
    433     CF_Log_Local_GetTimeString (datetime);
    434     sprintf (buffer, "[%s][%s] ", datetime, prefix);
    435     vsprintf (buffer + strlen (buffer), fmt, valist);
    436 
    437     CF_Log_Local_Push (context, buffer, strlen (buffer));
    438 
     652    result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
    439653    va_end (valist);
    440     UNLOCK_LOG_CTX (context);
    441 
    442     return CF_OK;
     654
     655    return result;
    443656}
    444657
     
    448661 * @return 성공 시, CF_OK; 실패 시, 오류 코드
    449662 *
    450  * @param ctx 로그 컨텍스트
     663 * @param mapid 로그의 아이디 넘버
    451664 */
    452665int
    453 CF_Log_Flush (CF_Log_Ctx ctx)
    454 {
    455     CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
    456 
    457     if (CHECK_INVALID_CTX (ctx))
    458         return CF_ERROR_LOG_INVALID_CTX;
    459 
    460     LOCK_LOG_CTX (context);
    461     CF_Log_Local_Flush (context);
    462     UNLOCK_LOG_CTX (context);
    463 
    464     return CF_OK;
    465 }
    466 
    467 /**
    468  * 로그 컨텍스트에 아이디 넘버 할당<br />
    469  * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
    470  *
    471  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    472  *
    473  * @param mapid 부여할 아이디 넘버
    474  * @param ctx   로그 컨텍스트
    475  *
    476  * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함
    477  *
    478  * @see CF_LOG_OPEN, CF_Log_CreateCtx
     666CF_Log_Flush (const int mapid)
     667{
     668    int         result = 0;
     669    CF_Log_Ctx  ctx = NULL;
     670
     671    result = CF_Log_GetMappedCtx (mapid, &ctx);
     672    if (result < 0)
     673        return result;
     674
     675    result = CF_Log_FlushCtx (ctx);
     676
     677    return result;
     678}
     679
     680/**
     681 * 로그 컨텍스트에 멀티쓰레드 모드 설정
     682 *
     683 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     684 *
     685 * @param mapid 로그의 아이디 넘버
     686 * @param flag  설정/해제 bool 플래그
     687 *
     688 * @see CF_BOOL
    479689 */
    480690int
    481 CF_Log_MapCtxID (const int          mapid,
    482                  const CF_Log_Ctx   ctx)
    483 {
    484     int result = 0;
    485 
    486     TRY
    487     {
    488         if (CHECK_INITIALIZED())
    489         {
    490             result = CF_ERROR_LOG_NOT_INITIALIZE;
    491             TRY_BREAK;
    492         }
    493         if (CHECK_INVALID_MAPID(mapid))
    494         {
    495             result = CF_ERROR_LOG_INVALID_MAPID;
    496             TRY_BREAK;
    497         }
    498         if (CHECK_MAPPED_ID(mapid))
    499         {
    500             result = CF_ERROR_LOG_ALREADY_MAPPED_ID;
    501             TRY_BREAK;
    502         }
    503     }
    504     CATCH_IF (result < 0)
    505     {
    506         CF_Log_DestroyCtx (ctx);
     691CF_Log_SetMT (const int     mapid,
     692              const CF_BOOL flag)
     693{
     694    int         result = 0;
     695    CF_Log_Ctx  ctx = NULL;
     696
     697    result = CF_Log_GetMappedCtx (mapid, &ctx);
     698    if (result < 0)
    507699        return result;
    508     }
    509 
    510     gLogEnvironment.ctxPool[mapid] = ctx;
    511 
    512     return CF_OK;
    513 }
    514 
    515 /**
    516  * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
    517  *
    518  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    519  *
    520  * @param mapid 로그의 아이디 넘버
    521  *
    522  * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
    523  *
    524  * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
    525  */
    526 int
    527 CF_Log_UnmapCtxID (const int mapid)
    528 {
    529     if (CHECK_INITIALIZED())
    530         return CF_ERROR_LOG_NOT_INITIALIZE;
    531     if (CHECK_INVALID_MAPID(mapid))
    532         return CF_ERROR_LOG_INVALID_MAPID;
    533     if (!CHECK_MAPPED_ID(mapid))
    534         return CF_ERROR_LOG_NOT_MAPPED_ID;
    535 
    536     CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]);
    537 
    538     free (gLogEnvironment.ctxPool[mapid]);
    539     gLogEnvironment.ctxPool[mapid] = NULL;
    540 
    541     return CF_OK;
    542 }
    543 
    544 /**
    545  * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
    546  *
    547  * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
    548  *
    549  * @param mapid 로그의 아이디 넘버
    550  */
    551 CF_Log_Ctx
    552 CF_Log_GetMappedCtx (const int mapid)
    553 {
    554     if (CHECK_INITIALIZED())
    555         return NULL;
    556     if (CHECK_INVALID_MAPID(mapid))
    557         return NULL;
    558     if (!CHECK_MAPPED_ID(mapid))
    559         return NULL;
    560 
    561     return gLogEnvironment.ctxPool[mapid];
    562 }
     700
     701    result = (flag == CF_TRUE)
     702           ? CF_Log_SetMultiThread (ctx)
     703           : CF_Log_UnsetMultiThread (ctx);
     704
     705    return result;
     706}
  • trunk/src/cf_socket.c

    r35 r40  
    66#include "cf_socket.h"
    77#include "cf_local.h"
     8#include "cf_error.h"
    89
    910#ifdef _WIN32
  • trunk/src/cf_thread.c

    r35 r40  
    66#include "cf_thread.h"
    77#include "cf_local.h"
     8#include "cf_error.h"
    89
    910#include <stdlib.h>
  • trunk/test/log.c

    r35 r40  
    77#include <stdio.h>
    88
    9 CF_Log_Ctx gLogCtx;
     9#define LOGID       1
    1010
    1111int main (void)
     
    1414    char        logname[16] = {0x00,};
    1515
    16     CF_Log_Initialize (10);
     16    if (CF_Log_Initialize (10) < 0)
     17    {
     18        fprintf (stderr, "failed to init. log\n");
     19        return -1;
     20    }
    1721
    18     // with context
    19     gLogCtx = CF_Log_CreateCtx ("log.txt", CF_LOG_BUFFER_DEFAULT);
    20     if (gLogCtx == NULL)
    21         fprintf (stderr, "create log ctx error\n");
    22 
    23     for (i = 0 ; i < 10000 ; i++)
    24         CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
    25 
    26     CF_Log_Write (gLogCtx, "prefix", "log test\n");
    27 
    28     CF_Log_DestroyCtx (gLogCtx);
    29 
    30     // with id number
    3122    for (i = 0 ; i < 10 ; i++)
    3223    {
    3324        sprintf (logname, "logid%d.txt", i);
    34         CF_LOG_OPEN (i, logname, CF_LOG_BUFFER_NO);
     25        if (CF_Log_Open (i, logname, CF_LOG_BUFFER_NO) < 0)
     26            fprintf (stderr, "failed to open log\n");
    3527    }
    3628
     
    3830    {
    3931        for (j = 0 ; j < 10000 ; j++)
    40             CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j);
     32        {
     33            int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
     34            if (result < 0)
     35                fprintf (stderr, "failed to write log %d\n", result);
     36        }
    4137
    42         CF_LOG_CLOSE (i);
     38        if (CF_Log_Close (i) < 0)
     39            fprintf (stderr, "failed to close log\n");
    4340    }
    4441
  • trunk/test/test.c

    r38 r40  
    2525CF_Debug_CallStack  gDebugCallstack;
    2626
    27 CF_Log_Ctx          gLogCtx;
     27#define LOG_SOCKET      0
     28#define LOG_MT          1
    2829
    2930const char * file = "./log.txt";
     
    3637    for (i = 0 ; i < 100000 ; i++)
    3738    {
    38         CF_Log_Write (gLogCtx, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
     39        CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
    3940    }
    4041
     
    5152
    5253    /* initialize */
    53     CF_Log_Initialize (10);
    54 
    55     /* context {{{ */
    56     gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
    57     if (gLogCtx == NULL)
     54    if (CF_Log_Initialize (10) < 0)
     55    {
     56        fprintf (stderr, "failed to init. log\n");
     57        return ;
     58    }
     59
     60    for (i = 0 ; i < 10 ; i++)
     61    {
     62        sprintf (idname, "logid%d.txt", i);
     63        if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
     64            fprintf (stderr, "failed to open log\n");
     65    }
     66
     67    for (i = 0 ; i < 10 ; i++)
     68    {
     69        for (j = 0 ; j < 10000 ; j++)
     70        {
     71            int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
     72            if (result < 0)
     73                fprintf (stderr, "failed to write log %d\n", result);
     74        }
     75
     76        if (CF_Log_Close (i) < 0)
     77            fprintf (stderr, "failed to close log\n");
     78    }
     79
     80    /* mt {{{ */
     81    if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
    5882        CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    5983
    60     for (i = 0 ; i < 1000 ; i++)
    61         CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
    62 
    63     CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
    64 
    65     CF_Log_Write (gLogCtx, message,
    66                             "here is the end of function [file:%s line:%d func:%s]\n",
    67                             gDebugCallstack.file,
    68                             gDebugCallstack.line,
    69                             gDebugCallstack.function);
    70 
    71     CF_Log_DestroyCtx (gLogCtx);
    72     /* }}} context */
    73 
    74     /* id {{{ */
    75     for (i = 0 ; i < 10 ; i++)
    76     {
    77         sprintf (idname, "logid%d.txt", i);
    78         CF_LOG_OPEN (i, idname, CF_LOG_BUFFER_NO);
    79     }
    80 
    81     for (i = 0 ; i < 10 ; i++)
    82     {
    83         for (j = 0 ; j < 10000 ; j++)
    84             CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j);
    85 
    86         CF_LOG_CLOSE (i);
    87     }
    88     /* }}} id */
    89 
    90     /* mt {{{ */
    91     gLogCtx = CF_Log_CreateCtx ("log_mt.txt", CF_LOG_BUFFER_DEFAULT);
    92     if (gLogCtx == NULL)
    93         CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    94 
    95     if (CF_Log_SetMultiThread (gLogCtx) < 0)
     84    if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
    9685        CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
    9786
     
    111100            CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
    112101    }
    113 
    114     CF_Log_DestroyCtx (gLogCtx);
    115102    /* }}} mt */
    116103
     
    184171        return (CF_THREAD_RETURN)-1;
    185172    }
    186     CF_Log_Write (gLogCtx, "SERVER", "accepted\n");
     173    CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
    187174
    188175    for (i = 0 ; i < 100 ; i++)
     
    193180            return (CF_THREAD_RETURN)-2;
    194181        }
    195         CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
     182        CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
    196183
    197184        if (CF_Socket_Send (clntsock, buf, recvd) < 0)
     
    199186            return (CF_THREAD_RETURN)-3;
    200187        }
    201         CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
     188        CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
    202189    }
    203190
     
    224211        return (CF_THREAD_RETURN)-1;
    225212    }
    226     CF_Log_Write (gLogCtx, "CLIENT", "connected\n");
     213    CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
    227214
    228215    for (i = 0 ; i < 100 ; i++)
     
    233220            return (CF_THREAD_RETURN)-2;
    234221        }
    235         CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
     222        CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
    236223
    237224        memset (buf, 0x00, sizeof (buf));
     
    242229            return (CF_THREAD_RETURN)-3;
    243230        }
    244         CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
     231        CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
    245232    }
    246233
     
    260247
    261248    /*------------------------------------------------------------*/
    262     gLogCtx = CF_Log_CreateCtx ("socket.txt", CF_LOG_BUFFER_DEFAULT);
    263     if (gLogCtx == NULL)
     249    if (CF_Log_Initialize (1) < 0)
     250    {
     251        fprintf (stderr, "failed to init. log\n");
     252        return ;
     253    }
     254
     255    if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
    264256        CF_DEBUG_PRINT (stderr, "failed to open log\n");
    265257
    266     CF_Log_Write (gLogCtx, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
     258    CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
    267259
    268260    if (CF_Socket_Initialize () < 0)
     
    271263        return ;
    272264    }
    273     CF_Log_Write (gLogCtx, "SOCKET", "socket initialized\n");
     265    CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
    274266
    275267    sock = CF_Socket_Server (PORT, 5);
     
    279271        return ;
    280272    }
    281     CF_Log_Write (gLogCtx, "SOCKET", "socket ready\n");
     273    CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
    282274
    283275    if (CF_Socket_SetOption (sock, SO_REUSEADDR, &reuse, sizeof (reuse)) < 0)
     
    286278        return ;
    287279    }
    288     CF_Log_Write (gLogCtx, "SOCKET", "set socket option\n");
     280    CF_Log_Write (LOG_SOCKET, "SOCKET", "set socket option\n");
    289281
    290282    for (iter = 0 ; iter < THREAD_POOL ; iter++)
     
    295287            return ;
    296288        }
    297         CF_Log_Write (gLogCtx, "SOCKET", "create server thread-%d\n", iter);
     289        CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
    298290    }
    299291
     
    306298            return ;
    307299        }
    308         CF_Log_Write (gLogCtx, "SOCKET", "create client thread-%d\n", iter);
     300        CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
    309301    }
    310302
     
    313305        CF_Thread_Join (&tid[iter]);
    314306        CF_Thread_Release (&tid[iter]);
    315         CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter);
     307        CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
    316308    }
    317309
     
    320312    CF_Socket_Finalize ();
    321313
    322     CF_Log_DestroyCtx (gLogCtx);
     314    CF_Log_Close (LOG_SOCKET);
     315
     316    CF_Log_Finalize ();
    323317    /*------------------------------------------------------------*/
    324318}
Note: See TracChangeset for help on using the changeset viewer.