1 | /*
|
---|
2 | * cf.c
|
---|
3 | */
|
---|
4 | #include "cf_file.h"
|
---|
5 | #include "cf_debug.h"
|
---|
6 | #include "cf_log.h"
|
---|
7 | #include "cf_socket.h"
|
---|
8 | #include "cf_thread.h"
|
---|
9 |
|
---|
10 | #include <stdlib.h>
|
---|
11 |
|
---|
12 | CF_Debug_Ctx gDebugCtx;
|
---|
13 | CF_Debug_CallStack gDebugCallstack;
|
---|
14 |
|
---|
15 | const char * file = "./log.txt";
|
---|
16 |
|
---|
17 | void log_test (const char * message)
|
---|
18 | {
|
---|
19 | int i, j;
|
---|
20 | char idname[16] = {0x00,};
|
---|
21 | CF_Log_Ctx gLogCtx;
|
---|
22 |
|
---|
23 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
24 |
|
---|
25 | CF_Log_Initialize (10);
|
---|
26 |
|
---|
27 | gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
|
---|
28 | if (gLogCtx == NULL)
|
---|
29 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
30 |
|
---|
31 | for (i = 0 ; i < 10000 ; i++)
|
---|
32 | CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
|
---|
33 |
|
---|
34 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
35 |
|
---|
36 | CF_Log_Write (gLogCtx, message,
|
---|
37 | "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
38 | gDebugCallstack.file,
|
---|
39 | gDebugCallstack.line,
|
---|
40 | gDebugCallstack.function);
|
---|
41 |
|
---|
42 | CF_Log_DestroyCtx (gLogCtx);
|
---|
43 |
|
---|
44 | ///////////////////
|
---|
45 |
|
---|
46 | for (i = 0 ; i < 10 ; i++)
|
---|
47 | {
|
---|
48 | sprintf (idname, "logid%d.txt", i);
|
---|
49 | CF_LOG_OPEN (i, idname, CF_LOG_BUFFER_NO);
|
---|
50 | }
|
---|
51 |
|
---|
52 | for (i = 0 ; i < 10 ; i++)
|
---|
53 | {
|
---|
54 | for (j = 0 ; j < 10000 ; j++)
|
---|
55 | CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
56 |
|
---|
57 | CF_LOG_CLOSE (i);
|
---|
58 | }
|
---|
59 |
|
---|
60 | CF_Log_Finalize ();
|
---|
61 | }
|
---|
62 |
|
---|
63 | void file_test (const char * message)
|
---|
64 | {
|
---|
65 | int size = 0;
|
---|
66 | int fd = 0;
|
---|
67 | char * buffer = NULL;
|
---|
68 |
|
---|
69 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
70 |
|
---|
71 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
72 | if (fd < 0)
|
---|
73 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
|
---|
74 |
|
---|
75 | size = CF_File_GetSize (fd);
|
---|
76 | if (size < 0)
|
---|
77 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n");
|
---|
78 |
|
---|
79 | buffer = (char *) calloc (1, (size_t) (size + 1));
|
---|
80 | if (buffer == NULL)
|
---|
81 | CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n");
|
---|
82 |
|
---|
83 | if (CF_File_Read (fd, buffer, (size_t)size) < 0)
|
---|
84 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
|
---|
85 | else
|
---|
86 | CF_DEBUG_TRACE (gDebugCtx, "%s", buffer);
|
---|
87 |
|
---|
88 | CF_File_Close (fd);
|
---|
89 |
|
---|
90 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void callee3 (void)
|
---|
94 | {
|
---|
95 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
96 | while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
|
---|
97 | {
|
---|
98 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
99 | gDebugCallstack.file,
|
---|
100 | gDebugCallstack.line,
|
---|
101 | gDebugCallstack.function);
|
---|
102 | }
|
---|
103 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
104 | }
|
---|
105 | void callee2 (void)
|
---|
106 | {
|
---|
107 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
108 | callee3 ();
|
---|
109 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
110 | }
|
---|
111 | void callee1 (void)
|
---|
112 | {
|
---|
113 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
114 | callee2 ();
|
---|
115 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
116 | }
|
---|
117 |
|
---|
118 | int main (int argc, char ** argv)
|
---|
119 | {
|
---|
120 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
121 |
|
---|
122 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
123 |
|
---|
124 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
125 | log_test ("LOG_TEST");
|
---|
126 |
|
---|
127 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
128 | file_test ("FILE_READ_TEST");
|
---|
129 |
|
---|
130 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
131 | callee1 ();
|
---|
132 |
|
---|
133 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
134 |
|
---|
135 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
136 | CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
137 | gDebugCallstack.file,
|
---|
138 | gDebugCallstack.line,
|
---|
139 | gDebugCallstack.function);
|
---|
140 |
|
---|
141 | CF_Debug_DestroyCtx (gDebugCtx);
|
---|
142 |
|
---|
143 | return 0;
|
---|
144 | }
|
---|