[23] | 1 | /**
|
---|
[28] | 2 | * @file cf_log.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
[100] | 4 | * @author vfire
|
---|
[117] | 5 | *
|
---|
| 6 | * @brief 로그 구현
|
---|
[11] | 7 | */
|
---|
[50] | 8 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[14] | 9 | # define _USE_32BIT_TIME_T
|
---|
[86] | 10 | # if (_MSC_VER >= 1400)
|
---|
| 11 | # define localtime _localtime32
|
---|
| 12 | # endif
|
---|
[14] | 13 | #endif
|
---|
| 14 |
|
---|
[11] | 15 | #include "cf_log.h"
|
---|
[12] | 16 | #include "cf_file.h"
|
---|
[11] | 17 | #include "cf_thread.h"
|
---|
[12] | 18 | #include "cf_local.h"
|
---|
[40] | 19 | #include "cf_error.h"
|
---|
[11] | 20 |
|
---|
[12] | 21 | #include <stdio.h>
|
---|
| 22 | #include <stdlib.h>
|
---|
[11] | 23 | #include <string.h>
|
---|
[12] | 24 | #include <stdarg.h>
|
---|
| 25 | #include <time.h>
|
---|
[11] | 26 |
|
---|
[50] | 27 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[13] | 28 | # include <Windows.h>
|
---|
[50] | 29 | #else
|
---|
[12] | 30 | # include <sys/time.h>
|
---|
[50] | 31 | #endif
|
---|
[11] | 32 |
|
---|
[85] | 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 |
|
---|
[25] | 54 | #define LOCK_LOG_CTX(__ctx) CF_Mutex_Lock (&__ctx->mutex)
|
---|
| 55 | #define UNLOCK_LOG_CTX(__ctx) CF_Mutex_Unlock (&__ctx->mutex)
|
---|
[12] | 56 |
|
---|
[42] | 57 | #define LOG_BUFFER_DEFAULT_SIZE 128 * 1024
|
---|
[12] | 58 |
|
---|
[51] | 59 | #define LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1
|
---|
[34] | 60 |
|
---|
[109] | 61 | /** 로그 컨텍스트 (Opaque) */
|
---|
[40] | 62 | typedef void * CF_Log_Ctx;
|
---|
| 63 |
|
---|
[12] | 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;
|
---|
[109] | 75 | } CF_LOG_DATETIME;
|
---|
[12] | 76 |
|
---|
[109] | 77 | /** 로그 컨텍스트 (CF_Log_Ctx의 구현) */
|
---|
[11] | 78 | typedef struct __cf_log_ctx__ {
|
---|
[109] | 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;
|
---|
[11] | 86 |
|
---|
[51] | 87 | typedef struct __cf_log_array__ {
|
---|
[12] | 88 | CF_Log_Ctx * ctxPool;
|
---|
| 89 | int ctxSize;
|
---|
[109] | 90 | } CF_LOG_ARRAY;
|
---|
[11] | 91 |
|
---|
[51] | 92 | static CF_LOG_ARRAY gLogArray;
|
---|
[11] | 93 |
|
---|
[12] | 94 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[66] | 95 | /* {{{ */
|
---|
[12] | 96 | struct timezone
|
---|
| 97 | {
|
---|
[66] | 98 | int tz_minuteswest; /* minutes W of Greenwich */
|
---|
| 99 | int tz_dsttime; /* type of dst correction */
|
---|
[12] | 100 | };
|
---|
| 101 |
|
---|
[66] | 102 | int 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] | 146 | static int
|
---|
[30] | 147 | CF_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] | 169 | static int
|
---|
[12] | 170 | CF_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] | 183 | static int
|
---|
[12] | 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 |
|
---|
[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
|
---|
| 206 | CF_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 | */
|
---|
| 251 | static int
|
---|
| 252 | CF_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 | /**
|
---|
[40] | 265 | * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
|
---|
[35] | 266 | *
|
---|
[40] | 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 |
|
---|
[85] | 278 | ASSERT_CTX (ctx);
|
---|
[40] | 279 |
|
---|
[109] | 280 | if (CF_Mutex_DestoryCtx (&context->mutex) < 0)
|
---|
[40] | 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] | 296 | static int
|
---|
| 297 | CF_Log_WriteCtx (CF_Log_Ctx ctx,
|
---|
| 298 | const char * prefix,
|
---|
| 299 | const char * fmt,
|
---|
| 300 | // ...)
|
---|
| 301 | va_list valist)
|
---|
[12] | 302 | {
|
---|
[107] | 303 | #define BUF_LEN 16 * 1024
|
---|
[40] | 304 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 305 | // va_list valist;
|
---|
[107] | 306 | char buffer[BUF_LEN + 1] = {0x00,};
|
---|
[42] | 307 | char datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
|
---|
[107] | 308 | int length = 0;
|
---|
[11] | 309 |
|
---|
[85] | 310 | ASSERT_CTX (ctx);
|
---|
[11] | 311 |
|
---|
[40] | 312 | LOCK_LOG_CTX (context);
|
---|
| 313 | // va_start (valist, fmt);
|
---|
| 314 |
|
---|
| 315 | CF_Log_Local_GetTimeString (datetime);
|
---|
[107] | 316 | length = snprintf (buffer, BUF_LEN, "[%s][%s] ", datetime, prefix);
|
---|
| 317 | vsnprintf (&buffer[length], BUF_LEN - (size_t)length, fmt, valist);
|
---|
[40] | 318 |
|
---|
| 319 | CF_Log_Local_Push (context, buffer, strlen (buffer));
|
---|
| 320 |
|
---|
| 321 | // va_end (valist);
|
---|
| 322 | UNLOCK_LOG_CTX (context);
|
---|
| 323 |
|
---|
[11] | 324 | return CF_OK;
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[35] | 327 | /**
|
---|
[40] | 328 | * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
|
---|
[35] | 329 | *
|
---|
[40] | 330 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 331 | *
|
---|
| 332 | * @param ctx 로그 컨텍스트
|
---|
[35] | 333 | */
|
---|
[40] | 334 | static int
|
---|
| 335 | CF_Log_FlushCtx (CF_Log_Ctx ctx)
|
---|
[11] | 336 | {
|
---|
[40] | 337 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
[19] | 338 |
|
---|
[85] | 339 | ASSERT_CTX (ctx);
|
---|
[40] | 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 |
|
---|
[85] | 360 | ASSERT_CTX (ctx);
|
---|
[40] | 361 |
|
---|
| 362 | if (context->size > 0)
|
---|
[19] | 363 | {
|
---|
[40] | 364 | CF_Log_FlushCtx (ctx);
|
---|
| 365 | free (context->buffer);
|
---|
| 366 | context->buffer = NULL;
|
---|
| 367 | context->size = 0;
|
---|
[19] | 368 | }
|
---|
| 369 |
|
---|
[40] | 370 | CF_File_Close (context->fd);
|
---|
[19] | 371 |
|
---|
[109] | 372 | CF_Mutex_DestoryCtx (&context->mutex);
|
---|
[45] | 373 | context->mutex = NULL;
|
---|
[11] | 374 |
|
---|
| 375 | return CF_OK;
|
---|
| 376 | }
|
---|
[12] | 377 |
|
---|
[35] | 378 | /**
|
---|
| 379 | * 로그 컨텍스트 생성
|
---|
| 380 | *
|
---|
| 381 | * @return 성공 시, 로그 컨텍스트; 실패 시, NULL
|
---|
| 382 | *
|
---|
| 383 | * @param path 로그 파일 경로
|
---|
| 384 | * @param memsize 로그 버퍼 크기
|
---|
[40] | 385 | * @param ctx 로그 컨텍스트 받을 주소
|
---|
[35] | 386 | *
|
---|
[103] | 387 | * @see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 388 | */
|
---|
[40] | 389 | static int
|
---|
[12] | 390 | CF_Log_CreateCtx (const char * path,
|
---|
[40] | 391 | const int memsize,
|
---|
| 392 | CF_Log_Ctx * ctx)
|
---|
[12] | 393 | {
|
---|
| 394 | int result = 0;
|
---|
[93] | 395 | int fileFlag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_APPEND;
|
---|
[103] | 396 | int size = (memsize == CF_LOG_DEFAULT_BUFFER)
|
---|
[42] | 397 | ? LOG_BUFFER_DEFAULT_SIZE
|
---|
[12] | 398 | : memsize;
|
---|
[87] | 399 |
|
---|
[12] | 400 | CF_LOG_CTX * context = NULL;
|
---|
| 401 |
|
---|
| 402 | if (path == NULL)
|
---|
[40] | 403 | return CF_ERROR_LOG_INVALID_ARGS;
|
---|
[12] | 404 |
|
---|
| 405 | TRY
|
---|
| 406 | {
|
---|
| 407 | context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
|
---|
| 408 | if (context == NULL)
|
---|
| 409 | {
|
---|
[109] | 410 | result = CF_ERROR_LOG_CREATE_CTX;
|
---|
[12] | 411 | TRY_BREAK;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[93] | 414 | context->fd = CF_File_Open (path, fileFlag);
|
---|
[12] | 415 | if (context->fd < 0)
|
---|
| 416 | {
|
---|
[40] | 417 | result = CF_ERROR_LOG_CREATE_FILE;
|
---|
[12] | 418 | TRY_BREAK;
|
---|
| 419 | }
|
---|
[46] | 420 | snprintf (context->path, sizeof (context->path) -1, "%s", path);
|
---|
[12] | 421 |
|
---|
| 422 | if (size > 0)
|
---|
| 423 | {
|
---|
| 424 | context->buffer = (char *) calloc ((size_t) size + 1, 1);
|
---|
| 425 | if (context->buffer == NULL)
|
---|
| 426 | {
|
---|
[40] | 427 | result = CF_ERROR_LOG_ALLOCATE_BUFFER;
|
---|
[12] | 428 | TRY_BREAK;
|
---|
| 429 | }
|
---|
| 430 | context->size = (size_t) size;
|
---|
| 431 | }
|
---|
[87] | 432 |
|
---|
| 433 | *ctx = (CF_Log_Ctx) context;
|
---|
[12] | 434 | }
|
---|
| 435 | CATCH_IF (result < 0)
|
---|
| 436 | {
|
---|
| 437 | CF_Log_DestroyCtx ((CF_Log_Ctx) context);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[93] | 440 | return result;
|
---|
[12] | 441 | }
|
---|
| 442 |
|
---|
[35] | 443 | /**
|
---|
[40] | 444 | * 로그 컨텍스트에 아이디 넘버 할당<br />
|
---|
| 445 | * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
|
---|
[35] | 446 | *
|
---|
[40] | 447 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 448 | *
|
---|
[40] | 449 | * @param mapid 부여할 아이디 넘버
|
---|
| 450 | * @param ctx 로그 컨텍스트
|
---|
| 451 | *
|
---|
[116] | 452 | * @remarks 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용
|
---|
[40] | 453 | *
|
---|
| 454 | * @see CF_LOG_OPEN, CF_Log_CreateCtx
|
---|
[35] | 455 | */
|
---|
[40] | 456 | static int
|
---|
| 457 | CF_Log_MapCtxID (const int mapid,
|
---|
| 458 | const CF_Log_Ctx ctx)
|
---|
[12] | 459 | {
|
---|
[85] | 460 | ASSERT_INIT ();
|
---|
| 461 | ASSERT_MAPID (mapid);
|
---|
| 462 | ASSERT_MAPPED_CTX (mapid);
|
---|
[12] | 463 |
|
---|
[51] | 464 | gLogArray.ctxPool[mapid] = ctx;
|
---|
[40] | 465 |
|
---|
[12] | 466 | return CF_OK;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
[35] | 469 | /**
|
---|
[40] | 470 | * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
|
---|
[35] | 471 | *
|
---|
[40] | 472 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 473 | *
|
---|
[40] | 474 | * @param mapid 로그의 아이디 넘버
|
---|
| 475 | *
|
---|
[116] | 476 | * @remarks 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
|
---|
[40] | 477 | *
|
---|
| 478 | * @see CF_LOG_CLOSE, CF_Log_DestroyCtx
|
---|
[35] | 479 | */
|
---|
[40] | 480 | static int
|
---|
| 481 | CF_Log_UnmapCtxID (const int mapid)
|
---|
[19] | 482 | {
|
---|
[85] | 483 | ASSERT_INIT ();
|
---|
| 484 | ASSERT_MAPID (mapid);
|
---|
| 485 | ASSERT_NOT_MAPPED_CTX (mapid);
|
---|
[19] | 486 |
|
---|
[51] | 487 | CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
|
---|
[19] | 488 |
|
---|
[51] | 489 | free (gLogArray.ctxPool[mapid]);
|
---|
| 490 | gLogArray.ctxPool[mapid] = NULL;
|
---|
[19] | 491 |
|
---|
| 492 | return CF_OK;
|
---|
| 493 | }
|
---|
| 494 |
|
---|
[35] | 495 | /**
|
---|
[40] | 496 | * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
|
---|
[35] | 497 | *
|
---|
[40] | 498 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 499 | *
|
---|
[40] | 500 | * @param mapid 로그의 아이디 넘버
|
---|
| 501 | * @param ctx 로그 컨텍스트 받을 주소
|
---|
[35] | 502 | */
|
---|
[40] | 503 | static int
|
---|
| 504 | CF_Log_GetMappedCtx (const int mapid,
|
---|
| 505 | CF_Log_Ctx * ctx)
|
---|
[19] | 506 | {
|
---|
[85] | 507 | ASSERT_INIT ();
|
---|
| 508 | ASSERT_MAPID (mapid);
|
---|
| 509 | ASSERT_NOT_MAPPED_CTX (mapid);
|
---|
[19] | 510 |
|
---|
[51] | 511 | *ctx = gLogArray.ctxPool[mapid];
|
---|
[19] | 512 |
|
---|
| 513 | return CF_OK;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[35] | 516 | /**
|
---|
[40] | 517 | * 로그를 사용하기 위해 초기화
|
---|
[35] | 518 | *
|
---|
| 519 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 520 | *
|
---|
[101] | 521 | * @param poolSize 로그 풀 크기로, 로그 아이디 넘버의 최대 값
|
---|
[35] | 522 | */
|
---|
[19] | 523 | int
|
---|
[108] | 524 | CF_Log_Initialize (const int poolSize)
|
---|
[12] | 525 | {
|
---|
[51] | 526 | memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
|
---|
[12] | 527 |
|
---|
[101] | 528 | if (poolSize > 0)
|
---|
[40] | 529 | {
|
---|
[51] | 530 | gLogArray.ctxPool =
|
---|
[101] | 531 | (CF_Log_Ctx *) calloc ((size_t) poolSize, sizeof (CF_Log_Ctx));
|
---|
[51] | 532 | if (gLogArray.ctxPool == NULL)
|
---|
[40] | 533 | return CF_ERROR_LOG_INITIALIZE;
|
---|
[101] | 534 | gLogArray.ctxSize = poolSize;
|
---|
[40] | 535 | }
|
---|
[12] | 536 |
|
---|
[40] | 537 | return CF_OK;
|
---|
| 538 | }
|
---|
[12] | 539 |
|
---|
[40] | 540 | /**
|
---|
| 541 | * 로그가 모두 사용된 후 자원 해제
|
---|
| 542 | *
|
---|
| 543 | * @return CF_OK 반환
|
---|
| 544 | */
|
---|
| 545 | int
|
---|
| 546 | CF_Log_Finalize (void)
|
---|
| 547 | {
|
---|
| 548 | int mapid = 0;
|
---|
[12] | 549 |
|
---|
[51] | 550 | for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
|
---|
[40] | 551 | {
|
---|
| 552 | CF_Log_UnmapCtxID (mapid);
|
---|
| 553 | }
|
---|
[12] | 554 |
|
---|
[51] | 555 | if (gLogArray.ctxPool != NULL)
|
---|
| 556 | free (gLogArray.ctxPool);
|
---|
[12] | 557 |
|
---|
[51] | 558 | memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
|
---|
[40] | 559 |
|
---|
[12] | 560 | return CF_OK;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
[35] | 563 | /**
|
---|
[40] | 564 | * 로그 열기
|
---|
[35] | 565 | *
|
---|
| 566 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 567 | *
|
---|
[98] | 568 | * @param mapid 로그의 아이디 넘버
|
---|
[40] | 569 | * @param path 로그 파일 경로
|
---|
| 570 | * @param memsize 로그 버퍼 크기
|
---|
| 571 | *
|
---|
[103] | 572 | * @see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 573 | */
|
---|
[12] | 574 | int
|
---|
[40] | 575 | CF_Log_Open (const int mapid,
|
---|
| 576 | const char * path,
|
---|
| 577 | const int memsize)
|
---|
[12] | 578 | {
|
---|
[40] | 579 | int result = 0;
|
---|
| 580 | CF_Log_Ctx ctx = NULL;
|
---|
[12] | 581 |
|
---|
[40] | 582 | result = CF_Log_CreateCtx (path, memsize, &ctx);
|
---|
| 583 | if (result < 0)
|
---|
| 584 | return result;
|
---|
[12] | 585 |
|
---|
[40] | 586 | result = CF_Log_MapCtxID (mapid, ctx);
|
---|
[85] | 587 | if (result < 0)
|
---|
| 588 | CF_Log_DestroyCtx (ctx);
|
---|
[12] | 589 |
|
---|
[40] | 590 | return result;
|
---|
[12] | 591 | }
|
---|
[19] | 592 |
|
---|
[35] | 593 | /**
|
---|
[40] | 594 | * 로그 닫기
|
---|
[35] | 595 | *
|
---|
| 596 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
| 597 | *
|
---|
[40] | 598 | * @param mapid 로그의 아이디 넘버
|
---|
| 599 | */
|
---|
| 600 | int
|
---|
| 601 | CF_Log_Close (const int mapid)
|
---|
| 602 | {
|
---|
| 603 | return CF_Log_UnmapCtxID (mapid);
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | /**
|
---|
[98] | 607 | * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
|
---|
[35] | 608 | *
|
---|
[40] | 609 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 610 | *
|
---|
[98] | 611 | * @param mapid 로그의 아이디 넘버
|
---|
[35] | 612 | */
|
---|
[19] | 613 | int
|
---|
[98] | 614 | CF_Log_Flush (const int mapid)
|
---|
[19] | 615 | {
|
---|
[40] | 616 | int result = 0;
|
---|
| 617 | CF_Log_Ctx ctx = NULL;
|
---|
[19] | 618 |
|
---|
[40] | 619 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 620 | if (result < 0)
|
---|
[25] | 621 | return result;
|
---|
[19] | 622 |
|
---|
[98] | 623 | result = CF_Log_FlushCtx (ctx);
|
---|
[19] | 624 |
|
---|
[40] | 625 | return result;
|
---|
[19] | 626 | }
|
---|
| 627 |
|
---|
[35] | 628 | /**
|
---|
[98] | 629 | * 로그 컨텍스트에 멀티쓰레드 모드 설정
|
---|
[35] | 630 | *
|
---|
[98] | 631 | * @return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 632 | *
|
---|
[98] | 633 | * @param mapid 로그의 아이디 넘버
|
---|
| 634 | * @param flag 설정/해제 bool 플래그
|
---|
| 635 | *
|
---|
| 636 | * @see CF_BOOL
|
---|
[35] | 637 | */
|
---|
[19] | 638 | int
|
---|
[98] | 639 | CF_Log_SetMT (const int mapid,
|
---|
| 640 | const CF_BOOL flag)
|
---|
[19] | 641 | {
|
---|
[40] | 642 | int result = 0;
|
---|
| 643 | CF_Log_Ctx ctx = NULL;
|
---|
[19] | 644 |
|
---|
[40] | 645 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 646 | if (result < 0)
|
---|
| 647 | return result;
|
---|
[19] | 648 |
|
---|
[98] | 649 | result = (flag) ? CF_Log_SetMultiThread (ctx) :
|
---|
| 650 | CF_Log_UnsetMultiThread (ctx);
|
---|
[19] | 651 |
|
---|
[40] | 652 | return result;
|
---|
[19] | 653 | }
|
---|
| 654 |
|
---|
[35] | 655 | /**
|
---|
[98] | 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 | {
|
---|
[40] | 670 | int result = 0;
|
---|
| 671 | CF_Log_Ctx ctx = NULL;
|
---|
[98] | 672 | va_list valist;
|
---|
[19] | 673 |
|
---|
[40] | 674 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 675 | if (result < 0)
|
---|
| 676 | return result;
|
---|
| 677 |
|
---|
[98] | 678 | va_start (valist, fmt);
|
---|
| 679 | result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
|
---|
| 680 | va_end (valist);
|
---|
[40] | 681 |
|
---|
| 682 | return result;
|
---|
[19] | 683 | }
|
---|