77#include < QJsonDocument>
88#include < QJsonObject>
99#include < QLibrary>
10+ #include < DSGApplication>
1011#include < DSysInfo>
1112
1213#include < mutex>
1314
1415#include < dlfcn.h>
1516#include < unistd.h>
1617
17- // Enable logging for current system version (debug only, remove before release)
18- // #define DDE_EVENTLOG_DEBUG_ENABLE_CURRENT_VERSION
19-
2018namespace DDE_EventLogger {
2119
2220// Check if event logging should be enabled based on system edition
2321// Only UosProfessional is enabled by default
2422inline bool shouldEnableEventLog ()
2523{
26- #ifdef DDE_EVENTLOG_DEBUG_ENABLE_CURRENT_VERSION
27- // Debug mode: enable for current system version
28- return true ;
29- #else
3024 // Production mode: only enable for UosProfessional edition
3125 // Note: DSysInfo is in Dtk::Core namespace
3226 return Dtk::Core::DSysInfo::uosEditionType () == Dtk::Core::DSysInfo::UosProfessional;
33- #endif
3427}
3528
3629typedef bool (*Initialize)(const std::string &package_id, bool enable_sig);
@@ -40,16 +33,16 @@ typedef struct _EventLoggerData
4033{
4134 qint64 tid;
4235 QString target;
43- QMap<QString, QString> message;
36+ QJsonObject message;
4437
4538 _EventLoggerData ()
4639 : tid(0 )
4740 , target(QString())
48- , message(QMap<QString, QString> ())
41+ , message(QJsonObject ())
4942 {
5043 }
5144
52- _EventLoggerData (qint64 tid, const QString &target, const QMap<QString, QString> &message)
45+ _EventLoggerData (qint64 tid, const QString &target, const QJsonObject &message)
5346 : tid(tid)
5447 , target(target)
5548 , message(message)
@@ -81,6 +74,9 @@ class EventLogger
8174 }
8275
8376 std::lock_guard<std::mutex> lock (m_mutex);
77+ if (!m_initialized) {
78+ doInit (QString::fromUtf8 (Dtk::Core::DSGApplication::id ()), false );
79+ }
8480 if (!m_initialized) {
8581 return ;
8682 }
@@ -89,35 +85,6 @@ class EventLogger
8985 m_writeEventLog (json.toJson (QJsonDocument::Compact).toStdString ());
9086 }
9187
92- void writeEventLog (qint64 tid, const QString &target, const QString &key, const QString &value)
93- {
94- writeEventLog (EventLoggerData (tid, target, {{key, value}}));
95- }
96-
97- bool init (QString package_id, bool enable_sig)
98- {
99- if (!shouldEnableEventLog ()) {
100- return false ;
101- }
102-
103- std::lock_guard<std::mutex> lock (m_mutex);
104-
105- if (nullptr == m_initialize || nullptr == m_writeEventLog) {
106- return false ;
107- }
108-
109- if (m_initialized) {
110- return true ;
111- }
112-
113- m_initialized = m_initialize (package_id.toStdString (), enable_sig);
114- if (!m_initialized) {
115- qDebug () << " Failed to initialize event logger" ;
116- return false ;
117- }
118- return true ;
119- }
120-
12188private:
12289 EventLogger ()
12390 : m_initialized(false )
@@ -147,20 +114,30 @@ class EventLogger
147114 EventLogger (const EventLogger &) = delete ; // 禁止拷贝构造函数
148115 EventLogger &operator =(const EventLogger &) = delete ; // 禁止赋值运算符
149116
117+ bool doInit (const QString &package_id, bool enable_sig)
118+ {
119+ if (nullptr == m_initialize || nullptr == m_writeEventLog) {
120+ return false ;
121+ }
122+
123+ if (m_initialized) {
124+ return true ;
125+ }
126+
127+ m_initialized = m_initialize (package_id.toStdString (), enable_sig);
128+ if (!m_initialized) {
129+ qDebug () << " Failed to initialize event logger" ;
130+ }
131+ return m_initialized;
132+ }
133+
150134 // 将结构体转换为 JSON 内容
151135 QJsonDocument structToJson (const EventLoggerData &data)
152136 {
153137 QJsonObject jsonObject;
154138 jsonObject[" tid" ] = data.tid ;
155139 jsonObject[" target" ] = data.target ;
156- QJsonObject msgJson;
157- QMapIterator<QString, QString> iterator (data.message );
158- while (iterator.hasNext ()) {
159- iterator.next ();
160- msgJson[iterator.key ()] = iterator.value ();
161- }
162-
163- jsonObject[" message" ] = msgJson;
140+ jsonObject[" message" ] = data.message ;
164141 QJsonDocument jsonDocument (jsonObject);
165142 return jsonDocument;
166143 }
0 commit comments