FOSSredder 0.5.0
Loading...
Searching...
No Matches
Trace.cpp
Go to the documentation of this file.
1
5
7
8#include <QDebug>
9#include <QProcessEnvironment>
10#include <QString>
11#include <QStringList>
12
13namespace ui::observability {
14namespace {
15
16bool debugBuild() {
17#if defined(QT_DEBUG) || defined(_DEBUG) || !defined(NDEBUG)
18 return true;
19#else
20 return false;
21#endif
22}
23
24bool envAllowsTrace() {
25 const auto env = QProcessEnvironment::systemEnvironment();
26 const QString value = env.value(QStringLiteral("FOSSREDDER_UI_TRACE"));
27 if (value.isEmpty()) {
28 return true;
29 }
30
31 const bool explicitlyDisabled =
32 value.compare(QStringLiteral("0"), Qt::CaseInsensitive) == 0 ||
33 value.compare(QStringLiteral("false"), Qt::CaseInsensitive) == 0 ||
34 value.compare(QStringLiteral("off"), Qt::CaseInsensitive) == 0;
35 if (explicitlyDisabled) {
36 return false;
37 }
38
39 return value.compare(QStringLiteral("1"), Qt::CaseInsensitive) == 0 ||
40 value.compare(QStringLiteral("true"), Qt::CaseInsensitive) == 0 ||
41 value.compare(QStringLiteral("on"), Qt::CaseInsensitive) == 0;
42}
43
44QString contextText(const core::errors::ErrorContext& context) {
45 QStringList parts;
46 parts.reserve(static_cast<int>(context.size()));
47 for (const auto& [key, value] : context) {
48 parts.push_back(QStringLiteral("%1=%2").arg(QString::fromStdString(key),
49 QString::fromStdString(value)));
50 }
51 return parts.join(QStringLiteral(" "));
52}
53
54} // namespace
55
57 return debugBuild() && envAllowsTrace();
58}
59
60void trace(const char* layer, const char* origin, std::string message,
62 if (!isTraceEnabled()) {
63 return;
64 }
65
66 const QString layerText = QString::fromUtf8(layer ? layer : "ui");
67 const QString originText = QString::fromUtf8(origin ? origin : "unknown");
68 QString line = QStringLiteral("[ui.trace] %1 %2: %3")
69 .arg(layerText, originText,
70 QString::fromStdString(std::move(message)));
71 const QString details = contextText(context);
72 if (!details.isEmpty()) {
73 line += QStringLiteral(" | %1").arg(details);
74 }
75 qInfo().noquote() << line;
76}
77
78} // namespace ui::observability
Declarations for the UI Trace component.
std::vector< std::pair< std::string, std::string > > ErrorContext
Key/value context attached to an error event.
Definition ErrorEvent.h:27
bool isTraceEnabled()
Reports whether UI trace emission is enabled.
Definition Trace.cpp:56
void trace(const char *layer, const char *origin, std::string message, core::errors::ErrorContext context={})
Emits a UI trace event.
Definition Trace.cpp:60