1 | /**
|
---|
2 | * @file log.c
|
---|
3 | * @author myusgun <myusgun@gmail.com>
|
---|
4 | */
|
---|
5 | #include "cf_log.h"
|
---|
6 | #include "cf_thread.h"
|
---|
7 | #include "cf_debug.h"
|
---|
8 |
|
---|
9 | #include <stdio.h>
|
---|
10 |
|
---|
11 | CF_Mutex globalMutex;
|
---|
12 |
|
---|
13 | #define LOG_MT 1
|
---|
14 |
|
---|
15 | int 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 | }
|
---|
40 |
|
---|
41 | int main (void)
|
---|
42 | {
|
---|
43 | int i, j;
|
---|
44 | char idname[16] = {0x00,};
|
---|
45 | CF_Thread tid[10];
|
---|
46 |
|
---|
47 | /* initialize */
|
---|
48 | if (CF_Log_Initialize (10, 2) < 0)
|
---|
49 | {
|
---|
50 | fprintf (stderr, "failed to init. log\n");
|
---|
51 | return -1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | for (i = 0 ; i < 10 ; i++)
|
---|
55 | {
|
---|
56 | sprintf (idname, "logid%d.txt", i);
|
---|
57 | if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0)
|
---|
58 | fprintf (stderr, "failed to open log\n");
|
---|
59 | }
|
---|
60 |
|
---|
61 | for (i = 0 ; i < 10 ; i++)
|
---|
62 | {
|
---|
63 | for (j = 0 ; j < 10000 ; j++)
|
---|
64 | {
|
---|
65 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
66 | if (result < 0)
|
---|
67 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
68 | }
|
---|
69 |
|
---|
70 | if (CF_Log_Close (i) < 0)
|
---|
71 | fprintf (stderr, "failed to close log\n");
|
---|
72 | }
|
---|
73 |
|
---|
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 */
|
---|
108 | CF_Log_Finalize ();
|
---|
109 |
|
---|
110 | return 0;
|
---|
111 | }
|
---|