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

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

#1 fix logging to append if the file exists

File size: 13.2 KB
RevLine 
[23]1/**
[28]2 * @file cf_log.c
3 * @author myusgun <myusgun@gmail.com>
[11]4 */
[50]5#if defined(_WIN32) || defined(_WIN64)
[14]6# define _USE_32BIT_TIME_T
[86]7# if (_MSC_VER >= 1400)
8# define localtime _localtime32
9# endif
[14]10#endif
11
[11]12#include "cf_log.h"
[12]13#include "cf_file.h"
[11]14#include "cf_thread.h"
[12]15#include "cf_local.h"
[40]16#include "cf_error.h"
[11]17
[12]18#include <stdio.h>
19#include <stdlib.h>
[11]20#include <string.h>
[12]21#include <stdarg.h>
22#include <time.h>
[11]23
[50]24#if defined(_WIN32) || defined(_WIN64)
[13]25# include <Windows.h>
[50]26#else
[12]27# include <sys/time.h>
[50]28#endif
[11]29
[85]30#define ASSERT_CTX(__ctx) \
31 if (__ctx == NULL) \
32 return CF_ERROR_LOG_INVALID_CTX
33
34#define ASSERT_INIT() \
35 if (gLogArray.ctxPool == NULL || \
36 gLogArray.ctxSize <= 0 ) \
37 return CF_ERROR_LOG_NOT_INITIALIZE
38
39#define ASSERT_MAPID(__mapid) \
40 if (gLogArray.ctxSize <= __mapid) \
41 return CF_ERROR_LOG_INVALID_MAPID
42
43#define ASSERT_MAPPED_CTX(__mapid) \
44 if (gLogArray.ctxPool[__mapid] != NULL) \
45 return CF_ERROR_LOG_ALREADY_MAPPED_ID
46
47#define ASSERT_NOT_MAPPED_CTX(__mapid) \
48 if (gLogArray.ctxPool[__mapid] == NULL) \
49 return CF_ERROR_LOG_NOT_MAPPED_ID
50
[25]51#define LOCK_LOG_CTX(__ctx) CF_Mutex_Lock (&__ctx->mutex)
52#define UNLOCK_LOG_CTX(__ctx) CF_Mutex_Unlock (&__ctx->mutex)
[12]53
[42]54#define LOG_BUFFER_DEFAULT_SIZE 128 * 1024
[12]55
[51]56#define LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1
[34]57
[40]58/**
59 * 로그 컨텍스트
60 *
61 * @remark change from public to private
62 */
63typedef void * CF_Log_Ctx;
64
[12]65typedef struct __cf_util_datetime__
66{
67 int year;
68 int month;
69 int day;
70 int week; /* SUN:0 ~ SAT:6 */
71
72 int hour;
73 int min;
74 int sec;
75 int usec;
[30]76} S_CF_LOG_DATETIME, CF_LOG_DATETIME;
[12]77
[11]78typedef struct __cf_log_ctx__ {
79 char path[NAME_LENGTH + 1];
80 int fd;
81 char * buffer;
[12]82 size_t size; /* entire size of buffer */
83 size_t length; /* data length in current */
[11]84 CF_Mutex mutex;
85} S_CF_LOG_CTX, CF_LOG_CTX;
86
[51]87typedef struct __cf_log_array__ {
[12]88 CF_Log_Ctx * ctxPool;
89 int ctxSize;
[51]90} S_CF_LOG_ARRAY, 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,
[14]176 "%02d-%02d-%02d %02d:%02d:%02d.%03d",
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/**
195 * 로그 데이터 처리
196 *
197 * @return 성공 시, CF_OK; 실패 시, 오류 코드
198 *
199 * @param ctx 로그 컨텍스트
200 * @param buffer 로그 데이터
201 * @param demandSize 로그 데이터 길이
202 *
203 * @author vfire
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
[64]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 }
231 else /* flush되어야 함. */
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/**
243 * 로그 컨텍스트에 멀티쓰레드 모드 설정
244 *
245 * @return 성공 시, CF_OK; 실패 시, 오류 코드
246 *
247 * @param ctx 로그 컨텍스트
248 *
249 * @see CF_Log_UnsetMultiThread
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
258 if (CF_Mutex_Create (&context->mutex) < 0)
259 return CF_ERROR_LOG_SET_MULTITHREAD;
260
[35]261 return CF_OK;
262}
263
264/**
[40]265 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
[35]266 *
[40]267 * @return 성공 시, CF_OK; 실패 시, 오류 코드
268 *
269 * @param ctx 로그 컨텍스트
270 *
271 * @see CF_Log_SetMultiThread
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
280 if (CF_Mutex_Destory (&context->mutex) < 0)
281 return CF_ERROR_LOG_UNSET_MULTITHREAD;
282
283 return CF_OK;
284}
285
286/**
287 * 로그 컨텍스트에 따라 로그 쓰기
288 *
[35]289 * @return 성공 시, CF_OK; 실패 시, 오류 코드
290 *
[40]291 * @param ctx 로그 컨텍스트
292 * @param prefix 로그의 프리픽스 문자열
293 * @param fmt 포맷 스트링
294 * @param ... 가변 인자
[35]295 */
[40]296static int
297CF_Log_WriteCtx (CF_Log_Ctx ctx,
298 const char * prefix,
299 const char * fmt,
300// ...)
301 va_list valist)
[12]302{
[40]303 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
304// va_list valist;
305 char buffer[16 * 1024] = {0x00,};
[42]306 char datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
[11]307
[85]308 ASSERT_CTX (ctx);
[11]309
[40]310 LOCK_LOG_CTX (context);
311// va_start (valist, fmt);
312
313 CF_Log_Local_GetTimeString (datetime);
[46]314 snprintf (buffer, sizeof (buffer) - 1, "[%s][%s] ", datetime, prefix);
[40]315 vsprintf (buffer + strlen (buffer), fmt, valist);
316
317 CF_Log_Local_Push (context, buffer, strlen (buffer));
318
319// va_end (valist);
320 UNLOCK_LOG_CTX (context);
321
[11]322 return CF_OK;
323}
324
[35]325/**
[40]326 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
[35]327 *
[40]328 * @return 성공 시, CF_OK; 실패 시, 오류 코드
329 *
330 * @param ctx 로그 컨텍스트
[35]331 */
[40]332static int
333CF_Log_FlushCtx (CF_Log_Ctx ctx)
[11]334{
[40]335 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
[19]336
[85]337 ASSERT_CTX (ctx);
[40]338
339 LOCK_LOG_CTX (context);
340 CF_Log_Local_Flush (context);
341 UNLOCK_LOG_CTX (context);
342
343 return CF_OK;
344}
345
346/**
347 * 로그 컨텍스트 해제
348 *
349 * @return 성공 시, CF_OK; 실패 시, 오류 코드
350 *
351 * @param ctx 로그 컨텍스트
352 */
353static int
354CF_Log_DestroyCtx (CF_Log_Ctx ctx)
355{
356 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
357
[85]358 ASSERT_CTX (ctx);
[40]359
360 if (context->size > 0)
[19]361 {
[40]362 CF_Log_FlushCtx (ctx);
363 free (context->buffer);
364 context->buffer = NULL;
365 context->size = 0;
[19]366 }
367
[40]368 CF_File_Close (context->fd);
[19]369
[45]370 CF_Mutex_Destory (&context->mutex);
371 context->mutex = NULL;
[11]372
373 return CF_OK;
374}
[12]375
[35]376/**
377 * 로그 컨텍스트 생성
378 *
379 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
380 *
381 * @param path 로그 파일 경로
382 * @param memsize 로그 버퍼 크기
[40]383 * @param ctx 로그 컨텍스트 받을 주소
[35]384 *
385 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO
386 */
[40]387static int
[12]388CF_Log_CreateCtx (const char * path,
[40]389 const int memsize,
390 CF_Log_Ctx * ctx)
[12]391{
392 int result = 0;
[93]393 int fileFlag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_APPEND;
[12]394 int size = (memsize == CF_LOG_BUFFER_DEFAULT)
[42]395 ? LOG_BUFFER_DEFAULT_SIZE
[12]396 : memsize;
[87]397
[12]398 CF_LOG_CTX * context = NULL;
399
400 if (path == NULL)
[40]401 return CF_ERROR_LOG_INVALID_ARGS;
[12]402
403 TRY
404 {
405 context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
406 if (context == NULL)
407 {
[40]408 result = CF_ERROR_LOG_ALLOCATE_CTX;
[12]409 TRY_BREAK;
410 }
411
[93]412 context->fd = CF_File_Open (path, fileFlag);
[12]413 if (context->fd < 0)
414 {
[40]415 result = CF_ERROR_LOG_CREATE_FILE;
[12]416 TRY_BREAK;
417 }
[46]418 snprintf (context->path, sizeof (context->path) -1, "%s", path);
[12]419
420 if (size > 0)
421 {
422 context->buffer = (char *) calloc ((size_t) size + 1, 1);
423 if (context->buffer == NULL)
424 {
[40]425 result = CF_ERROR_LOG_ALLOCATE_BUFFER;
[12]426 TRY_BREAK;
427 }
428 context->size = (size_t) size;
429 }
[87]430
431 *ctx = (CF_Log_Ctx) context;
[12]432 }
433 CATCH_IF (result < 0)
434 {
435 CF_Log_DestroyCtx ((CF_Log_Ctx) context);
436 }
437
[93]438 return result;
[12]439}
440
[35]441/**
[40]442 * 로그 컨텍스트에 아이디 넘버 할당<br />
443 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
[35]444 *
[40]445 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]446 *
[40]447 * @param mapid 부여할 아이디 넘버
448 * @param ctx 로그 컨텍스트
449 *
450 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함
451 *
452 * @see CF_LOG_OPEN, CF_Log_CreateCtx
[35]453 */
[40]454static int
455CF_Log_MapCtxID (const int mapid,
456 const CF_Log_Ctx ctx)
[12]457{
[85]458 ASSERT_INIT ();
459 ASSERT_MAPID (mapid);
460 ASSERT_MAPPED_CTX (mapid);
[12]461
[51]462 gLogArray.ctxPool[mapid] = ctx;
[40]463
[12]464 return CF_OK;
465}
466
[35]467/**
[40]468 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
[35]469 *
[40]470 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]471 *
[40]472 * @param mapid 로그의 아이디 넘버
473 *
474 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
475 *
476 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
[35]477 */
[40]478static int
479CF_Log_UnmapCtxID (const int mapid)
[19]480{
[85]481 ASSERT_INIT ();
482 ASSERT_MAPID (mapid);
483 ASSERT_NOT_MAPPED_CTX (mapid);
[19]484
[51]485 CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
[19]486
[51]487 free (gLogArray.ctxPool[mapid]);
488 gLogArray.ctxPool[mapid] = NULL;
[19]489
490 return CF_OK;
491}
492
[35]493/**
[40]494 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
[35]495 *
[40]496 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]497 *
[40]498 * @param mapid 로그의 아이디 넘버
499 * @param ctx 로그 컨텍스트 받을 주소
[35]500 */
[40]501static int
502CF_Log_GetMappedCtx (const int mapid,
503 CF_Log_Ctx * ctx)
[19]504{
[85]505 ASSERT_INIT ();
506 ASSERT_MAPID (mapid);
507 ASSERT_NOT_MAPPED_CTX (mapid);
[19]508
[51]509 *ctx = gLogArray.ctxPool[mapid];
[19]510
511 return CF_OK;
512}
513
[35]514/**
[40]515 * 로그를 사용하기 위해 초기화
[35]516 *
517 * @return 성공 시, CF_OK; 실패 시, 오류 코드
518 *
[40]519 * @param logPool 아이디 넘버 최대 값
[35]520 */
[19]521int
[40]522CF_Log_Initialize (const int logPool)
[12]523{
[51]524 memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
[12]525
[40]526 if (logPool > 0)
527 {
[51]528 gLogArray.ctxPool =
[40]529 (CF_Log_Ctx *) calloc ((size_t) logPool, sizeof (CF_Log_Ctx));
[51]530 if (gLogArray.ctxPool == NULL)
[40]531 return CF_ERROR_LOG_INITIALIZE;
[51]532 gLogArray.ctxSize = logPool;
[40]533 }
[12]534
[40]535 return CF_OK;
536}
[12]537
[40]538/**
539 * 로그가 모두 사용된 후 자원 해제
540 *
541 * @return CF_OK 반환
542 */
543int
544CF_Log_Finalize (void)
545{
546 int mapid = 0;
[12]547
[51]548 for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
[40]549 {
550 CF_Log_UnmapCtxID (mapid);
551 }
[12]552
[51]553 if (gLogArray.ctxPool != NULL)
554 free (gLogArray.ctxPool);
[12]555
[51]556 memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
[40]557
[12]558 return CF_OK;
559}
560
[35]561/**
[40]562 * 로그 열기
[35]563 *
564 * @return 성공 시, CF_OK; 실패 시, 오류 코드
565 *
[40]566 * @param mapid 로그의 아이디 넘버
567 * @param path 로그 파일 경로
568 * @param memsize 로그 버퍼 크기
569 *
570 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO
[35]571 */
[12]572int
[40]573CF_Log_Open (const int mapid,
574 const char * path,
575 const int memsize)
[12]576{
[40]577 int result = 0;
578 CF_Log_Ctx ctx = NULL;
[12]579
[40]580 result = CF_Log_CreateCtx (path, memsize, &ctx);
581 if (result < 0)
582 return result;
[12]583
[40]584 result = CF_Log_MapCtxID (mapid, ctx);
[85]585 if (result < 0)
586 CF_Log_DestroyCtx (ctx);
[12]587
[40]588 return result;
[12]589}
[19]590
[35]591/**
[40]592 * 로그 닫기
[35]593 *
594 * @return 성공 시, CF_OK; 실패 시, 오류 코드
595 *
[40]596 * @param mapid 로그의 아이디 넘버
597 */
598int
599CF_Log_Close (const int mapid)
600{
601 return CF_Log_UnmapCtxID (mapid);
602}
603
604/**
605 * 로그 쓰기
[35]606 *
[40]607 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]608 *
[40]609 * @param mapid 로그의 아이디 넘버
610 * @param prefix 로그의 프리픽스 문자열
611 * @param fmt 포맷 스트링
612 * @param ... 가변 인자
[35]613 */
[19]614int
[40]615CF_Log_Write (const int mapid,
616 const char * prefix,
617 const char * fmt, ...)
[19]618{
[40]619 int result = 0;
620 CF_Log_Ctx ctx = NULL;
621 va_list valist;
[19]622
[40]623 result = CF_Log_GetMappedCtx (mapid, &ctx);
624 if (result < 0)
[25]625 return result;
[19]626
[40]627 va_start (valist, fmt);
628 result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
629 va_end (valist);
[19]630
[40]631 return result;
[19]632}
633
[35]634/**
[40]635 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
[35]636 *
637 * @return 성공 시, CF_OK; 실패 시, 오류 코드
638 *
639 * @param mapid 로그의 아이디 넘버
640 */
[19]641int
[40]642CF_Log_Flush (const int mapid)
[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
[40]651 result = CF_Log_FlushCtx (ctx);
[19]652
[40]653 return result;
[19]654}
655
[35]656/**
[40]657 * 로그 컨텍스트에 멀티쓰레드 모드 설정
[35]658 *
[40]659 * @return 성공 시, CF_OK; 실패 시, 오류 코드
[35]660 *
[40]661 * @param mapid 로그의 아이디 넘버
662 * @param flag 설정/해제 bool 플래그
663 *
664 * @see CF_BOOL
[35]665 */
[40]666int
667CF_Log_SetMT (const int mapid,
668 const CF_BOOL flag)
[19]669{
[40]670 int result = 0;
671 CF_Log_Ctx ctx = NULL;
[19]672
[40]673 result = CF_Log_GetMappedCtx (mapid, &ctx);
674 if (result < 0)
675 return result;
676
[87]677 if (flag)
678 result = CF_Log_SetMultiThread (ctx);
679 else
680 result = CF_Log_UnsetMultiThread (ctx);
[40]681
682 return result;
[19]683}
Note: See TracBrowser for help on using the repository browser.