Changeset 51 in libcf for trunk


Ignore:
Timestamp:
04/02/13 10:23:52 (11 years ago)
Author:
cheese
Message:

#1 change the debugging utiltity to support single context with mutex

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_debug.h

    r50 r51  
    2727
    2828#ifdef _DEBUG
    29 /**
    30  * 디버그 컨텍스트 생성
    31  *
    32  * @see CF_Debug_CreateCtx
    33  */
    34 # define CF_DEBUG_CREATE_CTX()                                      \
    35     CF_Debug_CreateCtx ()
    36 
    37 /**
    38  * 디버그 컨텍스트 해제
    39  *
    40  * @param __ctx 디버그 컨텍스트
    41  *
    42  * @see CF_Debug_DestroyCtx
    43  */
    44 # define CF_DEBUG_DESTROY_CTX(__ctx)                                \
    45     CF_Debug_DestroyCtx (__ctx)
    4629
    4730/**
    4831 * 디버그 메시지를 지정된 파일 포인터로 출력
    4932 *
    50  * @param __fp  파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
    5133 * @param __fmt 포맷 스트링
    5234 * @param ...   가변 인자
     
    6042 * 바이너리 데이터를 디버그 메시지와 함께 지정된 파일 포인터로 출력
    6143 *
    62  * @param __fp  파일 포인터. 표준출력(stdout) 및 표준오류(stderr) 사용 가능
    6344 * @param __bin 바이너리 데이터
    6445 * @param __len 바이너리 길이
     
    7253
    7354/**
    74  * 컨텍스트를 업데이트하고 디버그 메시지를 출력
     55 * 함수에 진입
    7556 *
    76  * @param __ctx 디버그 컨텍스트
    77  * @param __fmt 포맷 스트링
    78  * @param ...   가변 인자
    79  *
    80  * @see CF_Debug_Trace
     57 * @see CF_Debug_EnterFunction
    8158 */
    82 # define CF_DEBUG_TRACE(__ctx,__fmt,...)                            \
    83     CF_Debug_Trace (__ctx,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__)
     59# define CF_DEBUG_BEGIN_FUNCTION                                \
     60    CF_Debug_EnterFunction (__FILE__,__func__,__LINE__)
    8461
    8562/**
    86  * 컨텍스트를 업데이트하고 바이너리 데이터를 디버그 메시지와 함께 출력
     63 * 함수에서 종료
    8764 *
    88  * @param __ctx 디버그 컨텍스트
    89  * @param __bin 바이너리 데이터
    90  * @param __len 바이너리 길이
    91  * @param __fmt 포맷 스트링
    92  * @param ...   가변 인자
    93  *
    94  * @see CF_Debug_TraceBin
     65 * @see CF_Debug_LeaveFunction
    9566 */
    96 # define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...)            \
    97     CF_Debug_TraceBin (__ctx,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__)
     67# define CF_DEBUG_END_FUNCTION                  \
     68    CF_Debug_LeaveFunction ()
    9869
    99 /**
    100  * 컨텍스트에 콜스택 푸시
    101  *
    102  * @param __ctx 디버그 컨텍스트
    103  *
    104  * @see CF_Debug_CallStackPush
    105  */
    106 # define CF_DEBUG_CALLSTACK_PUSH(__ctx)                             \
    107     CF_Debug_CallStackPush (__ctx,__FILE__,__func__,__LINE__)
    108 
    109 /**
    110  * 컨텍스트에서 콜스택 팝
    111  *
    112  * @param __ctx         디버그 컨텍스트
    113  * @param __callstack   콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
    114  *
    115  * @see CF_Debug_CallStackPop, CF_Debug_CallStack
    116  */
    117 # define CF_DEBUG_CALLSTACK_POP(__ctx,__callstack)                  \
    118     CF_Debug_CallStackPop (__ctx,__callstack)
    11970#else // #ifdef _DEBUG
    120 # define CF_DEBUG_CREATE_CTX()                          NULL
    121 # define CF_DEBUG_DESTROY_CTX(__ctx)
    12271# define CF_DEBUG_PRINT(__fp,__fmt,...)
    12372# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)
    124 # define CF_DEBUG_TRACE(__ctx,__fmt,...)
    125 # define CF_DEBUG_TRACE_BIN(__ctx,__bin,__len,__fmt,...)
    126 # define CF_DEBUG_CALLSTACK_PUSH(__ctx)
    127 # define CF_DEBUG_CALLSTACK_POP(__ctx,__callstack)      1
     73# define CF_DEBUG_BEGIN_FUNCTION
     74# define CF_DEBUG_END_FUNCTION
    12875#endif // #ifdef _DEBUG
    129 
    130 /** 디버그 컨텍스트 */
    131 typedef void *  CF_Debug_Ctx;
    132 
    133 /** 콜스택 데이터 */
    134 typedef struct cf_debug_callStack {
    135     char    file[NAME_LENGTH + 1];      /**< 파일 이름 */
    136     char    function[NAME_LENGTH + 1];  /**< 함수 이름 */
    137     int     line;                       /**< 라인 넘버 */
    138 } CF_Debug_CallStack;
    13976
    14077#ifdef __cplusplus
    14178extern "C" {
    14279#endif
    143 
    144 CF_EXPORT CF_Debug_Ctx
    145 CF_Debug_CreateCtx      (void);
    146 
    147 CF_EXPORT int
    148 CF_Debug_DestroyCtx     (CF_Debug_Ctx ctx);
    149 
    150 CF_EXPORT int
    151 CF_Debug_SetOutputFD    (CF_Debug_Ctx   ctx,
    152                          int            fd);
    15380
    15481CF_EXPORT int
     
    16996
    17097CF_EXPORT int
    171 CF_Debug_Trace          (CF_Debug_Ctx   ctx,
    172                          const char     * file,
    173                          const char     * func,
    174                          const int      line,
    175                          const char     * fmt, ...);
     98CF_Debug_PrintCallStack (FILE * fp);
    17699
    177100CF_EXPORT int
    178 CF_Debug_TraceBin       (CF_Debug_Ctx           ctx,
    179                          const char             * file,
    180                          const char             * func,
    181                          const int              line,
    182                          const unsigned char    * bin,
    183                          const int              len,
    184                          const char             * fmt, ...);
     101CF_Debug_EnterFunction  (const char * file,
     102                         const char * func,
     103                         const int  line);
    185104
    186105CF_EXPORT int
    187 CF_Debug_CallStackPush  (CF_Debug_Ctx   ctx,
    188                          const char     * file,
    189                          const char     * func,
    190                          const int      line);
    191 
    192 CF_EXPORT int
    193 CF_Debug_CallStackPop   (CF_Debug_Ctx       ctx,
    194                          CF_Debug_CallStack * callstack);
     106CF_Debug_LeaveFunction  (void);
    195107
    196108#ifdef __cplusplus
  • trunk/include/cf_error.h

    r46 r51  
    6262/*------------------------------------------------------------*/
    6363#define CF_ERROR_DEBUG_INVALID_CTX          CF_ERROR_DEBUG - 1
    64 #define CF_ERROR_DEBUG_SET_OUTPUT_FD        CF_ERROR_DEBUG - 2
    65 #define CF_ERROR_DEBUG_PUSH_CALLSTACK       CF_ERROR_DEBUG - 3
    66 #define CF_ERROR_DEBUG_POP_CALLSTACK        CF_ERROR_DEBUG - 4
     64#define CF_ERROR_DEBUG_PUSH_CALLSTACK       CF_ERROR_DEBUG - 2
     65#define CF_ERROR_DEBUG_POP_CALLSTACK        CF_ERROR_DEBUG - 3
     66#define CF_ERROR_DEBUG_PEEK_CALLSTACK       CF_ERROR_DEBUG - 4
     67#define CF_ERROR_DEBUG_ALLOCATE_CTX         CF_ERROR_DEBUG - 5
    6768/* }}} debug */
    6869
  • trunk/src/cf_debug.c

    r50 r51  
    77#include "cf_local.h"
    88#include "cf_error.h"
     9#include "cf_thread.h"
    910
    1011#include <stdlib.h>
     
    3031     : ((CF_DEBUG_CTX *)__ctx)->fp)
    3132
     33/**
     34 * 디버그 컨텍스트
     35 *
     36 * @remark change from public to private
     37 */
     38typedef void *  CF_Debug_Ctx;
     39
     40/**
     41 * 콜스택 데이터
     42 *
     43 * @remark change from public to private
     44 */
     45typedef struct cf_debug_callStack {
     46    char    file[NAME_LENGTH + 1];      /**< 파일 이름 */
     47    char    function[NAME_LENGTH + 1];  /**< 함수 이름 */
     48    int     line;                       /**< 라인 넘버 */
     49} CF_Debug_CallStack;
     50
    3251typedef struct __cf_debug_callstack__
    3352{
     
    3655    int     line;
    3756
     57    int     frameIndex;
     58
    3859    struct __cf_debug_callstack__   * caller;
    3960} S_CF_DEBUG_CALLSTACK, CF_DEBUG_CALLSTACK;
     
    4162typedef struct __cf_debug_ctx__
    4263{
    43     char    file[NAME_LENGTH + 1];
    44     char    func[NAME_LENGTH + 1];
    45     int     line;
    46 
    47     FILE    * fp;
     64    char        file[NAME_LENGTH + 1];
     65    char        func[NAME_LENGTH + 1];
     66    int         line;
     67
     68    FILE        * fp;
     69    CF_Mutex    mutex;
    4870
    4971    CF_DEBUG_CALLSTACK  callstack;
    5072} S_CF_DEBUG_CTX, CF_DEBUG_CTX;
     73
     74CF_Debug_Ctx    gDebugSingleCtx = NULL;
    5175
    5276static int
     
    117141
    118142/**
    119  * 디버그 컨텍스트를 생성
    120  *
    121  * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
    122  * @see CF_DEBUG_CREATE_CTX
    123  */
    124 CF_Debug_Ctx
    125 CF_Debug_CreateCtx (void)
    126 {
    127     CF_DEBUG_CTX * ctx = NULL;
    128 
    129     ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
    130 
    131     return (CF_Debug_Ctx) ctx;
    132 }
    133 
    134 /**
    135143 * 디버그 컨텍스트를 해제
    136144 *
     
    141149 * @see CF_DEBUG_DESTROY_CTX
    142150 */
    143 int
     151static int
    144152CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
    145153{
     
    159167    }
    160168
     169    if (context->mutex)
     170        CF_Mutex_Destory (&context->mutex);
     171
    161172    free (context);
    162173
     
    165176
    166177/**
    167  * 디버그 컨텍스트에 출력할 파일 디스크립터를 설정
    168  *
    169  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    170  *
    171  * @param ctx   디버그 컨텍스트
    172  * @param fd    파일 디스크립터
    173  *
    174  * @see CF_File_Open, CF_File_Create
    175  */
    176 int
    177 CF_Debug_SetOutputFD (CF_Debug_Ctx  ctx,
    178                       int           fd)
    179 {
    180     int result = 0;
    181     int dupfd = 0;
    182 
    183     CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
    184     FILE            * fp = NULL;
    185 
    186     CHECK_INVALID_CTX (ctx);
     178 * 디버그 컨텍스트를 생성
     179 *
     180 * @return 성공 시, CF_Debug_Ctx 형태의 컨텍스트; 실패 시, NULL
     181 * @see CF_DEBUG_CREATE_CTX
     182 */
     183static int
     184CF_Debug_CreateCtx (CF_Debug_Ctx * ctx)
     185{
     186    int             result = 0;
     187    CF_DEBUG_CTX    * context = NULL;
    187188
    188189    TRY
    189190    {
    190         dupfd = dup (fd);
    191         if (dupfd < 0)
     191        context = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
     192        if (context == NULL)
    192193        {
    193             result = -1;
     194            result = CF_ERROR_DEBUG_ALLOCATE_CTX;
    194195            TRY_BREAK;
    195196        }
    196197
    197         fp = fdopen (dupfd, "a");
    198         if (fp == NULL)
     198        result = CF_Mutex_Create (&context->mutex);
     199        if (result < 0)
    199200        {
    200             close (dupfd);
    201             result = -2;
    202201            TRY_BREAK;
    203202        }
    204203
    205         context->fp = fp;
     204        *ctx = (CF_Debug_Ctx) context;
    206205    }
    207206    CATCH_IF (result < 0)
    208207    {
    209         return CF_ERROR_DEBUG_SET_OUTPUT_FD;
     208        CF_Debug_DestroyCtx (context);
    210209    }
    211210
     
    280279
    281280/**
    282  * 컨텍스트를 업데이트하고 디버그 메시지를 출력
    283  *
    284  * @return CF_OK 반환
     281 * 컨텍스트에 콜스택 푸시
     282 *
     283 * @return 성공 시, CF_OK; 실패 시, 오류 코드
    285284 *
    286285 * @param ctx   디버그 컨텍스트
     
    288287 * @param func  함수 이름
    289288 * @param line  라인 넘버
    290  * @param fmt   포맷 스트링
    291  * @param ...   가변 인자
    292  *
    293  * @see CF_Debug_Trace
    294  */
    295 int
    296 CF_Debug_Trace (CF_Debug_Ctx    ctx,
    297                 const char      * file,
    298                 const char      * func,
    299                 const int       line,
    300                 const char      * fmt, ...)
    301 {
    302     va_list         valist;
    303     CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
    304 
    305     CHECK_INVALID_CTX (ctx);
    306 
    307     CF_Debug_Local_UpdateCtx (ctx, file, func, line);
    308 
    309     va_start (valist, fmt);
    310     CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
    311                           context->file,
    312                           context->func,
    313                           context->line,
    314                           fmt, valist);
    315     va_end (valist);
    316 
    317     return CF_OK;
    318 }
    319 
    320 /**
    321  * 컨텍스트를 업데이트하고 바이너리 데이터를 디버그 메시지와 함께 출력
    322  *
    323  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    324  *
    325  * @param ctx   디버그 컨텍스트
    326  * @param file  파일 경로
    327  * @param func  함수 이름
    328  * @param line  라인 넘버
    329  * @param bin   바이너리 데이터
    330  * @param len   바이너리 길이
    331  * @param fmt   포맷 스트링
    332  * @param ...   가변 인자
    333  *
    334  * @see CF_DEBUG_TRACE_BIN
    335  */
    336 int
    337 CF_Debug_TraceBin (CF_Debug_Ctx         ctx,
    338                    const char           * file,
    339                    const char           * func,
    340                    const int            line,
    341                    const unsigned char  * bin,
    342                    const int            len,
    343                    const char           * fmt, ...)
    344 {
    345     va_list         valist;
    346     CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
    347  
    348     CHECK_INVALID_CTX (ctx);
    349 
    350     CF_Debug_Local_UpdateCtx (ctx, file, func, line);
    351 
    352     va_start (valist, fmt);
    353     CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
    354                           context->file,
    355                           context->func,
    356                           context->line,
    357                           fmt, valist);
    358     va_end (valist);
    359 
    360     CF_Debug_Local_PrintBin (GET_CTX_OSTREAM (context),
    361                              context->file,
    362                              context->func,
    363                              context->line,
    364                              bin, len);
    365 
    366     return CF_OK;
    367 }
    368 
    369 /**
    370  * 컨텍스트에 콜스택 푸시
    371  *
    372  * @return 성공 시, CF_OK; 실패 시, 오류 코드
    373  *
    374  * @param ctx   디버그 컨텍스트
    375  * @param file  파일 경로
    376  * @param func  함수 이름
    377  * @param line  라인 넘버
    378  *
    379  * @see CF_DEBUG_CALLSTACK_PUSH
    380  */
    381 int
     289 */
     290static int
    382291CF_Debug_CallStackPush (CF_Debug_Ctx    ctx,
    383292                        const char      * file,
     
    399308    push->line = line;
    400309    push->caller = context->callstack.caller;
     310    push->frameIndex = context->callstack.frameIndex + 1;
    401311    context->callstack.caller = push;
    402312
     
    407317
    408318/**
    409  * 컨텍스트에서 콜스택 팝
     319 * 컨텍스트에서 콜스택에서 TOP을 제거하지 않고 가져옴
    410320 *
    411321 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     
    414324 * @param callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
    415325 *
    416  * @see CF_Debug_CallStackPop, CF_Debug_CallStack
    417  */
    418 int
     326 * @see CF_Debug_CallStack
     327 */
     328static int
     329CF_Debug_CallStackPeek (CF_Debug_Ctx        ctx,
     330                        CF_Debug_CallStack  * callstack)
     331{
     332    CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     333    CF_DEBUG_CALLSTACK  * pop = NULL;
     334
     335    pop = context->callstack.caller;
     336    if (pop == NULL)
     337        return CF_ERROR_DEBUG_PEEK_CALLSTACK;
     338
     339    if (callstack != NULL)
     340    {
     341        sprintf (callstack->file    , "%s", pop->file);
     342        sprintf (callstack->function, "%s", pop->func);
     343        callstack->line = pop->line;
     344    }
     345
     346    return CF_OK;
     347}
     348
     349/**
     350 * 컨텍스트에서 콜스택 팝
     351 *
     352 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     353 *
     354 * @param ctx       디버그 컨텍스트
     355 * @param callstack 콜스택 정보를 가져올 콜스택 데이터 구조체 포인터
     356 *
     357 * @see CF_Debug_CallStack
     358 */
     359static int
    419360CF_Debug_CallStackPop (CF_Debug_Ctx         ctx,
    420361                       CF_Debug_CallStack   * callstack)
     
    429370        return CF_ERROR_DEBUG_POP_CALLSTACK;
    430371
    431     if (callstack != NULL)
    432     {
    433         sprintf (callstack->file    , "%s", pop->file);
    434         sprintf (callstack->function, "%s", pop->func);
    435         callstack->line = pop->line;
    436     }
     372    if (CF_Debug_CallStackPeek (ctx, callstack) < 0)
     373        return CF_ERROR_DEBUG_PEEK_CALLSTACK;
    437374
    438375    memset (context->file, 0x00, sizeof (context->file));
     
    454391    return CF_OK;
    455392}
     393
     394/**
     395 * 현재 콜스택을 출력
     396 *
     397 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     398 *
     399 * @param fp 출력 할 파일 포인터
     400 */
     401int
     402CF_Debug_PrintCallStack (FILE * fp)
     403{
     404    int iter = 0;
     405
     406    CF_DEBUG_CTX        * ctx = gDebugSingleCtx;
     407    CF_DEBUG_CALLSTACK  * callstack = NULL;
     408
     409    if (ctx == NULL)
     410        return CF_ERROR_DEBUG_INVALID_CTX;
     411
     412    callstack = &ctx->callstack;
     413
     414    do
     415    {
     416        if (callstack == NULL)
     417            break;
     418
     419        fprintf (fp, "#%04d %s (%s:%d)\n",
     420                     iter,
     421                     callstack->func,
     422                     callstack->file,
     423                     callstack->line);
     424
     425        callstack = callstack->caller;
     426    } while (iter++ < ctx->callstack.frameIndex);
     427
     428    return CF_OK;
     429}
     430
     431/**
     432 * 함수 진입을 명시
     433 *
     434 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     435 *
     436 * @param file  파일 경로
     437 * @param func  함수 이름
     438 * @param line  라인 넘버
     439 *
     440 * @see CF_Debug_LeaveFunction
     441 */
     442int
     443CF_Debug_EnterFunction (const char  * file,
     444                        const char  * func,
     445                        const int   line)
     446{
     447    int             result = 0;
     448    CF_DEBUG_CTX    * ctx = NULL;
     449
     450    if (gDebugSingleCtx == NULL)
     451    {
     452        result = CF_Debug_CreateCtx (&gDebugSingleCtx);
     453        if (result != CF_OK)
     454            return result;
     455    }
     456    ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     457
     458    CF_Mutex_Lock (&ctx->mutex);
     459    CF_Debug_CallStackPush (gDebugSingleCtx, file, func, line);
     460    CF_Mutex_Unlock (&ctx->mutex);
     461
     462    return CF_OK;
     463}
     464
     465/**
     466 * 함수 종료를 명시
     467 *
     468 * @return 성공 시, CF_OK; 실패 시, 오류 코드
     469 *
     470 * @see CF_Debug_EnterFunction
     471 */
     472int
     473CF_Debug_LeaveFunction (void)
     474{
     475    CF_DEBUG_CTX * ctx = (CF_DEBUG_CTX *)gDebugSingleCtx;
     476
     477    if (ctx == NULL)
     478        return CF_ERROR_DEBUG_INVALID_CTX;
     479
     480    CF_Mutex_Lock (&ctx->mutex);
     481    CF_Debug_CallStackPop (gDebugSingleCtx, NULL);
     482    CF_Mutex_Unlock (&ctx->mutex);
     483
     484    return CF_OK;
     485}
  • trunk/src/cf_log.c

    r50 r51  
    3030#define LOCK_LOG_CTX(__ctx)             CF_Mutex_Lock (&__ctx->mutex)
    3131#define UNLOCK_LOG_CTX(__ctx)           CF_Mutex_Unlock (&__ctx->mutex)
    32 #define CHECK_INITIALIZED()             (gLogEnvironment.ctxPool == NULL||  \
    33                                          gLogEnvironment.ctxSize <= 0   )
    34 #define CHECK_INVALID_MAPID(__mapid)    (gLogEnvironment.ctxSize <= __mapid)
    35 #define CHECK_MAPPED_ID(__mapid)        (gLogEnvironment.ctxPool[__mapid] != NULL)  \
    36 
     32#define CHECK_INITIALIZED()             (gLogArray.ctxPool == NULL||    \
     33                                         gLogArray.ctxSize <= 0 )
     34#define CHECK_INVALID_MAPID(__mapid)    (gLogArray.ctxSize <= __mapid)
     35#define CHECK_MAPPED_ID(__mapid)        (gLogArray.ctxPool[__mapid] != NULL)
    3736
    3837#define LOG_BUFFER_DEFAULT_SIZE         128 * 1024
    3938
    40 #define LOG_DATETIME_LENGTH         sizeof ("0000-00-00 00:00:00.000") - 1
     39#define LOG_DATETIME_LENGTH             sizeof ("0000-00-00 00:00:00.000") - 1
    4140
    4241/**
     
    6968} S_CF_LOG_CTX, CF_LOG_CTX;
    7069
    71 typedef struct __cf_log_environment__ {
     70typedef struct __cf_log_array__ {
    7271    CF_Log_Ctx  * ctxPool;
    7372    int         ctxSize;
    74 } S_CF_LOG_ENVIRONMENT, CF_LOG_ENVIRONMENT;
    75 
    76 static CF_LOG_ENVIRONMENT   gLogEnvironment;
     73} S_CF_LOG_ARRAY, CF_LOG_ARRAY;
     74
     75static CF_LOG_ARRAY gLogArray;
    7776
    7877#if defined(_WIN32) || defined(_WIN64)
     
    475474    }
    476475
    477     gLogEnvironment.ctxPool[mapid] = ctx;
     476    gLogArray.ctxPool[mapid] = ctx;
    478477
    479478    return CF_OK;
     
    501500        return CF_ERROR_LOG_NOT_MAPPED_ID;
    502501
    503     CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]);
    504 
    505     free (gLogEnvironment.ctxPool[mapid]);
    506     gLogEnvironment.ctxPool[mapid] = NULL;
     502    CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
     503
     504    free (gLogArray.ctxPool[mapid]);
     505    gLogArray.ctxPool[mapid] = NULL;
    507506
    508507    return CF_OK;
     
    528527        return CF_ERROR_LOG_NOT_MAPPED_ID;
    529528
    530     *ctx = gLogEnvironment.ctxPool[mapid];
     529    *ctx = gLogArray.ctxPool[mapid];
    531530
    532531    return CF_OK;
     
    543542CF_Log_Initialize (const int logPool)
    544543{
    545     memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT));
     544    memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
    546545
    547546    if (logPool > 0)
    548547    {
    549         gLogEnvironment.ctxPool =
     548        gLogArray.ctxPool =
    550549            (CF_Log_Ctx *) calloc ((size_t) logPool, sizeof (CF_Log_Ctx));
    551         if (gLogEnvironment.ctxPool == NULL)
     550        if (gLogArray.ctxPool == NULL)
    552551            return CF_ERROR_LOG_INITIALIZE;
    553         gLogEnvironment.ctxSize = logPool;
     552        gLogArray.ctxSize = logPool;
    554553    }
    555554
     
    567566    int mapid = 0;
    568567
    569     for (mapid = 0 ; mapid < gLogEnvironment.ctxSize ; mapid++)
     568    for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
    570569    {
    571570        CF_Log_UnmapCtxID (mapid);
    572571    }
    573572
    574     if (gLogEnvironment.ctxPool != NULL)
    575         free (gLogEnvironment.ctxPool);
    576 
    577     memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT));
     573    if (gLogArray.ctxPool != NULL)
     574        free (gLogArray.ctxPool);
     575
     576    memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
    578577
    579578    return CF_OK;
  • trunk/test/debug.c

    r35 r51  
    77#include "cf_debug.h"
    88
    9 CF_Debug_Ctx gDebugCtx;
     9#include <string.h>
    1010
    1111void callee (void) {
    12     CF_Debug_CallStack callstack;
    13    
    14     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    15     CF_DEBUG_CALLSTACK_POP (gDebugCtx, &callstack);
     12    CF_DEBUG_BEGIN_FUNCTION;
     13
     14    CF_Debug_PrintCallStack (stdout);
     15
     16    CF_DEBUG_END_FUNCTION;
    1617}
    1718
    1819int main (void) {
    19     int fd = 0;
    20     gDebugCtx = CF_Debug_CreateCtx ();
     20    char data[] =
     21        "【 曲名 : 사랑하고 있는데 】\n"
     22        "《 歌 : Kylee 》\n"
     23        "『 절원의 템페스트 OP2 Theme 』\n";
    2124
    22     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     25    CF_DEBUG_BEGIN_FUNCTION;
    2326
    24     CF_DEBUG_TRACE (gDebugCtx, "print trace message with context to stderr\n");
     27    CF_DEBUG_PRINT (stderr, "print message with context to stderr\n");
    2528
    26     fd = CF_File_Create ("debug.txt");
    27     if (fd < 0) {
    28         CF_DEBUG_PRINT (stderr, "error ...\n");
    29         // error
    30     }
    31     CF_Debug_SetOutputFD (gDebugCtx, fd);
     29    CF_DEBUG_PRINT_BIN (stdout, (unsigned char *) data, (int) sizeof (data), "hehehehe\n");
    3230
    33     CF_DEBUG_TRACE (gDebugCtx, "print trace message with context to debug.txt\n");
     31    callee ();
    3432
    35     CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
    36 
    37     CF_Debug_DestroyCtx (gDebugCtx);
     33    CF_DEBUG_END_FUNCTION;
    3834
    3935    return 0;
  • trunk/test/test.c

    r49 r51  
    2222#define PORT        1234
    2323
    24 CF_Debug_Ctx        gDebugCtx;
    25 CF_Debug_CallStack  gDebugCallstack;
    2624CF_Mutex            globalMutex;
    2725
     
    6361    CF_Thread   tid[10];
    6462
    65     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     63    CF_DEBUG_BEGIN_FUNCTION;
    6664
    6765    /* initialize */
     
    125123    /* finalize */
    126124    CF_Log_Finalize ();
     125
     126    CF_DEBUG_END_FUNCTION;
    127127}
    128128
     
    132132    char    buffer[128] = {0x00,};
    133133
    134     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     134    CF_DEBUG_BEGIN_FUNCTION;
    135135
    136136    fd = CF_File_Open (file, CF_FILE_RO);
    137137    if (fd < 0)
    138         CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
     138        CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
    139139
    140140    if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
    141         CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
     141        CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
    142142    else
    143         CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
    144                                         sizeof (buffer),
    145                                         "-- %d bytes of %d bytes\n",
    146                                         sizeof (buffer),
    147                                         CF_File_GetSize (fd));
     143        CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
     144                                    sizeof (buffer),
     145                                    "-- %d bytes of %d bytes\n",
     146                                    sizeof (buffer),
     147                                    CF_File_GetSize (fd));
    148148
    149149    CF_File_Close (fd);
    150150
    151     CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     151    CF_DEBUG_END_FUNCTION;
    152152}
    153153
    154154void test_callstack3 (void)
    155155{
    156     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    157     while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
    158     {
    159         CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
    160                                     gDebugCallstack.file,
    161                                     gDebugCallstack.line,
    162                                     gDebugCallstack.function);
    163     }
    164     CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     156    CF_DEBUG_BEGIN_FUNCTION;
     157    CF_Debug_PrintCallStack (stdout);
     158    CF_DEBUG_END_FUNCTION;
    165159}
    166160void test_callstack2 (void)
    167161{
    168     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     162    CF_DEBUG_BEGIN_FUNCTION;
    169163    test_callstack3 ();
    170     CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     164    CF_DEBUG_END_FUNCTION;
    171165}
    172166void test_callstack1 (void)
    173167{
    174     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     168    CF_DEBUG_BEGIN_FUNCTION;
    175169    test_callstack2 ();
    176     CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
     170    CF_DEBUG_END_FUNCTION;
    177171}
    178172
     
    333327int main (int argc, char ** argv)
    334328{
    335     gDebugCtx = CF_Debug_CreateCtx ();
    336 
    337     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     329    CF_DEBUG_BEGIN_FUNCTION;
    338330
    339331    // 1
     
    353345    test_socket ();
    354346
    355     CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
    356 
    357347    CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
    358     CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
    359                                 gDebugCallstack.file,
    360                                 gDebugCallstack.line,
    361                                 gDebugCallstack.function);
    362 
    363     CF_Debug_DestroyCtx (gDebugCtx);
     348
     349    CF_DEBUG_END_FUNCTION;
    364350
    365351    return 0;
Note: See TracChangeset for help on using the changeset viewer.