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

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

#1 remove log-level. log id is used as log level instead.

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