[23] | 1 | /**
|
---|
[28] | 2 | * @file cf_log.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
| 4 | * @version 0.1
|
---|
[11] | 5 | */
|
---|
[14] | 6 | #ifdef _WIN32
|
---|
| 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"
|
---|
[11] | 14 |
|
---|
[12] | 15 | #include <stdio.h>
|
---|
| 16 | #include <stdlib.h>
|
---|
[11] | 17 | #include <string.h>
|
---|
[12] | 18 | #include <stdarg.h>
|
---|
| 19 | #include <time.h>
|
---|
[11] | 20 |
|
---|
| 21 | #ifdef _WIN32
|
---|
[14] | 22 | # define snprintf _snprintf
|
---|
[13] | 23 | # include <Windows.h>
|
---|
[11] | 24 | #else // #ifdef _WIN32
|
---|
[12] | 25 | # include <sys/time.h>
|
---|
[11] | 26 | #endif // #ifdef _WIN32
|
---|
| 27 |
|
---|
[25] | 28 | #define CHECK_INVALID_CTX(__ctx) (__ctx == NULL)
|
---|
| 29 | #define LOCK_LOG_CTX(__ctx) CF_Mutex_Lock (&__ctx->mutex)
|
---|
| 30 | #define UNLOCK_LOG_CTX(__ctx) CF_Mutex_Unlock (&__ctx->mutex)
|
---|
| 31 | #define CHECK_INITIALIZED() (gLogEnvironment.ctxPool == NULL|| \
|
---|
| 32 | gLogEnvironment.ctxSize <= 0 )
|
---|
| 33 | #define CHECK_INVALID_MAPID(__mapid) (gLogEnvironment.ctxSize <= __mapid)
|
---|
| 34 | #define CHECK_MAPPED_ID(__mapid) (gLogEnvironment.ctxPool[__mapid] != NULL) \
|
---|
[12] | 35 |
|
---|
| 36 |
|
---|
| 37 | #define CF_LOG_BUFFER_DEFAULT_SIZE 128 * 1024
|
---|
| 38 |
|
---|
[34] | 39 | #define CF_LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1
|
---|
| 40 |
|
---|
[12] | 41 | typedef struct __cf_util_datetime__
|
---|
| 42 | {
|
---|
| 43 | int year;
|
---|
| 44 | int month;
|
---|
| 45 | int day;
|
---|
| 46 | int week; /* SUN:0 ~ SAT:6 */
|
---|
| 47 |
|
---|
| 48 | int hour;
|
---|
| 49 | int min;
|
---|
| 50 | int sec;
|
---|
| 51 | int usec;
|
---|
[30] | 52 | } S_CF_LOG_DATETIME, CF_LOG_DATETIME;
|
---|
[12] | 53 |
|
---|
[11] | 54 | typedef struct __cf_log_ctx__ {
|
---|
| 55 | char path[NAME_LENGTH + 1];
|
---|
| 56 | int fd;
|
---|
| 57 | char * buffer;
|
---|
[12] | 58 | size_t size; /* entire size of buffer */
|
---|
| 59 | size_t length; /* data length in current */
|
---|
[11] | 60 | CF_Mutex mutex;
|
---|
| 61 | } S_CF_LOG_CTX, CF_LOG_CTX;
|
---|
| 62 |
|
---|
| 63 | typedef struct __cf_log_environment__ {
|
---|
[12] | 64 | CF_Log_Ctx * ctxPool;
|
---|
| 65 | int ctxSize;
|
---|
| 66 | } S_CF_LOG_ENVIRONMENT, CF_LOG_ENVIRONMENT;
|
---|
[11] | 67 |
|
---|
[29] | 68 | static CF_LOG_ENVIRONMENT gLogEnvironment;
|
---|
[11] | 69 |
|
---|
[12] | 70 | #if defined(_WIN32) || defined(_WIN64)
|
---|
| 71 | /* #if defined(_WIN32) || defined(_WIN64) {{{ */
|
---|
| 72 | struct timezone
|
---|
| 73 | {
|
---|
| 74 | int tz_minuteswest; /* minutes W of Greenwich */
|
---|
| 75 | int tz_dsttime; /* type of dst correction */
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | // Definition of a gettimeofday function
|
---|
| 79 | int gettimeofday(struct timeval *tv, struct timezone *tz)
|
---|
| 80 | {
|
---|
| 81 | // Define a structure to receive the current Windows filetime
|
---|
| 82 | FILETIME ft;
|
---|
| 83 |
|
---|
| 84 | // Initialize the present time to 0 and the timezone to UTC
|
---|
| 85 | unsigned __int64 ui64 =0;
|
---|
| 86 | static int tzflag = 0;
|
---|
| 87 |
|
---|
| 88 | if (NULL != tv)
|
---|
| 89 | {
|
---|
| 90 | GetSystemTimeAsFileTime(&ft);
|
---|
| 91 |
|
---|
| 92 | ui64 = (((unsigned __int64) ft.dwHighDateTime << 32)
|
---|
| 93 | + (unsigned __int64) ft.dwLowDateTime);
|
---|
| 94 |
|
---|
| 95 | if (ui64)
|
---|
| 96 | {
|
---|
| 97 | ui64 /= 10;
|
---|
| 98 | ui64 -= ((369 * 365 + 89) * (unsigned __int64) 86400) * 1000000;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | // Finally change microseconds to seconds and place in the seconds value.
|
---|
| 102 | // The modulus picks up the microseconds.
|
---|
| 103 | tv->tv_sec = (long)(ui64 / 1000000UL);
|
---|
| 104 | tv->tv_usec = (long)(ui64 % 1000000UL);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (NULL != tz)
|
---|
| 108 | {
|
---|
| 109 | if (!tzflag)
|
---|
| 110 | {
|
---|
| 111 | _tzset();
|
---|
| 112 | tzflag++;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | // Adjust for the timezone west of Greenwich
|
---|
| 116 | tz->tz_minuteswest = _timezone / 60;
|
---|
| 117 | tz->tz_dsttime = _daylight;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | return 0;
|
---|
| 121 | }
|
---|
| 122 | #endif // #if defined(_WIN32) || defined(_WIN64)
|
---|
| 123 | /* }}} #if defined(_WIN32) || defined(_WIN64) */
|
---|
| 124 |
|
---|
[23] | 125 | static int
|
---|
[30] | 126 | CF_Log_Local_GetTime (CF_LOG_DATETIME * dt)
|
---|
[11] | 127 | {
|
---|
[12] | 128 | struct timeval timeVal;
|
---|
| 129 | struct tm * timeSt;
|
---|
| 130 |
|
---|
| 131 | gettimeofday (&timeVal, NULL);
|
---|
[13] | 132 | timeSt = localtime ((const time_t *)&timeVal.tv_sec);
|
---|
[12] | 133 |
|
---|
| 134 | dt->year = timeSt->tm_year + 1900;
|
---|
| 135 | dt->month = timeSt->tm_mon + 1;
|
---|
| 136 | dt->day = timeSt->tm_mday;
|
---|
| 137 | dt->week = timeSt->tm_wday;
|
---|
| 138 |
|
---|
| 139 | dt->hour = timeSt->tm_hour;
|
---|
| 140 | dt->min = timeSt->tm_min;
|
---|
| 141 | dt->sec = timeSt->tm_sec;
|
---|
| 142 |
|
---|
| 143 | dt->usec = (int) timeVal.tv_usec;
|
---|
| 144 |
|
---|
| 145 | return CF_OK;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[23] | 148 | static int
|
---|
[12] | 149 | CF_Log_Local_GetTimeString (char * buffer)
|
---|
| 150 | {
|
---|
[30] | 151 | CF_LOG_DATETIME dt;
|
---|
[12] | 152 | CF_Log_Local_GetTime (&dt);
|
---|
| 153 |
|
---|
[34] | 154 | snprintf (buffer, CF_LOG_DATETIME_LENGTH,
|
---|
[14] | 155 | "%02d-%02d-%02d %02d:%02d:%02d.%03d",
|
---|
| 156 | dt.year, dt.month, dt.day,
|
---|
| 157 | dt.hour, dt.min, dt.sec, dt.usec);
|
---|
| 158 |
|
---|
[12] | 159 | return CF_OK;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[23] | 162 | static int
|
---|
[12] | 163 | CF_Log_Local_Push (CF_LOG_CTX * ctx,
|
---|
| 164 | const char * buffer)
|
---|
| 165 | {
|
---|
| 166 | strncat (ctx->buffer + ctx->length,
|
---|
| 167 | buffer,
|
---|
| 168 | ctx->size - ctx->length);
|
---|
| 169 | ctx->length = strlen (ctx->buffer);
|
---|
| 170 |
|
---|
| 171 | return CF_OK;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[23] | 174 | static int
|
---|
[12] | 175 | CF_Log_Local_Flush (CF_LOG_CTX * ctx)
|
---|
| 176 | {
|
---|
| 177 | if (CF_File_Write (ctx->fd, ctx->buffer, ctx->length) < 0)
|
---|
| 178 | return CF_ERROR_LOG_FLUSH;
|
---|
| 179 |
|
---|
| 180 | memset (ctx->buffer, 0x00, ctx->size);
|
---|
| 181 | ctx->length = 0;
|
---|
| 182 |
|
---|
| 183 | return CF_OK;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | int
|
---|
| 187 | CF_Log_Initialize (const int logPool)
|
---|
| 188 | {
|
---|
| 189 | memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT));
|
---|
[11] | 190 |
|
---|
[12] | 191 | if (logPool > 0)
|
---|
| 192 | {
|
---|
| 193 | gLogEnvironment.ctxPool =
|
---|
| 194 | (CF_Log_Ctx *) calloc ((size_t) logPool, sizeof (CF_Log_Ctx));
|
---|
[26] | 195 | if (gLogEnvironment.ctxPool == NULL)
|
---|
| 196 | return CF_ERROR_LOG_INITIALIZE;
|
---|
[12] | 197 | gLogEnvironment.ctxSize = logPool;
|
---|
| 198 | }
|
---|
[11] | 199 |
|
---|
| 200 | return CF_OK;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | int
|
---|
| 204 | CF_Log_Finalize (void)
|
---|
| 205 | {
|
---|
[19] | 206 | int mapid = 0;
|
---|
| 207 |
|
---|
| 208 | for (mapid = 0 ; mapid < gLogEnvironment.ctxSize ; mapid++)
|
---|
| 209 | {
|
---|
| 210 | CF_Log_UnmapCtxID (mapid);
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | if (gLogEnvironment.ctxPool != NULL)
|
---|
| 214 | free (gLogEnvironment.ctxPool);
|
---|
| 215 |
|
---|
[12] | 216 | memset (&gLogEnvironment, 0x00, sizeof (CF_LOG_ENVIRONMENT));
|
---|
[11] | 217 |
|
---|
| 218 | return CF_OK;
|
---|
| 219 | }
|
---|
[12] | 220 |
|
---|
| 221 | CF_Log_Ctx
|
---|
| 222 | CF_Log_CreateCtx (const char * path,
|
---|
| 223 | const int memsize)
|
---|
| 224 | {
|
---|
| 225 | int result = 0;
|
---|
| 226 | int size = (memsize == CF_LOG_BUFFER_DEFAULT)
|
---|
| 227 | ? CF_LOG_BUFFER_DEFAULT_SIZE
|
---|
| 228 | : memsize;
|
---|
| 229 | CF_LOG_CTX * context = NULL;
|
---|
| 230 |
|
---|
| 231 | if (path == NULL)
|
---|
| 232 | return NULL;
|
---|
| 233 |
|
---|
| 234 | TRY
|
---|
| 235 | {
|
---|
| 236 | context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);
|
---|
| 237 | if (context == NULL)
|
---|
| 238 | {
|
---|
| 239 | result = -1;
|
---|
| 240 | TRY_BREAK;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | context->fd = CF_File_Create (path);
|
---|
| 244 | if (context->fd < 0)
|
---|
| 245 | {
|
---|
| 246 | result = -2;
|
---|
| 247 | TRY_BREAK;
|
---|
| 248 | }
|
---|
| 249 | sprintf (context->path, "%s", path);
|
---|
| 250 |
|
---|
| 251 | if (size > 0)
|
---|
| 252 | {
|
---|
| 253 | context->buffer = (char *) calloc ((size_t) size + 1, 1);
|
---|
| 254 | if (context->buffer == NULL)
|
---|
| 255 | {
|
---|
| 256 | result = -3;
|
---|
| 257 | TRY_BREAK;
|
---|
| 258 | }
|
---|
| 259 | context->size = (size_t) size;
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | CATCH_IF (result < 0)
|
---|
| 263 | {
|
---|
| 264 | CF_Log_DestroyCtx ((CF_Log_Ctx) context);
|
---|
| 265 | return NULL;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | return (CF_Log_Ctx) context;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | int
|
---|
| 272 | CF_Log_DestroyCtx (CF_Log_Ctx ctx)
|
---|
| 273 | {
|
---|
| 274 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 275 |
|
---|
[25] | 276 | if (CHECK_INVALID_CTX (ctx))
|
---|
| 277 | return CF_ERROR_LOG_INVALID_CTX;
|
---|
[12] | 278 |
|
---|
| 279 | memset (context->path, 0x00, sizeof (context->path));
|
---|
| 280 |
|
---|
| 281 | if (context->buffer != NULL)
|
---|
| 282 | {
|
---|
[14] | 283 | CF_Log_Flush (ctx);
|
---|
[12] | 284 | free (context->buffer);
|
---|
| 285 | context->buffer = NULL;
|
---|
| 286 | context->size = 0;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | CF_File_Close (context->fd);
|
---|
| 290 |
|
---|
| 291 | if (context->mutex != NULL)
|
---|
| 292 | {
|
---|
| 293 | if (CF_Mutex_Destory (&context->mutex) < 0)
|
---|
| 294 | /* do something ? */;
|
---|
| 295 |
|
---|
| 296 | context->mutex = NULL;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | return CF_OK;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | int
|
---|
[19] | 303 | CF_Log_SetMultiThread (CF_Log_Ctx ctx)
|
---|
| 304 | {
|
---|
| 305 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 306 |
|
---|
[25] | 307 | if (CHECK_INVALID_CTX (ctx))
|
---|
| 308 | return CF_ERROR_LOG_INVALID_CTX;
|
---|
[19] | 309 |
|
---|
| 310 | if (CF_Mutex_Create (&context->mutex) < 0)
|
---|
| 311 | return CF_ERROR_LOG_SET_MULTITHREAD;
|
---|
| 312 |
|
---|
| 313 | return CF_OK;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | int
|
---|
| 317 | CF_Log_UnsetMultiThread (CF_Log_Ctx ctx)
|
---|
| 318 | {
|
---|
| 319 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 320 |
|
---|
[25] | 321 | if (CHECK_INVALID_CTX (ctx))
|
---|
| 322 | return CF_ERROR_LOG_INVALID_CTX;
|
---|
[19] | 323 |
|
---|
| 324 | if (CF_Mutex_Destory (&context->mutex) < 0)
|
---|
| 325 | return CF_ERROR_LOG_UNSET_MULTITHREAD;
|
---|
| 326 |
|
---|
| 327 | return CF_OK;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | int
|
---|
[12] | 331 | CF_Log_Write (CF_Log_Ctx ctx,
|
---|
| 332 | const char * prefix,
|
---|
| 333 | const char * fmt, ...)
|
---|
| 334 | {
|
---|
| 335 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 336 | va_list valist;
|
---|
| 337 | char buffer[4096] = {0x00,};
|
---|
[34] | 338 | char datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,};
|
---|
[12] | 339 | size_t length = 0;
|
---|
| 340 |
|
---|
[25] | 341 | if (CHECK_INVALID_CTX (ctx))
|
---|
| 342 | return CF_ERROR_LOG_INVALID_CTX;
|
---|
[12] | 343 |
|
---|
| 344 | LOCK_LOG_CTX (context);
|
---|
| 345 | va_start (valist, fmt);
|
---|
| 346 |
|
---|
[34] | 347 | CF_Log_Local_GetTimeString (datetime);
|
---|
| 348 | sprintf (buffer, "[%s][%s] ", datetime, prefix);
|
---|
[12] | 349 | vsprintf (buffer + strlen (buffer), fmt, valist);
|
---|
| 350 |
|
---|
| 351 | if (context->size == CF_LOG_BUFFER_NO)
|
---|
| 352 | {
|
---|
| 353 | context->buffer = buffer;
|
---|
| 354 | context->length = strlen (buffer);
|
---|
| 355 |
|
---|
| 356 | CF_Log_Local_Flush (context);
|
---|
| 357 |
|
---|
| 358 | context->buffer = NULL;
|
---|
| 359 | /* context->length = 0; // already did this in flush */
|
---|
| 360 | }
|
---|
| 361 | else /* (context->size > 0) */
|
---|
| 362 | {
|
---|
| 363 | length = context->size - context->length;
|
---|
| 364 | CF_Log_Local_Push (context, buffer);
|
---|
| 365 |
|
---|
| 366 | if (context->length == context->size)
|
---|
| 367 | {
|
---|
| 368 | CF_Log_Local_Flush (context);
|
---|
| 369 | CF_Log_Local_Push (context, buffer + length);
|
---|
| 370 | }
|
---|
| 371 | }
|
---|
| 372 |
|
---|
| 373 | va_end (valist);
|
---|
| 374 | UNLOCK_LOG_CTX (context);
|
---|
| 375 |
|
---|
| 376 | return CF_OK;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | int
|
---|
| 380 | CF_Log_Flush (CF_Log_Ctx ctx)
|
---|
| 381 | {
|
---|
| 382 | CF_LOG_CTX * context = (CF_LOG_CTX *) ctx;
|
---|
| 383 |
|
---|
[25] | 384 | if (CHECK_INVALID_CTX (ctx))
|
---|
| 385 | return CF_ERROR_LOG_INVALID_CTX;
|
---|
[12] | 386 |
|
---|
| 387 | LOCK_LOG_CTX (context);
|
---|
| 388 | CF_Log_Local_Flush (context);
|
---|
| 389 | UNLOCK_LOG_CTX (context);
|
---|
| 390 |
|
---|
| 391 | return CF_OK;
|
---|
| 392 | }
|
---|
[19] | 393 |
|
---|
| 394 | int
|
---|
| 395 | CF_Log_MapCtxID (const int mapid,
|
---|
| 396 | const CF_Log_Ctx ctx)
|
---|
| 397 | {
|
---|
[25] | 398 | int result = 0;
|
---|
[19] | 399 |
|
---|
[25] | 400 | TRY
|
---|
| 401 | {
|
---|
| 402 | if (CHECK_INITIALIZED())
|
---|
| 403 | {
|
---|
| 404 | result = CF_ERROR_LOG_NOT_INITIALIZE;
|
---|
| 405 | TRY_BREAK;
|
---|
| 406 | }
|
---|
| 407 | if (CHECK_INVALID_MAPID(mapid))
|
---|
| 408 | {
|
---|
| 409 | result = CF_ERROR_LOG_INVALID_MAPID;
|
---|
| 410 | TRY_BREAK;
|
---|
| 411 | }
|
---|
| 412 | if (CHECK_MAPPED_ID(mapid))
|
---|
| 413 | {
|
---|
| 414 | result = CF_ERROR_LOG_ALREADY_MAPPED_ID;
|
---|
| 415 | TRY_BREAK;
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | CATCH_IF (result < 0)
|
---|
| 419 | {
|
---|
| 420 | CF_Log_DestroyCtx (ctx);
|
---|
| 421 | return result;
|
---|
| 422 | }
|
---|
[19] | 423 |
|
---|
| 424 | gLogEnvironment.ctxPool[mapid] = ctx;
|
---|
| 425 |
|
---|
| 426 | return CF_OK;
|
---|
| 427 | }
|
---|
| 428 |
|
---|
| 429 | int
|
---|
| 430 | CF_Log_UnmapCtxID (const int mapid)
|
---|
| 431 | {
|
---|
[25] | 432 | if (CHECK_INITIALIZED())
|
---|
[19] | 433 | return CF_ERROR_LOG_NOT_INITIALIZE;
|
---|
[25] | 434 | if (CHECK_INVALID_MAPID(mapid))
|
---|
| 435 | return CF_ERROR_LOG_INVALID_MAPID;
|
---|
| 436 | if (!CHECK_MAPPED_ID(mapid))
|
---|
[19] | 437 | return CF_ERROR_LOG_NOT_MAPPED_ID;
|
---|
| 438 |
|
---|
| 439 | CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]);
|
---|
| 440 |
|
---|
| 441 | free (gLogEnvironment.ctxPool[mapid]);
|
---|
| 442 | gLogEnvironment.ctxPool[mapid] = NULL;
|
---|
| 443 |
|
---|
| 444 | return CF_OK;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | CF_Log_Ctx
|
---|
| 448 | CF_Log_GetMappedCtx (const int mapid)
|
---|
| 449 | {
|
---|
[25] | 450 | if (CHECK_INITIALIZED())
|
---|
[19] | 451 | return NULL;
|
---|
[25] | 452 | if (CHECK_INVALID_MAPID(mapid))
|
---|
| 453 | return NULL;
|
---|
| 454 | if (!CHECK_MAPPED_ID(mapid))
|
---|
| 455 | return NULL;
|
---|
[19] | 456 |
|
---|
| 457 | return gLogEnvironment.ctxPool[mapid];
|
---|
| 458 | }
|
---|