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