Changeset 34 in libcf
- Timestamp:
- 02/05/13 11:26:59 (11 years ago)
- Location:
- trunk
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/cf_debug.h
r30 r34 11 11 * 환경에 적합하도록 사용자가 구성한 컨텍스트를 이용하여 사용할 수도 있음 <br /> 12 12 * (단, 콜스택의 푸시/팝은 컨텍스트를 이용해야만 사용 가능) 13 * 14 * @section 샘플코드 15 * @code 16 17 #include "cf_debug.h" 18 19 CF_Debug_Ctx gDebugCtx; 20 21 void callee (void) { 22 CF_Debug_CallStack callstack; 23 24 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 25 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &callstack); 26 } 27 28 int main (void) { 29 int fd = 0; 30 gDebugCtx = CF_Debug_CreateCtx (); 31 32 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 33 34 CF_DEBUG_TRACE (gDebugCtx, "print trace message with context to stderr\n"); 35 36 fd = CF_File_Create ("debug.txt"); 37 if (fd < 0) { 38 CF_DEBUG_PRINT (stderr, "error ...\n"); 39 // error 40 } 41 CF_Debug_SetOutputFD (gDebugCtx, fd); 42 43 CF_DEBUG_TRACE (gDebugCtx, "print trace message with context to debug.txt\n"); 44 45 CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL); 46 47 CF_Debug_DestroyCtx (gDebugCtx); 48 49 return 0; 50 } 51 52 * @endcode 13 53 */ 14 54 #ifndef __CF_DEBUG_H__ … … 19 59 #include <stdio.h> 20 60 21 /** 잘못된 컨텍스트 오류 */22 61 #define CF_ERROR_DEBUG_INVALID_CTX CF_ERROR_DEBUG - 1 23 24 /** 디버그 메시지 출력을 위한 파일 포인터 설정 오류 */25 62 #define CF_ERROR_DEBUG_SET_OUTPUT_FD CF_ERROR_DEBUG - 2 26 27 /** 콜스택 푸시 오류 */28 63 #define CF_ERROR_DEBUG_PUSH_CALLSTACK CF_ERROR_DEBUG - 3 29 30 /** 콜스택 팝 오류 */31 64 #define CF_ERROR_DEBUG_POP_CALLSTACK CF_ERROR_DEBUG - 4 32 65 -
trunk/include/cf_file.h
r26 r34 3 3 * @author myusgun <myusgun@gmail.com> 4 4 * @version 0.1 5 * 6 * @section 샘플코드 7 * @code 8 9 #include "cf_file.h" 10 11 int main (void) { 12 int fd = 0; 13 char *name = "file.txt"; 14 char buffer[128] = {0x00,}; 15 16 fd = CF_File_Create (name); 17 if (fd < 0) { 18 // error 19 } 20 if (CF_File_Write (fd, "file test", 9) < 0) { 21 // error 22 } 23 CF_File_Close (fd); 24 25 fd = CF_File_Open (name, CF_FILE_RO); 26 if (fd < 0) { 27 // error 28 } 29 printf ("file size : %d\n", CF_File_GetSize (fd)); 30 if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0) { 31 // error 32 } 33 CF_File_Close (fd); 34 35 return 0; 36 } 37 38 * @endcode 5 39 */ 6 40 #ifndef __CF_FILE_H__ -
trunk/include/cf_log.h
r26 r34 3 3 * @author myusgun <myusgun@gmail.com> 4 4 * @version 0.1 5 * 6 * @section 샘플코드 7 * @code 8 9 #include "cf_log.h" 10 11 CF_Log_Ctx gLogCtx; 12 13 void main (const char * message) 14 { 15 int i, j; 16 char logname[16] = {0x00,}; 17 18 CF_DEBUG_CALLSTACK_PUSH (gDebugCtx); 19 20 CF_Log_Initialize (10); 21 22 // with context 23 gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT); 24 if (gLogCtx == NULL) 25 CF_DEBUG_PRINT (stderr, "create log ctx error\n"); 26 27 for (i = 0 ; i < 10000 ; i++) 28 CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i); 29 30 CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack); 31 32 CF_Log_Write (gLogCtx, message, 33 "here is the end of function [file:%s line:%d func:%s]\n", 34 gDebugCallstack.file, 35 gDebugCallstack.line, 36 gDebugCallstack.function); 37 38 CF_Log_DestroyCtx (gLogCtx); 39 40 // with id number 41 for (i = 0 ; i < 10 ; i++) 42 { 43 sprintf (logname, "logid%d.txt", i); 44 CF_LOG_OPEN (i, logname, CF_LOG_BUFFER_NO); 45 } 46 47 for (i = 0 ; i < 10 ; i++) 48 { 49 for (j = 0 ; j < 10000 ; j++) 50 CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j); 51 52 CF_LOG_CLOSE (i); 53 } 54 55 CF_Log_Finalize (); 56 } 57 58 * @endcode 5 59 */ 6 60 #ifndef __CF_LOG_H__ -
trunk/include/cf_socket.h
r26 r34 3 3 * @author myusgun <myusgun@gmail.com> 4 4 * @version 0.1 5 5 * 6 6 * @remark TCP 소켓만 지원됨 7 * 8 * @section 샘플코드(서버) 9 * @code 10 11 #include "cf_socket.h" 12 13 int main (void) { 14 int srvsock = 0; 15 int clntsock = 0; 16 short port = 12345; 17 int recvd = 0; 18 char buf[1024] = {0x00,}; 19 20 if (CF_Socket_Initialize () < 0) { 21 // error 22 } 23 srvsock = CF_Socket_Server (port, 5); 24 if (srvsock < 0) { 25 // error 26 } 27 clntsock = CF_Socket_Accept (srvsock, NULL); 28 if (clntsock < 0) { 29 // error 30 } 31 if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0) { 32 // error 33 } 34 if (CF_Socket_Send (clntsock, buf, recvd) < 0) { 35 // error 36 } 37 CF_Socket_Close (srvsock); 38 CF_Socket_Finalize (); 39 40 return 0; 41 } 42 43 * @endcode 44 * 45 * @section 샘플코드(클라이언트) 46 * @code 47 48 #include "cf_socket.h" 49 50 int main (void) { 51 int sock = 0; 52 int recvd = 0; 53 char buf[1024] = {0x00,}; 54 short port = 12345; 55 56 if (CF_Socket_Initialize () < 0) { 57 // error 58 } 59 sock = CF_Socket_Connect ("localhost", port); 60 if (sock < 0) { 61 // error 62 } 63 if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0) { 64 // error 65 } 66 if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0) { 67 // error 68 } 69 CF_Socket_Close (sock); 70 CF_Socket_Finalize (); 71 72 return 0; 73 } 74 75 * @endcode 7 76 */ 8 77 #ifndef __CF_SOCKET_H__ -
trunk/include/cf_thread.h
r33 r34 5 5 * 6 6 * @remark 멀티스레드 및 뮤텍스 지원 7 * 8 * @section 샘플코드 9 * @code 10 11 #include "cf_thread.h" 12 13 CF_Mutex globalMutex; 14 15 CF_THREAD_RETURN CF_THREAD_CALL worker (void * arg) { 16 if (CF_Mutex_Create (&globalMutex) < 0) { 17 // error 18 } 19 20 if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section 21 // error 22 } 23 24 // critical section 25 26 if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section 27 // error 28 } 29 30 if (CF_Mutex_Destory (&globalMutex) < 0) { 31 // error 32 } 33 } 34 35 int main (void) { 36 CF_Thread tid; 37 if (CF_Thread_Create (&tid, worker, NULL) < 0) { 38 // error 39 } 40 41 if (CF_Thread_Join (&tid) < 0) { // block 42 // error 43 } 44 45 if (CF_Thread_Release (&tid) < 0) { 46 // error 47 } 48 return 0; 49 } 50 51 * @endcode 7 52 */ 8 53 #ifndef __CF_THREAD_H__ … … 46 91 * @param callback 스레드 워커 함수 이름 47 92 * @param arg 스레드 함수로 전달할 인자 48 *49 * @code50 51 #include "cf_thread.h"52 53 CF_Mutex globalMutex;54 55 CF_THREAD_RETURN CF_THREAD_CALL worker (void * arg) {56 if (CF_Mutex_Create (&globalMutex) < 0) {57 // error58 }59 60 if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section61 // error62 }63 64 // critical section65 66 if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section67 // error68 }69 70 if (CF_Mutex_Destory (&globalMutex) < 0) {71 // error72 }73 }74 75 int main (void) {76 CF_Thread tid;77 if (CF_Thread_Create (&tid, worker, NULL) < 0) {78 // error79 }80 81 if (CF_Thread_Join (&tid) < 0) { // block82 // error83 }84 85 if (CF_Thread_Release (&tid) < 0) {86 // error87 }88 }89 90 * @endcode91 93 */ 92 94 CF_EXPORT int -
trunk/src/cf_debug.c
r28 r34 169 169 } 170 170 171 fp = fdopen (dupfd, "a +");171 fp = fdopen (dupfd, "a"); 172 172 if (fp == NULL) 173 173 { -
trunk/src/cf_log.c
r30 r34 37 37 #define CF_LOG_BUFFER_DEFAULT_SIZE 128 * 1024 38 38 39 #define CF_LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1 40 39 41 typedef struct __cf_util_datetime__ 40 42 { … … 150 152 CF_Log_Local_GetTime (&dt); 151 153 152 153 154 snprintf (buffer, strlen ("0000-00-00 00:00:00.000"), 154 snprintf (buffer, CF_LOG_DATETIME_LENGTH, 155 155 "%02d-%02d-%02d %02d:%02d:%02d.%03d", 156 156 dt.year, dt.month, dt.day, … … 336 336 va_list valist; 337 337 char buffer[4096] = {0x00,}; 338 char datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,}; 338 339 size_t length = 0; 339 340 … … 344 345 va_start (valist, fmt); 345 346 346 strncat (buffer, "[", 1); 347 CF_Log_Local_GetTimeString (buffer + strlen (buffer)); 348 sprintf (buffer + strlen (buffer), "][%s] ", prefix); 347 CF_Log_Local_GetTimeString (datetime); 348 sprintf (buffer, "[%s][%s] ", datetime, prefix); 349 349 vsprintf (buffer + strlen (buffer), fmt, valist); 350 350
Note:
See TracChangeset
for help on using the changeset viewer.