comparison Utils/LogConsole.cpp @ 1:0b3630a29ad8

Initial version based on previous repository. Project was ported to QT6 and in now cmake based.
author Ideenmodellierer <tiefenrauscher@web.de>
date Thu, 27 Nov 2025 18:40:28 +0100
parents
children
comparison
equal deleted inserted replaced
0:76ccd6ce50c0 1:0b3630a29ad8
1 //////////////////////////////////////////////////////////////////////////////
2 /// \file LogConsole.cpp
3 /// \brief Put Log message onto standard console.
4 /// \author JD Gascuel.
5 /// \copyright (c) 2011-2016 JD Gascuel. All rights reserved.
6 /// $Id$
7 //////////////////////////////////////////////////////////////////////////////
8 //
9 // BSD 2-Clause License:
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions
13 // are met:
14 //
15 // 1. Redistributions of source code must retain the above copyright notice,
16 // this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 // THE POSSIBILITY OF SUCH DAMAGE.
33 //
34 //////////////////////////////////////////////////////////////////////////////
35
36 #include "LogConsole.h"
37
38 #include "Log.h"
39
40 //#include <QRegExp>
41 #include <QRegularExpression>
42
43 #include <stdio.h>
44
45 #ifdef Q_OS_WIN32
46 # include <windows.h>
47 # include <io.h>
48 # include <fcntl.h>
49 # include <iostream>
50 #endif
51
52 //////////////////////////////////////////////////////////////////////////////
53
54 // Nothing to do ...
55 LogConsole::LogConsole(int argc, char *argv[])
56 : LogAppender(argc, argv)
57 {
58 setMinLevel(defaultMinLevel());
59
60 for(int i=1; i<argc; ++i)
61 {
62 if( strcmp("-dd", argv[i]) == 0 )
63 setMinLevel(Log::LEVEL_DEBUG);
64 else if( strcmp("-d", argv[i]) == 0 )
65 setMinLevel(Log::LEVEL_TRACE);
66 }
67 }
68
69 LogConsole::~LogConsole()
70 {}
71
72 //////////////////////////////////////////////////////////////////////////////
73
74 const char *LogConsole::type() const
75 {
76 return "Console";
77 }
78
79 Log::Level LogConsole::defaultMinLevel() const
80 {
81 #ifdef DEBUG
82 return Log::LEVEL_TRACE;
83 #else
84 return Log::LEVEL_INFO;
85 #endif
86 }
87
88 const char *LogConsole::defaultFormat() const
89 {
90 return "%t %m";
91 }
92
93 void LogConsole::operator()(const Log& log)
94 {
95 QString msg = format(log);
96
97 // 2016-04-19 jDG:
98 // Remove bad tags from console output... but keep it nice:
99 #if 0
100 msg.replace("<br>", "\n")
101 .replace(QRegExp("</?table[^>]*>"), "--------------------------------")
102 .replace(QRegExp("</tr>(?!\n)"), "\n")
103 .replace("</td>", "\t");
104
105 QRegExp re("</?[a-zA-Z]+[^>]*/?>");
106 while( msg.contains(re) )
107 msg.replace(re, "");
108 #endif
109 msg.replace("<br>", "\n")
110 .replace(QRegularExpression("</?table[^>]*>", QRegularExpression::CaseInsensitiveOption),
111 "--------------------------------")
112 .replace(QRegularExpression("</tr>(?!\n)"),
113 "\n")
114 .replace("</td>", "\t");
115
116 // allgemeiner Tag-Entferner
117 QRegularExpression re("</?[a-zA-Z]+[^>]*/?>");
118
119 while (re.match(msg).hasMatch()) {
120 msg.replace(re, "");
121 }
122 // 2014-03-21 jDG:
123 //
124 // In UNIX print everything in UTF-8, so that:
125 // * junitWrapper get it.
126 // * it is compatible with Linux console.
127 //
128 // In Windows, the console DOES NOT accept UTF_8, we just got giberrish...
129 // So defaults to Windows UNICODE which is Qt3's ucs2() or Qt4 toWCharArray()
130 //
131 // Note: it is correct for most Europe chars, but in corean (TCT example),
132 // the line is truncated.
133
134 #ifdef WIN32
135 wchar_t buffer[LOG_MAX_MESSAGE_LENGTH];
136 int len = msg.left(LOG_MAX_MESSAGE_LENGTH-2).toWCharArray(buffer);
137 buffer[len] = 0;
138 fprintf(stderr, "%S\n", buffer);
139 #else
140 QByteArray utf8 = msg.toUtf8();
141 fprintf(stderr, "%s\n", utf8.constData());
142 #endif
143 fflush(stderr);
144 }