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