Changeset 40 in libcf for trunk/src/cf_log.c
- Timestamp:
- 02/06/13 16:11:24 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/cf_log.c
r38 r40 12 12 #include "cf_thread.h" 13 13 #include "cf_local.h" 14 #include "cf_error.h" 14 15 15 16 #include <stdio.h> … … 38 39 39 40 #define CF_LOG_DATETIME_LENGTH sizeof ("0000-00-00 00:00:00.000") - 1 41 42 /** 43 * 로그 컨텍스트 44 * 45 * @remark change from public to private 46 */ 47 typedef void * CF_Log_Ctx; 40 48 41 49 typedef struct __cf_util_datetime__ … … 189 197 const size_t demandSize) 190 198 { 199 int result = CF_OK; 200 191 201 if( ctx->size > 0 ) /* 버퍼단위 버퍼링.... */ 192 202 { … … 213 223 ctx->length = demandSize; 214 224 215 if (CF_Log_Local_Flush (ctx) < 0) 216 return CF_ERROR_LOG_FLUSH; 217 } 225 result = CF_Log_Local_Flush (ctx); 226 } 227 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 251 return CF_OK; 252 } 253 254 /** 255 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제 256 * 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 * 280 * @return 성공 시, CF_OK; 실패 시, 오류 코드 281 * 282 * @param ctx 로그 컨텍스트 283 * @param prefix 로그의 프리픽스 문자열 284 * @param fmt 포맷 스트링 285 * @param ... 가변 인자 286 */ 287 static int 288 CF_Log_WriteCtx (CF_Log_Ctx ctx, 289 const char * prefix, 290 const char * fmt, 291 // ...) 292 va_list valist) 293 { 294 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 295 // va_list valist; 296 char buffer[16 * 1024] = {0x00,}; 297 char datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,}; 298 299 if (CHECK_INVALID_CTX (ctx)) 300 return CF_ERROR_LOG_INVALID_CTX; 301 302 LOCK_LOG_CTX (context); 303 // va_start (valist, fmt); 304 305 CF_Log_Local_GetTimeString (datetime); 306 sprintf (buffer, "[%s][%s] ", datetime, prefix); 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 314 return CF_OK; 315 } 316 317 /** 318 * 로그 버퍼의 데이터를 즉시 로그 파일에 쓰기 319 * 320 * @return 성공 시, CF_OK; 실패 시, 오류 코드 321 * 322 * @param ctx 로그 컨텍스트 323 */ 324 static int 325 CF_Log_FlushCtx (CF_Log_Ctx ctx) 326 { 327 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 328 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) 355 { 356 CF_Log_FlushCtx (ctx); 357 free (context->buffer); 358 context->buffer = NULL; 359 context->size = 0; 360 } 361 362 CF_File_Close (context->fd); 363 364 if (context->mutex != NULL) 365 { 366 if (CF_Mutex_Destory (&context->mutex) < 0) 367 /* do something ? */; 368 369 context->mutex = NULL; 370 } 371 372 return CF_OK; 373 } 374 375 /** 376 * 로그 컨텍스트 생성 377 * 378 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL 379 * 380 * @param path 로그 파일 경로 381 * @param memsize 로그 버퍼 크기 382 * @param ctx 로그 컨텍스트 받을 주소 383 * 384 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO 385 */ 386 static int 387 CF_Log_CreateCtx (const char * path, 388 const int memsize, 389 CF_Log_Ctx * ctx) 390 { 391 int result = 0; 392 int size = (memsize == CF_LOG_BUFFER_DEFAULT) 393 ? CF_LOG_BUFFER_DEFAULT_SIZE 394 : memsize; 395 CF_LOG_CTX * context = NULL; 396 397 if (path == NULL) 398 return CF_ERROR_LOG_INVALID_ARGS; 399 400 TRY 401 { 402 context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1); 403 if (context == NULL) 404 { 405 result = CF_ERROR_LOG_ALLOCATE_CTX; 406 TRY_BREAK; 407 } 408 409 context->fd = CF_File_Create (path); 410 if (context->fd < 0) 411 { 412 result = CF_ERROR_LOG_CREATE_FILE; 413 TRY_BREAK; 414 } 415 sprintf (context->path, "%s", path); 416 417 if (size > 0) 418 { 419 context->buffer = (char *) calloc ((size_t) size + 1, 1); 420 if (context->buffer == NULL) 421 { 422 result = CF_ERROR_LOG_ALLOCATE_BUFFER; 423 TRY_BREAK; 424 } 425 context->size = (size_t) size; 426 } 427 } 428 CATCH_IF (result < 0) 429 { 430 CF_Log_DestroyCtx ((CF_Log_Ctx) context); 431 return result; 432 } 433 434 *ctx = (CF_Log_Ctx) context; 435 436 return CF_OK; 437 } 438 439 /** 440 * 로그 컨텍스트에 아이디 넘버 할당<br /> 441 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음 442 * 443 * @return 성공 시, CF_OK; 실패 시, 오류 코드 444 * 445 * @param mapid 부여할 아이디 넘버 446 * @param ctx 로그 컨텍스트 447 * 448 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함 449 * 450 * @see CF_LOG_OPEN, CF_Log_CreateCtx 451 */ 452 static int 453 CF_Log_MapCtxID (const int mapid, 454 const CF_Log_Ctx ctx) 455 { 456 int result = 0; 457 458 TRY 459 { 460 if (CHECK_INITIALIZED ()) 461 { 462 result = CF_ERROR_LOG_NOT_INITIALIZE; 463 TRY_BREAK; 464 } 465 if (CHECK_INVALID_MAPID (mapid)) 466 { 467 result = CF_ERROR_LOG_INVALID_MAPID; 468 TRY_BREAK; 469 } 470 if (CHECK_MAPPED_ID (mapid)) 471 { 472 result = CF_ERROR_LOG_ALREADY_MAPPED_ID; 473 TRY_BREAK; 474 } 475 } 476 CATCH_IF (result < 0) 477 { 478 CF_Log_DestroyCtx (ctx); 479 return result; 480 } 481 482 gLogEnvironment.ctxPool[mapid] = ctx; 483 484 return CF_OK; 485 } 486 487 /** 488 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제 489 * 490 * @return 성공 시, CF_OK; 실패 시, 오류 코드 491 * 492 * @param mapid 로그의 아이디 넘버 493 * 494 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의 495 * 496 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx 497 */ 498 static int 499 CF_Log_UnmapCtxID (const int mapid) 500 { 501 if (CHECK_INITIALIZED ()) 502 return CF_ERROR_LOG_NOT_INITIALIZE; 503 if (CHECK_INVALID_MAPID (mapid)) 504 return CF_ERROR_LOG_INVALID_MAPID; 505 if (!CHECK_MAPPED_ID (mapid)) 506 return CF_ERROR_LOG_NOT_MAPPED_ID; 507 508 CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]); 509 510 free (gLogEnvironment.ctxPool[mapid]); 511 gLogEnvironment.ctxPool[mapid] = NULL; 512 513 return CF_OK; 514 } 515 516 /** 517 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기 518 * 519 * @return 성공 시, CF_OK; 실패 시, 오류 코드 520 * 521 * @param mapid 로그의 아이디 넘버 522 * @param ctx 로그 컨텍스트 받을 주소 523 */ 524 static int 525 CF_Log_GetMappedCtx (const int mapid, 526 CF_Log_Ctx * ctx) 527 { 528 if (CHECK_INITIALIZED ()) 529 return CF_ERROR_LOG_NOT_INITIALIZE; 530 if (CHECK_INVALID_MAPID (mapid)) 531 return CF_ERROR_LOG_INVALID_MAPID; 532 if (!CHECK_MAPPED_ID (mapid)) 533 return CF_ERROR_LOG_NOT_MAPPED_ID; 534 535 *ctx = gLogEnvironment.ctxPool[mapid]; 218 536 219 537 return CF_OK; … … 268 586 269 587 /** 270 * 로그 컨텍스트 생성 271 * 272 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL 273 * 588 * 로그 열기 589 * 590 * @return 성공 시, CF_OK; 실패 시, 오류 코드 591 * 592 * @param mapid 로그의 아이디 넘버 274 593 * @param path 로그 파일 경로 275 594 * @param memsize 로그 버퍼 크기 … … 277 596 * @see CF_LOG_BUFFER_DEFAULT, CF_LOG_BUFFER_NO 278 597 */ 279 CF_Log_Ctx280 CF_Log_CreateCtx (const char * path,281 const int memsize)282 {283 int result = 0;284 int size = (memsize == CF_LOG_BUFFER_DEFAULT)285 ? CF_LOG_BUFFER_DEFAULT_SIZE286 : memsize;287 CF_LOG_CTX * context = NULL;288 289 if (path == NULL)290 return NULL;291 292 TRY293 {294 context = (CF_LOG_CTX *) calloc (sizeof (CF_LOG_CTX), 1);295 if (context == NULL)296 {297 result = -1;298 TRY_BREAK;299 }300 301 context->fd = CF_File_Create (path);302 if (context->fd < 0)303 {304 result = -2;305 TRY_BREAK;306 }307 sprintf (context->path, "%s", path);308 309 if (size > 0)310 {311 context->buffer = (char *) calloc ((size_t) size + 1, 1);312 if (context->buffer == NULL)313 {314 result = -3;315 TRY_BREAK;316 }317 context->size = (size_t) size;318 }319 }320 CATCH_IF (result < 0)321 {322 CF_Log_DestroyCtx ((CF_Log_Ctx) context);323 return NULL;324 }325 326 return (CF_Log_Ctx) context;327 }328 329 /**330 * 로그 컨텍스트 해제331 *332 * @return 성공 시, CF_OK; 실패 시, 오류 코드333 *334 * @param ctx 로그 컨텍스트335 */336 598 int 337 CF_Log_DestroyCtx (CF_Log_Ctx ctx) 338 { 339 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 340 341 if (CHECK_INVALID_CTX (ctx)) 342 return CF_ERROR_LOG_INVALID_CTX; 343 344 if (context->size > 0) 345 { 346 CF_Log_Flush (ctx); 347 free (context->buffer); 348 context->buffer = NULL; 349 context->size = 0; 350 } 351 352 CF_File_Close (context->fd); 353 354 if (context->mutex != NULL) 355 { 356 if (CF_Mutex_Destory (&context->mutex) < 0) 357 /* do something ? */; 358 359 context->mutex = NULL; 360 } 361 362 return CF_OK; 363 } 364 365 /** 366 * 로그 컨텍스트에 멀티쓰레드 모드 설정 367 * 368 * @return 성공 시, CF_OK; 실패 시, 오류 코드 369 * 370 * @param ctx 로그 컨텍스트 599 CF_Log_Open (const int mapid, 600 const char * path, 601 const int memsize) 602 { 603 int result = 0; 604 CF_Log_Ctx ctx = NULL; 605 606 result = CF_Log_CreateCtx (path, memsize, &ctx); 607 if (result < 0) 608 return result; 609 610 result = CF_Log_MapCtxID (mapid, ctx); 611 612 return result; 613 } 614 615 /** 616 * 로그 닫기 617 * 618 * @return 성공 시, CF_OK; 실패 시, 오류 코드 619 * 620 * @param mapid 로그의 아이디 넘버 371 621 */ 372 622 int 373 CF_Log_SetMultiThread (CF_Log_Ctx ctx) 374 { 375 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 376 377 if (CHECK_INVALID_CTX (ctx)) 378 return CF_ERROR_LOG_INVALID_CTX; 379 380 if (CF_Mutex_Create (&context->mutex) < 0) 381 return CF_ERROR_LOG_SET_MULTITHREAD; 382 383 return CF_OK; 384 } 385 386 /** 387 * 로그 컨텍스트에 멀티쓰레드 모드 설정 해제 388 * 389 * @return 성공 시, CF_OK; 실패 시, 오류 코드 390 * 391 * @param ctx 로그 컨텍스트 392 */ 393 int 394 CF_Log_UnsetMultiThread (CF_Log_Ctx ctx) 395 { 396 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 397 398 if (CHECK_INVALID_CTX (ctx)) 399 return CF_ERROR_LOG_INVALID_CTX; 400 401 if (CF_Mutex_Destory (&context->mutex) < 0) 402 return CF_ERROR_LOG_UNSET_MULTITHREAD; 403 404 return CF_OK; 405 } 406 407 /** 408 * 로그 컨텍스트에 따라 로그 쓰기 409 * 410 * @return 성공 시, CF_OK; 실패 시, 오류 코드 411 * 412 * @param ctx 로그 컨텍스트 623 CF_Log_Close (const int mapid) 624 { 625 return CF_Log_UnmapCtxID (mapid); 626 } 627 628 /** 629 * 로그 쓰기 630 * 631 * @return 성공 시, CF_OK; 실패 시, 오류 코드 632 * 633 * @param mapid 로그의 아이디 넘버 413 634 * @param prefix 로그의 프리픽스 문자열 414 635 * @param fmt 포맷 스트링 … … 416 637 */ 417 638 int 418 CF_Log_Write ( CF_Log_Ctx ctx,639 CF_Log_Write (const int mapid, 419 640 const char * prefix, 420 641 const char * fmt, ...) 421 642 { 422 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 643 int result = 0; 644 CF_Log_Ctx ctx = NULL; 423 645 va_list valist; 424 char buffer[16 * 1024] = {0x00,}; 425 char datetime[CF_LOG_DATETIME_LENGTH + 1] = {0x00,}; 426 427 if (CHECK_INVALID_CTX (ctx)) 428 return CF_ERROR_LOG_INVALID_CTX; 429 430 LOCK_LOG_CTX (context); 646 647 result = CF_Log_GetMappedCtx (mapid, &ctx); 648 if (result < 0) 649 return result; 650 431 651 va_start (valist, fmt); 432 433 CF_Log_Local_GetTimeString (datetime); 434 sprintf (buffer, "[%s][%s] ", datetime, prefix); 435 vsprintf (buffer + strlen (buffer), fmt, valist); 436 437 CF_Log_Local_Push (context, buffer, strlen (buffer)); 438 652 result = CF_Log_WriteCtx (ctx, prefix, fmt, valist); 439 653 va_end (valist); 440 UNLOCK_LOG_CTX (context); 441 442 return CF_OK; 654 655 return result; 443 656 } 444 657 … … 448 661 * @return 성공 시, CF_OK; 실패 시, 오류 코드 449 662 * 450 * @param ctx 로그 컨텍스트663 * @param mapid 로그의 아이디 넘버 451 664 */ 452 665 int 453 CF_Log_Flush (CF_Log_Ctx ctx) 454 { 455 CF_LOG_CTX * context = (CF_LOG_CTX *) ctx; 456 457 if (CHECK_INVALID_CTX (ctx)) 458 return CF_ERROR_LOG_INVALID_CTX; 459 460 LOCK_LOG_CTX (context); 461 CF_Log_Local_Flush (context); 462 UNLOCK_LOG_CTX (context); 463 464 return CF_OK; 465 } 466 467 /** 468 * 로그 컨텍스트에 아이디 넘버 할당<br /> 469 * 로그 기록 시, 아이디 넘버를 사용하면 해당 로그로 기록할 수 있음 470 * 471 * @return 성공 시, CF_OK; 실패 시, 오류 코드 472 * 473 * @param mapid 부여할 아이디 넘버 474 * @param ctx 로그 컨텍스트 475 * 476 * @remark 반드시 먼저 초기화 해야하며, 초기화 시에 주어진 번호보다 작은 아이디 넘버를 사용해야 함 477 * 478 * @see CF_LOG_OPEN, CF_Log_CreateCtx 666 CF_Log_Flush (const int mapid) 667 { 668 int result = 0; 669 CF_Log_Ctx ctx = NULL; 670 671 result = CF_Log_GetMappedCtx (mapid, &ctx); 672 if (result < 0) 673 return result; 674 675 result = CF_Log_FlushCtx (ctx); 676 677 return result; 678 } 679 680 /** 681 * 로그 컨텍스트에 멀티쓰레드 모드 설정 682 * 683 * @return 성공 시, CF_OK; 실패 시, 오류 코드 684 * 685 * @param mapid 로그의 아이디 넘버 686 * @param flag 설정/해제 bool 플래그 687 * 688 * @see CF_BOOL 479 689 */ 480 690 int 481 CF_Log_MapCtxID (const int mapid, 482 const CF_Log_Ctx ctx) 483 { 484 int result = 0; 485 486 TRY 487 { 488 if (CHECK_INITIALIZED()) 489 { 490 result = CF_ERROR_LOG_NOT_INITIALIZE; 491 TRY_BREAK; 492 } 493 if (CHECK_INVALID_MAPID(mapid)) 494 { 495 result = CF_ERROR_LOG_INVALID_MAPID; 496 TRY_BREAK; 497 } 498 if (CHECK_MAPPED_ID(mapid)) 499 { 500 result = CF_ERROR_LOG_ALREADY_MAPPED_ID; 501 TRY_BREAK; 502 } 503 } 504 CATCH_IF (result < 0) 505 { 506 CF_Log_DestroyCtx (ctx); 691 CF_Log_SetMT (const int mapid, 692 const CF_BOOL flag) 693 { 694 int result = 0; 695 CF_Log_Ctx ctx = NULL; 696 697 result = CF_Log_GetMappedCtx (mapid, &ctx); 698 if (result < 0) 507 699 return result; 508 } 509 510 gLogEnvironment.ctxPool[mapid] = ctx; 511 512 return CF_OK; 513 } 514 515 /** 516 * 아이디 넘버에 해당하는 로그를 닫고 해당하는 컨텍스트를 해제 517 * 518 * @return 성공 시, CF_OK; 실패 시, 오류 코드 519 * 520 * @param mapid 로그의 아이디 넘버 521 * 522 * @remark 아이디 넘버에 해당하는 컨텍스트가 해제되므로 주의 523 * 524 * @see CF_LOG_CLOSE, CF_Log_DestroyCtx 525 */ 526 int 527 CF_Log_UnmapCtxID (const int mapid) 528 { 529 if (CHECK_INITIALIZED()) 530 return CF_ERROR_LOG_NOT_INITIALIZE; 531 if (CHECK_INVALID_MAPID(mapid)) 532 return CF_ERROR_LOG_INVALID_MAPID; 533 if (!CHECK_MAPPED_ID(mapid)) 534 return CF_ERROR_LOG_NOT_MAPPED_ID; 535 536 CF_Log_DestroyCtx (gLogEnvironment.ctxPool[mapid]); 537 538 free (gLogEnvironment.ctxPool[mapid]); 539 gLogEnvironment.ctxPool[mapid] = NULL; 540 541 return CF_OK; 542 } 543 544 /** 545 * 아이디 넘버에 해당하는 로그 컨텍스트를 얻기 546 * 547 * @return 성공 시, 로그 컨텍스트; 실패 시, NULL 548 * 549 * @param mapid 로그의 아이디 넘버 550 */ 551 CF_Log_Ctx 552 CF_Log_GetMappedCtx (const int mapid) 553 { 554 if (CHECK_INITIALIZED()) 555 return NULL; 556 if (CHECK_INVALID_MAPID(mapid)) 557 return NULL; 558 if (!CHECK_MAPPED_ID(mapid)) 559 return NULL; 560 561 return gLogEnvironment.ctxPool[mapid]; 562 } 700 701 result = (flag == CF_TRUE) 702 ? CF_Log_SetMultiThread (ctx) 703 : CF_Log_UnsetMultiThread (ctx); 704 705 return result; 706 }
Note:
See TracChangeset
for help on using the changeset viewer.