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