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