Changeset 38 in libcf for trunk


Ignore:
Timestamp:
02/06/13 10:26:54 (11 years ago)
Author:
cheese
Message:

#1 modify test.c for more detail test of multi-threaded logging

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_log.h

    r35 r38  
    3030#define CF_LOG_BUFFER_DEFAULT           -1
    3131
    32 /** 로그 기록 시 버퍼를 사용하지 않고 즉시 기록
     32/**
     33 * 로그 기록 시 버퍼를 사용하지 않고 즉시 기록
    3334 *
    3435 * @see CF_Log_CreateCtx, CF_LOG_OPEN, CF_Log_Initialize
  • trunk/src/cf_log.c

    r35 r38  
    8484    // Initialize the present time to 0 and the timezone to UTC
    8585    unsigned __int64 ui64 =0;
    86     static int tzflag = 0;
     86    //static int tzflag = 0;
    8787
    8888    if (NULL != tv)
     
    105105    }
    106106
     107    /*
    107108    if (NULL != tz)
    108109    {
     
    117118        tz->tz_dsttime = _daylight;
    118119    }
     120    */
    119121
    120122    return 0;
     
    166168        return CF_ERROR_LOG_FLUSH;
    167169
    168     memset (ctx->buffer, 0x00, ctx->size);
    169170    ctx->length = 0;
    170171
     
    209210    else /* flush되어야 함. */
    210211    {
    211         if (CF_File_Write (ctx->fd, buffer, demandSize) < 0)
     212        ctx->buffer = (char *)buffer;
     213        ctx->length = demandSize;
     214
     215        if (CF_Log_Local_Flush (ctx) < 0)
    212216            return CF_ERROR_LOG_FLUSH;
    213217    }
     
    338342        return CF_ERROR_LOG_INVALID_CTX;
    339343
    340     memset (context->path, 0x00, sizeof (context->path));
    341 
    342     if (context->buffer != NULL)
     344    if (context->size > 0)
    343345    {
    344346        CF_Log_Flush (ctx);
  • trunk/test/makefile

    r35 r38  
    8181    make dep
    8282    $(LD) $(LDFLAGS) $(LIBS) $(CDEFS) $(CFLAGS) $(INCLUDES) -o $(TARGET_PATH)/$@.$(EXT_EXECUTE) $?
    83     LD_LIBRARY_PATH=$(TARGET_PATH) $(TARGET_PATH)/$@.$(EXT_EXECUTE)
     83    cd $(TARGET_PATH); LD_LIBRARY_PATH=. ./$@.$(EXT_EXECUTE)
    8484
    8585clean: dummy
  • trunk/test/test.c

    r35 r38  
    2929const char * file = "./log.txt";
    3030
     31CF_THREAD_RETURN CF_THREAD_CALL test_log_mt (void * arg)
     32{
     33    int i = 0;
     34    int th = *((int *)arg);
     35
     36    for (i = 0 ; i < 100000 ; i++)
     37    {
     38        CF_Log_Write (gLogCtx, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
     39    }
     40
     41    return (CF_THREAD_RETURN)0;
     42}
     43
    3144void test_log (const char * message)
    3245{
    3346    int         i, j;
    3447    char        idname[16] = {0x00,};
     48    CF_Thread   tid[10];
    3549
    3650    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     
    4458        CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    4559
    46     for (i = 0 ; i < 10000 ; i++)
     60    for (i = 0 ; i < 1000 ; i++)
    4761        CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
    4862
     
    7488    /* }}} id */
    7589
     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)
     96        CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
     97
     98    for (i = 0 ; i < 10 ; i++)
     99    {
     100        if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
     101        {
     102            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
     103            return ;
     104        }
     105        CF_DEBUG_PRINT (stderr, "created %dth thread\n", i);
     106    }
     107
     108    for (i = 0 ; i < 10 ; i++)
     109    {
     110        if (CF_Thread_Join (&tid[i]) < 0)
     111            CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
     112    }
     113
     114    CF_Log_DestroyCtx (gLogCtx);
     115    /* }}} mt */
     116
    76117    /* finalize */
    77118    CF_Log_Finalize ();
     
    89130        CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
    90131
    91     if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0)
     132    if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
    92133        CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
    93134    else
    94135        CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
    95                                         sizeof (buffer) - 1,
     136                                        sizeof (buffer),
    96137                                        "-- %d bytes of %d bytes\n",
    97                                         sizeof (buffer) - 1,
     138                                        sizeof (buffer),
    98139                                        CF_File_GetSize (fd));
    99140
     
    134175    int recvd = 0;
    135176    char buf[1024] = {0x00,};
     177    int i = 0;
    136178
    137179    /*------------------------------------------------------------*/
     
    142184        return (CF_THREAD_RETURN)-1;
    143185    }
    144     CF_DEBUG_PRINT (stderr, "SERVER : accepted\n");
    145186    CF_Log_Write (gLogCtx, "SERVER", "accepted\n");
    146187
    147     if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
    148     {
    149         CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
    150         return (CF_THREAD_RETURN)-2;
    151     }
    152     CF_DEBUG_PRINT (stderr, "SERVER : recv {%s}\n", buf);
    153     CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
    154 
    155     if (CF_Socket_Send (clntsock, buf, recvd) < 0)
    156     {
    157         CF_DEBUG_PRINT (stderr, "failed to send on server\n");
    158         return (CF_THREAD_RETURN)-3;
    159     }
    160     CF_DEBUG_PRINT (stderr, "SERVER : resp {%s}\n", buf);
    161     CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
     188    for (i = 0 ; i < 100 ; i++)
     189    {
     190        if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
     191        {
     192            CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
     193            return (CF_THREAD_RETURN)-2;
     194        }
     195        CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
     196
     197        if (CF_Socket_Send (clntsock, buf, recvd) < 0)
     198        {
     199            return (CF_THREAD_RETURN)-3;
     200        }
     201        CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
     202    }
     203
     204    CF_Socket_Close (clntsock);
    162205
    163206    return (CF_THREAD_RETURN)0;
     
    170213    int recvd = 0;
    171214    char buf[1024] = {0x00,};
     215    int i = 0;
    172216
    173217    sprintf (buf, "...wow ? is it succeed ?");
     
    180224        return (CF_THREAD_RETURN)-1;
    181225    }
    182     CF_DEBUG_PRINT (stderr, "CLIENT : connected\n");
    183226    CF_Log_Write (gLogCtx, "CLIENT", "connected\n");
    184227
    185     if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
    186     {
    187         CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
    188         return (CF_THREAD_RETURN)-2;
    189     }
    190     CF_DEBUG_PRINT (stderr, "CLIENT : sent {%s}\n", buf);
    191     CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
    192 
    193     memset (buf, 0x00, sizeof (buf));
    194 
    195     if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
    196     {
    197         CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
    198         return (CF_THREAD_RETURN)-3;
    199     }
    200     CF_DEBUG_PRINT (stderr, "CLIENT : recv {%s}\n", buf);
    201     CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
     228    for (i = 0 ; i < 100 ; i++)
     229    {
     230        if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
     231        {
     232            CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
     233            return (CF_THREAD_RETURN)-2;
     234        }
     235        CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
     236
     237        memset (buf, 0x00, sizeof (buf));
     238
     239        if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
     240        {
     241            CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
     242            return (CF_THREAD_RETURN)-3;
     243        }
     244        CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
     245    }
    202246
    203247    CF_Socket_Close (sock);
     
    267311    for (iter = 0 ; iter < THREAD_POOL ; iter++)
    268312    {
    269         CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]);
    270313        CF_Thread_Join (&tid[iter]);
    271314        CF_Thread_Release (&tid[iter]);
     
    300343
    301344    // 4
    302     CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST with MULTI-THREADED LOGGING ==\n");
     345    CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
    303346    test_socket ();
    304347
Note: See TracChangeset for help on using the changeset viewer.