Changeset 15 in libcf
- Timestamp:
- 01/31/13 23:37:06 (11 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_file.h
r12 r15 15 15 #define CF_ERROR_FILE_CLOSE CF_ERROR_FILE - 6 16 16 #define CF_ERROR_FILE_GET_SIZE CF_ERROR_FILE - 7 17 18 #define RO O_RDONLY 19 #define WO O_WRONLY 20 #define WR O_RDWR 21 #define CR O_CREAT 22 #define TR O_TRUNC 17 23 18 24 #ifdef __cplusplus -
trunk/src/cf_file.c
r14 r15 22 22 #endif // #ifdef _WIN32 23 23 24 #define FILE_MODE S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH 25 24 26 int 25 27 CF_File_Open (const char * path, 26 28 const int flag) 27 29 { 28 int result = open (path, flag , 0);30 int result = open (path, flag); 29 31 30 32 if (result < 0) … … 37 39 CF_File_Create (const char * path) 38 40 { 39 int result = open (path, O_CREAT | O_WRONLY | O_TRUNC, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);41 int result = open (path, CR|WO|TR, FILE_MODE); 40 42 41 43 if (result < 0) -
trunk/test/test.c
r14 r15 8 8 #include "cf_thread.h" 9 9 10 #include <stdlib.h> 11 10 12 CF_Debug_Ctx gDebugCtx; 11 13 CF_Debug_CallStack gDebugCallstack; … … 13 15 CF_Log_Ctx gLogCtx; 14 16 15 void func1 (const char * message) 17 const char * file = "./log.txt"; 18 19 void log_test (const char * message) 16 20 { 21 int i = 0; 22 17 23 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 18 24 25 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); 26 if (gDebugCtx == NULL) 27 CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n"); 28 29 for (i = 0 ; i < 10000 ; i++) 30 CF_Log_Write (gLogCtx, "TEST", "turn %d\n", i); 19 31 20 32 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); 21 CF_Log_Write (gLogCtx, "test log", 33 34 CF_Log_Write (gLogCtx, message, 22 35 "here is the end of function [file:%s line:%d func:%s]\n", 23 36 gDebugCallstack.file, 24 37 gDebugCallstack.line, 25 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, 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); 26 96 } 27 97 28 98 int main (int argc, char ** argv) 29 99 { 30 gDebugCtx = CF_Debug_CreateCtx (); 31 gLogCtx = CF_Log_CreateCtx ("./log.txt", CF_LOG_BUFFER_DEFAULT); 32 33 if (gDebugCtx == NULL) 34 { 35 CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n"); 36 } 100 gDebugCtx = CF_Debug_CreateCtx (); 37 101 38 102 if (gLogCtx == NULL) 39 {40 103 CF_DEBUG_TRACE (gDebugCtx, "create log ctx error\n"); 41 }42 104 43 105 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 44 106 45 func1 ("merong kkkkkkkkkkk"); 107 CF_DEBUG_TRACE (gDebugCtx, " == LOG TEST ==\n"); 108 log_test ("LOG_TEST"); 109 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 (); 46 115 47 116 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); 117 118 CF_DEBUG_TRACE (gDebugCtx, " == END OF TEST ==\n"); 48 119 CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n", 49 120 gDebugCallstack.file, … … 51 122 gDebugCallstack.function); 52 123 53 CF_Log_DestroyCtx (gLogCtx);54 124 CF_Debug_DestroyCtx (gDebugCtx); 55 125
Note:
See TracChangeset
for help on using the changeset viewer.