Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1570217
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2014-05-22 13:02:28


  1. /*
  2.  * 支持linux,windows平台,不支持UNICODE
  3.  * 用例:
  4. #include "mylog.h"
  5. #ifdef _WIN32
  6. int tmain(int argc, _TCHAR* argv[])
  7. #else
  8. int main(int argc, char* argv[])
  9. #endif
  10. {
  11.     log_init("1.txt");
  12.     //log_init(NULL);
  13.     L_ENTER();
  14.     L_FATAL(_TEXT("fatal %d\n"), 1);
  15.     L_ERROR(_TEXT("fatal %d\n"), 1);
  16.     L_WARNING(_TEXT("fatal %d\n"), 1);
  17.     L_INFO(_TEXT("fatal %d\n"), 1);
  18.     L_DEBUG(_TEXT("fatal %d\n"), 1);
  19.     L_TRACE(_TEXT("fatal %d\n"), 1);
  20.     L_LEAVE();

  21.     return 0;
  22. }
  23.  */
  24. #ifndef __MYLOG_H__
  25. #define __MYLOG_H__

  26. // 日志格式
  27. //1812::INFO::2014-04-15 14:48:54::log_version::0.1.0.0

  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif


  31. #include <stdio.h>
  32. #include <stdint.h>
  33. #include <time.h>
  34. #include <wchar.h>

  35. #ifdef _WIN32
  36. #include <windows.h>
  37. #else
  38. #include <sys/types.h>
  39. #include <unistd.h>
  40. #endif


  41. enum LOG_LEVEL_ {
  42.     LOG_LEVEL__TRACE = 0,
  43.     LOG_LEVEL__DEBUG,
  44.     LOG_LEVEL__INFO,
  45.     LOG_LEVEL__WARNING,
  46.     LOG_LEVEL__ERROR,
  47.     LOG_LEVEL__FATAL
  48. };

  49. /*
  50.  *    配置
  51.  * MY_DEFAULT_LOG_FILE: 默认日志文件名
  52.  * MY_DEFAULT_LOG_LEVEL: 默认输出的日志级别
  53.  */
  54. #define MY_DEFAULT_LOG_FILE    "log.txt"
  55. #define MY_DEFAULT_LOG_LEVEL LOG_LEVEL__DEBUG


  56. /**************************************************************************/
  57. /**************************************************************************/


  58. #ifndef TRUE
  59. #define TRUE 1
  60. #endif

  61. #ifndef FALSE
  62. #define FALSE 0
  63. #endif

  64. #ifndef _WIN32
  65. #define _TEXT(x)    x
  66. #define _tfopen fopen
  67. #define _ftprintf fprintf
  68. #define GetCurrentThreadId getpid
  69. #define TCHAR char
  70. #endif

  71. #undef LOG_FILE_NAME
  72. #define LOG_FILE_NAME    _TEXT(MY_DEFAULT_LOG_FILE)

  73. static uint32_t log_level = MY_DEFAULT_LOG_LEVEL;
  74. static uint32_t log_enable = FALSE;
  75. static FILE *fp = NULL;

  76. #ifdef _WIN32
  77. # pragma warning(push)
  78. /* C4996: _CRT_SECURE_NO_WARNINGS. */
  79. # pragma warning(disable: 4996)
  80. #endif

  81. uint32_t log_init(const TCHAR* filename)
  82. {
  83.     if ( filename == NULL ) {
  84.         filename = LOG_FILE_NAME;
  85.     }

  86.     if ( fp != NULL ) {
  87.         fclose(fp);
  88.     }

  89.     fp = _tfopen(filename, _TEXT("a+"));
  90.     if ( fp == NULL ) {
  91.         _ftprintf(stderr, _TEXT("open %s failed, log disable\n"), filename);
  92.         return FALSE;
  93.     }

  94.     log_enable = TRUE;
  95.     return TRUE;
  96. }

  97. #ifdef _WIN32
  98. # pragma warning(pop)
  99. #endif

  100. void current_time(TCHAR *out, int out_len)
  101. {
  102.     time_t timer;                            
  103.     timer = time(NULL);                        
  104. #ifdef _WIN32
  105.     struct tm today;                        
  106.     localtime_s(&today, &timer);                
  107.     strftime(out, out_len, "%Y-%m-%d %H:%M:%S", &today);                        
  108. #else
  109.     struct tm *today;                        
  110.     today = localtime(&timer);                
  111.     strftime(out, out_len, "%Y-%m-%d %H:%M:%S", today);                        
  112. #endif
  113. }

  114. #define MYLOG(level, fmt, ...)    do                \
  115. {                                                \
  116.     if ( log_enable && level >= log_level ) {    \
  117.         static TCHAR level_as_string[][8] = {_TEXT("TRACE"), _TEXT("DEBUG"), _TEXT("INFO "), \
  118.                                             _TEXT("WARN "), _TEXT("ERROR"), _TEXT("FATAL")}; \
  119.         TCHAR datetime_str[20];                    \
  120.         current_time(datetime_str, sizeof(datetime_str)); \
  121.         _ftprintf(fp, _TEXT("%d::%s::%s::%s::")fmt,                                                        \
  122.                         GetCurrentThreadId(), level_as_string[level], datetime_str, __FUNCTION__, ##__VA_ARGS__); \
  123.         fflush(fp);                                \
  124.     }                                            \
  125. } while (0)



  126. #define L_FATAL(fmt, ...) MYLOG(LOG_LEVEL__FATAL, fmt, ## __VA_ARGS__)
  127. #define L_ERROR(fmt, ...) MYLOG(LOG_LEVEL__ERROR, fmt, ## __VA_ARGS__)
  128. #define L_WARNING(fmt, ...) MYLOG(LOG_LEVEL__WARNING, fmt, ## __VA_ARGS__)
  129. #define L_INFO(fmt, ...) MYLOG(LOG_LEVEL__INFO, fmt, ## __VA_ARGS__)
  130. #define L_DEBUG(fmt, ...) MYLOG(LOG_LEVEL__DEBUG, fmt, ## __VA_ARGS__)
  131. #define L_TRACE(fmt, ...) MYLOG(LOG_LEVEL__TRACE, fmt, ## __VA_ARGS__)
  132. #define L_ENTER()        L_DEBUG("Enter.\n")
  133. #define L_LEAVE()        L_DEBUG("Leave.\n")

  134. #ifdef __cplusplus
  135. }
  136. #endif

  137. #endif

阅读(982) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~