/* * cf.c */ #include "cf_file.h" #include "cf_debug.h" #include "cf_log.h" #include "cf_socket.h" #include "cf_thread.h" #include CF_Debug_Ctx gDebugCtx; CF_Debug_CallStack gDebugCallstack; CF_Log_Ctx gLogCtx; const char * file = "./log.txt"; void log_test (const char * message) { int i = 0; CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); if (gDebugCtx == NULL) CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n"); for (i = 0 ; i < 10000 ; i++) CF_Log_Write (gLogCtx, "TEST", "turn %d\n", i); CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); CF_Log_Write (gLogCtx, message, "here is the end of function [file:%s line:%d func:%s]\n", gDebugCallstack.file, gDebugCallstack.line, gDebugCallstack.function); CF_Log_DestroyCtx (gLogCtx); } void file_test (const char * message) { int size = 0; int fd = 0; char * buffer = NULL; CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); fd = CF_File_Open (file, RO); if (fd < 0) CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n"); size = CF_File_GetSize (fd); if (size < 0) CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n"); buffer = (char *) calloc (1, (size_t) (size + 1)); if (buffer == NULL) CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n"); if (CF_File_Read (fd, buffer, (size_t)size) < 0) CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n"); else CF_DEBUG_TRACE (gDebugCtx, "%s", buffer); CF_File_Close (fd); CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL); } void callee3 (void) { CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK) { CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n", gDebugCallstack.file, gDebugCallstack.line, gDebugCallstack.function); } CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL); } void callee2 (void) { CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); callee3 (); CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL); } void callee1 (void) { CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); callee2 (); CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL); } int main (int argc, char ** argv) { gDebugCtx = CF_Debug_CreateCtx (); if (gLogCtx == NULL) CF_DEBUG_TRACE (gDebugCtx, "create log ctx error\n"); CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); CF_DEBUG_TRACE (gDebugCtx, " == LOG TEST ==\n"); log_test ("LOG_TEST"); CF_DEBUG_TRACE (gDebugCtx, " == FILE READ TEST ==\n"); file_test ("FILE_READ_TEST"); CF_DEBUG_TRACE (gDebugCtx, " == CALLSTACK TEST ==\n"); callee1 (); CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); CF_DEBUG_TRACE (gDebugCtx, " == END OF TEST ==\n"); CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n", gDebugCallstack.file, gDebugCallstack.line, gDebugCallstack.function); CF_Debug_DestroyCtx (gDebugCtx); return 0; }