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