ToolX 1.11.1 [e3f477f]
A GUI debug tool chain for diagnosing GVA systems
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1//
2// Copyright (c) 2026, Astute Systems PTY LTD
3//
4// This file is part of the VivoeX project developed by Astute Systems.
5//
6// License. See the LICENSE file in the project root for full license details.
7//
12
13#ifndef COMMON_LOGGING_H
14#define COMMON_LOGGING_H
15
16#include <QByteArray>
17#include <QString>
18#include <QtGlobal>
19
20#include <atomic>
21#include <cstdio>
22#include <cstdlib>
23#include <iostream>
24
25#ifdef BUILD_MEDIAX
26# include "utils/log.h"
27#endif
28
29namespace toolx::logging {
30
34inline std::atomic<bool> &verboseFlag()
35{
36 static std::atomic<bool> verbose{ false };
37 return verbose;
38}
39
43inline bool isVerbose()
44{
45 return verboseFlag().load();
46}
47
55inline void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &message)
56{
57 const char *level = "error";
58 switch (type) {
59 case QtDebugMsg:
60 level = "debug";
61 break;
62 case QtInfoMsg:
63 level = "info";
64 break;
65 case QtWarningMsg:
66 level = "warning";
67 break;
68 case QtCriticalMsg:
69 level = "error";
70 break;
71 case QtFatalMsg:
72 level = "fatal";
73 break;
74 }
75
76 const bool isError = (type == QtCriticalMsg || type == QtFatalMsg);
77 if (isError || isVerbose()) {
78 const QByteArray text = message.toLocal8Bit();
79 std::fprintf(stderr, "[%s] %s\n", level, text.constData());
80 std::fflush(stderr);
81 }
82 if (type == QtFatalMsg) {
83 std::abort();
84 }
85}
86
93inline void configure(bool verbose)
94{
95 verboseFlag().store(verbose);
96 qInstallMessageHandler(messageHandler);
97
98 // libva prints "libva info" lines to stderr unless told otherwise
99 if (!verbose && !qEnvironmentVariableIsSet("LIBVA_MESSAGING_LEVEL")) {
100 qputenv("LIBVA_MESSAGING_LEVEL", "0");
101 }
102
103 // Anything written through std::cout (including the MediaX log) goes to stderr
104 std::cout.rdbuf(std::cerr.rdbuf());
105#ifdef BUILD_MEDIAX
106 ::mediax::SetLogLevel(verbose ? ::mediax::LogLevel::kInfo : ::mediax::LogLevel::kQuiet);
107#endif
108}
109
115inline bool verboseRequested(int argc, char *argv[])
116{
117 for (int i = 1; i < argc; ++i) {
118 const QByteArray arg(argv[i]);
119 if (arg == "--verbose") {
120 return true;
121 }
122 }
123 return false;
124}
125
126} // namespace toolx::logging
127
128#endif // COMMON_LOGGING_H
bool verboseRequested(int argc, char *argv[])
Is –verbose in the command line (-v prints the version instead).
Definition logging.h:115
std::atomic< bool > & verboseFlag()
Storage for the verbose flag.
Definition logging.h:34
bool isVerbose()
Is verbose output enabled (-v / –verbose).
Definition logging.h:43
void configure(bool verbose)
Apply the logging policy, call once at start up before any library is initialised.
Definition logging.h:93
void messageHandler(QtMsgType type, const QMessageLogContext &, const QString &message)
Qt message handler, writes to stderr only.
Definition logging.h:55