[14] | 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 |
|
---|
[15] | 10 | #include <stdlib.h>
|
---|
| 11 |
|
---|
[14] | 12 | CF_Debug_Ctx gDebugCtx;
|
---|
| 13 | CF_Debug_CallStack gDebugCallstack;
|
---|
| 14 |
|
---|
| 15 | CF_Log_Ctx gLogCtx;
|
---|
| 16 |
|
---|
[15] | 17 | const char * file = "./log.txt";
|
---|
| 18 |
|
---|
| 19 | void log_test (const char * message)
|
---|
[14] | 20 | {
|
---|
[15] | 21 | int i = 0;
|
---|
| 22 |
|
---|
[14] | 23 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 24 |
|
---|
[15] | 25 | gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
|
---|
| 26 | if (gDebugCtx == NULL)
|
---|
| 27 | CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n");
|
---|
[14] | 28 |
|
---|
[15] | 29 | for (i = 0 ; i < 10000 ; i++)
|
---|
| 30 | CF_Log_Write (gLogCtx, "TEST", "turn %d\n", i);
|
---|
| 31 |
|
---|
[14] | 32 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 33 |
|
---|
| 34 | CF_Log_Write (gLogCtx, message,
|
---|
[14] | 35 | "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
| 36 | gDebugCallstack.file,
|
---|
| 37 | gDebugCallstack.line,
|
---|
| 38 | gDebugCallstack.function);
|
---|
[15] | 39 |
|
---|
| 40 | CF_Log_DestroyCtx (gLogCtx);
|
---|
[14] | 41 | }
|
---|
| 42 |
|
---|
[15] | 43 | void file_test (const char * message)
|
---|
[14] | 44 | {
|
---|
[15] | 45 | int size = 0;
|
---|
| 46 | int fd = 0;
|
---|
| 47 | char * buffer = NULL;
|
---|
[14] | 48 |
|
---|
[15] | 49 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 50 |
|
---|
| 51 | fd = CF_File_Open (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)
|
---|
[14] | 77 | {
|
---|
[15] | 78 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
| 79 | gDebugCallstack.file,
|
---|
| 80 | gDebugCallstack.line,
|
---|
| 81 | gDebugCallstack.function);
|
---|
[14] | 82 | }
|
---|
[15] | 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 | }
|
---|
[14] | 97 |
|
---|
[15] | 98 | int main (int argc, char ** argv)
|
---|
| 99 | {
|
---|
| 100 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
| 101 |
|
---|
[14] | 102 | if (gLogCtx == NULL)
|
---|
| 103 | CF_DEBUG_TRACE (gDebugCtx, "create log ctx error\n");
|
---|
| 104 |
|
---|
| 105 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 106 |
|
---|
[15] | 107 | CF_DEBUG_TRACE (gDebugCtx, " == LOG TEST ==\n");
|
---|
| 108 | log_test ("LOG_TEST");
|
---|
[14] | 109 |
|
---|
[15] | 110 | CF_DEBUG_TRACE (gDebugCtx, " == FILE READ TEST ==\n");
|
---|
| 111 | file_test ("FILE_READ_TEST");
|
---|
| 112 |
|
---|
| 113 | CF_DEBUG_TRACE (gDebugCtx, " == CALLSTACK TEST ==\n");
|
---|
| 114 | callee1 ();
|
---|
| 115 |
|
---|
[14] | 116 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 117 |
|
---|
| 118 | CF_DEBUG_TRACE (gDebugCtx, " == END OF TEST ==\n");
|
---|
[14] | 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 | }
|
---|