Mercurial > public > ostc_companion
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Utils/LogConsole.cpp Thu Nov 27 18:40:28 2025 +0100 @@ -0,0 +1,144 @@ +////////////////////////////////////////////////////////////////////////////// +/// \file LogConsole.cpp +/// \brief Put Log message onto standard console. +/// \author JD Gascuel. +/// \copyright (c) 2011-2016 JD Gascuel. All rights reserved. +/// $Id$ +////////////////////////////////////////////////////////////////////////////// +// +// BSD 2-Clause License: +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// 1. Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// THE POSSIBILITY OF SUCH DAMAGE. +// +////////////////////////////////////////////////////////////////////////////// + +#include "LogConsole.h" + +#include "Log.h" + +//#include <QRegExp> +#include <QRegularExpression> + +#include <stdio.h> + +#ifdef Q_OS_WIN32 +# include <windows.h> +# include <io.h> +# include <fcntl.h> +# include <iostream> +#endif + +////////////////////////////////////////////////////////////////////////////// + +// Nothing to do ... +LogConsole::LogConsole(int argc, char *argv[]) + : LogAppender(argc, argv) +{ + setMinLevel(defaultMinLevel()); + + for(int i=1; i<argc; ++i) + { + if( strcmp("-dd", argv[i]) == 0 ) + setMinLevel(Log::LEVEL_DEBUG); + else if( strcmp("-d", argv[i]) == 0 ) + setMinLevel(Log::LEVEL_TRACE); + } +} + +LogConsole::~LogConsole() +{} + +////////////////////////////////////////////////////////////////////////////// + +const char *LogConsole::type() const +{ + return "Console"; +} + +Log::Level LogConsole::defaultMinLevel() const +{ +#ifdef DEBUG + return Log::LEVEL_TRACE; +#else + return Log::LEVEL_INFO; +#endif +} + +const char *LogConsole::defaultFormat() const +{ + return "%t %m"; +} + +void LogConsole::operator()(const Log& log) +{ + QString msg = format(log); + + // 2016-04-19 jDG: + // Remove bad tags from console output... but keep it nice: +#if 0 + msg.replace("<br>", "\n") + .replace(QRegExp("</?table[^>]*>"), "--------------------------------") + .replace(QRegExp("</tr>(?!\n)"), "\n") + .replace("</td>", "\t"); + + QRegExp re("</?[a-zA-Z]+[^>]*/?>"); + while( msg.contains(re) ) + msg.replace(re, ""); +#endif + msg.replace("<br>", "\n") + .replace(QRegularExpression("</?table[^>]*>", QRegularExpression::CaseInsensitiveOption), + "--------------------------------") + .replace(QRegularExpression("</tr>(?!\n)"), + "\n") + .replace("</td>", "\t"); + + // allgemeiner Tag-Entferner + QRegularExpression re("</?[a-zA-Z]+[^>]*/?>"); + + while (re.match(msg).hasMatch()) { + msg.replace(re, ""); + } + // 2014-03-21 jDG: + // + // In UNIX print everything in UTF-8, so that: + // * junitWrapper get it. + // * it is compatible with Linux console. + // + // In Windows, the console DOES NOT accept UTF_8, we just got giberrish... + // So defaults to Windows UNICODE which is Qt3's ucs2() or Qt4 toWCharArray() + // + // Note: it is correct for most Europe chars, but in corean (TCT example), + // the line is truncated. + +#ifdef WIN32 + wchar_t buffer[LOG_MAX_MESSAGE_LENGTH]; + int len = msg.left(LOG_MAX_MESSAGE_LENGTH-2).toWCharArray(buffer); + buffer[len] = 0; + fprintf(stderr, "%S\n", buffer); +#else + QByteArray utf8 = msg.toUtf8(); + fprintf(stderr, "%s\n", utf8.constData()); +#endif + fflush(stderr); +}
