[23] | 1 | /**
|
---|
[119] | 2 | * \file cf_log.c
|
---|
| 3 | * \author myusgun <myusgun@gmail.com>
|
---|
| 4 | * \author vfire
|
---|
[117] | 5 | *
|
---|
[123] | 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 |
|
---|
[122] | 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 |
|
---|
[123] | 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 |
|
---|
[123] | 77 | /** ·Î±× ÄÁÅؽºÆ® (CF_Log_CtxÀÇ ±¸Çö) */
|
---|
[122] | 78 | typedef struct __cf_log_ctx__
|
---|
| 79 | {
|
---|
[109] | 80 | char path[NAME_LENGTH + 1];
|
---|
| 81 | int fd;
|
---|
| 82 | char * buffer;
|
---|
| 83 | size_t size; /* entire size of buffer */
|
---|
| 84 | size_t length; /* data length in current */
|
---|
| 85 | CF_Mutex_Ctx mutex;
|
---|
| 86 | } CF_LOG_CTX;
|
---|
[11] | 87 |
|
---|
[122] | 88 | typedef struct __cf_log_array__
|
---|
| 89 | {
|
---|
[12] | 90 | CF_Log_Ctx * ctxPool;
|
---|
| 91 | int ctxSize;
|
---|
[109] | 92 | } CF_LOG_ARRAY;
|
---|
[11] | 93 |
|
---|
[51] | 94 | static CF_LOG_ARRAY gLogArray;
|
---|
[11] | 95 |
|
---|
[12] | 96 | #if defined(_WIN32) || defined(_WIN64)
|
---|
[66] | 97 | /* {{{ */
|
---|
[12] | 98 | struct timezone
|
---|
| 99 | {
|
---|
[66] | 100 | int tz_minuteswest; /* minutes W of Greenwich */
|
---|
| 101 | int tz_dsttime; /* type of dst correction */
|
---|
[12] | 102 | };
|
---|
| 103 |
|
---|
[66] | 104 | int gettimeofday (struct timeval *tv, struct timezone *tz)
|
---|
[12] | 105 | {
|
---|
[66] | 106 | FILETIME ft;
|
---|
| 107 | unsigned __int64 buf =0;
|
---|
[38] | 108 | //static int tzflag = 0;
|
---|
[12] | 109 |
|
---|
| 110 | if (NULL != tv)
|
---|
| 111 | {
|
---|
[66] | 112 | GetSystemTimeAsFileTime (&ft);
|
---|
[12] | 113 |
|
---|
[66] | 114 | buf |= ft.dwHighDateTime;
|
---|
[67] | 115 | buf <<= 32;
|
---|
[66] | 116 | buf |= ft.dwLowDateTime;
|
---|
[12] | 117 |
|
---|
[66] | 118 | if (buf)
|
---|
[12] | 119 | {
|
---|
[66] | 120 | buf /= 10;
|
---|
| 121 | buf -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
|
---|
[12] | 122 | }
|
---|
| 123 |
|
---|
[66] | 124 | tv->tv_sec = (long)(buf / 1000000UL);
|
---|
| 125 | tv->tv_usec = (long)(buf % 1000000UL);
|
---|
[12] | 126 | }
|
---|
| 127 |
|
---|
[38] | 128 | /*
|
---|
[12] | 129 | if (NULL != tz)
|
---|
| 130 | {
|
---|
| 131 | if (!tzflag)
|
---|
| 132 | {
|
---|
| 133 | _tzset();
|
---|
| 134 | tzflag++;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | // Adjust for the timezone west of Greenwich
|
---|
| 138 | tz->tz_minuteswest = _timezone / 60;
|
---|
| 139 | tz->tz_dsttime = _daylight;
|
---|
| 140 | }
|
---|
[38] | 141 | */
|
---|
[12] | 142 |
|
---|
| 143 | return 0;
|
---|
| 144 | }
|
---|
[66] | 145 | /* }}} */
|
---|
[50] | 146 | #endif
|
---|
[12] | 147 |
|
---|
[23] | 148 | static int
|
---|
[30] | 149 | CF_Log_Local_GetTime (CF_LOG_DATETIME * dt)
|
---|
[11] | 150 | {
|
---|
[12] | 151 | struct timeval timeVal;
|
---|
| 152 | struct tm * timeSt;
|
---|
| 153 |
|
---|
| 154 | gettimeofday (&timeVal, NULL);
|
---|
[13] | 155 | timeSt = localtime ((const time_t *)&timeVal.tv_sec);
|
---|
[12] | 156 |
|
---|
| 157 | dt->year = timeSt->tm_year + 1900;
|
---|
| 158 | dt->month = timeSt->tm_mon + 1;
|
---|
| 159 | dt->day = timeSt->tm_mday;
|
---|
| 160 | dt->week = timeSt->tm_wday;
|
---|
| 161 |
|
---|
| 162 | dt->hour = timeSt->tm_hour;
|
---|
| 163 | dt->min = timeSt->tm_min;
|
---|
| 164 | dt->sec = timeSt->tm_sec;
|
---|
| 165 |
|
---|
| 166 | dt->usec = (int) timeVal.tv_usec;
|
---|
| 167 |
|
---|
| 168 | return CF_OK;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[23] | 171 | static int
|
---|
[12] | 172 | CF_Log_Local_GetTimeString (char * buffer)
|
---|
| 173 | {
|
---|
[30] | 174 | CF_LOG_DATETIME dt;
|
---|
[12] | 175 | CF_Log_Local_GetTime (&dt);
|
---|
| 176 |
|
---|
[42] | 177 | snprintf (buffer, LOG_DATETIME_LENGTH,
|
---|
[14] | 178 | "%02d-%02d-%02d %02d:%02d:%02d.%03d",
|
---|
| 179 | dt.year, dt.month, dt.day,
|
---|
| 180 | dt.hour, dt.min, dt.sec, dt.usec);
|
---|
| 181 |
|
---|
[12] | 182 | return CF_OK;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[23] | 185 | static int
|
---|
[12] | 186 | CF_Log_Local_Flush (CF_LOG_CTX * ctx)
|
---|
| 187 | {
|
---|
| 188 | if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0)
|
---|
| 189 | return CF_ERROR_LOG_FLUSH;
|
---|
| 190 |
|
---|
| 191 | ctx->length = 0;
|
---|
| 192 |
|
---|
| 193 | return CF_OK;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[35] | 196 | /**
|
---|
[123] | 197 | * ·Î±× µ¥ÀÌÅÍ Ã³¸®
|
---|
[35] | 198 | *
|
---|
[123] | 199 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 200 | *
|
---|
[123] | 201 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
| 202 | * \param buffer ·Î±× µ¥ÀÌÅÍ
|
---|
| 203 | * \param demandSize ·Î±× µ¥ÀÌÅÍ ±æÀÌ
|
---|
[35] | 204 | *
|
---|
[119] | 205 | * \author vfire
|
---|
[35] | 206 | */
|
---|
| 207 | /* static */int
|
---|
| 208 | CF_Log_Local_Push (CF_LOG_CTX * ctx,
|
---|
| 209 | const char * buffer,
|
---|
| 210 | const size_t demandSize)
|
---|
| 211 | {
|
---|
[40] | 212 | int result = CF_OK;
|
---|
| 213 |
|
---|
[123] | 214 | if (ctx->size > 0) /* ¹öÆÛ´ÜÀ§ ¹öÆÛ¸µ.... */
|
---|
[35] | 215 | {
|
---|
| 216 | size_t writeSize;
|
---|
| 217 | size_t remainSize;
|
---|
| 218 |
|
---|
| 219 | remainSize = demandSize;
|
---|
| 220 | while (remainSize)
|
---|
| 221 | {
|
---|
| 222 | writeSize = (ctx->size - ctx->length) < remainSize ? (ctx->size - ctx->length) : remainSize;
|
---|
| 223 |
|
---|
| 224 | memcpy (ctx->buffer + ctx->length, buffer + demandSize - remainSize, writeSize);
|
---|
| 225 | ctx->length += writeSize;
|
---|
| 226 |
|
---|
| 227 | if (ctx->length == ctx->size)
|
---|
| 228 | CF_Log_Local_Flush (ctx);
|
---|
| 229 |
|
---|
| 230 | remainSize -= writeSize;
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
[123] | 233 | else /* flushµÇ¾î¾ß ÇÔ. */
|
---|
[35] | 234 | {
|
---|
[38] | 235 | ctx->buffer = (char *)buffer;
|
---|
| 236 | ctx->length = demandSize;
|
---|
| 237 |
|
---|
[40] | 238 | result = CF_Log_Local_Flush (ctx);
|
---|
[35] | 239 | }
|
---|
| 240 |
|
---|
[40] | 241 | return result;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /**
|
---|
[123] | 245 | * ·Î±× ÄÁÅؽºÆ®¿¡ ¸ÖƼ¾²·¹µå ¸ðµå ¼³Á¤
|
---|
[40] | 246 | *
|
---|
[123] | 247 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[40] | 248 | *
|
---|
[123] | 249 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
[40] | 250 | *
|
---|
[119] | 251 | * \see CF_Log_UnsetMultiThread
|
---|
[40] | 252 | */
|
---|
| 253 | static int
|
---|
| 254 | CF_Log_SetMultiThread (CF_Log_Ctx ctx)
|
---|
| 255 | {
|
---|
| 256 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 257 |
|
---|
[85] | 258 | ASSERT_CTX (ctx);
|
---|
[40] | 259 |
|
---|
[109] | 260 | if (CF_Mutex_CreateCtx (&context->mutex) < 0)
|
---|
[40] | 261 | return CF_ERROR_LOG_SET_MULTITHREAD;
|
---|
| 262 |
|
---|
[35] | 263 | return CF_OK;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
[123] | 267 | * ·Î±× ÄÁÅؽºÆ®¿¡ ¸ÖƼ¾²·¹µå ¸ðµå ¼³Á¤ ÇØÁ¦
|
---|
[35] | 268 | *
|
---|
[123] | 269 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[40] | 270 | *
|
---|
[123] | 271 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
[40] | 272 | *
|
---|
[119] | 273 | * \see CF_Log_SetMultiThread
|
---|
[40] | 274 | */
|
---|
| 275 | static int
|
---|
| 276 | CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
|
---|
| 277 | {
|
---|
| 278 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 279 |
|
---|
[85] | 280 | ASSERT_CTX (ctx);
|
---|
[40] | 281 |
|
---|
[122] | 282 | if (CF_Mutex_DestoryCtx (context->mutex) < 0)
|
---|
[40] | 283 | return CF_ERROR_LOG_UNSET_MULTITHREAD;
|
---|
| 284 |
|
---|
[122] | 285 | context->mutex = NULL;
|
---|
| 286 |
|
---|
[40] | 287 | return CF_OK;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
[123] | 291 | * ·Î±× ÄÁÅؽºÆ®¿¡ µû¶ó ·Î±× ¾²±â
|
---|
[40] | 292 | *
|
---|
[123] | 293 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 294 | *
|
---|
[123] | 295 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
| 296 | * \param prefix ·Î±×ÀÇ ÇÁ¸®ÇȽº ¹®ÀÚ¿
|
---|
| 297 | * \param fmt Æ÷¸Ë ½ºÆ®¸µ
|
---|
| 298 | * \param ... °¡º¯ ÀÎÀÚ
|
---|
[35] | 299 | */
|
---|
[40] | 300 | static int
|
---|
| 301 | CF_Log_WriteCtx (CF_Log_Ctx ctx,
|
---|
| 302 | const char * prefix,
|
---|
| 303 | const char * fmt,
|
---|
| 304 | // ...)
|
---|
| 305 | va_list valist)
|
---|
[12] | 306 | {
|
---|
[107] | 307 | #define BUF_LEN 16 * 1024
|
---|
[40] | 308 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 309 | // va_list valist;
|
---|
[107] | 310 | char buffer[BUF_LEN + 1] = {0x00,};
|
---|
[42] | 311 | char datetime[LOG_DATETIME_LENGTH + 1] = {0x00,};
|
---|
[107] | 312 | int length = 0;
|
---|
[11] | 313 |
|
---|
[85] | 314 | ASSERT_CTX (ctx);
|
---|
[11] | 315 |
|
---|
[40] | 316 | LOCK_LOG_CTX (context);
|
---|
| 317 | // va_start (valist, fmt);
|
---|
| 318 |
|
---|
| 319 | CF_Log_Local_GetTimeString (datetime);
|
---|
[107] | 320 | length = snprintf (buffer, BUF_LEN, "[%s][%s] ", datetime, prefix);
|
---|
| 321 | vsnprintf (&buffer[length], BUF_LEN - (size_t)length, fmt, valist);
|
---|
[40] | 322 |
|
---|
| 323 | CF_Log_Local_Push (context, buffer, strlen (buffer));
|
---|
| 324 |
|
---|
| 325 | // va_end (valist);
|
---|
| 326 | UNLOCK_LOG_CTX (context);
|
---|
| 327 |
|
---|
[11] | 328 | return CF_OK;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
[35] | 331 | /**
|
---|
[123] | 332 | * ·Î±× ¹öÆÛÀÇ µ¥ÀÌÅ͸¦ Áï½Ã ·Î±× ÆÄÀÏ¿¡ ¾²±â
|
---|
[35] | 333 | *
|
---|
[123] | 334 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[40] | 335 | *
|
---|
[123] | 336 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
[35] | 337 | */
|
---|
[40] | 338 | static int
|
---|
| 339 | CF_Log_FlushCtx (CF_Log_Ctx ctx)
|
---|
[11] | 340 | {
|
---|
[40] | 341 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
[19] | 342 |
|
---|
[85] | 343 | ASSERT_CTX (ctx);
|
---|
[40] | 344 |
|
---|
| 345 | LOCK_LOG_CTX (context);
|
---|
| 346 | CF_Log_Local_Flush (context);
|
---|
| 347 | UNLOCK_LOG_CTX (context);
|
---|
| 348 |
|
---|
| 349 | return CF_OK;
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | /**
|
---|
[123] | 353 | * ·Î±× ÄÁÅؽºÆ® ÇØÁ¦
|
---|
[40] | 354 | *
|
---|
[123] | 355 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[40] | 356 | *
|
---|
[123] | 357 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
[40] | 358 | */
|
---|
| 359 | static int
|
---|
| 360 | CF_Log_DestroyCtx (CF_Log_Ctx ctx)
|
---|
| 361 | {
|
---|
| 362 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 363 |
|
---|
[85] | 364 | ASSERT_CTX (ctx);
|
---|
[40] | 365 |
|
---|
| 366 | if (context->size > 0)
|
---|
[19] | 367 | {
|
---|
[40] | 368 | CF_Log_FlushCtx (ctx);
|
---|
| 369 | free (context->buffer);
|
---|
| 370 | context->buffer = NULL;
|
---|
| 371 | context->size = 0;
|
---|
[19] | 372 | }
|
---|
| 373 |
|
---|
[40] | 374 | CF_File_Close (context->fd);
|
---|
[19] | 375 |
|
---|
[122] | 376 | CF_Mutex_DestoryCtx (context->mutex);
|
---|
[45] | 377 | context->mutex = NULL;
|
---|
[11] | 378 |
|
---|
| 379 | return CF_OK;
|
---|
| 380 | }
|
---|
[12] | 381 |
|
---|
[35] | 382 | /**
|
---|
[123] | 383 | * ·Î±× ÄÁÅؽºÆ® »ý¼º
|
---|
[35] | 384 | *
|
---|
[123] | 385 | * \return ¼º°ø ½Ã, ·Î±× ÄÁÅؽºÆ®; ½ÇÆÐ ½Ã, NULL
|
---|
[35] | 386 | *
|
---|
[123] | 387 | * \param ctx ·Î±× ÄÁÅؽºÆ® ÁÖ¼Ò
|
---|
| 388 | * \param path ·Î±× ÆÄÀÏ °æ·Î
|
---|
| 389 | * \param memsize ·Î±× ¹öÆÛ Å©±â
|
---|
[35] | 390 | *
|
---|
[119] | 391 | * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 392 | */
|
---|
[40] | 393 | static int
|
---|
[122] | 394 | CF_Log_CreateCtx (CF_Log_Ctx * ctx,
|
---|
| 395 | const char * path,
|
---|
| 396 | const int memsize)
|
---|
[12] | 397 | {
|
---|
| 398 | int result = 0;
|
---|
[93] | 399 | int fileFlag = CF_FILE_CREATE|CF_FILE_WRITE|CF_FILE_APPEND;
|
---|
[103] | 400 | int size = (memsize == CF_LOG_DEFAULT_BUFFER)
|
---|
[42] | 401 | ? LOG_BUFFER_DEFAULT_SIZE
|
---|
[12] | 402 | : memsize;
|
---|
[87] | 403 |
|
---|
[12] | 404 | CF_LOG_CTX * context = NULL;
|
---|
| 405 |
|
---|
| 406 | if (path == NULL)
|
---|
[40] | 407 | return CF_ERROR_LOG_INVALID_ARGS;
|
---|
[12] | 408 |
|
---|
| 409 | TRY
|
---|
| 410 | {
|
---|
| 411 | context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
|
---|
| 412 | if (context == NULL)
|
---|
| 413 | {
|
---|
[109] | 414 | result = CF_ERROR_LOG_CREATE_CTX;
|
---|
[12] | 415 | TRY_BREAK;
|
---|
| 416 | }
|
---|
| 417 |
|
---|
[93] | 418 | context->fd = CF_File_Open (path, fileFlag);
|
---|
[12] | 419 | if (context->fd < 0)
|
---|
| 420 | {
|
---|
[40] | 421 | result = CF_ERROR_LOG_CREATE_FILE;
|
---|
[12] | 422 | TRY_BREAK;
|
---|
| 423 | }
|
---|
[46] | 424 | snprintf (context->path, sizeof (context->path) -1, "%s", path);
|
---|
[12] | 425 |
|
---|
| 426 | if (size > 0)
|
---|
| 427 | {
|
---|
| 428 | context->buffer = (char *) calloc ((size_t) size + 1, 1);
|
---|
| 429 | if (context->buffer == NULL)
|
---|
| 430 | {
|
---|
[40] | 431 | result = CF_ERROR_LOG_ALLOCATE_BUFFER;
|
---|
[12] | 432 | TRY_BREAK;
|
---|
| 433 | }
|
---|
| 434 | context->size = (size_t) size;
|
---|
| 435 | }
|
---|
[87] | 436 |
|
---|
| 437 | *ctx = (CF_Log_Ctx) context;
|
---|
[12] | 438 | }
|
---|
| 439 | CATCH_IF (result < 0)
|
---|
| 440 | {
|
---|
| 441 | CF_Log_DestroyCtx ((CF_Log_Ctx) context);
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[93] | 444 | return result;
|
---|
[12] | 445 | }
|
---|
| 446 |
|
---|
[35] | 447 | /**
|
---|
[123] | 448 | * ·Î±× ÄÁÅؽºÆ®¿¡ ¾ÆÀ̵ð ³Ñ¹ö ÇÒ´ç<br />
|
---|
| 449 | * ·Î±× ±â·Ï ½Ã, ¾ÆÀ̵ð ³Ñ¹ö¸¦ »ç¿ëÇϸé ÇØ´ç ·Î±×·Î ±â·ÏÇÒ ¼ö ÀÖÀ½
|
---|
[35] | 450 | *
|
---|
[123] | 451 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 452 | *
|
---|
[123] | 453 | * \param mapid ºÎ¿©ÇÒ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
| 454 | * \param ctx ·Î±× ÄÁÅؽºÆ®
|
---|
[40] | 455 | *
|
---|
[123] | 456 | * \remarks ¹Ýµå½Ã ¸ÕÀú ÃʱâÈ ÇؾßÇϸç, ÃʱâÈ ½Ã¿¡ ÁÖ¾îÁø ¹øÈ£º¸´Ù ÀÛÀº ¾ÆÀ̵ð ³Ñ¹ö¸¦ »ç¿ë
|
---|
[40] | 457 | *
|
---|
[119] | 458 | * \see CF_LOG_OPEN, CF_Log_CreateCtx
|
---|
[35] | 459 | */
|
---|
[40] | 460 | static int
|
---|
| 461 | CF_Log_MapCtxID (const int mapid,
|
---|
| 462 | const CF_Log_Ctx ctx)
|
---|
[12] | 463 | {
|
---|
[85] | 464 | ASSERT_INIT ();
|
---|
| 465 | ASSERT_MAPID (mapid);
|
---|
| 466 | ASSERT_MAPPED_CTX (mapid);
|
---|
[12] | 467 |
|
---|
[51] | 468 | gLogArray.ctxPool[mapid] = ctx;
|
---|
[40] | 469 |
|
---|
[12] | 470 | return CF_OK;
|
---|
| 471 | }
|
---|
| 472 |
|
---|
[35] | 473 | /**
|
---|
[123] | 474 | * ¾ÆÀ̵ð ³Ñ¹ö¿¡ ÇØ´çÇÏ´Â ·Î±×¸¦ ´Ý°í ÇØ´çÇÏ´Â ÄÁÅؽºÆ®¸¦ ÇØÁ¦
|
---|
[35] | 475 | *
|
---|
[123] | 476 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 477 | *
|
---|
[123] | 478 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
[40] | 479 | *
|
---|
[123] | 480 | * \remarks ¾ÆÀ̵ð ³Ñ¹ö¿¡ ÇØ´çÇÏ´Â ÄÁÅؽºÆ®°¡ ÇØÁ¦µÇ¹Ç·Î ÁÖÀÇ
|
---|
[40] | 481 | *
|
---|
[119] | 482 | * \see CF_LOG_CLOSE, CF_Log_DestroyCtx
|
---|
[35] | 483 | */
|
---|
[40] | 484 | static int
|
---|
| 485 | CF_Log_UnmapCtxID (const int mapid)
|
---|
[19] | 486 | {
|
---|
[85] | 487 | ASSERT_INIT ();
|
---|
| 488 | ASSERT_MAPID (mapid);
|
---|
| 489 | ASSERT_NOT_MAPPED_CTX (mapid);
|
---|
[19] | 490 |
|
---|
[51] | 491 | CF_Log_DestroyCtx (gLogArray.ctxPool[mapid]);
|
---|
[19] | 492 |
|
---|
[51] | 493 | free (gLogArray.ctxPool[mapid]);
|
---|
| 494 | gLogArray.ctxPool[mapid] = NULL;
|
---|
[19] | 495 |
|
---|
| 496 | return CF_OK;
|
---|
| 497 | }
|
---|
| 498 |
|
---|
[35] | 499 | /**
|
---|
[123] | 500 | * ¾ÆÀ̵ð ³Ñ¹ö¿¡ ÇØ´çÇÏ´Â ·Î±× ÄÁÅؽºÆ®¸¦ ¾ò±â
|
---|
[35] | 501 | *
|
---|
[123] | 502 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 503 | *
|
---|
[123] | 504 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
| 505 | * \param ctx ·Î±× ÄÁÅؽºÆ® ¹ÞÀ» ÁÖ¼Ò
|
---|
[35] | 506 | */
|
---|
[40] | 507 | static int
|
---|
| 508 | CF_Log_GetMappedCtx (const int mapid,
|
---|
| 509 | CF_Log_Ctx * ctx)
|
---|
[19] | 510 | {
|
---|
[85] | 511 | ASSERT_INIT ();
|
---|
| 512 | ASSERT_MAPID (mapid);
|
---|
| 513 | ASSERT_NOT_MAPPED_CTX (mapid);
|
---|
[19] | 514 |
|
---|
[51] | 515 | *ctx = gLogArray.ctxPool[mapid];
|
---|
[19] | 516 |
|
---|
| 517 | return CF_OK;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[35] | 520 | /**
|
---|
[123] | 521 | * ·Î±×¸¦ »ç¿ëÇϱâ À§ÇØ ÃʱâÈ
|
---|
[35] | 522 | *
|
---|
[123] | 523 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 524 | *
|
---|
[123] | 525 | * \param poolSize ·Î±× Ç® Å©±â·Î, ·Î±× ¾ÆÀ̵ð ³Ñ¹öÀÇ ÃÖ´ë °ª
|
---|
[35] | 526 | */
|
---|
[19] | 527 | int
|
---|
[108] | 528 | CF_Log_Initialize (const int poolSize)
|
---|
[12] | 529 | {
|
---|
[51] | 530 | memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
|
---|
[12] | 531 |
|
---|
[101] | 532 | if (poolSize > 0)
|
---|
[40] | 533 | {
|
---|
[51] | 534 | gLogArray.ctxPool =
|
---|
[101] | 535 | (CF_Log_Ctx *) calloc ((size_t) poolSize, sizeof (CF_Log_Ctx));
|
---|
[51] | 536 | if (gLogArray.ctxPool == NULL)
|
---|
[40] | 537 | return CF_ERROR_LOG_INITIALIZE;
|
---|
[101] | 538 | gLogArray.ctxSize = poolSize;
|
---|
[40] | 539 | }
|
---|
[12] | 540 |
|
---|
[40] | 541 | return CF_OK;
|
---|
| 542 | }
|
---|
[12] | 543 |
|
---|
[40] | 544 | /**
|
---|
[123] | 545 | * ·Î±×°¡ ¸ðµÎ »ç¿ëµÈ ÈÄ ÀÚ¿ø ÇØÁ¦
|
---|
[40] | 546 | *
|
---|
[123] | 547 | * \return CF_OK ¹Ýȯ
|
---|
[40] | 548 | */
|
---|
| 549 | int
|
---|
| 550 | CF_Log_Finalize (void)
|
---|
| 551 | {
|
---|
| 552 | int mapid = 0;
|
---|
[12] | 553 |
|
---|
[51] | 554 | for (mapid = 0 ; mapid < gLogArray.ctxSize ; mapid++)
|
---|
[40] | 555 | {
|
---|
| 556 | CF_Log_UnmapCtxID (mapid);
|
---|
| 557 | }
|
---|
[12] | 558 |
|
---|
[51] | 559 | if (gLogArray.ctxPool != NULL)
|
---|
| 560 | free (gLogArray.ctxPool);
|
---|
[12] | 561 |
|
---|
[51] | 562 | memset (&gLogArray, 0x00, sizeof (CF_LOG_ARRAY));
|
---|
[40] | 563 |
|
---|
[12] | 564 | return CF_OK;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[35] | 567 | /**
|
---|
[123] | 568 | * ·Î±× ¿±â
|
---|
[35] | 569 | *
|
---|
[123] | 570 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 571 | *
|
---|
[123] | 572 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
| 573 | * \param path ·Î±× ÆÄÀÏ °æ·Î
|
---|
| 574 | * \param memsize ·Î±× ¹öÆÛ Å©±â
|
---|
[40] | 575 | *
|
---|
[119] | 576 | * \see CF_LOG_DEFAULT_BUFFER, CF_LOG_NO_BUFFER
|
---|
[35] | 577 | */
|
---|
[12] | 578 | int
|
---|
[40] | 579 | CF_Log_Open (const int mapid,
|
---|
| 580 | const char * path,
|
---|
| 581 | const int memsize)
|
---|
[12] | 582 | {
|
---|
[40] | 583 | int result = 0;
|
---|
| 584 | CF_Log_Ctx ctx = NULL;
|
---|
[12] | 585 |
|
---|
[122] | 586 | result = CF_Log_CreateCtx (&ctx, path, memsize);
|
---|
[40] | 587 | if (result < 0)
|
---|
| 588 | return result;
|
---|
[12] | 589 |
|
---|
[40] | 590 | result = CF_Log_MapCtxID (mapid, ctx);
|
---|
[85] | 591 | if (result < 0)
|
---|
| 592 | CF_Log_DestroyCtx (ctx);
|
---|
[12] | 593 |
|
---|
[40] | 594 | return result;
|
---|
[12] | 595 | }
|
---|
[19] | 596 |
|
---|
[35] | 597 | /**
|
---|
[123] | 598 | * ·Î±× ´Ý±â
|
---|
[35] | 599 | *
|
---|
[123] | 600 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 601 | *
|
---|
[123] | 602 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
[40] | 603 | */
|
---|
| 604 | int
|
---|
| 605 | CF_Log_Close (const int mapid)
|
---|
| 606 | {
|
---|
| 607 | return CF_Log_UnmapCtxID (mapid);
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | /**
|
---|
[123] | 611 | * ·Î±× ¹öÆÛÀÇ µ¥ÀÌÅ͸¦ Áï½Ã ·Î±× ÆÄÀÏ¿¡ ¾²±â
|
---|
[35] | 612 | *
|
---|
[123] | 613 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 614 | *
|
---|
[123] | 615 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
[35] | 616 | */
|
---|
[19] | 617 | int
|
---|
[98] | 618 | CF_Log_Flush (const int mapid)
|
---|
[19] | 619 | {
|
---|
[40] | 620 | int result = 0;
|
---|
| 621 | CF_Log_Ctx ctx = NULL;
|
---|
[19] | 622 |
|
---|
[40] | 623 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 624 | if (result < 0)
|
---|
[25] | 625 | return result;
|
---|
[19] | 626 |
|
---|
[98] | 627 | result = CF_Log_FlushCtx (ctx);
|
---|
[19] | 628 |
|
---|
[40] | 629 | return result;
|
---|
[19] | 630 | }
|
---|
| 631 |
|
---|
[35] | 632 | /**
|
---|
[123] | 633 | * ·Î±× ÄÁÅؽºÆ®¿¡ ¸ÖƼ¾²·¹µå ¸ðµå ¼³Á¤
|
---|
[35] | 634 | *
|
---|
[123] | 635 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[35] | 636 | *
|
---|
[123] | 637 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
| 638 | * \param flag ¼³Á¤/ÇØÁ¦ bool Ç÷¡±×
|
---|
[98] | 639 | *
|
---|
[119] | 640 | * \see CF_BOOL
|
---|
[35] | 641 | */
|
---|
[19] | 642 | int
|
---|
[98] | 643 | CF_Log_SetMT (const int mapid,
|
---|
| 644 | const CF_BOOL flag)
|
---|
[19] | 645 | {
|
---|
[40] | 646 | int result = 0;
|
---|
| 647 | CF_Log_Ctx ctx = NULL;
|
---|
[19] | 648 |
|
---|
[40] | 649 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 650 | if (result < 0)
|
---|
| 651 | return result;
|
---|
[19] | 652 |
|
---|
[98] | 653 | result = (flag) ? CF_Log_SetMultiThread (ctx) :
|
---|
| 654 | CF_Log_UnsetMultiThread (ctx);
|
---|
[19] | 655 |
|
---|
[40] | 656 | return result;
|
---|
[19] | 657 | }
|
---|
| 658 |
|
---|
[35] | 659 | /**
|
---|
[123] | 660 | * ·Î±× ¾²±â
|
---|
[98] | 661 | *
|
---|
[123] | 662 | * \return ¼º°ø ½Ã, CF_OK; ½ÇÆÐ ½Ã, ¿À·ù ÄÚµå
|
---|
[98] | 663 | *
|
---|
[123] | 664 | * \param mapid ·Î±×ÀÇ ¾ÆÀ̵ð ³Ñ¹ö
|
---|
| 665 | * \param prefix ·Î±×ÀÇ ÇÁ¸®ÇȽº ¹®ÀÚ¿
|
---|
| 666 | * \param fmt Æ÷¸Ë ½ºÆ®¸µ
|
---|
| 667 | * \param ... °¡º¯ ÀÎÀÚ
|
---|
[98] | 668 | */
|
---|
| 669 | int
|
---|
| 670 | CF_Log_Write (const int mapid,
|
---|
| 671 | const char * prefix,
|
---|
| 672 | const char * fmt, ...)
|
---|
| 673 | {
|
---|
[40] | 674 | int result = 0;
|
---|
| 675 | CF_Log_Ctx ctx = NULL;
|
---|
[98] | 676 | va_list valist;
|
---|
[19] | 677 |
|
---|
[40] | 678 | result = CF_Log_GetMappedCtx (mapid, &ctx);
|
---|
| 679 | if (result < 0)
|
---|
| 680 | return result;
|
---|
| 681 |
|
---|
[98] | 682 | va_start (valist, fmt);
|
---|
| 683 | result = CF_Log_WriteCtx (ctx, prefix, fmt, valist);
|
---|
| 684 | va_end (valist);
|
---|
[40] | 685 |
|
---|
| 686 | return result;
|
---|
[19] | 687 | }
|
---|