|
25 | 25 | #include "libLogger/LogTemplate.hpp" |
26 | 26 |
|
27 | 27 | #include <QObject> |
| 28 | +#include <QCoreApplication> |
| 29 | +#include <QThread> |
28 | 30 |
|
29 | 31 | namespace Log |
30 | 32 | { |
31 | 33 |
|
32 | | - System::System() |
33 | | - : mNextId(1) |
34 | | - , mConsumer(nullptr) |
| 34 | + namespace Internal |
35 | 35 | { |
36 | | - createDefaultChannels(); |
37 | | - } |
38 | 36 |
|
39 | | - System::~System() |
40 | | - { |
41 | | - } |
| 37 | + void ThreadMover::newEvent(const Log::Event& e) |
| 38 | + { |
| 39 | + System::self()->emitEventAdded(e); |
| 40 | + } |
42 | 41 |
|
| 42 | + System::System() |
| 43 | + : mNextId(1) |
| 44 | + , mConsumer(nullptr) |
| 45 | + { |
| 46 | + createDefaultChannels(); |
| 47 | + } |
43 | 48 |
|
44 | | - void System::release() |
45 | | - { |
46 | | - sSelf.reset(); |
47 | | - } |
| 49 | + System::~System() |
| 50 | + { |
| 51 | + } |
48 | 52 |
|
49 | | - System::Ptr System::sSelf; |
50 | 53 |
|
51 | | - System::Ptr System::self() |
52 | | - { |
53 | | - if (!sSelf) { |
54 | | - sSelf = new System; |
| 54 | + void System::release() |
| 55 | + { |
| 56 | + sSelf.reset(); |
55 | 57 | } |
56 | | - return sSelf; |
57 | | - } |
58 | 58 |
|
59 | | - /** |
60 | | - * @internal |
61 | | - * @brief create the default channels |
62 | | - * |
63 | | - * This method sets up the default channels that are used for convenience logging. |
64 | | - * |
65 | | - */ |
66 | | - void System::createDefaultChannels() |
67 | | - { |
68 | | - Channel ch = Channel::create(CHNAME_ERROR); |
69 | | - ch.setDisplayName(QObject::trUtf8("Errors", "Channelname")); |
| 59 | + System::Ptr System::sSelf; |
70 | 60 |
|
71 | | - Template t = Template::create(CHNAME_ERROR); |
72 | | - t.setTransformation(QStringLiteral("<span style=\"color: red;\">$$</span>")); |
73 | | - ch.setDefaultTemplate(t); |
74 | | - addTemplate(t); |
| 61 | + System::Ptr System::self() |
| 62 | + { |
| 63 | + if (!sSelf) { |
| 64 | + sSelf = Ptr(new System); |
| 65 | + } |
| 66 | + return sSelf; |
| 67 | + } |
75 | 68 |
|
76 | | - addChannel(ch); |
| 69 | + /** |
| 70 | + * @internal |
| 71 | + * @brief create the default channels |
| 72 | + * |
| 73 | + * This method sets up the default channels that are used for convenience logging. |
| 74 | + * |
| 75 | + */ |
| 76 | + void System::createDefaultChannels() |
| 77 | + { |
| 78 | + Channel ch = Channel::create(CHNAME_ERROR); |
| 79 | + ch.setDisplayName(QObject::trUtf8("Errors", "Channelname")); |
77 | 80 |
|
78 | | - ch = Channel::create(CHNAME_WARNING); |
79 | | - ch.setDisplayName(QObject::trUtf8("Warnings", "Channelname")); |
| 81 | + Template t = Template::create(CHNAME_ERROR); |
| 82 | + t.setTransformation(QStringLiteral("<span style=\"color: red;\">$$</span>")); |
| 83 | + ch.setDefaultTemplate(t); |
| 84 | + addTemplate(t); |
80 | 85 |
|
81 | | - t = Template::create(CHNAME_WARNING); |
82 | | - t.setTransformation(QStringLiteral("<span style=\"color: yellow;\">$$</span>")); |
83 | | - addTemplate(t); |
84 | | - ch.setDefaultTemplate(t); |
| 86 | + addChannel(ch); |
85 | 87 |
|
86 | | - addChannel(ch); |
| 88 | + ch = Channel::create(CHNAME_WARNING); |
| 89 | + ch.setDisplayName(QObject::trUtf8("Warnings", "Channelname")); |
87 | 90 |
|
88 | | - ch = Channel::create(CHNAME_INFO); |
89 | | - ch.setDisplayName(QObject::trUtf8("Infos", "Channelname")); |
| 91 | + t = Template::create(CHNAME_WARNING); |
| 92 | + t.setTransformation(QStringLiteral("<span style=\"color: yellow;\">$$</span>")); |
| 93 | + addTemplate(t); |
| 94 | + ch.setDefaultTemplate(t); |
90 | 95 |
|
91 | | - t = Template::create(CHNAME_INFO); |
92 | | - t.setTransformation(QStringLiteral("<span style=\"color: blue;\">$$</span>")); |
93 | | - addTemplate(t); |
94 | | - ch.setDefaultTemplate(t); |
| 96 | + addChannel(ch); |
95 | 97 |
|
96 | | - addChannel(ch); |
| 98 | + ch = Channel::create(CHNAME_INFO); |
| 99 | + ch.setDisplayName(QObject::trUtf8("Infos", "Channelname")); |
97 | 100 |
|
98 | | - ch = Channel::create(CHNAME_DEBUG); |
99 | | - ch.setDisplayName(QObject::trUtf8("Debug", "Channelname")); |
| 101 | + t = Template::create(CHNAME_INFO); |
| 102 | + t.setTransformation(QStringLiteral("<span style=\"color: blue;\">$$</span>")); |
| 103 | + addTemplate(t); |
| 104 | + ch.setDefaultTemplate(t); |
100 | 105 |
|
101 | | - t = Template::create(CHNAME_DEBUG); |
102 | | - t.setTransformation(QStringLiteral("<span style=\"color: navy;\">$$</span>")); |
103 | | - addTemplate(t); |
104 | | - ch.setDefaultTemplate(t); |
| 106 | + addChannel(ch); |
105 | 107 |
|
106 | | - addChannel(ch); |
| 108 | + ch = Channel::create(CHNAME_DEBUG); |
| 109 | + ch.setDisplayName(QObject::trUtf8("Debug", "Channelname")); |
107 | 110 |
|
108 | | - ch = Channel::create(CHNAME_NORMAL); |
109 | | - ch.setDisplayName(QObject::trUtf8("Default output", "Channelname")); |
| 111 | + t = Template::create(CHNAME_DEBUG); |
| 112 | + t.setTransformation(QStringLiteral("<span style=\"color: navy;\">$$</span>")); |
| 113 | + addTemplate(t); |
| 114 | + ch.setDefaultTemplate(t); |
110 | 115 |
|
111 | | - t = Template::create(CHNAME_NORMAL); |
112 | | - t.setTransformation(QStringLiteral("$$")); |
113 | | - addTemplate(t); |
114 | | - ch.setDefaultTemplate(t); |
| 116 | + addChannel(ch); |
115 | 117 |
|
116 | | - addChannel(ch); |
117 | | - } |
| 118 | + ch = Channel::create(CHNAME_NORMAL); |
| 119 | + ch.setDisplayName(QObject::trUtf8("Default output", "Channelname")); |
118 | 120 |
|
119 | | - /** |
120 | | - * @internal |
121 | | - * @brief Inform log consumer of new event |
122 | | - * |
123 | | - * @param[in] event The new event that was added to a channel. |
124 | | - * |
125 | | - * This method is internally called from the Channel when a new Event was added to it. If a |
126 | | - * Consumer is installed, it will be informed about the new event. |
127 | | - * |
128 | | - */ |
129 | | - void System::eventAdded(const Event& event) |
130 | | - { |
131 | | - QMutexLocker l(&mMtx); |
| 121 | + t = Template::create(CHNAME_NORMAL); |
| 122 | + t.setTransformation(QStringLiteral("$$")); |
| 123 | + addTemplate(t); |
| 124 | + ch.setDefaultTemplate(t); |
132 | 125 |
|
133 | | - if (Consumer* c = mConsumer) { |
134 | | - l.unlock(); |
135 | | - c->eventAdded(event); |
| 126 | + addChannel(ch); |
136 | 127 | } |
137 | | - } |
138 | 128 |
|
139 | | - void System::addTemplate(const Template& t) |
140 | | - { |
141 | | - QMutexLocker l(&mMtx); |
142 | | - mTemplates.insert(t.name(), t); |
143 | | - } |
| 129 | + /** |
| 130 | + * @internal |
| 131 | + * @brief Inform log consumer of new event |
| 132 | + * |
| 133 | + * @param[in] event The new event that was added to a channel. |
| 134 | + * |
| 135 | + * This method is internally called from the Channel when a new Event was added to it. If a |
| 136 | + * Consumer is installed, it will be informed about the new event. |
| 137 | + * |
| 138 | + */ |
| 139 | + void System::eventAdded(const Event& event) |
| 140 | + { |
| 141 | + QMutexLocker l(&mMtx); |
| 142 | + |
| 143 | + if (mConsumer) { |
| 144 | + l.unlock(); |
| 145 | + if (qApp->thread() == QThread::currentThread()) { |
| 146 | + emitEventAdded(event); |
| 147 | + } |
| 148 | + else { |
| 149 | + QMetaObject::invokeMethod(&mMover, "newEvent", Qt::QueuedConnection, Q_ARG(Event, event)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
144 | 153 |
|
145 | | - Template System::findTemplate(const QString& name) const |
146 | | - { |
147 | | - QMutexLocker l(&mMtx); |
148 | | - return mTemplates.value(name, Template()); |
149 | | - } |
| 154 | + void System::emitEventAdded(const Event& event) |
| 155 | + { |
| 156 | + QMutexLocker l(&mMtx); |
150 | 157 |
|
151 | | - void System::addChannel(const Channel& ch) |
152 | | - { |
153 | | - QMutexLocker l(&mMtx); |
154 | | - mChannels.insert(ch.name(), ch); |
| 158 | + if (Consumer* c = mConsumer) { |
| 159 | + l.unlock(); |
| 160 | + c->eventAdded(event); |
| 161 | + } |
| 162 | + } |
155 | 163 |
|
156 | | - if (Consumer* c = mConsumer) { |
157 | | - l.unlock(); |
158 | | - c->channelAdded(ch); |
| 164 | + void System::addTemplate(const Template& t) |
| 165 | + { |
| 166 | + QMutexLocker l(&mMtx); |
| 167 | + mTemplates.push_back(t); |
159 | 168 | } |
160 | | - } |
161 | 169 |
|
162 | | - Channel System::findChannel(const QString& name) const |
163 | | - { |
164 | | - QMutexLocker l(&mMtx); |
165 | | - return mChannels.value(name, Channel()); |
166 | | - } |
| 170 | + void System::addTemplate(Template&& t) |
| 171 | + { |
| 172 | + QMutexLocker l(&mMtx); |
| 173 | + mTemplates.push_back(t); |
| 174 | + } |
167 | 175 |
|
168 | | - System::ChannelList System::channels() const |
169 | | - { |
170 | | - ChannelList l; |
| 176 | + Template System::findTemplate(const QString& name) const |
| 177 | + { |
| 178 | + QMutexLocker l(&mMtx); |
| 179 | + for (const Template& t : mTemplates) { |
| 180 | + if (t.name() == name) { |
| 181 | + return t; |
| 182 | + } |
| 183 | + } |
| 184 | + return Template(); |
| 185 | + } |
| 186 | + |
| 187 | + void System::addChannel(const Channel& ch) |
| 188 | + { |
| 189 | + QMutexLocker l(&mMtx); |
| 190 | + mChannels.push_back(ch); |
171 | 191 |
|
172 | | - foreach (const Channel& c, mChannels) { |
173 | | - l << c; |
| 192 | + if (Consumer* c = mConsumer) { |
| 193 | + l.unlock(); |
| 194 | + c->channelAdded(ch); |
| 195 | + } |
174 | 196 | } |
175 | 197 |
|
176 | | - return l; |
177 | | - } |
| 198 | + Channel System::findChannel(const QString& name) const |
| 199 | + { |
| 200 | + QMutexLocker l(&mMtx); |
| 201 | + for (const Channel& ch : mChannels) { |
| 202 | + if (ch.name() == name) { |
| 203 | + return ch; |
| 204 | + } |
| 205 | + } |
| 206 | + return Channel(); |
| 207 | + } |
178 | 208 |
|
179 | | - void System::setConsumer(Consumer* consumer) |
180 | | - { |
181 | | - QMutexLocker l(&mMtx); |
| 209 | + const System::Channels& System::channels() const |
| 210 | + { |
| 211 | + return mChannels; |
| 212 | + } |
182 | 213 |
|
183 | | - if (consumer) { |
184 | | - if (mConsumer != consumer) { |
185 | | - if (mConsumer) { |
186 | | - qDebug("A Log-Consumer was already set..."); |
| 214 | + void System::setConsumer(Consumer* consumer) |
| 215 | + { |
| 216 | + QMutexLocker l(&mMtx); |
| 217 | + |
| 218 | + if (consumer) { |
| 219 | + if (mConsumer != consumer) { |
| 220 | + if (mConsumer) { |
| 221 | + qDebug("A Log-Consumer was already set..."); |
| 222 | + } |
| 223 | + mConsumer = consumer; |
187 | 224 | } |
188 | | - mConsumer = consumer; |
| 225 | + } |
| 226 | + else { |
| 227 | + mConsumer = nullptr; |
189 | 228 | } |
190 | 229 | } |
191 | | - else { |
192 | | - mConsumer = nullptr; |
| 230 | + |
| 231 | + Consumer* System::consumer() const |
| 232 | + { |
| 233 | + QMutexLocker l(&mMtx); |
| 234 | + return mConsumer; |
193 | 235 | } |
194 | | - } |
195 | 236 |
|
196 | | - Consumer* System::consumer() const |
197 | | - { |
198 | | - QMutexLocker l(&mMtx); |
199 | | - return mConsumer; |
200 | | - } |
| 237 | + /** |
| 238 | + * @internal |
| 239 | + * @brief Get tne next event id |
| 240 | + * |
| 241 | + * @return The next available unique id for events. |
| 242 | + * |
| 243 | + */ |
| 244 | + quint64 System::nextLogEventId() |
| 245 | + { |
| 246 | + QMutexLocker l(&mMtx); |
| 247 | + return mNextId++; |
| 248 | + } |
201 | 249 |
|
202 | | - /** |
203 | | - * @internal |
204 | | - * @brief Get tne next event id |
205 | | - * |
206 | | - * @return The next available unique id for events. |
207 | | - * |
208 | | - */ |
209 | | - quint64 System::nextLogEventId() |
210 | | - { |
211 | | - QMutexLocker l(&mMtx); |
212 | | - return mNextId++; |
213 | 250 | } |
214 | 251 |
|
215 | | - |
216 | 252 | } |
0 commit comments