source: libcf/trunk/src/cf_log.c@ 103

Last change on this file since 103 was 103, checked in by cheese, 11 years ago

#1 fix some symbol names, test code and exception handling logic

File size: 13.5 KB
RevLine 
[23]1/**
[28]2 * @file cf_log.c
3 * @author myusgun <myusgun@gmail.com>
[100]4 * @author vfire
[11]5 */
[50]6#if defined(_WIN32) || defined(_WIN64)
[14]7# define _USE_32BIT_TIME_T
[86]8# if (_MSC_VER >= 1400)
9# define localtime _localtime32
10# endif
[14]11#endif
12
[11]13#include "cf_log.h"
[12]14#include "cf_file.h"
[11]15#include "cf_thread.h"
[12]16#include "cf_local.h"
[40]17#include "cf_error.h"
[11]18
[12]19#include <stdio.h>
20#include <stdlib.h>
[11]21#include <string.h>
[12]22#include <stdarg.h>
23#include <time.h>
[11]24
[50]25#if defined(_WIN32) || defined(_WIN64)
[13]26# include <Windows.h>
[50]27#else
[12]28# include <sys/time.h>
[50]29#endif
[11]30
[85]31#define ASSERT_CTX(__ctx) \
32 if (__ctx == NULL) \
33 return CF_ERROR_LOG_INVALID_CTX
34
35#define ASSERT_INIT() \
36 if (gLogArray.ctxPool == NULL || \
37 gLogArray.ctxSize <= 0 ) \
38 return CF_ERROR_LOG_NOT_INITIALIZE
39
40#define ASSERT_MAPID(__mapid) \
41 if (gLogArray.ctxSize <= __mapid) \
42 return CF_ERROR_LOG_INVALID_MAPID
43
44#define ASSERT_MAPPED_CTX(__mapid) \
45 if (gLogArray.ctxPool[__mapid] != NULL) \
46 return CF_ERROR_LOG_ALREADY_MAPPED_ID
47
48#define ASSERT_NOT_MAPPED_CTX(__mapid) \
49 if (gLogArray.ctxPool[__mapid] == NULL) \
50 return CF_ERROR_LOG_NOT_MAPPED_ID
51
[102]52#define CHECK_LOG_LEVEL(__mapid) \
53 if (gLogArray.level < mapid) \
[101]54 return CF_OK
55
[25]56#define LOCK_LOG_CTX(__ctx) CF_Mutex_Lock (&__ctx->mutex)
57#define UNLOCK_LOG_CTX(__ctx) CF_Mutex_Unlock (&__ctx->mutex)
[12]58
[42]59#define LOG_BUFFER_DEFAULT_SIZE 128 * 1024
[12]60
[51]61#define LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1
[34]62
[40]63/**
64 * 로그 컨텍스트
65 *
66 * @remark change from public to private
67 */
68typedef void * CF_Log_Ctx;
69
[12]70typedef struct __cf_util_datetime__
71{
72 int year;
73 int month;
74 int day;
75 int week; /* SUN:0 ~ SAT:6 */
76
77 int hour;
78 int min;
79 int sec;
80 int usec;
[30]81} S_CF_LOG_DATETIME, CF_LOG_DATETIME;
[12]82
[11]83typedef struct __cf_log_ctx__ {
84 char path[NAME_LENGTH + 1];
85 int fd;
86 char * buffer;
[12]87 size_t size; /* entire size of buffer */
88 size_t length; /* data length in current */
[11]89 CF_Mutex mutex;
90} S_CF_LOG_CTX, CF_LOG_CTX;
91
[51]92typedef struct __cf_log_array__ {
[12]93 CF_Log_Ctx * ctxPool;
94 int ctxSize;
[98]95 int level;
[51]96} S_CF_LOG_ARRAY, CF_LOG_ARRAY;
[11]97
[51]98static CF_LOG_ARRAY gLogArray;
[11]99
[12]100#if defined(_WIN32) || defined(_WIN64)
[66]101/* {{{ */
[12]102struct timezone
103{
[66]104 int tz_minuteswest; /* minutes W of Greenwich */
105 int tz_dsttime; /* type of dst correction */
[12]106};
107
[66]108int gettimeofday (struct timeval *tv, struct timezone *tz)
[12]109{
[66]110 FILETIME ft;
111 unsigned __int64 buf =0;
[38]112 //static int tzflag = 0;
[12]113
114 if (NULL != tv)
115 {
[66]116 GetSystemTimeAsFileTime (&ft);
[12]117
[66]118 buf |= ft.dwHighDateTime;
[67]119 buf <<= 32;
[66]120 buf |= ft.dwLowDateTime;
[12]121
[66]122 if (buf)
[12]123 {
[66]124 buf /= 10;
125 buf -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
[12]126 }
127
[66]128 tv->tv_sec = (long)(buf / 1000000UL);
129 tv->tv_usec = (long)(buf % 1000000UL);
[12]130 }
131
[38]132 /*
[12]133 if (NULL != tz)
134 {
135 if (!tzflag)
136 {
137 _tzset();
138 tzflag++;
139 }
140
141 // Adjust for the timezone west of Greenwich
142 tz->tz_minuteswest = _timezone / 60;
143 tz->tz_dsttime = _daylight;
144 }
[38]145 */
[12]146
147 return 0;
148}
[66]149/* }}} */
[50]150#endif
[12]151
[23]152static int
[30]153CF_Log_Local_GetTime (CF_LOG_DATETIME * dt)
[11]154{
[12]155 struct timeval timeVal;
156 struct tm * timeSt;
157
158 gettimeofday (&timeVal, NULL);
[13]159 timeSt = localtime ((const time_t *)&timeVal.tv_sec);
[12]160
161 dt->year = timeSt->tm_year + 1900;
162 dt->month = timeSt->tm_mon + 1;
163 dt->day = timeSt->tm_mday;
164 dt->week = timeSt->tm_wday;
165
166 dt->hour = timeSt->tm_hour;
167 dt->min = timeSt->tm_min;
168 dt->sec = timeSt->tm_sec;
169
170 dt->usec = (int) timeVal.tv_usec;
171
172 return CF_OK;
173}
174
[23]175static int
[12]176CF_Log_Local_GetTimeString (char * buffer)
177{
[30]178 CF_LOG_DATETIME dt;
[12]179 CF_Log_Local_GetTime (&dt);
180
[42]181 snprintf (buffer, LOG_DATETIME_LENGTH,
[14]182 "%02d-%02d-%02d %02d:%02d:%02d.%03d",
183 dt.year, dt.month, dt.day,
184 dt.hour, dt.min, dt.sec, dt.usec);
185
[12]186 return CF_OK;
187}
188
[23]189static int
[12]190CF_Log_Local_Flush (CF_LOG_CTX * ctx)
191{
192 if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0)
193 return CF_ERROR_LOG_FLUSH;
194
195 ctx->length = 0;
196
197 return CF_OK;
198}
199
[35]200/**
201 * 로그 데이터 처리
202 *
203 * @return 성공 시, CF_OK; 실패 시, 오류 코드
204 *
205 * @param ctx 로그 컨텍스트
206 * @param buffer 로그 데이터
207 * @param demandSize 로그 데이터 길이
208 *
209 * @author vfire
210 */
211/* static */int
212CF_Log_Local_Push (CF_LOG_CTX * ctx,
213 const char * buffer,
214 const size_t demandSize)
215{
[40]216 int result = CF_OK;
217
[64]218 if (ctx->size > 0) /* 버퍼단위 버퍼링.... */
[35]219 {
220 size_t writeSize;
221 size_t remainSize;
222
223 remainSize = demandSize;
224 while (remainSize)
225 {
226 writeSize = (ctx->size - ctx->length) < remainSize ? (ctx->size - ctx->length) : remainSize;
227
228 memcpy (ctx->buffer + ctx->length, buffer + demandSize - remainSize, writeSize);
229 ctx->length += writeSize;
230
231 if (ctx->length == ctx->size)
232 CF_Log_Local_Flush (ctx);
233
234 remainSize -= writeSize;
235 }
236 }
237 else /* flush되어야 함. */
238 {
[38]239 ctx->buffer = (char *)buffer;
240 ctx->length = demandSize;
241
[40]242 result = CF_Log_Local_Flush (ctx);
[35]243 }
244
[40]245 return result;
246}
247
248/**
249 * 로그 컨텍스트에 멀티쓰레드 모드 설정
250 *
251 * @return 성공 시, CF_OK; 실패 시, 오류 코드
252 *
253 * @param ctx 로그 컨텍스트
254 *
255 * @see CF_Log_UnsetMultiThread
256 */
257static int
258CF_Log_SetMultiThread (CF_Log_Ctx ctx)
259{
260 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
261
[85]262 ASSERT_CTX (ctx);
[40]263
264 if (CF_Mutex_Create (&context->mutex) < 0)
265 return CF_ERROR_LOG_SET_MULTITHREAD;
266
[35]267 return CF_OK;
268}
269
270/**
[40]271 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
[35]272 *
[40]273 * @return 성공 시, CF_OK; 실패 시, 오류 코드
274 *
275 * @param ctx 로그 컨텍스트
276 *
277 * @see CF_Log_SetMultiThread
278 */
279static int
280CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
281{
282 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
283
[85]284 ASSERT_CTX (ctx);
[40]285
286 if (CF_Mutex_Destory (&context->mutex) < 0)
287 return CF_ERROR_LOG_UNSET_MULTITHREAD;
288
289 return CF_OK;
290}
291
292/**
293 * 로그 컨텍스트에 따라 로그 쓰기
294 *
[35]295 * @return 성공 시, CF_OK; 실패 시, 오류 코드
296 *
[40]297 * @param ctx 로그 컨텍스트
298 * @param prefix 로그의 프리픽스 문자열
299 * @param fmt 포맷 스트링
300 * @param ... 가변 인자
[35]301 */
[40]302static int
303CF_Log_WriteCtx (CF_Log_Ctx ctx,
304 const char * prefix,
305 const char * fmt,
306// ...)
307 va_list valist)
[12]308{
[40]309 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
310// va_list valist;
311 char buffer[16 * 1024] = {0x00,};
[42]312 char datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
[11]313
[85]314 ASSERT_CTX (ctx);
[11]315
[40]316 LOCK_LOG_CTX (context);
317// va_start (valist, fmt);
318
319 CF_Log_Local_GetTimeString (datetime);
[46]320 snprintf (buffer, sizeof (buffer) - 1, "[%s][%s] ", datetime, prefix);
[40]321 vsprintf (buffer + strlen (buffer), fmt, valist);
322
323 CF_Log_Local_Push (context, buffer, strlen (buffer));
324
325// va_end (valist);
326 UNLOCK_LOG_CTX (context);
327
[11]328 return CF_OK;
329}
330
[35]331/**
[40]332 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
[35]333 *
[40]334 * @return 성공 시, CF_OK; 실패 시, 오류 코드
335 *
336 * @param ctx 로그 컨텍스트
[35]337 */
[40]338static int
339CF_Log_FlushCtx (CF_Log_Ctx ctx)
[11]340{
[40]341 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
[19]342
[85]343 ASSERT_CTX (ctx);
[40]344
345 LOCK_LOG_CTX (context);
346 CF_Log_Local_Flush (context);
347 UNLOCK_LOG_CTX (context);
348
349 return CF_OK;
350}
351
352/**
353 * 로그 컨텍스트 해제
354 *
355 * @return 성공 시, CF_OK; 실패 시, 오류 코드
356 *
357 * @param ctx 로그 컨텍스트
358 */
359static int
360CF_Log_DestroyCtx (CF_Log_Ctx ctx)
361{
362 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
363
[85]364 ASSERT_CTX (ctx);
[40]365
366 if (context->size > 0)
[19]367 {
[40]368 CF_Log_FlushCtx (ctx);
369 free (context->buffer);
370 context->buffer = NULL;
371 context->size = 0;
[19]372 }
373
[40]374 CF_File_Close (context->fd);
[19]375
[45]376 CF_Mutex_Destory (&context->mutex);
377 context->mutex = NULL;
[11]378
379 return CF_OK;
380}
[12]381
[35]382/**
383 * 로그 컨텍스트 생성
384 *
385 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
386 *
387 * @param path 로그 파일 경로
388 * @param memsize 로그 버퍼 크기
[40]389 * @param ctx 로그 컨텍스트 받을 주소
[35]390 *
[103]391 * @see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
[35]392 */
[40]393static int
[12]394CF_Log_CreateCtx (const char * path,
[40]395 const int memsize,
396 CF_Log_Ctx * ctx)
[12]397{
398 int result = 0;
[93]399 int fileFlag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_APPEND;
[103]400 int size = (memsize == CF_LOG_DEFAULT_BUFFER)
[42]401 ? LOG_BUFFER_DEFAULT_SIZE
[12]402 : memsize;
[87]403
[12]404 CF_LOG_CTX * context = NULL;
405
406 if (path == NULL)
[40]407 return CF_ERROR_LOG_INVALID_ARGS;
[12]408
409 TRY
410 {
411 context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
412 if (context == NULL)
413 {
[40]414 result = CF_ERROR_LOG_ALLOCATE_CTX;
[12]415 TRY_BREAK;
416 }
417
[93]418 context->fd = CF_File_Open (path, fileFlag);
[12]419 if (context->fd < 0)
420 {
[40]421 result = CF_ERROR_LOG_CREATE_FILE;
[12]422 TRY_BREAK;
423 }
[46]424 snprintf (context->path, sizeof (context->path) -1, "%s", path);
[12]425
426 if (size > 0)
427 {
428 context->buffer = (char *) calloc ((size_t) size + 1, 1);
429 if (context->buffer == NULL)
430 {
[40]431 result = CF_ERROR_LOG_ALLOCATE_BUFFER;
[12]432 TRY_BREAK;
433 }
434 context->size = (size_t) size;
435 }
[87]436
437 *ctx = (CF_Log_Ctx) context;
[12]438 }
439 CATCH_IF (result < 0)
440 {
441 CF_Log_DestroyCtx ((CF_Log_Ctx) context);
442 }
443
[93]444 return result;
[12]445}
446
[35]447/**
[40]448 * 로그 컨텍스트에 아이디 넘버 할당<br />
449 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
[35]450 *
[40]451 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]452 *
[40]453 * @param mapid 부여할 아이디 넘버
454 * @param ctx 로그 컨텍스트
455 *
[98]456 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용
[40]457 *
458 * @see CF_LOG_OPEN, CF_Log_CreateCtx
[35]459 */
[40]460static int
461CF_Log_MapCtxID (const int mapid,
462 const CF_Log_Ctx ctx)
[12]463{
[85]464 ASSERT_INIT ();
465 ASSERT_MAPID (mapid);
466 ASSERT_MAPPED_CTX (mapid);
[12]467
[51]468 gLogArray.ctxPool[mapid] = ctx;
[40]469
[12]470 return CF_OK;
471}
472
[35]473/**
[40]474 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
[35]475 *
[40]476 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]477 *
[40]478 * @param mapid 로그의 아이디 넘버
479 *
480 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
481 *
482 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
[35]483 */
[40]484static int
485CF_Log_UnmapCtxID (const int mapid)
[19]486{
[85]487 ASSERT_INIT ();
488 ASSERT_MAPID (mapid);
489 ASSERT_NOT_MAPPED_CTX (mapid);
[19]490
[51]491 CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
[19]492
[51]493 free (gLogArray.ctxPool[mapid]);
494 gLogArray.ctxPool[mapid] = NULL;
[19]495
496 return CF_OK;
497}
498
[35]499/**
[40]500 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
[35]501 *
[40]502 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]503 *
[40]504 * @param mapid 로그의 아이디 넘버
505 * @param ctx 로그 컨텍스트 받을 주소
[35]506 */
[40]507static int
508CF_Log_GetMappedCtx (const int mapid,
509 CF_Log_Ctx * ctx)
[19]510{
[85]511 ASSERT_INIT ();
512 ASSERT_MAPID (mapid);
513 ASSERT_NOT_MAPPED_CTX (mapid);
[19]514
[51]515 *ctx = gLogArray.ctxPool[mapid];
[19]516
517 return CF_OK;
518}
519
[35]520/**
[40]521 * 로그를 사용하기 위해 초기화
[35]522 *
523 * @return 성공 시, CF_OK; 실패 시, 오류 코드
524 *
[101]525 * @param poolSize 로그 풀 크기로, 로그 아이디 넘버의 최대 값
526 * @param level 로그 레벨
527 *
528 * @see CF_LOG_NO_LEVEL
[35]529 */
[19]530int
[102]531CF_Log_Initialize (const int poolSize,
532 const int level)
[12]533{
[51]534 memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
[12]535
[101]536 if (poolSize > 0)
[40]537 {
[51]538 gLogArray.ctxPool =
[101]539 (CF_Log_Ctx *) calloc ((size_t) poolSize, sizeof (CF_Log_Ctx));
[51]540 if (gLogArray.ctxPool == NULL)
[40]541 return CF_ERROR_LOG_INITIALIZE;
[101]542 gLogArray.ctxSize = poolSize;
[102]543 gLogArray.level = (level == CF_LOG_NO_LEVEL) ? poolSize : level;
[40]544 }
[12]545
[40]546 return CF_OK;
547}
[12]548
[40]549/**
550 * 로그가 모두 사용된 후 자원 해제
551 *
552 * @return CF_OK 반환
553 */
554int
555CF_Log_Finalize (void)
556{
557 int mapid = 0;
[12]558
[51]559 for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
[40]560 {
561 CF_Log_UnmapCtxID (mapid);
562 }
[12]563
[51]564 if (gLogArray.ctxPool != NULL)
565 free (gLogArray.ctxPool);
[12]566
[51]567 memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
[40]568
[12]569 return CF_OK;
570}
571
[35]572/**
[40]573 * 로그 열기
[35]574 *
575 * @return 성공 시, CF_OK; 실패 시, 오류 코드
576 *
[98]577 * @param mapid 로그의 아이디 넘버
[40]578 * @param path 로그 파일 경로
579 * @param memsize 로그 버퍼 크기
580 *
[103]581 * @see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
[35]582 */
[12]583int
[40]584CF_Log_Open (const int mapid,
585 const char * path,
586 const int memsize)
[12]587{
[40]588 int result = 0;
589 CF_Log_Ctx ctx = NULL;
[12]590
[101]591 CHECK_LOG_LEVEL (mapid);
592
[40]593 result = CF_Log_CreateCtx (path, memsize, &ctx);
594 if (result < 0)
595 return result;
[12]596
[40]597 result = CF_Log_MapCtxID (mapid, ctx);
[85]598 if (result < 0)
599 CF_Log_DestroyCtx (ctx);
[12]600
[40]601 return result;
[12]602}
[19]603
[35]604/**
[40]605 * 로그 닫기
[35]606 *
607 * @return 성공 시, CF_OK; 실패 시, 오류 코드
608 *
[40]609 * @param mapid 로그의 아이디 넘버
610 */
611int
612CF_Log_Close (const int mapid)
613{
[101]614 CHECK_LOG_LEVEL (mapid);
615
[40]616 return CF_Log_UnmapCtxID (mapid);
617}
618
619/**
[98]620 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
[35]621 *
[40]622 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]623 *
[98]624 * @param mapid 로그의 아이디 넘버
[35]625 */
[19]626int
[98]627CF_Log_Flush (const int mapid)
[19]628{
[40]629 int result = 0;
630 CF_Log_Ctx ctx = NULL;
[19]631
[40]632 result = CF_Log_GetMappedCtx (mapid, &ctx);
633 if (result < 0)
[25]634 return result;
[19]635
[98]636 result = CF_Log_FlushCtx (ctx);
[19]637
[40]638 return result;
[19]639}
640
[35]641/**
[98]642 * 로그 컨텍스트에 멀티쓰레드 모드 설정
[35]643 *
[98]644 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]645 *
[98]646 * @param mapid 로그의 아이디 넘버
647 * @param flag 설정/해제 bool 플래그
648 *
649 * @see CF_BOOL
[35]650 */
[19]651int
[98]652CF_Log_SetMT (const int mapid,
653 const CF_BOOL flag)
[19]654{
[40]655 int result = 0;
656 CF_Log_Ctx ctx = NULL;
[19]657
[40]658 result = CF_Log_GetMappedCtx (mapid, &ctx);
659 if (result < 0)
660 return result;
[19]661
[98]662 result = (flag) ? CF_Log_SetMultiThread (ctx) :
663 CF_Log_UnsetMultiThread (ctx);
[19]664
[40]665 return result;
[19]666}
667
[35]668/**
[98]669 * 로그 쓰기
670 *
671 * @return 성공 시, CF_OK; 실패 시, 오류 코드
672 *
673 * @param mapid 로그의 아이디 넘버
674 * @param prefix 로그의 프리픽스 문자열
675 * @param fmt 포맷 스트링
676 * @param ... 가변 인자
677 */
678int
679CF_Log_Write (const int mapid,
680 const char * prefix,
681 const char * fmt, ...)
682{
[40]683 int result = 0;
684 CF_Log_Ctx ctx = NULL;
[98]685 va_list valist;
[19]686
[101]687 CHECK_LOG_LEVEL (mapid);
[98]688
[40]689 result = CF_Log_GetMappedCtx (mapid, &ctx);
690 if (result < 0)
691 return result;
692
[98]693 va_start (valist, fmt);
694 result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
695 va_end (valist);
[40]696
697 return result;
[19]698}
Note: See TracBrowser for help on using the repository browser.