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

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

#1 add bitwise util from ARIA of chevalier

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