Package: musescore2 / 2.3.2+dfsg4-16

experiments/our-message-handler.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Description: install a message handler for Qt
 If the QT_MESSAGE_PATTERN envvar is set (i.e. the user has defined
 their own log pattern), just skip loglevel debug messages unless
 running in debug mode. Otherwise (default), format messages in the
 Debian-præfixed style and with file/function/line number.
Author: mirabilos <tg@debian.org>
Forwarded: no

--- a/mscore/musescore.cpp
+++ b/mscore/musescore.cpp
@@ -2843,6 +2843,104 @@ static void mscoreMessageHandler(QtMsgTy
          abort();
          }
      }
+#else
+// helper
+static void
+mudeb_debmsgout(const void *s_)
+{
+	const unsigned char *s;
+	unsigned char c;
+
+	s = (const unsigned char *)s_;
+	if (!s || !*s) {
+		fputs("(nil)\n", stderr);
+		return;
+	}
+	do {
+		c = *s++;
+		putc(c, stderr);
+		if (c == '\n' && *s) {
+			putc('N', stderr);
+			putc(':', stderr);
+			putc(' ', stderr);
+		}
+	} while (*s);
+	if (c != '\n')
+		putc('\n', stderr);
+}
+
+// by default; Debian loglevel præficēs, file/func/line, silence debug unless enabled
+static void
+mudeb_debmsghandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
+{
+	QString s;
+	bool needsp;
+
+	switch (type) {
+	case QtDebugMsg:
+		if (!MScore::debugMode)
+			return;
+		putc('D', stderr);
+		break;
+	case QtWarningMsg:
+		putc('W', stderr);
+		break;
+	case QtCriticalMsg:
+	case QtFatalMsg:
+		putc('E', stderr);
+		break;
+	default:
+		putc('I', stderr);
+	}
+	putc(':', stderr);
+	if (ctx.category && strcmp(ctx.category, "default") != 0) {
+		putc(' ', stderr);
+		putc('<', stderr);
+		fputs(ctx.category, stderr);
+		putc('>', stderr);
+	}
+	needsp = true;
+	if (ctx.file) {
+		if (needsp) {
+			putc(' ', stderr);
+			needsp = false;
+		}
+		fputs(ctx.file, stderr);
+	}
+	if (ctx.function) {
+		if (needsp) {
+			putc(' ', stderr);
+			needsp = false;
+		}
+		putc('{', stderr);
+		fputs(ctx.function, stderr);
+		putc('}', stderr);
+	}
+	if (ctx.line && (ctx.file || ctx.function))
+		fprintf(stderr, ":%d", ctx.line);
+	if (!needsp)
+		putc(':', stderr);
+	putc(' ', stderr);
+	mudeb_debmsgout(qPrintable(msg));
+	fflush(stderr);
+}
+
+#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
+// if QT_MESSAGE_PATTERN is set: silence debug unless enabled
+static void
+mudeb_qtmsghandler(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
+{
+	if (type == QtDebugMsg && !MScore::debugMode)
+		return;
+
+	QString s = qFormatLogMessage(type, ctx, msg);
+	if (s.isNull())
+		return;
+	fputs(qPrintable(s), stderr);
+	putc('\n', stderr);
+	fflush(stderr);
+}
+#endif
 #endif
 
 //---------------------------------------------------------
@@ -5769,8 +5867,14 @@ int main(int argc, char* av[])
       QApplication::setDesktopSettingsAware(true);
 #if defined(QT_DEBUG) && defined(Q_OS_WIN)
       MScore::msgHandler = mscoreMessageHandler;
-      qInstallMessageHandler(mscoreMessageHandler);
+#else
+      MScore::msgHandler = mudeb_debmsghandler;
+#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
+      if (getenv("QT_MESSAGE_PATTERN") != NULL)
+            MScore::msgHandler = mudeb_qtmsghandler;
+#endif
 #endif
+      qInstallMessageHandler(MScore::msgHandler);
 
       QFile f(":/revision.h");
       f.open(QIODevice::ReadOnly);