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 | CF_Log_Ctx gLogCtx;
|
---|
16 |
|
---|
17 | const char * file = "./log.txt";
|
---|
18 |
|
---|
19 | void log_test (const char * message)
|
---|
20 | {
|
---|
21 | int i = 0;
|
---|
22 |
|
---|
23 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
24 |
|
---|
25 | gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
|
---|
26 | if (gDebugCtx == NULL)
|
---|
27 | CF_DEBUG_PRINT (stderr, "create debug ctx error\n");
|
---|
28 |
|
---|
29 | for (i = 0 ; i < 10000 ; i++)
|
---|
30 | CF_Log_Write (gLogCtx, "TEST", "turn %d\n", i);
|
---|
31 |
|
---|
32 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
33 |
|
---|
34 | CF_Log_Write (gLogCtx, message,
|
---|
35 | "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
36 | gDebugCallstack.file,
|
---|
37 | gDebugCallstack.line,
|
---|
38 | gDebugCallstack.function);
|
---|
39 |
|
---|
40 | CF_Log_DestroyCtx (gLogCtx);
|
---|
41 | }
|
---|
42 |
|
---|
43 | void file_test (const char * message)
|
---|
44 | {
|
---|
45 | int size = 0;
|
---|
46 | int fd = 0;
|
---|
47 | char * buffer = NULL;
|
---|
48 |
|
---|
49 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
50 |
|
---|
51 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
52 | if (fd < 0)
|
---|
53 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
|
---|
54 |
|
---|
55 | size = CF_File_GetSize (fd);
|
---|
56 | if (size < 0)
|
---|
57 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n");
|
---|
58 |
|
---|
59 | buffer = (char *) calloc (1, (size_t) (size + 1));
|
---|
60 | if (buffer == NULL)
|
---|
61 | CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n");
|
---|
62 |
|
---|
63 | if (CF_File_Read (fd, buffer, (size_t)size) < 0)
|
---|
64 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
|
---|
65 | else
|
---|
66 | CF_DEBUG_TRACE (gDebugCtx, "%s", buffer);
|
---|
67 |
|
---|
68 | CF_File_Close (fd);
|
---|
69 |
|
---|
70 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void callee3 (void)
|
---|
74 | {
|
---|
75 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
76 | while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
|
---|
77 | {
|
---|
78 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
79 | gDebugCallstack.file,
|
---|
80 | gDebugCallstack.line,
|
---|
81 | gDebugCallstack.function);
|
---|
82 | }
|
---|
83 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
84 | }
|
---|
85 | void callee2 (void)
|
---|
86 | {
|
---|
87 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
88 | callee3 ();
|
---|
89 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
90 | }
|
---|
91 | void callee1 (void)
|
---|
92 | {
|
---|
93 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
94 | callee2 ();
|
---|
95 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
96 | }
|
---|
97 |
|
---|
98 | int main (int argc, char ** argv)
|
---|
99 | {
|
---|
100 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
101 |
|
---|
102 | if (gLogCtx == NULL)
|
---|
103 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
104 |
|
---|
105 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
106 |
|
---|
107 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
108 | log_test ("LOG_TEST");
|
---|
109 |
|
---|
110 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
111 | file_test ("FILE_READ_TEST");
|
---|
112 |
|
---|
113 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
114 | callee1 ();
|
---|
115 |
|
---|
116 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
117 |
|
---|
118 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
119 | CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
120 | gDebugCallstack.file,
|
---|
121 | gDebugCallstack.line,
|
---|
122 | gDebugCallstack.function);
|
---|
123 |
|
---|
124 | CF_Debug_DestroyCtx (gDebugCtx);
|
---|
125 |
|
---|
126 | return 0;
|
---|
127 | }
|
---|