diff options
Diffstat (limited to 'utils/rbutilqt/logger')
-rw-r--r-- | utils/rbutilqt/logger/LICENSE.LGPL | 504 | ||||
-rw-r--r-- | utils/rbutilqt/logger/README.ROCKBOX | 7 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/AbstractAppender.h | 49 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/AbstractStringAppender.h | 46 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/ConsoleAppender.h | 36 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/CuteLogger_global.h | 16 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/FileAppender.h | 49 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/Logger.h | 238 | ||||
-rw-r--r-- | utils/rbutilqt/logger/include/OutputDebugAppender.h | 29 | ||||
-rw-r--r-- | utils/rbutilqt/logger/logger.pri | 22 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/AbstractAppender.cpp | 147 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/AbstractStringAppender.cpp | 460 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/ConsoleAppender.cpp | 64 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/FileAppender.cpp | 116 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/Logger.cpp | 1108 | ||||
-rw-r--r-- | utils/rbutilqt/logger/src/OutputDebugAppender.cpp | 43 |
16 files changed, 2934 insertions, 0 deletions
diff --git a/utils/rbutilqt/logger/LICENSE.LGPL b/utils/rbutilqt/logger/LICENSE.LGPL new file mode 100644 index 0000000000..5ab7695ab8 --- /dev/null +++ b/utils/rbutilqt/logger/LICENSE.LGPL | |||
@@ -0,0 +1,504 @@ | |||
1 | GNU LESSER GENERAL PUBLIC LICENSE | ||
2 | Version 2.1, February 1999 | ||
3 | |||
4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. | ||
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
6 | Everyone is permitted to copy and distribute verbatim copies | ||
7 | of this license document, but changing it is not allowed. | ||
8 | |||
9 | [This is the first released version of the Lesser GPL. It also counts | ||
10 | as the successor of the GNU Library Public License, version 2, hence | ||
11 | the version number 2.1.] | ||
12 | |||
13 | Preamble | ||
14 | |||
15 | The licenses for most software are designed to take away your | ||
16 | freedom to share and change it. By contrast, the GNU General Public | ||
17 | Licenses are intended to guarantee your freedom to share and change | ||
18 | free software--to make sure the software is free for all its users. | ||
19 | |||
20 | This license, the Lesser General Public License, applies to some | ||
21 | specially designated software packages--typically libraries--of the | ||
22 | Free Software Foundation and other authors who decide to use it. You | ||
23 | can use it too, but we suggest you first think carefully about whether | ||
24 | this license or the ordinary General Public License is the better | ||
25 | strategy to use in any particular case, based on the explanations below. | ||
26 | |||
27 | When we speak of free software, we are referring to freedom of use, | ||
28 | not price. Our General Public Licenses are designed to make sure that | ||
29 | you have the freedom to distribute copies of free software (and charge | ||
30 | for this service if you wish); that you receive source code or can get | ||
31 | it if you want it; that you can change the software and use pieces of | ||
32 | it in new free programs; and that you are informed that you can do | ||
33 | these things. | ||
34 | |||
35 | To protect your rights, we need to make restrictions that forbid | ||
36 | distributors to deny you these rights or to ask you to surrender these | ||
37 | rights. These restrictions translate to certain responsibilities for | ||
38 | you if you distribute copies of the library or if you modify it. | ||
39 | |||
40 | For example, if you distribute copies of the library, whether gratis | ||
41 | or for a fee, you must give the recipients all the rights that we gave | ||
42 | you. You must make sure that they, too, receive or can get the source | ||
43 | code. If you link other code with the library, you must provide | ||
44 | complete object files to the recipients, so that they can relink them | ||
45 | with the library after making changes to the library and recompiling | ||
46 | it. And you must show them these terms so they know their rights. | ||
47 | |||
48 | We protect your rights with a two-step method: (1) we copyright the | ||
49 | library, and (2) we offer you this license, which gives you legal | ||
50 | permission to copy, distribute and/or modify the library. | ||
51 | |||
52 | To protect each distributor, we want to make it very clear that | ||
53 | there is no warranty for the free library. Also, if the library is | ||
54 | modified by someone else and passed on, the recipients should know | ||
55 | that what they have is not the original version, so that the original | ||
56 | author's reputation will not be affected by problems that might be | ||
57 | introduced by others. | ||
58 | |||
59 | Finally, software patents pose a constant threat to the existence of | ||
60 | any free program. We wish to make sure that a company cannot | ||
61 | effectively restrict the users of a free program by obtaining a | ||
62 | restrictive license from a patent holder. Therefore, we insist that | ||
63 | any patent license obtained for a version of the library must be | ||
64 | consistent with the full freedom of use specified in this license. | ||
65 | |||
66 | Most GNU software, including some libraries, is covered by the | ||
67 | ordinary GNU General Public License. This license, the GNU Lesser | ||
68 | General Public License, applies to certain designated libraries, and | ||
69 | is quite different from the ordinary General Public License. We use | ||
70 | this license for certain libraries in order to permit linking those | ||
71 | libraries into non-free programs. | ||
72 | |||
73 | When a program is linked with a library, whether statically or using | ||
74 | a shared library, the combination of the two is legally speaking a | ||
75 | combined work, a derivative of the original library. The ordinary | ||
76 | General Public License therefore permits such linking only if the | ||
77 | entire combination fits its criteria of freedom. The Lesser General | ||
78 | Public License permits more lax criteria for linking other code with | ||
79 | the library. | ||
80 | |||
81 | We call this license the "Lesser" General Public License because it | ||
82 | does Less to protect the user's freedom than the ordinary General | ||
83 | Public License. It also provides other free software developers Less | ||
84 | of an advantage over competing non-free programs. These disadvantages | ||
85 | are the reason we use the ordinary General Public License for many | ||
86 | libraries. However, the Lesser license provides advantages in certain | ||
87 | special circumstances. | ||
88 | |||
89 | For example, on rare occasions, there may be a special need to | ||
90 | encourage the widest possible use of a certain library, so that it becomes | ||
91 | a de-facto standard. To achieve this, non-free programs must be | ||
92 | allowed to use the library. A more frequent case is that a free | ||
93 | library does the same job as widely used non-free libraries. In this | ||
94 | case, there is little to gain by limiting the free library to free | ||
95 | software only, so we use the Lesser General Public License. | ||
96 | |||
97 | In other cases, permission to use a particular library in non-free | ||
98 | programs enables a greater number of people to use a large body of | ||
99 | free software. For example, permission to use the GNU C Library in | ||
100 | non-free programs enables many more people to use the whole GNU | ||
101 | operating system, as well as its variant, the GNU/Linux operating | ||
102 | system. | ||
103 | |||
104 | Although the Lesser General Public License is Less protective of the | ||
105 | users' freedom, it does ensure that the user of a program that is | ||
106 | linked with the Library has the freedom and the wherewithal to run | ||
107 | that program using a modified version of the Library. | ||
108 | |||
109 | The precise terms and conditions for copying, distribution and | ||
110 | modification follow. Pay close attention to the difference between a | ||
111 | "work based on the library" and a "work that uses the library". The | ||
112 | former contains code derived from the library, whereas the latter must | ||
113 | be combined with the library in order to run. | ||
114 | |||
115 | GNU LESSER GENERAL PUBLIC LICENSE | ||
116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | ||
117 | |||
118 | 0. This License Agreement applies to any software library or other | ||
119 | program which contains a notice placed by the copyright holder or | ||
120 | other authorized party saying it may be distributed under the terms of | ||
121 | this Lesser General Public License (also called "this License"). | ||
122 | Each licensee is addressed as "you". | ||
123 | |||
124 | A "library" means a collection of software functions and/or data | ||
125 | prepared so as to be conveniently linked with application programs | ||
126 | (which use some of those functions and data) to form executables. | ||
127 | |||
128 | The "Library", below, refers to any such software library or work | ||
129 | which has been distributed under these terms. A "work based on the | ||
130 | Library" means either the Library or any derivative work under | ||
131 | copyright law: that is to say, a work containing the Library or a | ||
132 | portion of it, either verbatim or with modifications and/or translated | ||
133 | straightforwardly into another language. (Hereinafter, translation is | ||
134 | included without limitation in the term "modification".) | ||
135 | |||
136 | "Source code" for a work means the preferred form of the work for | ||
137 | making modifications to it. For a library, complete source code means | ||
138 | all the source code for all modules it contains, plus any associated | ||
139 | interface definition files, plus the scripts used to control compilation | ||
140 | and installation of the library. | ||
141 | |||
142 | Activities other than copying, distribution and modification are not | ||
143 | covered by this License; they are outside its scope. The act of | ||
144 | running a program using the Library is not restricted, and output from | ||
145 | such a program is covered only if its contents constitute a work based | ||
146 | on the Library (independent of the use of the Library in a tool for | ||
147 | writing it). Whether that is true depends on what the Library does | ||
148 | and what the program that uses the Library does. | ||
149 | |||
150 | 1. You may copy and distribute verbatim copies of the Library's | ||
151 | complete source code as you receive it, in any medium, provided that | ||
152 | you conspicuously and appropriately publish on each copy an | ||
153 | appropriate copyright notice and disclaimer of warranty; keep intact | ||
154 | all the notices that refer to this License and to the absence of any | ||
155 | warranty; and distribute a copy of this License along with the | ||
156 | Library. | ||
157 | |||
158 | You may charge a fee for the physical act of transferring a copy, | ||
159 | and you may at your option offer warranty protection in exchange for a | ||
160 | fee. | ||
161 | |||
162 | 2. You may modify your copy or copies of the Library or any portion | ||
163 | of it, thus forming a work based on the Library, and copy and | ||
164 | distribute such modifications or work under the terms of Section 1 | ||
165 | above, provided that you also meet all of these conditions: | ||
166 | |||
167 | a) The modified work must itself be a software library. | ||
168 | |||
169 | b) You must cause the files modified to carry prominent notices | ||
170 | stating that you changed the files and the date of any change. | ||
171 | |||
172 | c) You must cause the whole of the work to be licensed at no | ||
173 | charge to all third parties under the terms of this License. | ||
174 | |||
175 | d) If a facility in the modified Library refers to a function or a | ||
176 | table of data to be supplied by an application program that uses | ||
177 | the facility, other than as an argument passed when the facility | ||
178 | is invoked, then you must make a good faith effort to ensure that, | ||
179 | in the event an application does not supply such function or | ||
180 | table, the facility still operates, and performs whatever part of | ||
181 | its purpose remains meaningful. | ||
182 | |||
183 | (For example, a function in a library to compute square roots has | ||
184 | a purpose that is entirely well-defined independent of the | ||
185 | application. Therefore, Subsection 2d requires that any | ||
186 | application-supplied function or table used by this function must | ||
187 | be optional: if the application does not supply it, the square | ||
188 | root function must still compute square roots.) | ||
189 | |||
190 | These requirements apply to the modified work as a whole. If | ||
191 | identifiable sections of that work are not derived from the Library, | ||
192 | and can be reasonably considered independent and separate works in | ||
193 | themselves, then this License, and its terms, do not apply to those | ||
194 | sections when you distribute them as separate works. But when you | ||
195 | distribute the same sections as part of a whole which is a work based | ||
196 | on the Library, the distribution of the whole must be on the terms of | ||
197 | this License, whose permissions for other licensees extend to the | ||
198 | entire whole, and thus to each and every part regardless of who wrote | ||
199 | it. | ||
200 | |||
201 | Thus, it is not the intent of this section to claim rights or contest | ||
202 | your rights to work written entirely by you; rather, the intent is to | ||
203 | exercise the right to control the distribution of derivative or | ||
204 | collective works based on the Library. | ||
205 | |||
206 | In addition, mere aggregation of another work not based on the Library | ||
207 | with the Library (or with a work based on the Library) on a volume of | ||
208 | a storage or distribution medium does not bring the other work under | ||
209 | the scope of this License. | ||
210 | |||
211 | 3. You may opt to apply the terms of the ordinary GNU General Public | ||
212 | License instead of this License to a given copy of the Library. To do | ||
213 | this, you must alter all the notices that refer to this License, so | ||
214 | that they refer to the ordinary GNU General Public License, version 2, | ||
215 | instead of to this License. (If a newer version than version 2 of the | ||
216 | ordinary GNU General Public License has appeared, then you can specify | ||
217 | that version instead if you wish.) Do not make any other change in | ||
218 | these notices. | ||
219 | |||
220 | Once this change is made in a given copy, it is irreversible for | ||
221 | that copy, so the ordinary GNU General Public License applies to all | ||
222 | subsequent copies and derivative works made from that copy. | ||
223 | |||
224 | This option is useful when you wish to copy part of the code of | ||
225 | the Library into a program that is not a library. | ||
226 | |||
227 | 4. You may copy and distribute the Library (or a portion or | ||
228 | derivative of it, under Section 2) in object code or executable form | ||
229 | under the terms of Sections 1 and 2 above provided that you accompany | ||
230 | it with the complete corresponding machine-readable source code, which | ||
231 | must be distributed under the terms of Sections 1 and 2 above on a | ||
232 | medium customarily used for software interchange. | ||
233 | |||
234 | If distribution of object code is made by offering access to copy | ||
235 | from a designated place, then offering equivalent access to copy the | ||
236 | source code from the same place satisfies the requirement to | ||
237 | distribute the source code, even though third parties are not | ||
238 | compelled to copy the source along with the object code. | ||
239 | |||
240 | 5. A program that contains no derivative of any portion of the | ||
241 | Library, but is designed to work with the Library by being compiled or | ||
242 | linked with it, is called a "work that uses the Library". Such a | ||
243 | work, in isolation, is not a derivative work of the Library, and | ||
244 | therefore falls outside the scope of this License. | ||
245 | |||
246 | However, linking a "work that uses the Library" with the Library | ||
247 | creates an executable that is a derivative of the Library (because it | ||
248 | contains portions of the Library), rather than a "work that uses the | ||
249 | library". The executable is therefore covered by this License. | ||
250 | Section 6 states terms for distribution of such executables. | ||
251 | |||
252 | When a "work that uses the Library" uses material from a header file | ||
253 | that is part of the Library, the object code for the work may be a | ||
254 | derivative work of the Library even though the source code is not. | ||
255 | Whether this is true is especially significant if the work can be | ||
256 | linked without the Library, or if the work is itself a library. The | ||
257 | threshold for this to be true is not precisely defined by law. | ||
258 | |||
259 | If such an object file uses only numerical parameters, data | ||
260 | structure layouts and accessors, and small macros and small inline | ||
261 | functions (ten lines or less in length), then the use of the object | ||
262 | file is unrestricted, regardless of whether it is legally a derivative | ||
263 | work. (Executables containing this object code plus portions of the | ||
264 | Library will still fall under Section 6.) | ||
265 | |||
266 | Otherwise, if the work is a derivative of the Library, you may | ||
267 | distribute the object code for the work under the terms of Section 6. | ||
268 | Any executables containing that work also fall under Section 6, | ||
269 | whether or not they are linked directly with the Library itself. | ||
270 | |||
271 | 6. As an exception to the Sections above, you may also combine or | ||
272 | link a "work that uses the Library" with the Library to produce a | ||
273 | work containing portions of the Library, and distribute that work | ||
274 | under terms of your choice, provided that the terms permit | ||
275 | modification of the work for the customer's own use and reverse | ||
276 | engineering for debugging such modifications. | ||
277 | |||
278 | You must give prominent notice with each copy of the work that the | ||
279 | Library is used in it and that the Library and its use are covered by | ||
280 | this License. You must supply a copy of this License. If the work | ||
281 | during execution displays copyright notices, you must include the | ||
282 | copyright notice for the Library among them, as well as a reference | ||
283 | directing the user to the copy of this License. Also, you must do one | ||
284 | of these things: | ||
285 | |||
286 | a) Accompany the work with the complete corresponding | ||
287 | machine-readable source code for the Library including whatever | ||
288 | changes were used in the work (which must be distributed under | ||
289 | Sections 1 and 2 above); and, if the work is an executable linked | ||
290 | with the Library, with the complete machine-readable "work that | ||
291 | uses the Library", as object code and/or source code, so that the | ||
292 | user can modify the Library and then relink to produce a modified | ||
293 | executable containing the modified Library. (It is understood | ||
294 | that the user who changes the contents of definitions files in the | ||
295 | Library will not necessarily be able to recompile the application | ||
296 | to use the modified definitions.) | ||
297 | |||
298 | b) Use a suitable shared library mechanism for linking with the | ||
299 | Library. A suitable mechanism is one that (1) uses at run time a | ||
300 | copy of the library already present on the user's computer system, | ||
301 | rather than copying library functions into the executable, and (2) | ||
302 | will operate properly with a modified version of the library, if | ||
303 | the user installs one, as long as the modified version is | ||
304 | interface-compatible with the version that the work was made with. | ||
305 | |||
306 | c) Accompany the work with a written offer, valid for at | ||
307 | least three years, to give the same user the materials | ||
308 | specified in Subsection 6a, above, for a charge no more | ||
309 | than the cost of performing this distribution. | ||
310 | |||
311 | d) If distribution of the work is made by offering access to copy | ||
312 | from a designated place, offer equivalent access to copy the above | ||
313 | specified materials from the same place. | ||
314 | |||
315 | e) Verify that the user has already received a copy of these | ||
316 | materials or that you have already sent this user a copy. | ||
317 | |||
318 | For an executable, the required form of the "work that uses the | ||
319 | Library" must include any data and utility programs needed for | ||
320 | reproducing the executable from it. However, as a special exception, | ||
321 | the materials to be distributed need not include anything that is | ||
322 | normally distributed (in either source or binary form) with the major | ||
323 | components (compiler, kernel, and so on) of the operating system on | ||
324 | which the executable runs, unless that component itself accompanies | ||
325 | the executable. | ||
326 | |||
327 | It may happen that this requirement contradicts the license | ||
328 | restrictions of other proprietary libraries that do not normally | ||
329 | accompany the operating system. Such a contradiction means you cannot | ||
330 | use both them and the Library together in an executable that you | ||
331 | distribute. | ||
332 | |||
333 | 7. You may place library facilities that are a work based on the | ||
334 | Library side-by-side in a single library together with other library | ||
335 | facilities not covered by this License, and distribute such a combined | ||
336 | library, provided that the separate distribution of the work based on | ||
337 | the Library and of the other library facilities is otherwise | ||
338 | permitted, and provided that you do these two things: | ||
339 | |||
340 | a) Accompany the combined library with a copy of the same work | ||
341 | based on the Library, uncombined with any other library | ||
342 | facilities. This must be distributed under the terms of the | ||
343 | Sections above. | ||
344 | |||
345 | b) Give prominent notice with the combined library of the fact | ||
346 | that part of it is a work based on the Library, and explaining | ||
347 | where to find the accompanying uncombined form of the same work. | ||
348 | |||
349 | 8. You may not copy, modify, sublicense, link with, or distribute | ||
350 | the Library except as expressly provided under this License. Any | ||
351 | attempt otherwise to copy, modify, sublicense, link with, or | ||
352 | distribute the Library is void, and will automatically terminate your | ||
353 | rights under this License. However, parties who have received copies, | ||
354 | or rights, from you under this License will not have their licenses | ||
355 | terminated so long as such parties remain in full compliance. | ||
356 | |||
357 | 9. You are not required to accept this License, since you have not | ||
358 | signed it. However, nothing else grants you permission to modify or | ||
359 | distribute the Library or its derivative works. These actions are | ||
360 | prohibited by law if you do not accept this License. Therefore, by | ||
361 | modifying or distributing the Library (or any work based on the | ||
362 | Library), you indicate your acceptance of this License to do so, and | ||
363 | all its terms and conditions for copying, distributing or modifying | ||
364 | the Library or works based on it. | ||
365 | |||
366 | 10. Each time you redistribute the Library (or any work based on the | ||
367 | Library), the recipient automatically receives a license from the | ||
368 | original licensor to copy, distribute, link with or modify the Library | ||
369 | subject to these terms and conditions. You may not impose any further | ||
370 | restrictions on the recipients' exercise of the rights granted herein. | ||
371 | You are not responsible for enforcing compliance by third parties with | ||
372 | this License. | ||
373 | |||
374 | 11. If, as a consequence of a court judgment or allegation of patent | ||
375 | infringement or for any other reason (not limited to patent issues), | ||
376 | conditions are imposed on you (whether by court order, agreement or | ||
377 | otherwise) that contradict the conditions of this License, they do not | ||
378 | excuse you from the conditions of this License. If you cannot | ||
379 | distribute so as to satisfy simultaneously your obligations under this | ||
380 | License and any other pertinent obligations, then as a consequence you | ||
381 | may not distribute the Library at all. For example, if a patent | ||
382 | license would not permit royalty-free redistribution of the Library by | ||
383 | all those who receive copies directly or indirectly through you, then | ||
384 | the only way you could satisfy both it and this License would be to | ||
385 | refrain entirely from distribution of the Library. | ||
386 | |||
387 | If any portion of this section is held invalid or unenforceable under any | ||
388 | particular circumstance, the balance of the section is intended to apply, | ||
389 | and the section as a whole is intended to apply in other circumstances. | ||
390 | |||
391 | It is not the purpose of this section to induce you to infringe any | ||
392 | patents or other property right claims or to contest validity of any | ||
393 | such claims; this section has the sole purpose of protecting the | ||
394 | integrity of the free software distribution system which is | ||
395 | implemented by public license practices. Many people have made | ||
396 | generous contributions to the wide range of software distributed | ||
397 | through that system in reliance on consistent application of that | ||
398 | system; it is up to the author/donor to decide if he or she is willing | ||
399 | to distribute software through any other system and a licensee cannot | ||
400 | impose that choice. | ||
401 | |||
402 | This section is intended to make thoroughly clear what is believed to | ||
403 | be a consequence of the rest of this License. | ||
404 | |||
405 | 12. If the distribution and/or use of the Library is restricted in | ||
406 | certain countries either by patents or by copyrighted interfaces, the | ||
407 | original copyright holder who places the Library under this License may add | ||
408 | an explicit geographical distribution limitation excluding those countries, | ||
409 | so that distribution is permitted only in or among countries not thus | ||
410 | excluded. In such case, this License incorporates the limitation as if | ||
411 | written in the body of this License. | ||
412 | |||
413 | 13. The Free Software Foundation may publish revised and/or new | ||
414 | versions of the Lesser General Public License from time to time. | ||
415 | Such new versions will be similar in spirit to the present version, | ||
416 | but may differ in detail to address new problems or concerns. | ||
417 | |||
418 | Each version is given a distinguishing version number. If the Library | ||
419 | specifies a version number of this License which applies to it and | ||
420 | "any later version", you have the option of following the terms and | ||
421 | conditions either of that version or of any later version published by | ||
422 | the Free Software Foundation. If the Library does not specify a | ||
423 | license version number, you may choose any version ever published by | ||
424 | the Free Software Foundation. | ||
425 | |||
426 | 14. If you wish to incorporate parts of the Library into other free | ||
427 | programs whose distribution conditions are incompatible with these, | ||
428 | write to the author to ask for permission. For software which is | ||
429 | copyrighted by the Free Software Foundation, write to the Free | ||
430 | Software Foundation; we sometimes make exceptions for this. Our | ||
431 | decision will be guided by the two goals of preserving the free status | ||
432 | of all derivatives of our free software and of promoting the sharing | ||
433 | and reuse of software generally. | ||
434 | |||
435 | NO WARRANTY | ||
436 | |||
437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO | ||
438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. | ||
439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR | ||
440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY | ||
441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE | ||
442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE | ||
444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME | ||
445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | ||
446 | |||
447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN | ||
448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY | ||
449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU | ||
450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR | ||
451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE | ||
452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING | ||
453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A | ||
454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF | ||
455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH | ||
456 | DAMAGES. | ||
457 | |||
458 | END OF TERMS AND CONDITIONS | ||
459 | |||
460 | How to Apply These Terms to Your New Libraries | ||
461 | |||
462 | If you develop a new library, and you want it to be of the greatest | ||
463 | possible use to the public, we recommend making it free software that | ||
464 | everyone can redistribute and change. You can do so by permitting | ||
465 | redistribution under these terms (or, alternatively, under the terms of the | ||
466 | ordinary General Public License). | ||
467 | |||
468 | To apply these terms, attach the following notices to the library. It is | ||
469 | safest to attach them to the start of each source file to most effectively | ||
470 | convey the exclusion of warranty; and each file should have at least the | ||
471 | "copyright" line and a pointer to where the full notice is found. | ||
472 | |||
473 | <one line to give the library's name and a brief idea of what it does.> | ||
474 | Copyright (C) <year> <name of author> | ||
475 | |||
476 | This library is free software; you can redistribute it and/or | ||
477 | modify it under the terms of the GNU Lesser General Public | ||
478 | License as published by the Free Software Foundation; either | ||
479 | version 2.1 of the License, or (at your option) any later version. | ||
480 | |||
481 | This library is distributed in the hope that it will be useful, | ||
482 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
484 | Lesser General Public License for more details. | ||
485 | |||
486 | You should have received a copy of the GNU Lesser General Public | ||
487 | License along with this library; if not, write to the Free Software | ||
488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
489 | |||
490 | Also add information on how to contact you by electronic and paper mail. | ||
491 | |||
492 | You should also get your employer (if you work as a programmer) or your | ||
493 | school, if any, to sign a "copyright disclaimer" for the library, if | ||
494 | necessary. Here is a sample; alter the names: | ||
495 | |||
496 | Yoyodyne, Inc., hereby disclaims all copyright interest in the | ||
497 | library `Frob' (a library for tweaking knobs) written by James Random Hacker. | ||
498 | |||
499 | <signature of Ty Coon>, 1 April 1990 | ||
500 | Ty Coon, President of Vice | ||
501 | |||
502 | That's all there is to it! | ||
503 | |||
504 | |||
diff --git a/utils/rbutilqt/logger/README.ROCKBOX b/utils/rbutilqt/logger/README.ROCKBOX new file mode 100644 index 0000000000..f501a410dd --- /dev/null +++ b/utils/rbutilqt/logger/README.ROCKBOX | |||
@@ -0,0 +1,7 @@ | |||
1 | This folder contains the cutelogger project for logging functionality. | ||
2 | These files are distributed under the LGPL v2 or later. | ||
3 | The source files have been last synced with the projects at | ||
4 | https://github.com/dept2/CuteLogger/ to commit | ||
5 | 5ae6b9ac13e0cc2821d236e3542a83990b63c95c | ||
6 | on Aug 7, 2020. | ||
7 | |||
diff --git a/utils/rbutilqt/logger/include/AbstractAppender.h b/utils/rbutilqt/logger/include/AbstractAppender.h new file mode 100644 index 0000000000..e029b045aa --- /dev/null +++ b/utils/rbutilqt/logger/include/AbstractAppender.h | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | #ifndef ABSTRACTAPPENDER_H | ||
15 | #define ABSTRACTAPPENDER_H | ||
16 | |||
17 | // Local | ||
18 | #include "CuteLogger_global.h" | ||
19 | #include <Logger.h> | ||
20 | |||
21 | // Qt | ||
22 | #include <QMutex> | ||
23 | |||
24 | |||
25 | class CUTELOGGERSHARED_EXPORT AbstractAppender | ||
26 | { | ||
27 | public: | ||
28 | AbstractAppender(); | ||
29 | virtual ~AbstractAppender(); | ||
30 | |||
31 | Logger::LogLevel detailsLevel() const; | ||
32 | void setDetailsLevel(Logger::LogLevel level); | ||
33 | void setDetailsLevel(const QString& level); | ||
34 | |||
35 | void write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, const char* function, | ||
36 | const QString& category, const QString& message); | ||
37 | |||
38 | protected: | ||
39 | virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
40 | const char* function, const QString& category, const QString& message) = 0; | ||
41 | |||
42 | private: | ||
43 | QMutex m_writeMutex; | ||
44 | |||
45 | Logger::LogLevel m_detailsLevel; | ||
46 | mutable QMutex m_detailsLevelMutex; | ||
47 | }; | ||
48 | |||
49 | #endif // ABSTRACTAPPENDER_H | ||
diff --git a/utils/rbutilqt/logger/include/AbstractStringAppender.h b/utils/rbutilqt/logger/include/AbstractStringAppender.h new file mode 100644 index 0000000000..78df9e6176 --- /dev/null +++ b/utils/rbutilqt/logger/include/AbstractStringAppender.h | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | #ifndef ABSTRACTSTRINGAPPENDER_H | ||
15 | #define ABSTRACTSTRINGAPPENDER_H | ||
16 | |||
17 | // Local | ||
18 | #include "CuteLogger_global.h" | ||
19 | #include <AbstractAppender.h> | ||
20 | |||
21 | // Qt | ||
22 | #include <QReadWriteLock> | ||
23 | |||
24 | |||
25 | class CUTELOGGERSHARED_EXPORT AbstractStringAppender : public AbstractAppender | ||
26 | { | ||
27 | public: | ||
28 | AbstractStringAppender(); | ||
29 | |||
30 | virtual QString format() const; | ||
31 | void setFormat(const QString&); | ||
32 | |||
33 | static QString stripFunctionName(const char*); | ||
34 | |||
35 | protected: | ||
36 | QString formattedString(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
37 | const char* function, const QString& category, const QString& message) const; | ||
38 | |||
39 | private: | ||
40 | static QByteArray qCleanupFuncinfo(const char*); | ||
41 | |||
42 | QString m_format; | ||
43 | mutable QReadWriteLock m_formatLock; | ||
44 | }; | ||
45 | |||
46 | #endif // ABSTRACTSTRINGAPPENDER_H | ||
diff --git a/utils/rbutilqt/logger/include/ConsoleAppender.h b/utils/rbutilqt/logger/include/ConsoleAppender.h new file mode 100644 index 0000000000..64ef2e7a19 --- /dev/null +++ b/utils/rbutilqt/logger/include/ConsoleAppender.h | |||
@@ -0,0 +1,36 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | #ifndef CONSOLEAPPENDER_H | ||
15 | #define CONSOLEAPPENDER_H | ||
16 | |||
17 | #include "CuteLogger_global.h" | ||
18 | #include <AbstractStringAppender.h> | ||
19 | |||
20 | |||
21 | class CUTELOGGERSHARED_EXPORT ConsoleAppender : public AbstractStringAppender | ||
22 | { | ||
23 | public: | ||
24 | ConsoleAppender(); | ||
25 | virtual QString format() const; | ||
26 | void ignoreEnvironmentPattern(bool ignore); | ||
27 | |||
28 | protected: | ||
29 | virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
30 | const char* function, const QString& category, const QString& message); | ||
31 | |||
32 | private: | ||
33 | bool m_ignoreEnvPattern; | ||
34 | }; | ||
35 | |||
36 | #endif // CONSOLEAPPENDER_H | ||
diff --git a/utils/rbutilqt/logger/include/CuteLogger_global.h b/utils/rbutilqt/logger/include/CuteLogger_global.h new file mode 100644 index 0000000000..c5e7680845 --- /dev/null +++ b/utils/rbutilqt/logger/include/CuteLogger_global.h | |||
@@ -0,0 +1,16 @@ | |||
1 | #ifndef CUTELOGGER_GLOBAL_H | ||
2 | #define CUTELOGGER_GLOBAL_H | ||
3 | |||
4 | #include <QtCore/qglobal.h> | ||
5 | |||
6 | #if !defined(CUTELOGGER_STATIC) | ||
7 | #if defined(CUTELOGGER_LIBRARY) | ||
8 | # define CUTELOGGERSHARED_EXPORT Q_DECL_EXPORT | ||
9 | #else | ||
10 | # define CUTELOGGERSHARED_EXPORT Q_DECL_IMPORT | ||
11 | #endif | ||
12 | #else | ||
13 | #define CUTELOGGERSHARED_EXPORT | ||
14 | #endif | ||
15 | |||
16 | #endif // CUTELOGGER_GLOBAL_H | ||
diff --git a/utils/rbutilqt/logger/include/FileAppender.h b/utils/rbutilqt/logger/include/FileAppender.h new file mode 100644 index 0000000000..ab9e12a91d --- /dev/null +++ b/utils/rbutilqt/logger/include/FileAppender.h | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | #ifndef FILEAPPENDER_H | ||
15 | #define FILEAPPENDER_H | ||
16 | |||
17 | // Logger | ||
18 | #include "CuteLogger_global.h" | ||
19 | #include <AbstractStringAppender.h> | ||
20 | |||
21 | // Qt | ||
22 | #include <QFile> | ||
23 | #include <QTextStream> | ||
24 | |||
25 | |||
26 | class CUTELOGGERSHARED_EXPORT FileAppender : public AbstractStringAppender | ||
27 | { | ||
28 | public: | ||
29 | FileAppender(const QString& fileName = QString()); | ||
30 | ~FileAppender(); | ||
31 | |||
32 | QString fileName() const; | ||
33 | void setFileName(const QString&); | ||
34 | |||
35 | bool reopenFile(); | ||
36 | |||
37 | protected: | ||
38 | virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
39 | const char* function, const QString& category, const QString& message); | ||
40 | bool openFile(); | ||
41 | void closeFile(); | ||
42 | |||
43 | private: | ||
44 | QFile m_logFile; | ||
45 | QTextStream m_logStream; | ||
46 | mutable QMutex m_logFileMutex; | ||
47 | }; | ||
48 | |||
49 | #endif // FILEAPPENDER_H | ||
diff --git a/utils/rbutilqt/logger/include/Logger.h b/utils/rbutilqt/logger/include/Logger.h new file mode 100644 index 0000000000..941e556eb9 --- /dev/null +++ b/utils/rbutilqt/logger/include/Logger.h | |||
@@ -0,0 +1,238 @@ | |||
1 | /* | ||
2 | Copyright (c) 2012 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | #ifndef LOGGER_H | ||
15 | #define LOGGER_H | ||
16 | |||
17 | // Qt | ||
18 | #include <QString> | ||
19 | #include <QDebug> | ||
20 | #include <QDateTime> | ||
21 | #include <QElapsedTimer> | ||
22 | |||
23 | // Local | ||
24 | #include "CuteLogger_global.h" | ||
25 | class AbstractAppender; | ||
26 | |||
27 | |||
28 | class Logger; | ||
29 | CUTELOGGERSHARED_EXPORT Logger* cuteLoggerInstance(); | ||
30 | #define cuteLogger cuteLoggerInstance() | ||
31 | |||
32 | |||
33 | #define LOG_TRACE CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
34 | #define LOG_DEBUG CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
35 | #define LOG_INFO CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
36 | #define LOG_WARNING CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
37 | #define LOG_ERROR CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
38 | #define LOG_FATAL CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO).write | ||
39 | |||
40 | #define LOG_CTRACE(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
41 | #define LOG_CDEBUG(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
42 | #define LOG_CINFO(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
43 | #define LOG_CWARNING(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Warning, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
44 | #define LOG_CERROR(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Error, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
45 | #define LOG_CFATAL(category) CuteMessageLogger(cuteLoggerInstance(), Logger::Fatal, __FILE__, __LINE__, Q_FUNC_INFO, category).write() | ||
46 | |||
47 | #define LOG_TRACE_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Trace, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start | ||
48 | #define LOG_DEBUG_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Debug, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start | ||
49 | #define LOG_INFO_TIME LoggerTimingHelper loggerTimingHelper(cuteLoggerInstance(), Logger::Info, __FILE__, __LINE__, Q_FUNC_INFO); loggerTimingHelper.start | ||
50 | |||
51 | #define LOG_ASSERT(cond) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, #cond) : qt_noop()) | ||
52 | #define LOG_ASSERT_X(cond, msg) ((!(cond)) ? cuteLoggerInstance()->writeAssert(__FILE__, __LINE__, Q_FUNC_INFO, msg) : qt_noop()) | ||
53 | |||
54 | #if (__cplusplus >= 201103L) | ||
55 | #include <functional> | ||
56 | |||
57 | #define LOG_CATEGORY(category) \ | ||
58 | Logger customCuteLoggerInstance{category};\ | ||
59 | std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\ | ||
60 | return &customCuteLoggerInstance;\ | ||
61 | };\ | ||
62 | |||
63 | #define LOG_GLOBAL_CATEGORY(category) \ | ||
64 | Logger customCuteLoggerInstance{category, true};\ | ||
65 | std::function<Logger*()> cuteLoggerInstance = [&customCuteLoggerInstance]() {\ | ||
66 | return &customCuteLoggerInstance;\ | ||
67 | };\ | ||
68 | |||
69 | #else | ||
70 | |||
71 | #define LOG_CATEGORY(category) \ | ||
72 | Logger* cuteLoggerInstance()\ | ||
73 | {\ | ||
74 | static Logger customCuteLoggerInstance(category);\ | ||
75 | return &customCuteLoggerInstance;\ | ||
76 | }\ | ||
77 | |||
78 | #define LOG_GLOBAL_CATEGORY(category) \ | ||
79 | Logger* cuteLoggerInstance()\ | ||
80 | {\ | ||
81 | static Logger customCuteLoggerInstance(category);\ | ||
82 | customCuteLoggerInstance.logToGlobalInstance(category, true);\ | ||
83 | return &customCuteLoggerInstance;\ | ||
84 | }\ | ||
85 | |||
86 | #endif | ||
87 | |||
88 | |||
89 | class LoggerPrivate; | ||
90 | class CUTELOGGERSHARED_EXPORT Logger | ||
91 | { | ||
92 | Q_DISABLE_COPY(Logger) | ||
93 | |||
94 | public: | ||
95 | Logger(); | ||
96 | Logger(const QString& defaultCategory, bool writeToGlobalInstance = false); | ||
97 | ~Logger(); | ||
98 | |||
99 | //! Describes the possible severity levels of the log records | ||
100 | enum LogLevel | ||
101 | { | ||
102 | Trace, //!< Trace level. Can be used for mostly unneeded records used for internal code tracing. | ||
103 | Debug, //!< Debug level. Useful for non-necessary records used for the debugging of the software. | ||
104 | Info, //!< Info level. Can be used for informational records, which may be interesting for not only developers. | ||
105 | Warning, //!< Warning. May be used to log some non-fatal warnings detected by your application. | ||
106 | Error, //!< Error. May be used for a big problems making your application work wrong but not crashing. | ||
107 | Fatal //!< Fatal. Used for unrecoverable errors, crashes the application right after the log record is written. | ||
108 | }; | ||
109 | |||
110 | //! Sets the timing display mode for the LOG_TRACE_TIME, LOG_DEBUG_TIME and LOG_INFO_TIME macros | ||
111 | enum TimingMode | ||
112 | { | ||
113 | TimingAuto, //!< Show time in seconds, if it exceeds 10s (default) | ||
114 | TimingMs //!< Always use milliseconds to display | ||
115 | }; | ||
116 | |||
117 | static QString levelToString(LogLevel logLevel); | ||
118 | static LogLevel levelFromString(const QString& s); | ||
119 | |||
120 | static Logger* globalInstance(); | ||
121 | |||
122 | void registerAppender(AbstractAppender* appender); | ||
123 | void registerCategoryAppender(const QString& category, AbstractAppender* appender); | ||
124 | |||
125 | void removeAppender(AbstractAppender* appender); | ||
126 | |||
127 | void logToGlobalInstance(const QString& category, bool logToGlobal = false); | ||
128 | |||
129 | void setDefaultCategory(const QString& category); | ||
130 | QString defaultCategory() const; | ||
131 | |||
132 | void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category, | ||
133 | const QString& message); | ||
134 | void write(LogLevel logLevel, const char* file, int line, const char* function, const char* category, const QString& message); | ||
135 | |||
136 | void writeAssert(const char* file, int line, const char* function, const char* condition); | ||
137 | |||
138 | private: | ||
139 | void write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category, | ||
140 | const QString& message, bool fromLocalInstance); | ||
141 | Q_DECLARE_PRIVATE(Logger) | ||
142 | LoggerPrivate* d_ptr; | ||
143 | }; | ||
144 | |||
145 | |||
146 | class CUTELOGGERSHARED_EXPORT CuteMessageLogger | ||
147 | { | ||
148 | Q_DISABLE_COPY(CuteMessageLogger) | ||
149 | |||
150 | public: | ||
151 | CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function) | ||
152 | : m_l(l), | ||
153 | m_level(level), | ||
154 | m_file(file), | ||
155 | m_line(line), | ||
156 | m_function(function), | ||
157 | m_category(nullptr) | ||
158 | {} | ||
159 | |||
160 | CuteMessageLogger(Logger* l, Logger::LogLevel level, const char* file, int line, const char* function, const char* category) | ||
161 | : m_l(l), | ||
162 | m_level(level), | ||
163 | m_file(file), | ||
164 | m_line(line), | ||
165 | m_function(function), | ||
166 | m_category(category) | ||
167 | {} | ||
168 | |||
169 | ~CuteMessageLogger(); | ||
170 | |||
171 | void write(const char* msg, ...) | ||
172 | #if defined(Q_CC_GNU) && !defined(__INSURE__) | ||
173 | # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG) | ||
174 | __attribute__ ((format (gnu_printf, 2, 3))) | ||
175 | # else | ||
176 | __attribute__ ((format (printf, 2, 3))) | ||
177 | # endif | ||
178 | #endif | ||
179 | ; | ||
180 | |||
181 | void write(const QString& msg); | ||
182 | |||
183 | QDebug write(); | ||
184 | |||
185 | private: | ||
186 | Logger* m_l; | ||
187 | Logger::LogLevel m_level; | ||
188 | const char* m_file; | ||
189 | int m_line; | ||
190 | const char* m_function; | ||
191 | const char* m_category; | ||
192 | QString m_message; | ||
193 | }; | ||
194 | |||
195 | |||
196 | class CUTELOGGERSHARED_EXPORT LoggerTimingHelper | ||
197 | { | ||
198 | Q_DISABLE_COPY(LoggerTimingHelper) | ||
199 | |||
200 | public: | ||
201 | inline explicit LoggerTimingHelper(Logger* l, Logger::LogLevel logLevel, const char* file, int line, | ||
202 | const char* function) | ||
203 | : m_logger(l), | ||
204 | m_logLevel(logLevel), | ||
205 | m_timingMode(Logger::TimingAuto), | ||
206 | m_file(file), | ||
207 | m_line(line), | ||
208 | m_function(function) | ||
209 | {} | ||
210 | |||
211 | void start(const char* msg, ...) | ||
212 | #if defined(Q_CC_GNU) && !defined(__INSURE__) | ||
213 | # if defined(Q_CC_MINGW) && !defined(Q_CC_CLANG) | ||
214 | __attribute__ ((format (gnu_printf, 2, 3))) | ||
215 | # else | ||
216 | __attribute__ ((format (printf, 2, 3))) | ||
217 | # endif | ||
218 | #endif | ||
219 | ; | ||
220 | |||
221 | void start(const QString& msg = QString()); | ||
222 | void start(Logger::TimingMode mode, const QString& msg); | ||
223 | |||
224 | ~LoggerTimingHelper(); | ||
225 | |||
226 | private: | ||
227 | Logger* m_logger; | ||
228 | QElapsedTimer m_time; | ||
229 | Logger::LogLevel m_logLevel; | ||
230 | Logger::TimingMode m_timingMode; | ||
231 | const char* m_file; | ||
232 | int m_line; | ||
233 | const char* m_function; | ||
234 | QString m_block; | ||
235 | }; | ||
236 | |||
237 | |||
238 | #endif // LOGGER_H | ||
diff --git a/utils/rbutilqt/logger/include/OutputDebugAppender.h b/utils/rbutilqt/logger/include/OutputDebugAppender.h new file mode 100644 index 0000000000..dd7ad4deb7 --- /dev/null +++ b/utils/rbutilqt/logger/include/OutputDebugAppender.h | |||
@@ -0,0 +1,29 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Karl-Heinz Reichel (khreichel at googlemail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | |||
15 | #ifndef OUTPUTDEBUGAPPENDER_H | ||
16 | #define OUTPUTDEBUGAPPENDER_H | ||
17 | |||
18 | #include "CuteLogger_global.h" | ||
19 | #include <AbstractStringAppender.h> | ||
20 | |||
21 | |||
22 | class CUTELOGGERSHARED_EXPORT OutputDebugAppender : public AbstractStringAppender | ||
23 | { | ||
24 | protected: | ||
25 | virtual void append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
26 | const char* function, const QString& category, const QString& message); | ||
27 | }; | ||
28 | |||
29 | #endif // OUTPUTDEBUGAPPENDER_H | ||
diff --git a/utils/rbutilqt/logger/logger.pri b/utils/rbutilqt/logger/logger.pri new file mode 100644 index 0000000000..e34bccb3cf --- /dev/null +++ b/utils/rbutilqt/logger/logger.pri | |||
@@ -0,0 +1,22 @@ | |||
1 | |||
2 | SOURCES += \ | ||
3 | $$PWD/src/AbstractAppender.cpp \ | ||
4 | $$PWD/src/AbstractStringAppender.cpp \ | ||
5 | $$PWD/src/ConsoleAppender.cpp \ | ||
6 | $$PWD/src/FileAppender.cpp \ | ||
7 | $$PWD/src/Logger.cpp \ | ||
8 | |||
9 | HEADERS += \ | ||
10 | $$PWD/include/AbstractAppender.h \ | ||
11 | $$PWD/include/ConsoleAppender.h \ | ||
12 | $$PWD/include/FileAppender.h \ | ||
13 | $$PWD/include/OutputDebugAppender.h \ | ||
14 | $$PWD/include/AbstractStringAppender.h \ | ||
15 | $$PWD/include/CuteLogger_global.h \ | ||
16 | $$PWD/include/Logger.h \ | ||
17 | |||
18 | INCLUDEPATH += $$PWD/include | ||
19 | |||
20 | DEFINES += \ | ||
21 | CUTELOGGER_STATIC | ||
22 | |||
diff --git a/utils/rbutilqt/logger/src/AbstractAppender.cpp b/utils/rbutilqt/logger/src/AbstractAppender.cpp new file mode 100644 index 0000000000..778bbddd11 --- /dev/null +++ b/utils/rbutilqt/logger/src/AbstractAppender.cpp | |||
@@ -0,0 +1,147 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "AbstractAppender.h" | ||
16 | |||
17 | // Qt | ||
18 | #include <QMutexLocker> | ||
19 | |||
20 | |||
21 | /** | ||
22 | * \class AbstractAppender | ||
23 | * | ||
24 | * \brief The AbstractAppender class provides an abstract base class for writing a log entries. | ||
25 | * | ||
26 | * The AbstractAppender class is the base interface class for all log appenders that could be used with Logger. | ||
27 | * | ||
28 | * AbstractAppender provides a common implementation for the thread safe, mutex-protected logging of application | ||
29 | * messages, such as ConsoleAppender, FileAppender or something else. AbstractAppender is abstract and can not be | ||
30 | * instantiated, but you can use any of its subclasses or create a custom log appender at your choice. | ||
31 | * | ||
32 | * Appenders are the logical devices that is aimed to be attached to Logger object by calling | ||
33 | * Logger::registerAppender(). On each log record call from the application Logger object sequentially calls write() | ||
34 | * function on all the appenders registered in it. | ||
35 | * | ||
36 | * You can subclass AbstractAppender to implement a logging target of any kind you like. It may be the external logging | ||
37 | * subsystem (for example, syslog in *nix), XML file, SQL database entries, D-Bus messages or anything else you can | ||
38 | * imagine. | ||
39 | * | ||
40 | * For the simple non-structured plain text logging (for example, to a plain text file or to the console output) you may | ||
41 | * like to subclass the AbstractStringAppender instead of AbstractAppender, which will give you a more convinient way to | ||
42 | * control the format of the log output. | ||
43 | * | ||
44 | * \sa AbstractStringAppender | ||
45 | * \sa Logger::registerAppender() | ||
46 | */ | ||
47 | |||
48 | |||
49 | //! Constructs a AbstractAppender object. | ||
50 | AbstractAppender::AbstractAppender() | ||
51 | : m_detailsLevel(Logger::Debug) | ||
52 | {} | ||
53 | |||
54 | |||
55 | //! Destructs the AbstractAppender object. | ||
56 | AbstractAppender::~AbstractAppender() | ||
57 | {} | ||
58 | |||
59 | |||
60 | //! Returns the current details level of appender. | ||
61 | /** | ||
62 | * Log records with a log level lower than a current detailsLevel() will be silently ignored by appender and would not | ||
63 | * be sent to its append() function. | ||
64 | * | ||
65 | * It provides additional logging flexibility, allowing you to set the different severity levels for different types | ||
66 | * of logs. | ||
67 | * | ||
68 | * \note This function is thread safe. | ||
69 | * | ||
70 | * \sa setDetailsLevel() | ||
71 | * \sa Logger::LogLevel | ||
72 | */ | ||
73 | Logger::LogLevel AbstractAppender::detailsLevel() const | ||
74 | { | ||
75 | QMutexLocker locker(&m_detailsLevelMutex); | ||
76 | return m_detailsLevel; | ||
77 | } | ||
78 | |||
79 | |||
80 | //! Sets the current details level of appender. | ||
81 | /** | ||
82 | * Default details level is Logger::Debug | ||
83 | * | ||
84 | * \note This function is thread safe. | ||
85 | * | ||
86 | * \sa detailsLevel() | ||
87 | * \sa Logger::LogLevel | ||
88 | */ | ||
89 | void AbstractAppender::setDetailsLevel(Logger::LogLevel level) | ||
90 | { | ||
91 | QMutexLocker locker(&m_detailsLevelMutex); | ||
92 | m_detailsLevel = level; | ||
93 | } | ||
94 | |||
95 | |||
96 | |||
97 | //! Sets the current details level of appender | ||
98 | /** | ||
99 | * This function is provided for convenience, it behaves like an above function. | ||
100 | * | ||
101 | * \sa detailsLevel() | ||
102 | * \sa Logger::LogLevel | ||
103 | */ | ||
104 | void AbstractAppender::setDetailsLevel(const QString& level) | ||
105 | { | ||
106 | setDetailsLevel(Logger::levelFromString(level)); | ||
107 | } | ||
108 | |||
109 | |||
110 | //! Tries to write the log record to this logger | ||
111 | /** | ||
112 | * This is the function called by Logger object to write a log message to the appender. | ||
113 | * | ||
114 | * \note This function is thread safe. | ||
115 | * | ||
116 | * \sa Logger::write() | ||
117 | * \sa detailsLevel() | ||
118 | */ | ||
119 | void AbstractAppender::write(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
120 | const char* function, const QString& category, const QString& message) | ||
121 | { | ||
122 | if (logLevel >= detailsLevel()) | ||
123 | { | ||
124 | QMutexLocker locker(&m_writeMutex); | ||
125 | append(timeStamp, logLevel, file, line, function, category, message); | ||
126 | } | ||
127 | } | ||
128 | |||
129 | |||
130 | /** | ||
131 | * \fn virtual void AbstractAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, | ||
132 | * int line, const char* function, const QString& message) | ||
133 | * | ||
134 | * \brief Writes the log record to the logger instance | ||
135 | * | ||
136 | * This function is called every time when user tries to write a message to this AbstractAppender instance using | ||
137 | * the write() function. Write function works as proxy and transfers only the messages with log level more or equal | ||
138 | * to the current logLevel(). | ||
139 | * | ||
140 | * Overload this function when you are implementing a custom appender. | ||
141 | * | ||
142 | * \note This function is not needed to be thread safe because it is never called directly by Logger object. The | ||
143 | * write() function works as a proxy and protects this function from concurrent access. | ||
144 | * | ||
145 | * \sa Logger::write() | ||
146 | */ | ||
147 | |||
diff --git a/utils/rbutilqt/logger/src/AbstractStringAppender.cpp b/utils/rbutilqt/logger/src/AbstractStringAppender.cpp new file mode 100644 index 0000000000..ea5883f744 --- /dev/null +++ b/utils/rbutilqt/logger/src/AbstractStringAppender.cpp | |||
@@ -0,0 +1,460 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) Nikolay Matyunin (matyunin.n at gmail dot com) | ||
3 | |||
4 | Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). | ||
5 | |||
6 | This program is free software: you can redistribute it and/or modify | ||
7 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
8 | as published by the Free Software Foundation and appearing in the file | ||
9 | LICENSE.LGPL included in the packaging of this file. | ||
10 | |||
11 | This program is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | GNU Lesser General Public License for more details. | ||
15 | */ | ||
16 | // Local | ||
17 | #include "AbstractStringAppender.h" | ||
18 | |||
19 | // Qt | ||
20 | #include <QReadLocker> | ||
21 | #include <QWriteLocker> | ||
22 | #include <QDateTime> | ||
23 | #include <QRegularExpression> | ||
24 | #include <QCoreApplication> | ||
25 | #include <QThread> | ||
26 | |||
27 | |||
28 | /** | ||
29 | * \class AbstractStringAppender | ||
30 | * | ||
31 | * \brief The AbstractStringAppender class provides a convinient base for appenders working with plain text formatted | ||
32 | * logs. | ||
33 | * | ||
34 | * AbstractSringAppender is the simple extension of the AbstractAppender class providing the convinient way to create | ||
35 | * custom log appenders working with a plain text formatted log targets. | ||
36 | * | ||
37 | * It have the formattedString() protected function that formats the logging arguments according to a format set with | ||
38 | * setFormat(). | ||
39 | * | ||
40 | * This class can not be directly instantiated because it contains pure virtual function inherited from AbstractAppender | ||
41 | * class. | ||
42 | * | ||
43 | * For more detailed description of customizing the log output format see the documentation on the setFormat() function. | ||
44 | */ | ||
45 | |||
46 | |||
47 | const char formattingMarker = '%'; | ||
48 | |||
49 | |||
50 | //! Constructs a new string appender object | ||
51 | AbstractStringAppender::AbstractStringAppender() | ||
52 | : m_format(QLatin1String("%{time}{yyyy-MM-ddTHH:mm:ss.zzz} [%{type:-7}] <%{function}> %{message}\n")) | ||
53 | {} | ||
54 | |||
55 | |||
56 | //! Returns the current log format string. | ||
57 | /** | ||
58 | * The default format is set to "%{time}{yyyy-MM-ddTHH:mm:ss.zzz} [%{type:-7}] <%{function}> %{message}\n". You can set a different log record | ||
59 | * format using the setFormat() function. | ||
60 | * | ||
61 | * \sa setFormat(const QString&) | ||
62 | */ | ||
63 | QString AbstractStringAppender::format() const | ||
64 | { | ||
65 | QReadLocker locker(&m_formatLock); | ||
66 | return m_format; | ||
67 | } | ||
68 | |||
69 | |||
70 | //! Sets the logging format for writing strings to the log target with this appender. | ||
71 | /** | ||
72 | * The string format seems to be very common to those developers who have used a standart sprintf function. | ||
73 | * | ||
74 | * Log output format is a simple QString with the special markers (starting with % sign) which will be replaced with | ||
75 | * it's internal meaning when writing a log record. | ||
76 | * | ||
77 | * Controlling marker begins with the percent sign (%) which is followed by the command inside {} brackets | ||
78 | * (the command describes, what will be put to log record instead of marker). | ||
79 | * Optional field width argument may be specified right after the command (through the colon symbol before the closing bracket) | ||
80 | * Some commands requires an additional formatting argument (in the second {} brackets). | ||
81 | * | ||
82 | * Field width argument works almost identically to the \c QString::arg() \c fieldWidth argument (and uses it | ||
83 | * internally). For example, \c "%{type:-7}" will be replaced with the left padded debug level of the message | ||
84 | * (\c "Debug ") or something. For the more detailed description of it you may consider to look to the Qt | ||
85 | * Reference Documentation. | ||
86 | * | ||
87 | * Supported marker commands are: | ||
88 | * \arg \c %{time} - timestamp. You may specify your custom timestamp format using the second {} brackets after the marker, | ||
89 | * timestamp format here will be similiar to those used in QDateTime::toString() function. For example, | ||
90 | * "%{time}{dd-MM-yyyy, HH:mm}" may be replaced with "17-12-2010, 20:17" depending on current date and time. | ||
91 | * The default format used here is "HH:mm:ss.zzz". | ||
92 | * \arg \c %{type} - Log level. Possible log levels are shown in the Logger::LogLevel enumerator. | ||
93 | * \arg \c %{Type} - Uppercased log level. | ||
94 | * \arg \c %{typeOne} - One letter log level. | ||
95 | * \arg \c %{TypeOne} - One uppercase letter log level. | ||
96 | * \arg \c %{File} - Full source file name (with path) of the file that requested log recording. Uses the \c __FILE__ | ||
97 | * preprocessor macro. | ||
98 | * \arg \c %{file} - Short file name (with stripped path). | ||
99 | * \arg \c %{line} - Line number in the source file. Uses the \c __LINE__ preprocessor macro. | ||
100 | * \arg \c %{Function} - Name of function that called on of the LOG_* macros. Uses the \c Q_FUNC_INFO macro provided with | ||
101 | * Qt. | ||
102 | * \arg \c %{function} - Similiar to the %{Function}, but the function name is stripped using stripFunctionName | ||
103 | * \arg \c %{message} - The log message sent by the caller. | ||
104 | * \arg \c %{category} - The log category. | ||
105 | * \arg \c %{appname} - Application name (returned by QCoreApplication::applicationName() function). | ||
106 | * \arg \c %{pid} - Application pid (returned by QCoreApplication::applicationPid() function). | ||
107 | * \arg \c %{threadid} - ID of current thread. | ||
108 | * \arg \c %% - Convinient marker that is replaced with the single \c % mark. | ||
109 | * | ||
110 | * \note Format doesn't add \c '\\n' to the end of the format line. Please consider adding it manually. | ||
111 | * | ||
112 | * \sa format() | ||
113 | * \sa stripFunctionName() | ||
114 | * \sa Logger::LogLevel | ||
115 | */ | ||
116 | void AbstractStringAppender::setFormat(const QString& format) | ||
117 | { | ||
118 | QWriteLocker locker(&m_formatLock); | ||
119 | m_format = format; | ||
120 | } | ||
121 | |||
122 | |||
123 | //! Strips the long function signature (as added by Q_FUNC_INFO macro) | ||
124 | /** | ||
125 | * The string processing drops the returning type, arguments and template parameters of function. It is definitely | ||
126 | * useful for enchancing the log output readability. | ||
127 | * \return stripped function name | ||
128 | */ | ||
129 | QString AbstractStringAppender::stripFunctionName(const char* name) | ||
130 | { | ||
131 | return QString::fromLatin1(qCleanupFuncinfo(name)); | ||
132 | } | ||
133 | |||
134 | |||
135 | // The function was backported from Qt5 sources (qlogging.h) | ||
136 | QByteArray AbstractStringAppender::qCleanupFuncinfo(const char* name) | ||
137 | { | ||
138 | QByteArray info(name); | ||
139 | |||
140 | // Strip the function info down to the base function name | ||
141 | // note that this throws away the template definitions, | ||
142 | // the parameter types (overloads) and any const/volatile qualifiers. | ||
143 | if (info.isEmpty()) | ||
144 | return info; | ||
145 | |||
146 | int pos; | ||
147 | |||
148 | // skip trailing [with XXX] for templates (gcc) | ||
149 | pos = info.size() - 1; | ||
150 | if (info.endsWith(']')) { | ||
151 | while (--pos) { | ||
152 | if (info.at(pos) == '[') | ||
153 | info.truncate(pos); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | bool hasLambda = false; | ||
158 | QRegularExpression lambdaRegex("::<lambda\\(.*?\\)>"); | ||
159 | QRegularExpressionMatch match = lambdaRegex.match(QString::fromLatin1(info)); | ||
160 | int lambdaIndex = match.capturedStart(); | ||
161 | if (lambdaIndex != -1) | ||
162 | { | ||
163 | hasLambda = true; | ||
164 | info.remove(lambdaIndex, match.capturedLength()); | ||
165 | } | ||
166 | |||
167 | // operator names with '(', ')', '<', '>' in it | ||
168 | static const char operator_call[] = "operator()"; | ||
169 | static const char operator_lessThan[] = "operator<"; | ||
170 | static const char operator_greaterThan[] = "operator>"; | ||
171 | static const char operator_lessThanEqual[] = "operator<="; | ||
172 | static const char operator_greaterThanEqual[] = "operator>="; | ||
173 | |||
174 | // canonize operator names | ||
175 | info.replace("operator ", "operator"); | ||
176 | |||
177 | // remove argument list | ||
178 | forever { | ||
179 | int parencount = 0; | ||
180 | pos = info.lastIndexOf(')'); | ||
181 | if (pos == -1) { | ||
182 | // Don't know how to parse this function name | ||
183 | return info; | ||
184 | } | ||
185 | |||
186 | // find the beginning of the argument list | ||
187 | --pos; | ||
188 | ++parencount; | ||
189 | while (pos && parencount) { | ||
190 | if (info.at(pos) == ')') | ||
191 | ++parencount; | ||
192 | else if (info.at(pos) == '(') | ||
193 | --parencount; | ||
194 | --pos; | ||
195 | } | ||
196 | if (parencount != 0) | ||
197 | return info; | ||
198 | |||
199 | info.truncate(++pos); | ||
200 | |||
201 | if (info.at(pos - 1) == ')') { | ||
202 | if (info.indexOf(operator_call) == pos - (int)strlen(operator_call)) | ||
203 | break; | ||
204 | |||
205 | // this function returns a pointer to a function | ||
206 | // and we matched the arguments of the return type's parameter list | ||
207 | // try again | ||
208 | info.remove(0, info.indexOf('(')); | ||
209 | info.chop(1); | ||
210 | continue; | ||
211 | } else { | ||
212 | break; | ||
213 | } | ||
214 | } | ||
215 | |||
216 | if (hasLambda) | ||
217 | info.append("::lambda"); | ||
218 | |||
219 | // find the beginning of the function name | ||
220 | int parencount = 0; | ||
221 | int templatecount = 0; | ||
222 | --pos; | ||
223 | |||
224 | // make sure special characters in operator names are kept | ||
225 | if (pos > -1) { | ||
226 | switch (info.at(pos)) { | ||
227 | case ')': | ||
228 | if (info.indexOf(operator_call) == pos - (int)strlen(operator_call) + 1) | ||
229 | pos -= 2; | ||
230 | break; | ||
231 | case '<': | ||
232 | if (info.indexOf(operator_lessThan) == pos - (int)strlen(operator_lessThan) + 1) | ||
233 | --pos; | ||
234 | break; | ||
235 | case '>': | ||
236 | if (info.indexOf(operator_greaterThan) == pos - (int)strlen(operator_greaterThan) + 1) | ||
237 | --pos; | ||
238 | break; | ||
239 | case '=': { | ||
240 | int operatorLength = (int)strlen(operator_lessThanEqual); | ||
241 | if (info.indexOf(operator_lessThanEqual) == pos - operatorLength + 1) | ||
242 | pos -= 2; | ||
243 | else if (info.indexOf(operator_greaterThanEqual) == pos - operatorLength + 1) | ||
244 | pos -= 2; | ||
245 | break; | ||
246 | } | ||
247 | default: | ||
248 | break; | ||
249 | } | ||
250 | } | ||
251 | |||
252 | while (pos > -1) { | ||
253 | if (parencount < 0 || templatecount < 0) | ||
254 | return info; | ||
255 | |||
256 | char c = info.at(pos); | ||
257 | if (c == ')') | ||
258 | ++parencount; | ||
259 | else if (c == '(') | ||
260 | --parencount; | ||
261 | else if (c == '>') | ||
262 | ++templatecount; | ||
263 | else if (c == '<') | ||
264 | --templatecount; | ||
265 | else if (c == ' ' && templatecount == 0 && parencount == 0) | ||
266 | break; | ||
267 | |||
268 | --pos; | ||
269 | } | ||
270 | info = info.mid(pos + 1); | ||
271 | |||
272 | // remove trailing '*', '&' that are part of the return argument | ||
273 | while ((info.at(0) == '*') | ||
274 | || (info.at(0) == '&')) | ||
275 | info = info.mid(1); | ||
276 | |||
277 | // we have the full function name now. | ||
278 | // clean up the templates | ||
279 | while ((pos = info.lastIndexOf('>')) != -1) { | ||
280 | if (!info.contains('<')) | ||
281 | break; | ||
282 | |||
283 | // find the matching close | ||
284 | int end = pos; | ||
285 | templatecount = 1; | ||
286 | --pos; | ||
287 | while (pos && templatecount) { | ||
288 | char c = info.at(pos); | ||
289 | if (c == '>') | ||
290 | ++templatecount; | ||
291 | else if (c == '<') | ||
292 | --templatecount; | ||
293 | --pos; | ||
294 | } | ||
295 | ++pos; | ||
296 | info.remove(pos, end - pos + 1); | ||
297 | } | ||
298 | |||
299 | return info; | ||
300 | } | ||
301 | |||
302 | |||
303 | //! Returns the string to record to the logging target, formatted according to the format(). | ||
304 | /** | ||
305 | * \sa format() | ||
306 | * \sa setFormat(const QString&) | ||
307 | */ | ||
308 | QString AbstractStringAppender::formattedString(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, | ||
309 | int line, const char* function, const QString& category, const QString& message) const | ||
310 | { | ||
311 | QString f = format(); | ||
312 | const int size = f.size(); | ||
313 | |||
314 | QString result; | ||
315 | |||
316 | int i = 0; | ||
317 | while (i < f.size()) | ||
318 | { | ||
319 | QChar c = f.at(i); | ||
320 | |||
321 | // We will silently ignore the broken % marker at the end of string | ||
322 | if (c != QLatin1Char(formattingMarker) || (i + 2) >= size) | ||
323 | { | ||
324 | result.append(c); | ||
325 | } | ||
326 | else | ||
327 | { | ||
328 | i += 2; | ||
329 | QChar currentChar = f.at(i); | ||
330 | QString command; | ||
331 | int fieldWidth = 0; | ||
332 | |||
333 | if (currentChar.isLetter()) | ||
334 | { | ||
335 | command.append(currentChar); | ||
336 | int j = 1; | ||
337 | while ((i + j) < size && f.at(i + j).isLetter()) | ||
338 | { | ||
339 | command.append(f.at(i+j)); | ||
340 | j++; | ||
341 | } | ||
342 | |||
343 | i+=j; | ||
344 | currentChar = f.at(i); | ||
345 | |||
346 | // Check for the padding instruction | ||
347 | if (currentChar == QLatin1Char(':')) | ||
348 | { | ||
349 | currentChar = f.at(++i); | ||
350 | if (currentChar.isDigit() || currentChar.category() == QChar::Punctuation_Dash) | ||
351 | { | ||
352 | int j = 1; | ||
353 | while ((i + j) < size && f.at(i + j).isDigit()) | ||
354 | j++; | ||
355 | fieldWidth = f.mid(i, j).toInt(); | ||
356 | |||
357 | i += j; | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | |||
362 | // Log record chunk to insert instead of formatting instruction | ||
363 | QString chunk; | ||
364 | |||
365 | // Time stamp | ||
366 | if (command == QLatin1String("time")) | ||
367 | { | ||
368 | if (f.at(i + 1) == QLatin1Char('{')) | ||
369 | { | ||
370 | int j = 1; | ||
371 | while ((i + 2 + j) < size && f.at(i + 2 + j) != QLatin1Char('}')) | ||
372 | j++; | ||
373 | |||
374 | if ((i + 2 + j) < size) | ||
375 | { | ||
376 | chunk = timeStamp.toString(f.mid(i + 2, j)); | ||
377 | |||
378 | i += j; | ||
379 | i += 2; | ||
380 | } | ||
381 | } | ||
382 | |||
383 | if (chunk.isNull()) | ||
384 | chunk = timeStamp.toString(QLatin1String("HH:mm:ss.zzz")); | ||
385 | } | ||
386 | |||
387 | // Log level | ||
388 | else if (command == QLatin1String("type")) | ||
389 | chunk = Logger::levelToString(logLevel); | ||
390 | |||
391 | // Uppercased log level | ||
392 | else if (command == QLatin1String("Type")) | ||
393 | chunk = Logger::levelToString(logLevel).toUpper(); | ||
394 | |||
395 | // One letter log level | ||
396 | else if (command == QLatin1String("typeOne")) | ||
397 | chunk = Logger::levelToString(logLevel).left(1).toLower(); | ||
398 | |||
399 | // One uppercase letter log level | ||
400 | else if (command == QLatin1String("TypeOne")) | ||
401 | chunk = Logger::levelToString(logLevel).left(1).toUpper(); | ||
402 | |||
403 | // Filename | ||
404 | else if (command == QLatin1String("File")) | ||
405 | chunk = QLatin1String(file); | ||
406 | |||
407 | // Filename without a path | ||
408 | else if (command == QLatin1String("file")) | ||
409 | chunk = QString(QLatin1String(file)).section(QRegularExpression("[/\\\\]"), -1); | ||
410 | |||
411 | // Source line number | ||
412 | else if (command == QLatin1String("line")) | ||
413 | chunk = QString::number(line); | ||
414 | |||
415 | // Function name, as returned by Q_FUNC_INFO | ||
416 | else if (command == QLatin1String("Function")) | ||
417 | chunk = QString::fromLatin1(function); | ||
418 | |||
419 | // Stripped function name | ||
420 | else if (command == QLatin1String("function")) | ||
421 | chunk = stripFunctionName(function); | ||
422 | |||
423 | // Log message | ||
424 | else if (command == QLatin1String("message")) | ||
425 | chunk = message; | ||
426 | |||
427 | else if (command == QLatin1String("category")) | ||
428 | chunk = category; | ||
429 | |||
430 | // Application pid | ||
431 | else if (command == QLatin1String("pid")) | ||
432 | chunk = QString::number(QCoreApplication::applicationPid()); | ||
433 | |||
434 | // Appplication name | ||
435 | else if (command == QLatin1String("appname")) | ||
436 | chunk = QCoreApplication::applicationName(); | ||
437 | |||
438 | // Thread ID (duplicates Qt5 threadid debbuging way) | ||
439 | else if (command == QLatin1String("threadid")) | ||
440 | chunk = QLatin1String("0x") + QString::number(qlonglong(QThread::currentThread()->currentThread()), 16); | ||
441 | |||
442 | // We simply replace the double formatting marker (%) with one | ||
443 | else if (command == QString(formattingMarker)) | ||
444 | chunk = QLatin1Char(formattingMarker); | ||
445 | |||
446 | // Do not process any unknown commands | ||
447 | else | ||
448 | { | ||
449 | chunk = QString(formattingMarker); | ||
450 | chunk.append(command); | ||
451 | } | ||
452 | |||
453 | result.append(QString(QLatin1String("%1")).arg(chunk, fieldWidth)); | ||
454 | } | ||
455 | |||
456 | ++i; | ||
457 | } | ||
458 | |||
459 | return result; | ||
460 | } | ||
diff --git a/utils/rbutilqt/logger/src/ConsoleAppender.cpp b/utils/rbutilqt/logger/src/ConsoleAppender.cpp new file mode 100644 index 0000000000..932ffab787 --- /dev/null +++ b/utils/rbutilqt/logger/src/ConsoleAppender.cpp | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "ConsoleAppender.h" | ||
16 | |||
17 | // STL | ||
18 | #include <iostream> | ||
19 | |||
20 | |||
21 | /** | ||
22 | * \class ConsoleAppender | ||
23 | * | ||
24 | * \brief ConsoleAppender is the simple appender that writes the log records to the std::cerr output stream. | ||
25 | * | ||
26 | * ConsoleAppender uses "[%{type:-7}] <%{function}> %{message}\n" as a default output format. It is similar to the | ||
27 | * AbstractStringAppender but doesn't show a timestamp. | ||
28 | * | ||
29 | * You can modify ConsoleAppender output format without modifying your code by using \c QT_MESSAGE_PATTERN environment | ||
30 | * variable. If you need your application to ignore this environment variable you can call | ||
31 | * ConsoleAppender::ignoreEnvironmentPattern(true) | ||
32 | */ | ||
33 | |||
34 | |||
35 | ConsoleAppender::ConsoleAppender() | ||
36 | : AbstractStringAppender() | ||
37 | , m_ignoreEnvPattern(false) | ||
38 | { | ||
39 | setFormat("[%{type:-7}] <%{function}> %{message}\n"); | ||
40 | } | ||
41 | |||
42 | |||
43 | QString ConsoleAppender::format() const | ||
44 | { | ||
45 | const QString envPattern = QString::fromLocal8Bit(qgetenv("QT_MESSAGE_PATTERN")); | ||
46 | return (m_ignoreEnvPattern || envPattern.isEmpty()) ? AbstractStringAppender::format() : (envPattern + "\n"); | ||
47 | } | ||
48 | |||
49 | |||
50 | void ConsoleAppender::ignoreEnvironmentPattern(bool ignore) | ||
51 | { | ||
52 | m_ignoreEnvPattern = ignore; | ||
53 | } | ||
54 | |||
55 | |||
56 | //! Writes the log record to the std::cerr stream. | ||
57 | /** | ||
58 | * \sa AbstractStringAppender::format() | ||
59 | */ | ||
60 | void ConsoleAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
61 | const char* function, const QString& category, const QString& message) | ||
62 | { | ||
63 | std::cerr << qPrintable(formattedString(timeStamp, logLevel, file, line, function, category, message)); | ||
64 | } | ||
diff --git a/utils/rbutilqt/logger/src/FileAppender.cpp b/utils/rbutilqt/logger/src/FileAppender.cpp new file mode 100644 index 0000000000..b9018b0324 --- /dev/null +++ b/utils/rbutilqt/logger/src/FileAppender.cpp | |||
@@ -0,0 +1,116 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "FileAppender.h" | ||
16 | |||
17 | // STL | ||
18 | #include <iostream> | ||
19 | |||
20 | /** | ||
21 | * \class FileAppender | ||
22 | * | ||
23 | * \brief Simple appender that writes the log records to the plain text file. | ||
24 | */ | ||
25 | |||
26 | |||
27 | //! Constructs the new file appender assigned to file with the given name. | ||
28 | FileAppender::FileAppender(const QString& fileName) | ||
29 | { | ||
30 | setFileName(fileName); | ||
31 | } | ||
32 | |||
33 | |||
34 | FileAppender::~FileAppender() | ||
35 | { | ||
36 | closeFile(); | ||
37 | } | ||
38 | |||
39 | |||
40 | //! Returns the name set by setFileName() or to the FileAppender constructor. | ||
41 | /** | ||
42 | * \sa setFileName() | ||
43 | */ | ||
44 | QString FileAppender::fileName() const | ||
45 | { | ||
46 | QMutexLocker locker(&m_logFileMutex); | ||
47 | return m_logFile.fileName(); | ||
48 | } | ||
49 | |||
50 | |||
51 | //! Sets the name of the file. The name can have no path, a relative path, or an absolute path. | ||
52 | /** | ||
53 | * \sa fileName() | ||
54 | */ | ||
55 | void FileAppender::setFileName(const QString& s) | ||
56 | { | ||
57 | if (s.isEmpty()) | ||
58 | std::cerr << "<FileAppender::FileAppender> File name is empty. The appender will do nothing" << std::endl; | ||
59 | |||
60 | QMutexLocker locker(&m_logFileMutex); | ||
61 | if (m_logFile.isOpen()) | ||
62 | m_logFile.close(); | ||
63 | |||
64 | m_logFile.setFileName(s); | ||
65 | } | ||
66 | |||
67 | |||
68 | bool FileAppender::reopenFile() | ||
69 | { | ||
70 | closeFile(); | ||
71 | return openFile(); | ||
72 | } | ||
73 | |||
74 | |||
75 | bool FileAppender::openFile() | ||
76 | { | ||
77 | if (m_logFile.fileName().isEmpty()) | ||
78 | return false; | ||
79 | |||
80 | bool isOpen = m_logFile.isOpen(); | ||
81 | if (!isOpen) | ||
82 | { | ||
83 | isOpen = m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text); | ||
84 | if (isOpen) | ||
85 | m_logStream.setDevice(&m_logFile); | ||
86 | else | ||
87 | std::cerr << "<FileAppender::append> Cannot open the log file " << qPrintable(m_logFile.fileName()) << std::endl; | ||
88 | } | ||
89 | return isOpen; | ||
90 | } | ||
91 | |||
92 | |||
93 | //! Write the log record to the file. | ||
94 | /** | ||
95 | * \sa fileName() | ||
96 | * \sa AbstractStringAppender::format() | ||
97 | */ | ||
98 | void FileAppender::append(const QDateTime& timeStamp, Logger::LogLevel logLevel, const char* file, int line, | ||
99 | const char* function, const QString& category, const QString& message) | ||
100 | { | ||
101 | QMutexLocker locker(&m_logFileMutex); | ||
102 | |||
103 | if (openFile()) | ||
104 | { | ||
105 | m_logStream << formattedString(timeStamp, logLevel, file, line, function, category, message); | ||
106 | m_logStream.flush(); | ||
107 | m_logFile.flush(); | ||
108 | } | ||
109 | } | ||
110 | |||
111 | |||
112 | void FileAppender::closeFile() | ||
113 | { | ||
114 | QMutexLocker locker(&m_logFileMutex); | ||
115 | m_logFile.close(); | ||
116 | } | ||
diff --git a/utils/rbutilqt/logger/src/Logger.cpp b/utils/rbutilqt/logger/src/Logger.cpp new file mode 100644 index 0000000000..689bc42e80 --- /dev/null +++ b/utils/rbutilqt/logger/src/Logger.cpp | |||
@@ -0,0 +1,1108 @@ | |||
1 | /* | ||
2 | Copyright (c) 2012 Boris Moiseev (cyberbobs at gmail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "Logger.h" | ||
16 | #include "AbstractAppender.h" | ||
17 | #include "AbstractStringAppender.h" | ||
18 | |||
19 | // Qt | ||
20 | #include <QCoreApplication> | ||
21 | #include <QReadWriteLock> | ||
22 | #include <QSemaphore> | ||
23 | #include <QDateTime> | ||
24 | #include <QIODevice> | ||
25 | #include <QTextCodec> | ||
26 | |||
27 | #if defined(Q_OS_ANDROID) | ||
28 | # include <android/log.h> | ||
29 | # include <AndroidAppender.h> | ||
30 | #endif | ||
31 | |||
32 | // STL | ||
33 | #include <iostream> | ||
34 | |||
35 | |||
36 | /** | ||
37 | * \file Logger.h | ||
38 | * \brief A file containing the description of Logger class and and additional useful macros for logging | ||
39 | */ | ||
40 | |||
41 | |||
42 | /** | ||
43 | * \mainpage | ||
44 | * | ||
45 | * Logger is a simple way to write the history of your application lifecycle to any target logging device (which is | ||
46 | * called Appender and may write to any target you will implement with it: console, text file, XML or something - you | ||
47 | * choose) and to map logging message to a class, function, source file and line of code which it is called from. | ||
48 | * | ||
49 | * Some simple appenders (which may be considered an examples) are provided with the Logger itself: see ConsoleAppender | ||
50 | * and FileAppender documentation. | ||
51 | * | ||
52 | * It supports using it in a multithreaded applications, so all of its functions are thread safe. | ||
53 | * | ||
54 | * Simple usage example: | ||
55 | * \code | ||
56 | * #include <QCoreApplication> | ||
57 | * | ||
58 | * #include <Logger.h> | ||
59 | * #include <ConsoleAppender.h> | ||
60 | * | ||
61 | * int main(int argc, char* argv[]) | ||
62 | * { | ||
63 | * QCoreApplication app(argc, argv); | ||
64 | * ... | ||
65 | * ConsoleAppender* consoleAppender = new ConsoleAppender; | ||
66 | * consoleAppender->setFormat("[%{type:-7}] <%{Function}> %{message}\n"); | ||
67 | * cuteLogger->registerAppender(consoleAppender); | ||
68 | * ... | ||
69 | * LOG_INFO("Starting the application"); | ||
70 | * int result = app.exec(); | ||
71 | * ... | ||
72 | * if (result) | ||
73 | * LOG_WARNING() << "Something went wrong." << "Result code is" << result; | ||
74 | * | ||
75 | * return result; | ||
76 | * } | ||
77 | * \endcode | ||
78 | * | ||
79 | * Logger internally uses the lazy-initialized singleton object and needs no definite initialization, but you may | ||
80 | * consider registering a log appender before calling any log recording functions or macros. | ||
81 | * | ||
82 | * The library design of Logger allows you to simply mass-replace all occurrences of qDebug and similar calls with | ||
83 | * similar Logger macros (e.g. LOG_DEBUG()) | ||
84 | * | ||
85 | * \note Logger uses a singleton global instance which lives through all the application life cycle and self-destroys | ||
86 | * destruction of the QCoreApplication (or QApplication) instance. It needs a QCoreApplication instance to be | ||
87 | * created before any of the Logger's functions are called. | ||
88 | * | ||
89 | * \sa cuteLogger | ||
90 | * \sa LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL | ||
91 | * \sa LOG_CTRACE, LOG_CDEBUG, LOG_CINFO, LOG_CWARNING, LOG_CERROR, LOG_CFATAL | ||
92 | * \sa LOG_ASSERT | ||
93 | * \sa LOG_TRACE_TIME, LOG_DEBUG_TIME, LOG_INFO_TIME | ||
94 | * \sa AbstractAppender | ||
95 | */ | ||
96 | |||
97 | |||
98 | /** | ||
99 | * \def cuteLogger | ||
100 | * | ||
101 | * \brief Macro returning the current instance of Logger object | ||
102 | * | ||
103 | * If you haven't created a local Logger object it returns the same value as the Logger::globalInstance() functions. | ||
104 | * This macro is a recommended way to get an access to the Logger instance used in current class. | ||
105 | * | ||
106 | * Example: | ||
107 | * \code | ||
108 | * ConsoleAppender* consoleAppender = new ConsoleAppender; | ||
109 | * cuteLogger->registerAppender(consoleAppender); | ||
110 | * \endcode | ||
111 | * | ||
112 | * \sa Logger::globalInstance() | ||
113 | */ | ||
114 | |||
115 | |||
116 | /** | ||
117 | * \def LOG_TRACE | ||
118 | * | ||
119 | * \brief Writes the trace log record | ||
120 | * | ||
121 | * This macro is the convinient way to call Logger::write(). It uses the common preprocessor macros \c __FILE__, | ||
122 | * \c __LINE__ and the standart Qt \c Q_FUNC_INFO macros to automatically determine the needed parameters to call | ||
123 | * Logger::write(). | ||
124 | * | ||
125 | * \note This and other (LOG_INFO() etc...) macros uses the variadic macro arguments to give convinient usage form for | ||
126 | * the different versions of Logger::write() (using the QString or const char* argument or returning the QDebug class | ||
127 | * instance). Not all compilers will support this. Please, consider reviewing your compiler documentation to ensure | ||
128 | * it support __VA_ARGS__ macro. | ||
129 | * | ||
130 | * \sa Logger::LogLevel | ||
131 | * \sa Logger::write() | ||
132 | */ | ||
133 | |||
134 | |||
135 | /** | ||
136 | * \def LOG_DEBUG | ||
137 | * | ||
138 | * \brief Writes the debug log record | ||
139 | * | ||
140 | * This macro records the debug log record using the Logger::write() function. It works similar to the LOG_TRACE() | ||
141 | * macro. | ||
142 | * | ||
143 | * \sa LOG_TRACE() | ||
144 | * \sa Logger::LogLevel | ||
145 | * \sa Logger::write() | ||
146 | */ | ||
147 | |||
148 | |||
149 | /** | ||
150 | * \def LOG_INFO | ||
151 | * | ||
152 | * \brief Writes the info log record | ||
153 | * | ||
154 | * This macro records the info log record using the Logger::write() function. It works similar to the LOG_TRACE() | ||
155 | * macro. | ||
156 | * | ||
157 | * \sa LOG_TRACE() | ||
158 | * \sa Logger::LogLevel | ||
159 | * \sa Logger::write() | ||
160 | */ | ||
161 | |||
162 | |||
163 | /** | ||
164 | * \def LOG_WARNING | ||
165 | * | ||
166 | * \brief Write the warning log record | ||
167 | * | ||
168 | * This macro records the warning log record using the Logger::write() function. It works similar to the LOG_TRACE() | ||
169 | * macro. | ||
170 | * | ||
171 | * \sa LOG_TRACE() | ||
172 | * \sa Logger::LogLevel | ||
173 | * \sa Logger::write() | ||
174 | */ | ||
175 | |||
176 | |||
177 | /** | ||
178 | * \def LOG_ERROR | ||
179 | * | ||
180 | * \brief Write the error log record | ||
181 | * This macro records the error log record using the Logger::write() function. It works similar to the LOG_TRACE() | ||
182 | * macro. | ||
183 | * | ||
184 | * \sa LOG_TRACE() | ||
185 | * \sa Logger::LogLevel | ||
186 | * \sa Logger::write() | ||
187 | */ | ||
188 | |||
189 | |||
190 | /** | ||
191 | * \def LOG_FATAL | ||
192 | * | ||
193 | * \brief Write the fatal log record | ||
194 | * | ||
195 | * This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_TRACE() | ||
196 | * macro. | ||
197 | * | ||
198 | * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort() | ||
199 | * function, which will interrupt the running of your software and begin the writing of the core dump. | ||
200 | * | ||
201 | * \sa LOG_TRACE() | ||
202 | * \sa Logger::LogLevel | ||
203 | * \sa Logger::write() | ||
204 | */ | ||
205 | |||
206 | |||
207 | /** | ||
208 | * \def LOG_CTRACE(category) | ||
209 | * | ||
210 | * \brief Writes the trace log record to the specific category | ||
211 | * | ||
212 | * This macro is the similar to the LOG_TRACE() macro, but has a category parameter | ||
213 | * to write only to the category appenders (registered using Logger::registerCategoryAppender() method). | ||
214 | * | ||
215 | * \param category category name string | ||
216 | * | ||
217 | * \sa LOG_TRACE() | ||
218 | * \sa Logger::LogLevel | ||
219 | * \sa Logger::registerCategoryAppender() | ||
220 | * \sa Logger::write() | ||
221 | * \sa LOG_CATEGORY(), LOG_GLOBAL_CATEGORY() | ||
222 | */ | ||
223 | |||
224 | |||
225 | /** | ||
226 | * \def LOG_CDEBUG | ||
227 | * | ||
228 | * \brief Writes the debug log record to the specific category | ||
229 | * | ||
230 | * This macro records the debug log record using the Logger::write() function. It works similar to the LOG_CTRACE() | ||
231 | * macro. | ||
232 | * | ||
233 | * \sa LOG_CTRACE() | ||
234 | */ | ||
235 | |||
236 | |||
237 | /** | ||
238 | * \def LOG_CINFO | ||
239 | * | ||
240 | * \brief Writes the info log record to the specific category | ||
241 | * | ||
242 | * This macro records the info log record using the Logger::write() function. It works similar to the LOG_CTRACE() | ||
243 | * macro. | ||
244 | * | ||
245 | * \sa LOG_CTRACE() | ||
246 | */ | ||
247 | |||
248 | |||
249 | /** | ||
250 | * \def LOG_CWARNING | ||
251 | * | ||
252 | * \brief Writes the warning log record to the specific category | ||
253 | * | ||
254 | * This macro records the warning log record using the Logger::write() function. It works similar to the LOG_CTRACE() | ||
255 | * macro. | ||
256 | * | ||
257 | * \sa LOG_CTRACE() | ||
258 | */ | ||
259 | |||
260 | |||
261 | /** | ||
262 | * \def LOG_CERROR | ||
263 | * | ||
264 | * \brief Writes the error log record to the specific category | ||
265 | * | ||
266 | * This macro records the error log record using the Logger::write() function. It works similar to the LOG_CTRACE() | ||
267 | * macro. | ||
268 | * | ||
269 | * \sa LOG_CTRACE() | ||
270 | */ | ||
271 | |||
272 | |||
273 | /** | ||
274 | * \def LOG_CFATAL | ||
275 | * | ||
276 | * \brief Write the fatal log record to the specific category | ||
277 | * | ||
278 | * This macro records the fatal log record using the Logger::write() function. It works similar to the LOG_CTRACE() | ||
279 | * macro. | ||
280 | * | ||
281 | * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort() | ||
282 | * function, which will interrupt the running of your software and begin the writing of the core dump. | ||
283 | * | ||
284 | * \sa LOG_CTRACE() | ||
285 | */ | ||
286 | |||
287 | |||
288 | /** | ||
289 | * \def LOG_CATEGORY(category) | ||
290 | * | ||
291 | * \brief Create logger instance inside your custom class to log all messages to the specified category | ||
292 | * | ||
293 | * This macro is used to pass all log messages inside your custom class to the specific category. | ||
294 | * You must include this macro inside your class declaration (similarly to the Q_OBJECT macro). | ||
295 | * Internally, this macro redefines cuteLoggerInstance() function, creates the local Logger object inside your class and | ||
296 | * sets the default category to the specified parameter. | ||
297 | * | ||
298 | * Thus, any call to cuteLoggerInstance() (for example, inside LOG_TRACE() macro) will return the local Logger object, | ||
299 | * so any logging message will be directed to the default category. | ||
300 | * | ||
301 | * \note This macro does not register any appender to the newly created logger instance. You should register | ||
302 | * logger appenders manually, inside your class. | ||
303 | * | ||
304 | * Usage example: | ||
305 | * \code | ||
306 | * class CustomClass : public QObject | ||
307 | * { | ||
308 | * Q_OBJECT | ||
309 | * LOG_CATEGORY("custom_category") | ||
310 | * ... | ||
311 | * }; | ||
312 | * | ||
313 | * CustomClass::CustomClass(QObject* parent) : QObject(parent) | ||
314 | * { | ||
315 | * cuteLogger->registerAppender(new FileAppender("custom_category_log")); | ||
316 | * LOG_TRACE() << "Trace to the custom category log"; | ||
317 | * } | ||
318 | * \endcode | ||
319 | * | ||
320 | * If used compiler supports C++11 standard, LOG_CATEGORY and LOG_GLOBAL_CATEGORY macros would also work when added | ||
321 | * inside of any scope. It could be useful, for example, to log every single run of a method to a different file. | ||
322 | * | ||
323 | * \code | ||
324 | * void foo() | ||
325 | * { | ||
326 | * QString categoryName = QDateTime::currentDateTime().toString("yyyy-MM-ddThh-mm-ss-zzz"); | ||
327 | * LOG_CATEGORY(categoryName); | ||
328 | * cuteLogger->registerAppender(new FileAppender(categoryName + ".log")); | ||
329 | * ... | ||
330 | * } | ||
331 | * \endcode | ||
332 | * | ||
333 | * \sa Logger::write() | ||
334 | * \sa LOG_TRACE | ||
335 | * \sa Logger::registerCategoryAppender() | ||
336 | * \sa Logger::setDefaultCategory() | ||
337 | * \sa LOG_GLOBAL_CATEGORY | ||
338 | */ | ||
339 | |||
340 | |||
341 | /** | ||
342 | * \def LOG_GLOBAL_CATEGORY(category) | ||
343 | * | ||
344 | * \brief Create logger instance inside your custom class to log all messages both to the specified category and to | ||
345 | * the global logger instance. | ||
346 | * | ||
347 | * This macro is similar to LOG_CATEGORY(), but also passes all log messages to the global logger instance appenders. | ||
348 | * It is equal to defining the local category logger using LOG_CATEGORY macro and calling: | ||
349 | * \code cuteLogger->logToGlobalInstance(cuteLogger->defaultCategory(), true); \endcode | ||
350 | * | ||
351 | * \sa LOG_CATEGORY | ||
352 | * \sa Logger::logToGlobalInstance() | ||
353 | * \sa Logger::defaultCategory() | ||
354 | * \sa Logger::registerCategoryAppender() | ||
355 | * \sa Logger::write() | ||
356 | */ | ||
357 | |||
358 | |||
359 | |||
360 | /** | ||
361 | * \def LOG_ASSERT | ||
362 | * | ||
363 | * \brief Check the assertion | ||
364 | * | ||
365 | * This macro is a convinient and recommended to use way to call Logger::writeAssert() function. It uses the | ||
366 | * preprocessor macros (as the LOG_DEBUG() does) to fill the necessary arguments of the Logger::writeAssert() call. It | ||
367 | * also uses undocumented but rather mature and stable \c qt_noop() function (which does nothing) when the assertion | ||
368 | * is true. | ||
369 | * | ||
370 | * Example: | ||
371 | * \code | ||
372 | * bool b = checkSomething(); | ||
373 | * ... | ||
374 | * LOG_ASSERT(b == true); | ||
375 | * \endcode | ||
376 | * | ||
377 | * \sa Logger::writeAssert() | ||
378 | */ | ||
379 | |||
380 | |||
381 | /** | ||
382 | * \def LOG_TRACE_TIME | ||
383 | * | ||
384 | * \brief Logs the processing time of current function / code block | ||
385 | * | ||
386 | * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Trace | ||
387 | * level log record. | ||
388 | * | ||
389 | * Example: | ||
390 | * \code | ||
391 | * int foo() | ||
392 | * { | ||
393 | * LOG_TRACE_TIME(); | ||
394 | * ... // Do some long operations | ||
395 | * return 0; | ||
396 | * } // Outputs: Function foo finished in <time> ms. | ||
397 | * \endcode | ||
398 | * | ||
399 | * If you are measuring a code of block execution time you may also add a name of block to the macro: | ||
400 | * \code | ||
401 | * int bar(bool doFoo) | ||
402 | * { | ||
403 | * LOG_TRACE_TIME(); | ||
404 | * | ||
405 | * if (doFoo) | ||
406 | * { | ||
407 | * LOG_TRACE_TIME("Foo"); | ||
408 | * ... | ||
409 | * } | ||
410 | * | ||
411 | * ... | ||
412 | * } | ||
413 | * // Outputs: | ||
414 | * // "Foo" finished in <time1> ms. | ||
415 | * // Function bar finished in <time2> ms. | ||
416 | * \endcode | ||
417 | * | ||
418 | * \note Macro switches to logging the seconds instead of milliseconds when the execution time reaches 10000 ms. | ||
419 | * \sa LOG_DEBUG_TIME, LOG_INFO_TIME | ||
420 | */ | ||
421 | |||
422 | |||
423 | /** | ||
424 | * \def LOG_DEBUG_TIME | ||
425 | * | ||
426 | * \brief Logs the processing time of current function / code block | ||
427 | * | ||
428 | * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Debug | ||
429 | * level log record. It works similar to LOG_TRACE_TIME() macro. | ||
430 | * | ||
431 | * \sa LOG_TRACE_TIME | ||
432 | */ | ||
433 | |||
434 | |||
435 | /** | ||
436 | * \def LOG_INFO_TIME | ||
437 | * | ||
438 | * \brief Logs the processing time of current function / code block | ||
439 | * | ||
440 | * This macro automagically measures the function or code of block execution time and outputs it as a Logger::Info | ||
441 | * level log record. It works similar to LOG_TRACE_TIME() macro. | ||
442 | * | ||
443 | * \sa LOG_TRACE_TIME | ||
444 | */ | ||
445 | |||
446 | |||
447 | /** | ||
448 | * \class Logger | ||
449 | * | ||
450 | * \brief Very simple but rather powerful component which may be used for logging your application activities. | ||
451 | * | ||
452 | * Global logger instance created on a first access to it (e.g. registering appenders, calling a LOG_DEBUG() macro | ||
453 | * etc.) registers itself as a Qt default message handler and captures all the qDebug/qWarning/qCritical output. | ||
454 | * | ||
455 | * \note Qt 4 qDebug set of macro doesn't support capturing source function name, file name or line number so we | ||
456 | * recommend to use LOG_DEBUG() and other Logger macros instead. | ||
457 | * | ||
458 | * \sa cuteLogger | ||
459 | * \sa [CuteLogger Documentation](index.html) | ||
460 | */ | ||
461 | |||
462 | // Forward declarations | ||
463 | static void cleanupLoggerGlobalInstance(); | ||
464 | |||
465 | #if QT_VERSION >= 0x050000 | ||
466 | static void qtLoggerMessageHandler(QtMsgType, const QMessageLogContext& context, const QString& msg); | ||
467 | #else | ||
468 | static void qtLoggerMessageHandler(QtMsgType type, const char* msg); | ||
469 | #endif | ||
470 | |||
471 | /** | ||
472 | * \internal | ||
473 | * | ||
474 | * LoggerPrivate class implements the Singleton pattern in a thread-safe way. It contains a static pointer to the | ||
475 | * global logger instance protected by QReadWriteLock | ||
476 | */ | ||
477 | class LoggerPrivate | ||
478 | { | ||
479 | public: | ||
480 | static Logger* globalInstance; | ||
481 | static QReadWriteLock globalInstanceLock; | ||
482 | |||
483 | QList<AbstractAppender*> appenders; | ||
484 | QMutex loggerMutex; | ||
485 | |||
486 | QMap<QString, bool> categories; | ||
487 | QMultiMap<QString, AbstractAppender*> categoryAppenders; | ||
488 | QStringList noAppendersCategories; //<! Categories without appenders that was already warned about | ||
489 | QString defaultCategory; | ||
490 | bool writeDefaultCategoryToGlobalInstance; | ||
491 | }; | ||
492 | |||
493 | |||
494 | // Static fields initialization | ||
495 | Logger* LoggerPrivate::globalInstance = nullptr; | ||
496 | QReadWriteLock LoggerPrivate::globalInstanceLock; | ||
497 | |||
498 | |||
499 | static void cleanupLoggerGlobalInstance() | ||
500 | { | ||
501 | QWriteLocker locker(&LoggerPrivate::globalInstanceLock); | ||
502 | |||
503 | delete LoggerPrivate::globalInstance; | ||
504 | LoggerPrivate::globalInstance = nullptr; | ||
505 | } | ||
506 | |||
507 | |||
508 | #if QT_VERSION >= 0x050000 | ||
509 | static void qtLoggerMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg) | ||
510 | { | ||
511 | Logger::LogLevel level = Logger::Debug; | ||
512 | switch (type) | ||
513 | { | ||
514 | case QtDebugMsg: | ||
515 | level = Logger::Debug; | ||
516 | break; | ||
517 | #if QT_VERSION >= 0x050500 | ||
518 | case QtInfoMsg: | ||
519 | level = Logger::Info; | ||
520 | break; | ||
521 | #endif | ||
522 | case QtWarningMsg: | ||
523 | level = Logger::Warning; | ||
524 | break; | ||
525 | case QtCriticalMsg: | ||
526 | level = Logger::Error; | ||
527 | break; | ||
528 | case QtFatalMsg: | ||
529 | level = Logger::Fatal; | ||
530 | break; | ||
531 | } | ||
532 | |||
533 | bool isDefaultCategory = QString::fromLatin1(context.category) == "default"; | ||
534 | Logger::globalInstance()->write(level, context.file, context.line, context.function, isDefaultCategory ? nullptr : context.category, msg); | ||
535 | } | ||
536 | |||
537 | #else | ||
538 | |||
539 | static void qtLoggerMessageHandler(QtMsgType type, const char* msg) | ||
540 | { | ||
541 | switch (type) | ||
542 | { | ||
543 | case QtDebugMsg: | ||
544 | cuteLoggerInstance()->write(Logger::Debug, "", 0, "qDebug", 0, msg); | ||
545 | break; | ||
546 | case QtWarningMsg: | ||
547 | cuteLoggerInstance()->write(Logger::Warning, "", 0, "qDebug", 0, msg); | ||
548 | break; | ||
549 | case QtCriticalMsg: | ||
550 | cuteLoggerInstance()->write(Logger::Error, "", 0, "qDebug", 0, msg); | ||
551 | break; | ||
552 | case QtFatalMsg: | ||
553 | cuteLoggerInstance()->write(Logger::Fatal, "", 0, "qDebug", 0, msg); | ||
554 | break; | ||
555 | } | ||
556 | } | ||
557 | #endif | ||
558 | |||
559 | |||
560 | //! Construct the instance of Logger | ||
561 | /** | ||
562 | * If you're only using one global instance of logger you wouldn't probably need to use this constructor manually. | ||
563 | * Consider using [cuteLogger](@ref cuteLogger) macro instead to access the logger instance | ||
564 | */ | ||
565 | Logger::Logger() | ||
566 | : d_ptr(new LoggerPrivate) | ||
567 | { | ||
568 | Q_D(Logger); | ||
569 | d->writeDefaultCategoryToGlobalInstance = false; | ||
570 | } | ||
571 | |||
572 | |||
573 | //! Construct the instance of Logger and set logger default category | ||
574 | /** | ||
575 | * If you're only using one global instance of logger you wouldn't probably need to use this constructor manually. | ||
576 | * Consider using [cuteLogger](@ref cuteLogger) macro instead to access the logger instance and call | ||
577 | * [setDefaultCategory](@ref setDefaultCategory) method. | ||
578 | * | ||
579 | * \sa Logger() | ||
580 | * \sa setDefaultCategory() | ||
581 | */ | ||
582 | Logger::Logger(const QString& defaultCategory, bool writeToGlobalInstance) | ||
583 | : d_ptr(new LoggerPrivate) | ||
584 | { | ||
585 | Q_D(Logger); | ||
586 | d->writeDefaultCategoryToGlobalInstance = writeToGlobalInstance; | ||
587 | |||
588 | setDefaultCategory(defaultCategory); | ||
589 | } | ||
590 | |||
591 | |||
592 | //! Destroy the instance of Logger | ||
593 | /** | ||
594 | * You probably wouldn't need to use this function directly. Global instance of logger will be destroyed automatically | ||
595 | * at the end of your QCoreApplication execution | ||
596 | */ | ||
597 | Logger::~Logger() | ||
598 | { | ||
599 | Q_D(Logger); | ||
600 | |||
601 | // Cleanup appenders | ||
602 | QMutexLocker appendersLocker(&d->loggerMutex); | ||
603 | #if QT_VERSION >= 0x050e00 | ||
604 | QSet<AbstractAppender*> deleteList(QSet<AbstractAppender*>(d->appenders.begin(), d->appenders.end())); | ||
605 | deleteList.unite(QSet<AbstractAppender*>(d->categoryAppenders.values().begin(), d->categoryAppenders.values().end())); | ||
606 | #else | ||
607 | QSet<AbstractAppender*> deleteList(QSet<AbstractAppender*>::fromList(d->appenders)); | ||
608 | deleteList.unite(QSet<AbstractAppender*>::fromList(d->categoryAppenders.values())); | ||
609 | #endif | ||
610 | qDeleteAll(deleteList); | ||
611 | |||
612 | appendersLocker.unlock(); | ||
613 | |||
614 | delete d_ptr; | ||
615 | } | ||
616 | |||
617 | |||
618 | //! Converts the LogLevel enum value to its string representation | ||
619 | /** | ||
620 | * \param logLevel Log level to convert | ||
621 | * | ||
622 | * \sa LogLevel | ||
623 | * \sa levelFromString() | ||
624 | */ | ||
625 | QString Logger::levelToString(Logger::LogLevel logLevel) | ||
626 | { | ||
627 | switch (logLevel) | ||
628 | { | ||
629 | case Trace: | ||
630 | return QLatin1String("Trace"); | ||
631 | case Debug: | ||
632 | return QLatin1String("Debug"); | ||
633 | case Info: | ||
634 | return QLatin1String("Info"); | ||
635 | case Warning: | ||
636 | return QLatin1String("Warning"); | ||
637 | case Error: | ||
638 | return QLatin1String("Error"); | ||
639 | case Fatal: | ||
640 | return QLatin1String("Fatal"); | ||
641 | } | ||
642 | |||
643 | return QString(); | ||
644 | } | ||
645 | |||
646 | |||
647 | //! Converts the LogLevel string representation to enum value | ||
648 | /** | ||
649 | * Comparation of the strings is case independent. If the log level string provided cannot be understood | ||
650 | * Logger::Debug is returned. | ||
651 | * | ||
652 | * \param s String to be decoded | ||
653 | * | ||
654 | * \sa LogLevel | ||
655 | * \sa levelToString() | ||
656 | */ | ||
657 | Logger::LogLevel Logger::levelFromString(const QString& s) | ||
658 | { | ||
659 | QString str = s.trimmed().toLower(); | ||
660 | |||
661 | LogLevel result = Debug; | ||
662 | |||
663 | if (str == QLatin1String("trace")) | ||
664 | result = Trace; | ||
665 | else if (str == QLatin1String("debug")) | ||
666 | result = Debug; | ||
667 | else if (str == QLatin1String("info")) | ||
668 | result = Info; | ||
669 | else if (str == QLatin1String("warning")) | ||
670 | result = Warning; | ||
671 | else if (str == QLatin1String("error")) | ||
672 | result = Error; | ||
673 | else if (str == QLatin1String("fatal")) | ||
674 | result = Fatal; | ||
675 | |||
676 | return result; | ||
677 | } | ||
678 | |||
679 | |||
680 | //! Returns the global instance of Logger | ||
681 | /** | ||
682 | * In a most cases you shouldn't use this function directly. Consider using [cuteLogger](@ref cuteLogger) macro instead. | ||
683 | * | ||
684 | * \sa cuteLogger | ||
685 | */ | ||
686 | Logger* Logger::globalInstance() | ||
687 | { | ||
688 | Logger* result = nullptr; | ||
689 | { | ||
690 | QReadLocker locker(&LoggerPrivate::globalInstanceLock); | ||
691 | result = LoggerPrivate::globalInstance; | ||
692 | } | ||
693 | |||
694 | if (!result) | ||
695 | { | ||
696 | QWriteLocker locker(&LoggerPrivate::globalInstanceLock); | ||
697 | LoggerPrivate::globalInstance = new Logger; | ||
698 | |||
699 | #if QT_VERSION >= 0x050000 | ||
700 | qInstallMessageHandler(qtLoggerMessageHandler); | ||
701 | #else | ||
702 | qInstallMsgHandler(qtLoggerMessageHandler); | ||
703 | #endif | ||
704 | qAddPostRoutine(cleanupLoggerGlobalInstance); | ||
705 | result = LoggerPrivate::globalInstance; | ||
706 | } | ||
707 | |||
708 | return result; | ||
709 | } | ||
710 | |||
711 | |||
712 | //! Registers the appender to write the log records to | ||
713 | /** | ||
714 | * On the log writing call (using one of the macros or the write() function) Logger traverses through the list of | ||
715 | * the appenders and writes a log records to the each of them. Please, look through the AbstractAppender | ||
716 | * documentation to understand the concept of appenders. | ||
717 | * | ||
718 | * If no appenders was added to Logger, it falls back to logging into the \c std::cerr STL stream. | ||
719 | * | ||
720 | * \param appender Appender to register in the Logger | ||
721 | * | ||
722 | * \note Logger takes ownership on the appender and it will delete it on the application exit. According to this, | ||
723 | * appenders must be created on heap to prevent double destruction of the appender. | ||
724 | * | ||
725 | * \sa registerCategoryAppender | ||
726 | * \sa AbstractAppender | ||
727 | */ | ||
728 | void Logger::registerAppender(AbstractAppender* appender) | ||
729 | { | ||
730 | Q_D(Logger); | ||
731 | |||
732 | QMutexLocker locker(&d->loggerMutex); | ||
733 | |||
734 | if (!d->appenders.contains(appender)) | ||
735 | d->appenders.append(appender); | ||
736 | else | ||
737 | std::cerr << "Trying to register appender that was already registered" << std::endl; | ||
738 | } | ||
739 | |||
740 | //! Registers the appender to write the log records to the specific category | ||
741 | /** | ||
742 | * Calling this method, you can link some appender with the named category. | ||
743 | * On the log writing call to the specific category (calling write() with category parameter directly, | ||
744 | * writing to the default category, or using special LOG_CDEBUG(), LOG_CWARNING() etc. macros), | ||
745 | * Logger writes the log message only to the list of registered category appenders. | ||
746 | * | ||
747 | * You can call logToGlobalInstance() to pass all category log messages to the global logger instance appenders | ||
748 | * (registered using registerAppender()). | ||
749 | * If no category appenders with specific name was registered to the Logger, | ||
750 | * it falls back to logging into the \c std::cerr STL stream, both with simple warning message. | ||
751 | * | ||
752 | * \param category Category name | ||
753 | * \param appender Appender to register in the Logger | ||
754 | * | ||
755 | * \note Logger takes ownership on the appender and it will delete it on the application exit. According to this, | ||
756 | * appenders must be created on heap to prevent double destruction of the appender. | ||
757 | * | ||
758 | * \sa registerAppender | ||
759 | * \sa LOG_CTRACE(), LOG_CDEBUG(), LOG_CINFO(), LOG_CWARNING(), LOG_CERROR(), LOG_CFATAL() | ||
760 | * \sa LOG_CATEGORY(), LOG_GLOBAL_CATEGORY() | ||
761 | * \sa logToGlobalInstance | ||
762 | * \sa setDefaultCategory | ||
763 | */ | ||
764 | void Logger::registerCategoryAppender(const QString& category, AbstractAppender* appender) | ||
765 | { | ||
766 | Q_D(Logger); | ||
767 | |||
768 | QMutexLocker locker(&d->loggerMutex); | ||
769 | |||
770 | if (!d->categoryAppenders.values().contains(appender)) | ||
771 | d->categoryAppenders.insert(category, appender); | ||
772 | else | ||
773 | std::cerr << "Trying to register appender that was already registered" << std::endl; | ||
774 | } | ||
775 | |||
776 | |||
777 | //! Removes the registered appender from logger | ||
778 | /** | ||
779 | * After calling this function logger stops writing any of the records to the appender. | ||
780 | * | ||
781 | * \param appender Pointer to appender to remove from logger | ||
782 | * \note Removed appender will not be deleted on the application shutdown and you will need to destroy the object | ||
783 | * yourself. | ||
784 | * \sa registerAppender | ||
785 | */ | ||
786 | void Logger::removeAppender(AbstractAppender* appender) | ||
787 | { | ||
788 | Q_D(Logger); | ||
789 | |||
790 | QMutexLocker locker(&d->loggerMutex); | ||
791 | |||
792 | d->appenders.removeAll(appender); | ||
793 | for (QMultiMap<QString,AbstractAppender*>::iterator it = d->categoryAppenders.begin(); it != d->categoryAppenders.end();) | ||
794 | { | ||
795 | if (it.value() == appender) | ||
796 | it = d->categoryAppenders.erase(it); | ||
797 | else | ||
798 | ++it; | ||
799 | } | ||
800 | } | ||
801 | |||
802 | |||
803 | //! Sets default logging category | ||
804 | /** | ||
805 | * All log messages to this category appenders will also be written to general logger instance appenders (registered | ||
806 | * using [registerAppender](@ref registerAppender) method), and vice versa. | ||
807 | * In particular, any calls to the LOG_DEBUG() macro will be treated as category logging, | ||
808 | * so you needn't to specify category name using LOG_CDEBUG() macro. | ||
809 | * | ||
810 | * To unset the default category, pass a null string as a parameter. | ||
811 | * | ||
812 | * \param category Category name | ||
813 | * | ||
814 | * \note "category" format marker will be set to the category name for all of these messages | ||
815 | * (see [AbstractStringAppender::setFormat](@ref AbstractStringAppender::setFormat)). | ||
816 | * | ||
817 | * \sa defaultCategory() | ||
818 | * \sa registerCategoryAppender() | ||
819 | * \sa logToGlobalInstance() | ||
820 | */ | ||
821 | void Logger::setDefaultCategory(const QString& category) | ||
822 | { | ||
823 | Q_D(Logger); | ||
824 | |||
825 | QMutexLocker locker(&d->loggerMutex); | ||
826 | |||
827 | d->defaultCategory = category; | ||
828 | } | ||
829 | |||
830 | //! Returns default logging category name | ||
831 | /** | ||
832 | * \sa setDefaultCategory | ||
833 | */ | ||
834 | QString Logger::defaultCategory() const | ||
835 | { | ||
836 | Q_D(const Logger); | ||
837 | return d->defaultCategory; | ||
838 | } | ||
839 | |||
840 | //! Links some logging category with the global logger instance appenders. | ||
841 | /** | ||
842 | * If set to true, all log messages to the specified category appenders will also be written to the global logger instance appenders, | ||
843 | * registered using registerAppender(). | ||
844 | * | ||
845 | * By default, all messages to the specific category are written only to the specific category appenders | ||
846 | * (registered using registerCategoryAppender()). | ||
847 | * | ||
848 | * \param category Category name | ||
849 | * \param logToGlobal Link or onlink the category from global logger instance appender | ||
850 | * | ||
851 | * \sa globalInstance | ||
852 | * \sa registerAppender | ||
853 | * \sa registerCategoryAppender | ||
854 | */ | ||
855 | void Logger::logToGlobalInstance(const QString& category, bool logToGlobal) | ||
856 | { | ||
857 | Q_D(Logger); | ||
858 | |||
859 | if (this == globalInstance()) | ||
860 | { | ||
861 | QMutexLocker locker(&d->loggerMutex); | ||
862 | d->categories.insert(category, logToGlobal); | ||
863 | } | ||
864 | else | ||
865 | { | ||
866 | globalInstance()->logToGlobalInstance(category, logToGlobal); | ||
867 | } | ||
868 | } | ||
869 | |||
870 | |||
871 | void Logger::write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category, | ||
872 | const QString& message, bool fromLocalInstance) | ||
873 | { | ||
874 | Q_D(Logger); | ||
875 | |||
876 | QMutexLocker locker(&d->loggerMutex); | ||
877 | |||
878 | QString logCategory = QString::fromLatin1(category); | ||
879 | if (logCategory.isNull() && !d->defaultCategory.isNull()) | ||
880 | logCategory = d->defaultCategory; | ||
881 | |||
882 | bool wasWritten = false; | ||
883 | bool isGlobalInstance = this == globalInstance(); | ||
884 | bool linkedToGlobal = isGlobalInstance && d->categories.value(logCategory, false); | ||
885 | |||
886 | if (!logCategory.isNull()) | ||
887 | { | ||
888 | QList<AbstractAppender*> appenders = d->categoryAppenders.values(logCategory); | ||
889 | if (appenders.length() == 0) | ||
890 | { | ||
891 | if (logCategory != d->defaultCategory && !linkedToGlobal && !fromLocalInstance && !d->noAppendersCategories.contains(logCategory)) | ||
892 | { | ||
893 | std::cerr << "No appenders associated with category " << qPrintable(logCategory) << std::endl; | ||
894 | d->noAppendersCategories.append(logCategory); | ||
895 | } | ||
896 | } | ||
897 | else | ||
898 | { | ||
899 | foreach (AbstractAppender* appender, appenders) | ||
900 | appender->write(timeStamp, logLevel, file, line, function, logCategory, message); | ||
901 | wasWritten = true; | ||
902 | } | ||
903 | } | ||
904 | |||
905 | // the default category is linked to the main logger appenders | ||
906 | // global logger instance also writes all linked categories to the main appenders | ||
907 | if (logCategory.isNull() || logCategory == d->defaultCategory || linkedToGlobal) | ||
908 | { | ||
909 | if (!d->appenders.isEmpty()) | ||
910 | { | ||
911 | foreach (AbstractAppender* appender, d->appenders) | ||
912 | appender->write(timeStamp, logLevel, file, line, function, logCategory, message); | ||
913 | wasWritten = true; | ||
914 | } | ||
915 | else | ||
916 | { | ||
917 | static bool noAppendersWarningShown = false; | ||
918 | if (!noAppendersWarningShown) | ||
919 | { | ||
920 | #if defined(Q_OS_ANDROID) | ||
921 | __android_log_write(ANDROID_LOG_WARN, "Logger", "No appenders registered with logger"); | ||
922 | #else | ||
923 | std::cerr << "No appenders registered with logger" << std::endl; | ||
924 | #endif | ||
925 | noAppendersWarningShown = true; | ||
926 | } | ||
927 | } | ||
928 | } | ||
929 | |||
930 | // local logger instances send category messages to the global instance | ||
931 | if (!isGlobalInstance) | ||
932 | { | ||
933 | if (!logCategory.isNull()) | ||
934 | { | ||
935 | globalInstance()->write(timeStamp, logLevel, file, line, function, logCategory.toLatin1(), message, true); | ||
936 | wasWritten = true; | ||
937 | } | ||
938 | |||
939 | if (d->writeDefaultCategoryToGlobalInstance && logCategory == d->defaultCategory) | ||
940 | { | ||
941 | globalInstance()->write(timeStamp, logLevel, file, line, function, nullptr, message, true); | ||
942 | wasWritten = true; | ||
943 | } | ||
944 | } | ||
945 | |||
946 | if (!wasWritten && !fromLocalInstance) | ||
947 | { | ||
948 | // Fallback | ||
949 | #if defined(Q_OS_ANDROID) | ||
950 | QString result = QString(QLatin1String("<%2> %3")).arg(AbstractStringAppender::stripFunctionName(function)).arg(message); | ||
951 | __android_log_write(AndroidAppender::androidLogPriority(logLevel), "Logger", qPrintable(result)); | ||
952 | #else | ||
953 | QString result = QString(QLatin1String("[%1] <%2> %3")).arg(levelToString(logLevel), -7) | ||
954 | .arg(AbstractStringAppender::stripFunctionName(function)).arg(message); | ||
955 | std::cerr << qPrintable(result) << std::endl; | ||
956 | #endif | ||
957 | } | ||
958 | |||
959 | if (logLevel == Logger::Fatal) | ||
960 | abort(); | ||
961 | } | ||
962 | |||
963 | |||
964 | //! Writes the log record | ||
965 | /** | ||
966 | * Writes the log records with the supplied arguments to all the registered appenders. | ||
967 | * | ||
968 | * \note It is not recommended to call this function directly. Instead of this you can just call one of the macros | ||
969 | * (LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL) that will supply all the needed | ||
970 | * information to this function. | ||
971 | * | ||
972 | * \param timeStamp - the time stamp of the record | ||
973 | * \param logLevel - the log level of the record | ||
974 | * \param file - the name of the source file that requested the log record | ||
975 | * \param line - the line of the code of source file that requested the log record | ||
976 | * \param function - name of the function that requested the log record | ||
977 | * \param category - logging category (0 for default category) | ||
978 | * \param message - log message | ||
979 | * | ||
980 | * \note Recording of the log record using the Logger::Fatal log level will lead to calling the STL abort() | ||
981 | * function, which will interrupt the running of your software and begin the writing of the core dump. | ||
982 | * | ||
983 | * \sa LogLevel | ||
984 | * \sa LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARNING, LOG_ERROR, LOG_FATAL | ||
985 | * \sa AbstractAppender | ||
986 | */ | ||
987 | void Logger::write(const QDateTime& timeStamp, LogLevel logLevel, const char* file, int line, const char* function, const char* category, | ||
988 | const QString& message) | ||
989 | { | ||
990 | write(timeStamp, logLevel, file, line, function, category, message, /* fromLocalInstance = */ false); | ||
991 | } | ||
992 | |||
993 | /** | ||
994 | * This is the overloaded function provided for the convinience. It behaves similar to the above function. | ||
995 | * | ||
996 | * This function uses the current timestamp obtained with \c QDateTime::currentDateTime(). | ||
997 | * | ||
998 | * \sa write() | ||
999 | */ | ||
1000 | void Logger::write(LogLevel logLevel, const char* file, int line, const char* function, const char* category, | ||
1001 | const QString& message) | ||
1002 | { | ||
1003 | write(QDateTime::currentDateTime(), logLevel, file, line, function, category, message); | ||
1004 | } | ||
1005 | |||
1006 | |||
1007 | //! Writes the assertion | ||
1008 | /** | ||
1009 | * This function writes the assertion record using the write() function. | ||
1010 | * | ||
1011 | * The assertion record is always written using the Logger::Fatal log level which leads to the abortation of the | ||
1012 | * program and generation of the core dump (if supported). | ||
1013 | * | ||
1014 | * The message written to the appenders will be identical to the \c condition argument prefixed with the | ||
1015 | * <tt>ASSERT:</tt> notification. | ||
1016 | * | ||
1017 | * \note It is not recommended to call this function directly. Instead of this you can just call the LOG_ASSERT | ||
1018 | * macro that will supply all the needed information to this function. | ||
1019 | * | ||
1020 | * \sa LOG_ASSERT | ||
1021 | * \sa write() | ||
1022 | */ | ||
1023 | void Logger::writeAssert(const char* file, int line, const char* function, const char* condition) | ||
1024 | { | ||
1025 | write(Logger::Fatal, file, line, function, nullptr, QString("ASSERT: \"%1\"").arg(condition)); | ||
1026 | } | ||
1027 | |||
1028 | |||
1029 | Logger* cuteLoggerInstance() | ||
1030 | { | ||
1031 | return Logger::globalInstance(); | ||
1032 | } | ||
1033 | |||
1034 | |||
1035 | |||
1036 | void LoggerTimingHelper::start(const char* msg, ...) | ||
1037 | { | ||
1038 | va_list va; | ||
1039 | va_start(va, msg); | ||
1040 | #if QT_VERSION >= 0x050500 | ||
1041 | m_block = QString().vasprintf(msg, va); | ||
1042 | #else | ||
1043 | m_block = QString().vsprintf(msg, va); | ||
1044 | #endif | ||
1045 | va_end(va); | ||
1046 | |||
1047 | m_time.start(); | ||
1048 | } | ||
1049 | |||
1050 | |||
1051 | void LoggerTimingHelper::start(const QString& block) | ||
1052 | { | ||
1053 | m_block = block; | ||
1054 | m_time.start(); | ||
1055 | } | ||
1056 | |||
1057 | |||
1058 | void LoggerTimingHelper::start(Logger::TimingMode mode, const QString& block) | ||
1059 | { | ||
1060 | m_timingMode = mode; | ||
1061 | m_block = block; | ||
1062 | m_time.start(); | ||
1063 | } | ||
1064 | |||
1065 | |||
1066 | LoggerTimingHelper::~LoggerTimingHelper() | ||
1067 | { | ||
1068 | QString message; | ||
1069 | if (m_block.isEmpty()) | ||
1070 | message = QString(QLatin1String("Function %1 finished in ")).arg(AbstractStringAppender::stripFunctionName(m_function)); | ||
1071 | else | ||
1072 | message = QString(QLatin1String("\"%1\" finished in ")).arg(m_block); | ||
1073 | |||
1074 | qint64 elapsed = m_time.elapsed(); | ||
1075 | if (elapsed >= 10000 && m_timingMode == Logger::TimingAuto) | ||
1076 | message += QString(QLatin1String("%1 s.")).arg(elapsed / 1000); | ||
1077 | else | ||
1078 | message += QString(QLatin1String("%1 ms.")).arg(elapsed); | ||
1079 | |||
1080 | m_logger->write(m_logLevel, m_file, m_line, m_function, nullptr, message); | ||
1081 | } | ||
1082 | |||
1083 | |||
1084 | CuteMessageLogger::~CuteMessageLogger() | ||
1085 | { | ||
1086 | m_l->write(m_level, m_file, m_line, m_function, m_category, m_message); | ||
1087 | } | ||
1088 | |||
1089 | void CuteMessageLogger::write(const char* msg, ...) | ||
1090 | { | ||
1091 | va_list va; | ||
1092 | va_start(va, msg); | ||
1093 | m_message = QString::vasprintf(msg, va); | ||
1094 | va_end(va); | ||
1095 | } | ||
1096 | |||
1097 | |||
1098 | void CuteMessageLogger::write(const QString& msg) | ||
1099 | { | ||
1100 | m_message = msg; | ||
1101 | } | ||
1102 | |||
1103 | |||
1104 | QDebug CuteMessageLogger::write() | ||
1105 | { | ||
1106 | QDebug d(&m_message); | ||
1107 | return d; | ||
1108 | } | ||
diff --git a/utils/rbutilqt/logger/src/OutputDebugAppender.cpp b/utils/rbutilqt/logger/src/OutputDebugAppender.cpp new file mode 100644 index 0000000000..44d640512f --- /dev/null +++ b/utils/rbutilqt/logger/src/OutputDebugAppender.cpp | |||
@@ -0,0 +1,43 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Karl-Heinz Reichel (khreichel at googlemail dot com) | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License version 2.1 | ||
6 | as published by the Free Software Foundation and appearing in the file | ||
7 | LICENSE.LGPL included in the packaging of this file. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | */ | ||
14 | // Local | ||
15 | #include "OutputDebugAppender.h" | ||
16 | |||
17 | // STL | ||
18 | #include <windows.h> | ||
19 | |||
20 | |||
21 | /** | ||
22 | * \class OutputDebugAppender | ||
23 | * | ||
24 | * \brief Appender that writes the log records to the Microsoft Debug Log | ||
25 | */ | ||
26 | |||
27 | |||
28 | //! Writes the log record to the windows debug log. | ||
29 | /** | ||
30 | * \sa AbstractStringAppender::format() | ||
31 | */ | ||
32 | void OutputDebugAppender::append(const QDateTime& timeStamp, | ||
33 | Logger::LogLevel logLevel, | ||
34 | const char* file, | ||
35 | int line, | ||
36 | const char* function, | ||
37 | const QString& category, | ||
38 | const QString& message) | ||
39 | { | ||
40 | QString s = formattedString(timeStamp, logLevel, file, line, function, category, message); | ||
41 | OutputDebugStringW((LPCWSTR) s.utf16()); | ||
42 | } | ||
43 | |||