C++客户端开发接口库 V5.1.8.15
连接信创集成中间件服务程序的二次开发接口
载入中...
搜索中...
未找到
AppLogger.h
1#pragma once
2#include "mdwclient_global.h"
3#include <fstream>
4#include <string>
5#include <list>
6#include <thread>
7#include <mutex>
8#include <chrono>
9#include <atomic>
10#include <condition_variable>
11#ifdef QT_CORE_LIB
12#include <QApplication>
13#include <QObject>
14#include <QString>
15#include <QColor>
16#include <QMutex>
17#include <QDateTime>
18#endif
19
21enum LOG_MSG_TYPE
22{
23 LT_DEBUG = 0,
24 LT_NORMAL = 1,
25 LT_WARN = 2,
26 LT_ERROR = 3,
27};
30{
32 std::chrono::system_clock::time_point dtTime;
34 LOG_MSG_TYPE eLogType;
36 std::string sFunctionName;
38 std::string sLogMessage;
39};
40
42#ifdef QT_CORE_LIB
44struct SHOW_MSGBUF
45{
46 /* 显示颜色 */
47 QColor Color;
49 QString usMessage;
50};
51
52typedef QList<SHOW_MSGBUF> SHOW_MSGBUFS;
53//---------------------------------------------------------------------------
54#endif
55
56class MDWC_COMPLILE CAppLogger
57{
58private:
60 std::ofstream m_fsLogFileObj;
62 std::string m_sLogFileName;
64 std::string m_sAppIdentity;
66 std::string m_sExecPath;
68 std::tm m_LogFileDate;
70 std::thread *m_thrProcLog;
72 bool m_bWriteDebugLog;
74 std::list<AppLogMsg> m_LogMsgQueue;
76 std::mutex m_queueMutex;
78 std::mutex m_fileMutex;
80 std::condition_variable m_cvHavelLogMsg;
82 std::atomic<bool> m_bStopThread;
84 int64_t m_iLastWriteFileTime;
86 std::chrono::system_clock::time_point m_tLastLogTime;
88 int m_iLogFileRetentionDays;
90#ifdef QT_CORE_LIB
91 QString m_sLastLogMessage;
93 // 该标志在日志线程与主线程间并发读写,改为原子类型消除数据竞争
94 std::atomic<bool> m_bSendShowNotify;
96 QObject* m_pShowLogWnd;
98 SHOW_MSGBUFS* m_lstNotShowLogMsg;
100 QMutex* m_MutexShowLog;
101#else
102 std::string m_sLastLogMessage;
103#endif
104private:
105 // 禁止拷贝构造函数
106 CAppLogger(const CAppLogger&) = delete;
107 // 禁止拷贝赋值运算符
108 CAppLogger& operator=(const CAppLogger&) = delete;
109#ifdef QT_CORE_LIB
116 void AddShowLogMessage(LOG_MSG_TYPE eLogType, const std::string& sLogMessage);
118 void NotifyShowMessage();
119#endif
126 bool Pop(AppLogMsg& logmsg);
132 bool WriteFinished();
134 void ClearFloderLogFile();
136 void CreateLogFile();
138 void CloseLogFile();
140 void WriteLogMsgToFile();
141public:
143 void TruncateLogFile();
145 void EnabledDebugLog(bool bEnabled) { m_bWriteDebugLog = bEnabled; };
147 bool IsEnabledDebugLog() const { return m_bWriteDebugLog; };
154 void WriteLog(const std::string& sFunctionName, const std::string& sLogMsg);
155
162 void WriteLog(const std::string& sFunctionName, const char* fmt, ...);
163
170 void WriteDebug(const std::string& sFunctionName, const std::string& sLogMsg);
177 void WriteDebug(const std::string& sFunctionName, const char* fmt, ...);
178
185 void WriteDebugCompile(const std::string& sFunctionName, const char* fmt, ...);
186
193 void WriteErrorLog(const std::string& sFunctionName, const std::string& sLogMsg);
200 void WriteErrorLog(const std::string& sFunctionName, const char* fmt, ...);
201
208 void WriteWaringLog(const std::string& sFunctionName, const std::string& sLogMsg);
215 void WriteWaringLog(const std::string& sFunctionName, const char* fmt, ...);
216
223 void WriteExceptionLog(const std::string& sFunctionName, const std::exception& ex);
224
225#ifdef QT_CORE_LIB
232 void WriteLog(const QString& usFunctionName, const QString& usLogMsg)
233 {
234 WriteLog(usFunctionName.toStdString(), usLogMsg.toStdString());
235 }
236
243 void WriteDebug(const QString& sFunctionName, const QString& sLogMsg)
244 {
245 return WriteDebug(sFunctionName.toStdString(), sLogMsg.toStdString());
246 }
247
254 void WriteDebugCompile(const QString& sFunctionName, const QString& sLogMsg)
255 {
256#ifdef _DEBUG
257 return WriteDebug(sFunctionName.toStdString(), sLogMsg.toStdString());
258#else
259 (void)sFunctionName;
260 (void)sLogMsg;
261#endif
262 }
263
270 void WriteErrorLog(const QString& sFunctionName, const QString& sLogMsg)
271 {
272 return WriteErrorLog(sFunctionName.toStdString(), sLogMsg.toStdString());
273 }
274
281 void WriteWaringLog(const QString& sFunctionName, const QString& sLogMsg)
282 {
283 return WriteWaringLog(sFunctionName.toStdString(), sLogMsg.toStdString());
284 }
290 QString GetLastLogMessage()
291 {
292 std::lock_guard<std::mutex> lock(m_queueMutex);
293 return m_sLastLogMessage;
294 }
295#else
296 std::string GetLastLogMessage()
297 {
298 std::lock_guard<std::mutex> lock(m_queueMutex);
299 return m_sLastLogMessage;
300 }
301#endif
302public:
307 void StartAppLogger();
309 void StopAppLogger();
311 void OpenLogFile();
313 void ClearLogFile();
314
321 CAppLogger(const std::string& sAppIdentity, const std::string& sWorkDirectory = "", int iLogFileRetentionDays = 100);
322#ifdef QT_CORE_LIB
329 CAppLogger(const QString& usAppIdentity, const QString& usWorkDirectory = "", int iLogFileRetentionDays = 100)
330 : CAppLogger(usAppIdentity.toStdString(), usWorkDirectory.toStdString(), iLogFileRetentionDays)
331 {
332 }
339 void SetShowLogPara(QObject* pShowLogWnd, SHOW_MSGBUFS* lstNotShowLogMsg, QMutex* pMutexShowLog);
340#endif
342 ~CAppLogger();
343};
344
345/* 使用此功能定义对象变量,使用DLL引用时,要在应用程序中定义类实例*/
346extern CAppLogger* AppLogger;
定义 AppLogger.h:57
void EnabledDebugLog(bool bEnabled)
定义 AppLogger.h:145
std::string GetLastLogMessage()
定义 AppLogger.h:296
void TruncateLogFile()
定义 AppLogger.cpp:206
bool IsEnabledDebugLog() const
定义 AppLogger.h:147
定义 AppLogger.h:30
std::string sFunctionName
定义 AppLogger.h:36
LOG_MSG_TYPE eLogType
定义 AppLogger.h:34
std::string sLogMessage
定义 AppLogger.h:38
std::chrono::system_clock::time_point dtTime
定义 AppLogger.h:32