QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
GStreamerLogging.cc
Go to the documentation of this file.
1#include "GStreamerLogging.h"
2
3#include <algorithm>
4#include <atomic>
5
6#include <QtCore/QString>
7
8#include "GStreamer.h"
9#include "GStreamerHelpers.h"
10#include "QGCLoggingCategory.h"
11
12QGC_LOGGING_CATEGORY(GStreamerLoggingLog, "Video.GStreamer.GStreamerLogging")
13QGC_LOGGING_CATEGORY_ON(GStreamerAPILog, "Video.GStreamer.GStreamerAPI")
14
15namespace {
16
17std::atomic<bool> s_externalPluginLoaderFailed = false;
18
19#ifdef QT_DEBUG
20constexpr int kMaxBridgedLevel = GST_LEVEL_MEMDUMP;
21#else
22// LOG and finer would flood Qt logging in release builds; GStreamer's own output (GST_DEBUG) is not capped.
23constexpr int kMaxBridgedLevel = GST_LEVEL_DEBUG;
24#endif
25
26void glib_print_handler(const gchar* string)
27{
28 qCInfo(GStreamerLoggingLog) << string;
29}
30
31void glib_printerr_handler(const gchar* string)
32{
33 qCWarning(GStreamerLoggingLog) << string;
34}
35
36void glib_log_handler(const gchar* log_domain, GLogLevelFlags log_level, const gchar* message, gpointer user_data)
37{
38 Q_UNUSED(user_data);
39 const QString domain = log_domain ? QString::fromUtf8(log_domain) : QStringLiteral("GLib");
40 const QString msg = QString::fromUtf8(message);
41
42 if (msg.contains(QStringLiteral("External plugin loader failed"))) {
43 s_externalPluginLoaderFailed.store(true);
44 }
45
46 if (msg.contains(QStringLiteral("pygobject initialization failed"), Qt::CaseInsensitive)) {
47 qCDebug(GStreamerLoggingLog) << domain << msg;
48 return;
49 }
50
51 switch (log_level & G_LOG_LEVEL_MASK) {
52 case G_LOG_LEVEL_ERROR:
53 case G_LOG_LEVEL_CRITICAL:
54 qCCritical(GStreamerLoggingLog) << domain << msg;
55 break;
56 case G_LOG_LEVEL_WARNING:
57 qCWarning(GStreamerLoggingLog) << domain << msg;
58 break;
59 case G_LOG_LEVEL_MESSAGE:
60 case G_LOG_LEVEL_INFO:
61 qCInfo(GStreamerLoggingLog) << domain << msg;
62 break;
63 case G_LOG_LEVEL_DEBUG:
64 default:
65 qCDebug(GStreamerLoggingLog) << domain << msg;
66 break;
67 }
68}
69
71void applyDebugLevel(int level)
72{
73 const int maxLevel = GStreamer::nativeDebugLogRequested() ? static_cast<int>(GST_LEVEL_MEMDUMP) : kMaxBridgedLevel;
74 const int clamped = std::clamp(level, 0, maxLevel);
75 gst_debug_set_default_threshold(static_cast<GstDebugLevel>(clamped));
76 qCDebug(GStreamerLoggingLog) << "GStreamer debug threshold set to" << clamped;
77}
78
79} // anonymous namespace
80
81namespace GStreamer {
82
84{
85 return s_externalPluginLoaderFailed.load();
86}
87
89{
90 s_externalPluginLoaderFailed.store(false);
91}
92
94{
95 g_set_print_handler(glib_print_handler);
96 g_set_printerr_handler(glib_printerr_handler);
97 g_log_set_default_handler(glib_log_handler, nullptr);
98}
99
101{
102 return !qEnvironmentVariableIsEmpty("GST_DEBUG") || !qEnvironmentVariableIsEmpty("GST_DEBUG_FILE") ||
103 !qEnvironmentVariableIsEmpty("GST_TRACERS");
104}
105
106void qtGstLog(GstDebugCategory* category, GstDebugLevel level, const gchar* file, const gchar* function, gint line,
107 GObject* object, GstDebugMessage* message, gpointer data)
108{
109 Q_UNUSED(category);
110 Q_UNUSED(data);
111
112 // gst_debug_log() has already applied the category threshold.
113 QtMsgType type = QtDebugMsg;
114 switch (level) {
115 case GST_LEVEL_ERROR:
116 type = QtCriticalMsg;
117 break;
118 case GST_LEVEL_WARNING:
119 type = QtWarningMsg;
120 break;
121 case GST_LEVEL_FIXME:
122 case GST_LEVEL_INFO:
123 type = QtInfoMsg;
124 break;
125 default:
126 break;
127 }
128 if (!GStreamerAPILog().isEnabled(type)) {
129 return;
130 }
131
132 QString source;
133 if (object) {
134 const GStreamer::GCharPtr objectInfo =
135 GStreamer::adoptGChar(gst_info_strdup_printf("%" GST_PTR_FORMAT " ", object));
136 source = QString::fromUtf8(objectInfo.get());
137 }
138#if GST_CHECK_VERSION(1, 22, 0)
139 // A line logged with GST_*_ID names its source by an id instead of an object.
140 else if (const gchar* const id = gst_debug_message_get_id(message); id && (id[0] != '\0')) {
141 source = QLatin1Char('<') + QString::fromUtf8(id) + QLatin1String("> ");
142 }
143#endif
144 qt_message_output(type, QMessageLogContext(file, line, function, GStreamerAPILog().categoryName()),
145 source + QString::fromUtf8(gst_debug_message_get(message)));
146}
147
148void configureDebugLogging(std::optional<int> level)
149{
151 // Keep GStreamer's own output; a bridge left by an earlier call would duplicate every line.
152 gst_debug_remove_log_function(GStreamer::qtGstLog);
153 } else {
154 gst_debug_remove_log_function(gst_debug_log_default);
155 gst_debug_remove_log_function(GStreamer::qtGstLog);
156 gst_debug_add_log_function(GStreamer::qtGstLog, nullptr, nullptr);
157 }
158
159 if (!qEnvironmentVariableIsEmpty("GST_DEBUG")) {
160 return;
161 }
162
163 // A GStreamer development build (nano > 0, as the official 1.28.8 Windows SDK is) defaults to GST_LEVEL_ERROR
164 // rather than GST_LEVEL_NONE, so the setting's default is applied explicitly.
165 applyDebugLevel(level.value_or(GST_LEVEL_NONE));
166}
167
168void setDebugLevel(int level)
169{
170 if (!gst_is_initialized()) {
171 return;
172 }
173 applyDebugLevel(level);
174}
175
176} // namespace GStreamer
#define QGC_LOGGING_CATEGORY_ON(name, categoryStr)
#define QGC_LOGGING_CATEGORY(name, categoryStr)
GCharPtr adoptGChar(gchar *str) noexcept
Definition GstScoped.h:134
void qtGstLog(GstDebugCategory *category, GstDebugLevel level, const gchar *file, const gchar *function, gint line, GObject *object, GstDebugMessage *message, gpointer data)
void setDebugLevel(int level)
bool externalPluginLoaderFailed()
void resetExternalPluginLoaderFailed()
void redirectGLibLogging()
bool nativeDebugLogRequested()
void configureDebugLogging(std::optional< int > level)
std::unique_ptr< gchar, GFreeDeleter > GCharPtr
Definition GstScoped.h:71