Changeset 105 in libcf for trunk/test/log.c


Ignore:
Timestamp:
05/29/13 14:10:04 (11 years ago)
Author:
cheese
Message:

#1 remove integrated test code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/log.c

    r103 r105  
    44 */
    55#include "cf_log.h"
     6#include "cf_thread.h"
     7#include "cf_debug.h"
     8
    69#include <stdio.h>
     10
     11CF_Mutex globalMutex;
     12
     13#define LOG_MT      1
     14
     15int test_log_mt (void * arg)
     16{
     17    static int cnt = 0;
     18    int i = 0;
     19    int th = 0;
     20   
     21    if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section
     22        // error
     23    }
     24    th = cnt++;
     25    if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section
     26        // error
     27    }
     28
     29    CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
     30
     31    for (i = 0 ; i < 100000 ; i++)
     32    {
     33        CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
     34    }
     35
     36    CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
     37
     38    return 0;
     39}
    740
    841int main (void)
    942{
    1043    int         i, j;
    11     char        logname[16] = {0x00,};
     44    char        idname[16] = {0x00,};
     45    CF_Thread   tid[10];
    1246
    13     if (CF_Log_Initialize (10, CF_LOG_NO_LEVEL) < 0)
     47    /* initialize */
     48    if (CF_Log_Initialize (10, 2) < 0)
    1449    {
    1550        fprintf (stderr, "failed to init. log\n");
     
    1954    for (i = 0 ; i < 10 ; i++)
    2055    {
    21         sprintf (logname, "logid%d.txt", i);
    22         if (CF_Log_Open (i, logname, CF_LOG_NO_BUFFER) < 0)
     56        sprintf (idname, "logid%d.txt", i);
     57        if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
    2358            fprintf (stderr, "failed to open log\n");
    2459    }
     
    3772    }
    3873
     74    /* mt {{{ */
     75    if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_DEFAULT_BUFFER) < 0)
     76        CF_DEBUG_PRINT (stderr, "create log ctx error\n");
     77
     78    if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
     79        CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
     80
     81    if (CF_Mutex_Create (&globalMutex) < 0) {
     82        // error
     83    }
     84
     85    for (i = 0 ; i < 10 ; i++)
     86    {
     87        if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
     88        {
     89            CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
     90            return -2;
     91        }
     92    }
     93
     94    for (i = 0 ; i < 10 ; i++)
     95    {
     96        if (CF_Thread_Join (&tid[i]) < 0)
     97            CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
     98        if (CF_Thread_Release (&tid[i]) < 0)
     99            CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
     100    }
     101
     102    if (CF_Mutex_Destory (&globalMutex) < 0) {
     103        // error
     104    }
     105    /* }}} mt */
     106
     107    /* finalize */
    39108    CF_Log_Finalize ();
    40109
Note: See TracChangeset for help on using the changeset viewer.