logout.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * 用于输出log文件的类.
  3. */
  4. #ifndef LOG_H
  5. #define LOG_H
  6. //log文件路径
  7. #define LOG_FILE_NAME "log_319.txt"
  8. //启用开关
  9. #define LOG_ENABLE
  10. #include <fstream>
  11. #include <string>
  12. #include <ctime>
  13. //#include <Windows.h>
  14. //#include <tchar.h>
  15. /*
  16. using namespace std;
  17. class CLog
  18. {
  19. public:
  20. static void GetLogFilePath(CHAR* szPath)
  21. {
  22. GetModuleFileNameA(NULL, szPath, MAX_PATH);
  23. memset(strrchr(szPath, _T('\\')), NULL, strlen(strrchr(szPath, _T('\\'))) * sizeof(CHAR));
  24. strcat(szPath, "\\");
  25. strcat(szPath, LOG_FILE_NAME);
  26. }
  27. //输出一个内容,可以是字符串(ascii)、整数、浮点数、布尔、枚举
  28. //格式为:[2011-11-11 11:11:11] aaaaaaa并换行
  29. template <class T>
  30. static void WriteLog(T x)
  31. {
  32. CHAR szPath[MAX_PATH] = { 0 };
  33. GetLogFilePath(szPath);
  34. ofstream fout(szPath, ios::app);
  35. fout.seekp(ios::end);
  36. fout << GetSystemTime() << x << endl;
  37. fout.close();
  38. }
  39. //输出2个内容,以等号连接。一般用于前面是一个变量的描述字符串,后面接这个变量的值
  40. template<class T1, class T2>
  41. static void WriteLog2(T1 x1, T2 x2)
  42. {
  43. CHAR szPath[MAX_PATH] = { 0 };
  44. GetLogFilePath(szPath);
  45. ofstream fout(szPath, ios::app);
  46. fout.seekp(ios::end);
  47. fout << GetSystemTime() << x1 << " = " << x2 << endl;
  48. fout.close();
  49. }
  50. template<class T1, class T2, class T3, class T4, class T5, class T6 >
  51. static void WriteLog6(T1 x1, T2 x2, T3 x3, T4 x4, T5 x5, T6 x6)
  52. {
  53. CHAR szPath[MAX_PATH] = { 0 };
  54. GetLogFilePath(szPath);
  55. ofstream fout(szPath, ios::app);
  56. fout.seekp(ios::end);
  57. fout << GetSystemTime() << x1 << " " << x2 << " "
  58. << x3 << " " << x4 << " "
  59. << x5 << " " << x6 << endl;
  60. fout.close();
  61. }
  62. //输出一行当前函数开始的标志,宏传入__FUNCTION__
  63. template <class T>
  64. static void WriteFuncBegin(T x)
  65. {
  66. CHAR szPath[MAX_PATH] = { 0 };
  67. GetLogFilePath(szPath);
  68. ofstream fout(szPath, ios::app);
  69. fout.seekp(ios::end);
  70. fout << GetSystemTime() << " --------------------" << x << " Begin--------------------" << endl;
  71. fout.close();
  72. }
  73. //输出一行当前函数结束的标志,宏传入__FUNCTION__
  74. template <class T>
  75. static void WriteFuncEnd(T x)
  76. {
  77. CHAR szPath[MAX_PATH] = { 0 };
  78. GetLogFilePath(szPath);
  79. ofstream fout(szPath, ios::app);
  80. fout.seekp(ios::end);
  81. fout << GetSystemTime() << "--------------------" << x << " End --------------------" << endl;
  82. fout.close();
  83. }
  84. private:
  85. //获取本地时间,格式如"[2011-11-11 11:11:11] ";
  86. static string GetSystemTime()
  87. {
  88. time_t tNowTime;
  89. time(&tNowTime);
  90. tm* tLocalTime = localtime(&tNowTime);
  91. char szTime[30] = { '\0' };
  92. strftime(szTime, 30, "[%Y-%m-%d %H:%M:%S] ", tLocalTime);
  93. string strTime = szTime;
  94. return strTime;
  95. }
  96. };
  97. #ifdef LOG_ENABLE
  98. //用下面这些宏来使用本文件
  99. #define LOG(x) CLog::WriteLog(x); //括号内可以是字符串(ascii)、整数、浮点数、bool等
  100. #define LOG2(x1,x2) CLog::WriteLog2(x1,x2);
  101. #define LOG6(x1,x2,x3,x4,x5,x6) CLog::WriteLog6(x1,x2,x3,x4,x5,x6);
  102. #define LOG_FUNC LOG(__FUNCTION__) //输出当前所在函数名
  103. #define LOG_LINE LOG(__LINE__) //输出当前行号
  104. #define LOG_FUNC_BEGIN CLog::WriteFuncBegin(__FUNCTION__); //形式如:[时间]"------------FuncName Begin------------"
  105. #define LOG_FUNC_END CLog::WriteFuncEnd(__FUNCTION__); //形式如:[时间]"------------FuncName End------------"
  106. #else
  107. #define LOG(x)
  108. #define LOG2(x1,x2)
  109. #define LOG6(x1,x2,x3,x4,x5,x6)
  110. #define LOG_FUNC
  111. #define LOG_LINE
  112. #define LOG_FUNC_BEGIN
  113. #define LOG_FUNC_END
  114. #endif
  115. */
  116. #endif