LogManage.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //-----------------------------调用示例--------------------------------Begin
  2. ////引用库
  3. //#include "..\DCAccessWrapperCPlus\DCAccessWrapperCPlus.h"
  4. //using namespace DCAccess;
  5. //
  6. ////用户可以重新初始化覆盖默认设置
  7. //gLogManage.InitInstance(
  8. // CDCAccessPro_Ip.GetBuffer(),
  9. // CDCAccessPro_Port.GetBuffer(),
  10. // CDCAccessPro_TargetId.GetBuffer());
  11. //
  12. //CDCAccessPro_Ip.ReleaseBuffer();
  13. //CDCAccessPro_Port.ReleaseBuffer();
  14. //CDCAccessPro_TargetId.ReleaseBuffer();
  15. //
  16. //SLogRecordPo tmplog;
  17. //tmplog.Modules = "GEN";
  18. //tmplog.MsgId = "1";
  19. //tmplog.Source = "Generator";
  20. //tmplog.HostName = "Hardware";
  21. //tmplog.IPAddress = "192.168.2.82";
  22. //tmplog.ProcessName = "JsonTestDlg";
  23. //tmplog.MsgFilesName = "JsonTestDlg";
  24. //tmplog.LineName = "351";
  25. //tmplog.FreeText = "hello";
  26. //tmplog.Version = "2.2200";
  27. //
  28. //CString strComment;
  29. //GetDlgItemText(IDC_EDIT_COMMENT,strComment);
  30. //tmplog.Comments = strComment;
  31. //
  32. ////设置时间
  33. //SYSTEMTIME localtime;
  34. //GetLocalTime(&localtime);
  35. //tmplog.SetTime(localtime);
  36. //
  37. ////设置参数列表
  38. //tmplog.ClearParaList();
  39. //tmplog.AddPara("name","wanghongwei");
  40. //tmplog.AddPara("age","34");
  41. //tmplog.AddPara("city","beijing");
  42. //
  43. ////Post方式写日志
  44. //bool bPostResult = gLogManage.UpdateLogRecord(tmplog);
  45. //
  46. ////Send方式写日志
  47. //bool bSendResult = gLogManage.UpdateLogRecord(tmplog,ST_SEND);
  48. //-----------------------------调用示例--------------------------------End
  49. #ifndef _LOGMANAGE_H_
  50. #define _LOGMANAGE_H_
  51. #include "AccessBase.h"
  52. namespace DCAccess
  53. {
  54. //--日志--
  55. struct SLogRecordPo
  56. {
  57. string Modules; //(必填)模块ID
  58. string MsgId; //(必填)消息Id
  59. string Source; //(必填)来源
  60. int OccurDate; //(必填)UTC时间与1970年1月1日之间的天数差 设置接口SetTime
  61. int OccurTime; //(必填)UTC时间与1970年1月1日之间的毫秒差 设置接口SetTime
  62. string HostName; //(必填)HostName
  63. string IPAddress; //(可空)IPAddress
  64. string ProcessName; //(可空)ProcessName
  65. string MsgFilesName;//(可空)MsgFilesName
  66. string LineName; //(可空)LineName
  67. string ParaList; //(可空)ParaList 格式,如:"{P1}={V1};{P2}={V2};{P3}={V3};" 设置接口ClearParaList,AddPara
  68. string FreeText; //(可空)FreeText
  69. string Version; //(必填)Version
  70. string Comments; //(可空)Comments
  71. SLogRecordPo()
  72. {
  73. SYSTEMTIME localtime;
  74. GetLocalTime(&localtime);
  75. SetTime(localtime);
  76. }
  77. //将当前系统本地时间转换为UTC时间设置到结构中
  78. //注意:不调用该设置的话,默认设置时间将会将当前PC的本地时间转换为UTC时间设置到结构中
  79. void SetTime(const SYSTEMTIME& localtime)
  80. {
  81. FILETIME localFileTime;
  82. SystemTimeToFileTime(&localtime,&localFileTime);
  83. FILETIME utcFileTime;
  84. LocalFileTimeToFileTime(&localFileTime,&utcFileTime);
  85. SYSTEMTIME utcSysTime;
  86. FileTimeToSystemTime(&utcFileTime,&utcSysTime);
  87. CTimeSpan span = CTime(utcFileTime) - CTime(0);
  88. OccurDate = span.GetDays();
  89. OccurTime = (utcSysTime.wHour*3600 + utcSysTime.wMinute*60 + utcSysTime.wSecond)*1000 + utcSysTime.wMilliseconds;
  90. }
  91. //清空ParaList
  92. void ClearParaList()
  93. {
  94. ParaList = "";
  95. }
  96. //向ParaList中添加参数
  97. void AddPara(string key,string value)
  98. {
  99. CString strtmp;
  100. strtmp.Format("%s{%s}={%s};",ParaList.c_str(),key.c_str(),value.c_str());
  101. ParaList = strtmp;
  102. }
  103. };
  104. class DCACCESS_API LogManage : public AccessBase
  105. {
  106. private:
  107. LogManage(void);
  108. public:
  109. static LogManage& GetInstance();
  110. //默认设置
  111. //InitInstance("127.0.0.1","6630","DIOS.DCService","DIOS.DLL.DCAccessWrapperCPlus");
  112. //日志
  113. //仅供内部模块调用,无须登录
  114. //logRecord 日志信息
  115. //type 发送方式
  116. bool UpdateLogRecord(SLogRecordPo& logRecord,SendType type=ST_POST);
  117. };
  118. };
  119. #define gLogManage DCAccess::LogManage::GetInstance()
  120. #endif