[23] | 1 | /**
|
---|
[119] | 2 | * \file cf_log.c
|
---|
| 3 | * \author myusgun <myusgun@gmail.com>
|
---|
| 4 | * \author vfire
|
---|
[117] | 5 | *
|
---|
[127] | 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 |
|
---|
[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 |
|
---|
[127] | 58 | /** 로그 컨텍스트 (Opaque) */
|
---|
[40] | 59 | typedef void * CF_Log_Ctx;
|
---|
| 60 |
|
---|
[12] | 61 | typedef struct __cf_util_datetime__
|
---|
| 62 | {
|
---|
| 63 | int year;
|
---|
| 64 | int month;
|
---|
| 65 | int day;
|
---|
| 66 | int week; /* SUN:0 ~ SAT:6 */
|
---|
| 67 |
|
---|
| 68 | int hour;
|
---|
| 69 | int min;
|
---|
| 70 | int sec;
|
---|
| 71 | int usec;
|
---|
[109] | 72 | } CF_LOG_DATETIME;
|
---|
[12] | 73 |
|
---|
[127] | 74 | /** 로그 컨텍스트 (CF_Log_Ctx의 구현) */
|
---|
[122] | 75 | typedef struct __cf_log_ctx__
|
---|
| 76 | {
|
---|
[109] | 77 | char path[NAME_LENGTH + 1];
|
---|
| 78 | int fd;
|
---|
| 79 | char * buffer;
|
---|
| 80 | size_t size; /* entire size of buffer */
|
---|
| 81 | size_t length; /* data length in current */
|
---|
| 82 | CF_Mutex_Ctx mutex;
|
---|
| 83 | } CF_LOG_CTX;
|
---|
[11] | 84 |
|
---|
[122] | 85 | typedef struct __cf_log_array__
|
---|
| 86 | {
|
---|
[12] | 87 | CF_Log_Ctx * ctxPool;
|
---|
| 88 | int ctxSize;
|
---|
[109] | 89 | } CF_LOG_ARRAY;
|
---|
[11] | 90 |
|
---|
[51] | 91 | static CF_LOG_ARRAY gLogArray;
|
---|
[11] | 92 |
|
---|
[12] | 93 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[66] | 94 | /* {{{ */
|
---|
[12] | 95 | struct timezone
|
---|
| 96 | {
|
---|
[66] | 97 | int tz_minuteswest; /* minutes W of Greenwich */
|
---|
| 98 | int tz_dsttime; /* type of dst correction */
|
---|
[12] | 99 | };
|
---|
| 100 |
|
---|
[66] | 101 | int gettimeofday (struct timeval *tv, struct timezone *tz)
|
---|
[12] | 102 | {
|
---|
[66] | 103 | FILETIME ft;
|
---|
| 104 | unsigned __int64 buf =0;
|
---|
[38] | 105 | //static int tzflag = 0;
|
---|
[12] | 106 |
|
---|
| 107 | if (NULL != tv)
|
---|
| 108 | {
|
---|
[66] | 109 | GetSystemTimeAsFileTime (&ft);
|
---|
[12] | 110 |
|
---|
[66] | 111 | buf |= ft.dwHighDateTime;
|
---|
[67] | 112 | buf <<= 32;
|
---|
[66] | 113 | buf |= ft.dwLowDateTime;
|
---|
[12] | 114 |
|
---|
[66] | 115 | if (buf)
|
---|
[12] | 116 | {
|
---|
[66] | 117 | buf /= 10;
|
---|
| 118 | buf -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
|
---|
[12] | 119 | }
|
---|
| 120 |
|
---|
[66] | 121 | tv->tv_sec = (long)(buf / 1000000UL);
|
---|
| 122 | tv->tv_usec = (long)(buf % 1000000UL);
|
---|
[12] | 123 | }
|
---|
| 124 |
|
---|
[38] | 125 | /*
|
---|
[12] | 126 | if (NULL != tz)
|
---|
| 127 | {
|
---|
| 128 | if (!tzflag)
|
---|
| 129 | {
|
---|
| 130 | _tzset();
|
---|
| 131 | tzflag++;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | // Adjust for the timezone west of Greenwich
|
---|
| 135 | tz->tz_minuteswest = _timezone / 60;
|
---|
| 136 | tz->tz_dsttime = _daylight;
|
---|
| 137 | }
|
---|
[38] | 138 | */
|
---|
[12] | 139 |
|
---|
| 140 | return 0;
|
---|
| 141 | }
|
---|
[66] | 142 | /* }}} */
|
---|
[50] | 143 | #endif
|
---|
[12] | 144 |
|
---|
[23] | 145 | static int
|
---|
[30] | 146 | CF_Log_Local_GetTime (CF_LOG_DATETIME * dt)
|
---|
[11] | 147 | {
|
---|
[12] | 148 | struct timeval timeVal;
|
---|
| 149 | struct tm * timeSt;
|
---|
| 150 |
|
---|
| 151 | gettimeofday (&timeVal, NULL);
|
---|
[13] | 152 | timeSt = localtime ((const time_t *)&timeVal.tv_sec);
|
---|
[12] | 153 |
|
---|
| 154 | dt->year = timeSt->tm_year + 1900;
|
---|
| 155 | dt->month = timeSt->tm_mon + 1;
|
---|
| 156 | dt->day = timeSt->tm_mday;
|
---|
| 157 | dt->week = timeSt->tm_wday;
|
---|
| 158 |
|
---|
| 159 | dt->hour = timeSt->tm_hour;
|
---|
| 160 | dt->min = timeSt->tm_min;
|
---|
| 161 | dt->sec = timeSt->tm_sec;
|
---|
| 162 |
|
---|
| 163 | dt->usec = (int) timeVal.tv_usec;
|
---|
| 164 |
|
---|
| 165 | return CF_OK;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[23] | 168 | static int
|
---|
[12] | 169 | CF_Log_Local_GetTimeString (char * buffer)
|
---|
| 170 | {
|
---|
[30] | 171 | CF_LOG_DATETIME dt;
|
---|
[12] | 172 | CF_Log_Local_GetTime (&dt);
|
---|
| 173 |
|
---|
[42] | 174 | snprintf (buffer, LOG_DATETIME_LENGTH,
|
---|
[127] | 175 | "%04d-%02d-%02d %02d:%02d:%02d.%03d",
|
---|
[14] | 176 | dt.year, dt.month, dt.day,
|
---|
| 177 | dt.hour, dt.min, dt.sec, dt.usec);
|
---|
| 178 |
|
---|
[12] | 179 | return CF_OK;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[23] | 182 | static int
|
---|
[12] | 183 | CF_Log_Local_Flush (CF_LOG_CTX * ctx)
|
---|
| 184 | {
|
---|
| 185 | if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0)
|
---|
| 186 | return CF_ERROR_LOG_FLUSH;
|
---|
| 187 |
|
---|
| 188 | ctx->length = 0;
|
---|
| 189 |
|
---|
| 190 | return CF_OK;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[35] | 193 | /**
|
---|
[127] | 194 | * 로그 데이터 처리
|
---|
[35] | 195 | *
|
---|
[127] | 196 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 197 | *
|
---|
[127] | 198 | * \param ctx 로그 컨텍스트
|
---|
| 199 | * \param buffer 로그 데이터
|
---|
| 200 | * \param demandSize 로그 데이터 길이
|
---|
[35] | 201 | *
|
---|
[119] | 202 | * \author vfire
|
---|
[35] | 203 | */
|
---|
| 204 | /* static */int
|
---|
| 205 | CF_Log_Local_Push (CF_LOG_CTX * ctx,
|
---|
| 206 | const char * buffer,
|
---|
| 207 | const size_t demandSize)
|
---|
| 208 | {
|
---|
[40] | 209 | int result = CF_OK;
|
---|
| 210 |
|
---|
[127] | 211 | if (ctx->size > 0) /* 버퍼단위 버퍼링.... */
|
---|
[35] | 212 | {
|
---|
| 213 | size_t writeSize;
|
---|
| 214 | size_t remainSize;
|
---|
| 215 |
|
---|
| 216 | remainSize = demandSize;
|
---|
| 217 | while (remainSize)
|
---|
| 218 | {
|
---|
| 219 | writeSize = (ctx->size - ctx->length) < remainSize ? (ctx->size - ctx->length) : remainSize;
|
---|
| 220 |
|
---|
| 221 | memcpy (ctx->buffer + ctx->length, buffer + demandSize - remainSize, writeSize);
|
---|
| 222 | ctx->length += writeSize;
|
---|
| 223 |
|
---|
| 224 | if (ctx->length == ctx->size)
|
---|
| 225 | CF_Log_Local_Flush (ctx);
|
---|
| 226 |
|
---|
| 227 | remainSize -= writeSize;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
[127] | 230 | else /* flush되어야 함. */
|
---|
[35] | 231 | {
|
---|
[38] | 232 | ctx->buffer = (char *)buffer;
|
---|
| 233 | ctx->length = demandSize;
|
---|
| 234 |
|
---|
[40] | 235 | result = CF_Log_Local_Flush (ctx);
|
---|
[35] | 236 | }
|
---|
| 237 |
|
---|
[40] | 238 | return result;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
[127] | 242 | * 로그 컨텍스트에 멀티쓰레드 모드 설정
|
---|
[40] | 243 | *
|
---|
[127] | 244 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[40] | 245 | *
|
---|
[127] | 246 | * \param ctx 로그 컨텍스트
|
---|
[40] | 247 | *
|
---|
[119] | 248 | * \see CF_Log_UnsetMultiThread
|
---|
[40] | 249 | */
|
---|
| 250 | static int
|
---|
| 251 | CF_Log_SetMultiThread (CF_Log_Ctx ctx)
|
---|
| 252 | {
|
---|
| 253 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 254 |
|
---|
[85] | 255 | ASSERT_CTX (ctx);
|
---|
[40] | 256 |
|
---|
[109] | 257 | if (CF_Mutex_CreateCtx (&context->mutex) < 0)
|
---|
[40] | 258 | return CF_ERROR_LOG_SET_MULTITHREAD;
|
---|
| 259 |
|
---|
[35] | 260 | return CF_OK;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
[127] | 264 | * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제
|
---|
[35] | 265 | *
|
---|
[127] | 266 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[40] | 267 | *
|
---|
[127] | 268 | * \param ctx 로그 컨텍스트
|
---|
[40] | 269 | *
|
---|
[119] | 270 | * \see CF_Log_SetMultiThread
|
---|
[40] | 271 | */
|
---|
| 272 | static int
|
---|
| 273 | CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
|
---|
| 274 | {
|
---|
| 275 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 276 |
|
---|
[85] | 277 | ASSERT_CTX (ctx);
|
---|
[40] | 278 |
|
---|
[122] | 279 | if (CF_Mutex_DestoryCtx (context->mutex) < 0)
|
---|
[40] | 280 | return CF_ERROR_LOG_UNSET_MULTITHREAD;
|
---|
| 281 |
|
---|
[122] | 282 | context->mutex = NULL;
|
---|
| 283 |
|
---|
[40] | 284 | return CF_OK;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
[127] | 288 | * 로그 컨텍스트에 따라 로그 쓰기
|
---|
[40] | 289 | *
|
---|
[127] | 290 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 291 | *
|
---|
[127] | 292 | * \param ctx 로그 컨텍스트
|
---|
| 293 | * \param prefix 로그의 프리픽스 문자열
|
---|
| 294 | * \param fmt 포맷 스트링
|
---|
| 295 | * \param ... 가변 인자
|
---|
[35] | 296 | */
|
---|
[40] | 297 | static int
|
---|
| 298 | CF_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 |
|
---|
[125] | 313 | CF_Mutex_Lock (context->mutex);
|
---|
[40] | 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);
|
---|
[125] | 323 | CF_Mutex_Unlock (context->mutex);
|
---|
[40] | 324 |
|
---|
[11] | 325 | return CF_OK;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[35] | 328 | /**
|
---|
[127] | 329 | * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
|
---|
[35] | 330 | *
|
---|
[127] | 331 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[40] | 332 | *
|
---|
[127] | 333 | * \param ctx 로그 컨텍스트
|
---|
[35] | 334 | */
|
---|
[40] | 335 | static int
|
---|
| 336 | CF_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 |
|
---|
[125] | 342 | CF_Mutex_Lock (context->mutex);
|
---|
[40] | 343 | CF_Log_Local_Flush (context);
|
---|
[125] | 344 | CF_Mutex_Unlock (context->mutex);
|
---|
[40] | 345 |
|
---|
| 346 | return CF_OK;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | /**
|
---|
[127] | 350 | * 로그 컨텍스트 해제
|
---|
[40] | 351 | *
|
---|
[127] | 352 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[40] | 353 | *
|
---|
[127] | 354 | * \param ctx 로그 컨텍스트
|
---|
[40] | 355 | */
|
---|
| 356 | static int
|
---|
| 357 | CF_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 |
|
---|
[122] | 373 | CF_Mutex_DestoryCtx (context->mutex);
|
---|
[45] | 374 | context->mutex = NULL;
|
---|
[11] | 375 |
|
---|
| 376 | return CF_OK;
|
---|
| 377 | }
|
---|
[12] | 378 |
|
---|
[35] | 379 | /**
|
---|
[127] | 380 | * 로그 컨텍스트 생성
|
---|
[35] | 381 | *
|
---|
[127] | 382 | * \return 성공 시, 로그 컨텍스트; 실패 시, NULL
|
---|
[35] | 383 | *
|
---|
[127] | 384 | * \param ctx 로그 컨텍스트 주소
|
---|
| 385 | * \param path 로그 파일 경로
|
---|
| 386 | * \param memsize 로그 버퍼 크기
|
---|
[35] | 387 | *
|
---|
[119] | 388 | * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 389 | */
|
---|
[40] | 390 | static int
|
---|
[122] | 391 | CF_Log_CreateCtx (CF_Log_Ctx * ctx,
|
---|
| 392 | const char * path,
|
---|
| 393 | const int memsize)
|
---|
[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 | {
|
---|
[109] | 411 | result = CF_ERROR_LOG_CREATE_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 | /**
|
---|
[127] | 445 | * 로그 컨텍스트에 아이디 넘버 할당<br />
|
---|
| 446 | * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음
|
---|
[35] | 447 | *
|
---|
[127] | 448 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 449 | *
|
---|
[127] | 450 | * \param mapid 부여할 아이디 넘버
|
---|
| 451 | * \param ctx 로그 컨텍스트
|
---|
[40] | 452 | *
|
---|
[127] | 453 | * \remarks 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용
|
---|
[40] | 454 | *
|
---|
[119] | 455 | * \see CF_LOG_OPEN, CF_Log_CreateCtx
|
---|
[35] | 456 | */
|
---|
[40] | 457 | static int
|
---|
| 458 | CF_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 | /**
|
---|
[127] | 471 | * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제
|
---|
[35] | 472 | *
|
---|
[127] | 473 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 474 | *
|
---|
[127] | 475 | * \param mapid 로그의 아이디 넘버
|
---|
[40] | 476 | *
|
---|
[127] | 477 | * \remarks 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의
|
---|
[40] | 478 | *
|
---|
[119] | 479 | * \see CF_LOG_CLOSE, CF_Log_DestroyCtx
|
---|
[35] | 480 | */
|
---|
[40] | 481 | static int
|
---|
| 482 | CF_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 | /**
|
---|
[127] | 497 | * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기
|
---|
[35] | 498 | *
|
---|
[127] | 499 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 500 | *
|
---|
[127] | 501 | * \param mapid 로그의 아이디 넘버
|
---|
| 502 | * \param ctx 로그 컨텍스트 받을 주소
|
---|
[35] | 503 | */
|
---|
[40] | 504 | static int
|
---|
| 505 | CF_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 | /**
|
---|
[127] | 518 | * 로그를 사용하기 위해 초기화
|
---|
[35] | 519 | *
|
---|
[127] | 520 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 521 | *
|
---|
[127] | 522 | * \param poolSize 로그 풀 크기로, 로그 아이디 넘버의 최대 값
|
---|
[35] | 523 | */
|
---|
[19] | 524 | int
|
---|
[108] | 525 | CF_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 | /**
|
---|
[127] | 542 | * 로그가 모두 사용된 후 자원 해제
|
---|
[40] | 543 | *
|
---|
[127] | 544 | * \return CF_OK 반환
|
---|
[40] | 545 | */
|
---|
| 546 | int
|
---|
| 547 | CF_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 | /**
|
---|
[127] | 565 | * 로그 열기
|
---|
[35] | 566 | *
|
---|
[127] | 567 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 568 | *
|
---|
[127] | 569 | * \param mapid 로그의 아이디 넘버
|
---|
| 570 | * \param path 로그 파일 경로
|
---|
| 571 | * \param memsize 로그 버퍼 크기
|
---|
[40] | 572 | *
|
---|
[119] | 573 | * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 574 | */
|
---|
[12] | 575 | int
|
---|
[40] | 576 | CF_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 |
|
---|
[122] | 583 | result = CF_Log_CreateCtx (&ctx, path, memsize);
|
---|
[40] | 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 | /**
|
---|
[127] | 595 | * 로그 닫기
|
---|
[35] | 596 | *
|
---|
[127] | 597 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 598 | *
|
---|
[127] | 599 | * \param mapid 로그의 아이디 넘버
|
---|
[40] | 600 | */
|
---|
| 601 | int
|
---|
| 602 | CF_Log_Close (const int mapid)
|
---|
| 603 | {
|
---|
| 604 | return CF_Log_UnmapCtxID (mapid);
|
---|
| 605 | }
|
---|
| 606 |
|
---|
| 607 | /**
|
---|
[127] | 608 | * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기
|
---|
[35] | 609 | *
|
---|
[127] | 610 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 611 | *
|
---|
[127] | 612 | * \param mapid 로그의 아이디 넘버
|
---|
[35] | 613 | */
|
---|
[19] | 614 | int
|
---|
[98] | 615 | CF_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 | /**
|
---|
[127] | 630 | * 로그 컨텍스트에 멀티쓰레드 모드 설정
|
---|
[35] | 631 | *
|
---|
[127] | 632 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[35] | 633 | *
|
---|
[127] | 634 | * \param mapid 로그의 아이디 넘버
|
---|
| 635 | * \param flag 설정/해제 bool 플래그
|
---|
[98] | 636 | *
|
---|
[119] | 637 | * \see CF_BOOL
|
---|
[35] | 638 | */
|
---|
[19] | 639 | int
|
---|
[98] | 640 | CF_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 | /**
|
---|
[127] | 657 | * 로그 쓰기
|
---|
[98] | 658 | *
|
---|
[127] | 659 | * \return 성공 시, CF_OK; 실패 시, 오류 코드
|
---|
[98] | 660 | *
|
---|
[127] | 661 | * \param mapid 로그의 아이디 넘버
|
---|
| 662 | * \param prefix 로그의 프리픽스 문자열
|
---|
| 663 | * \param fmt 포맷 스트링
|
---|
| 664 | * \param ... 가변 인자
|
---|
[98] | 665 | */
|
---|
| 666 | int
|
---|
| 667 | CF_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 | }
|
---|