Skip to content

Commit cbaa8eb

Browse files
committed
Extract debug_info class from main.cpp
1 parent cd1c148 commit cbaa8eb

File tree

4 files changed

+211
-183
lines changed

4 files changed

+211
-183
lines changed

lib/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ db_x509super.h pki_evp.h xfile.h
3636
dhgen.cpp dhgen.h XcaProgress.cpp
3737
XcaProgress.h XcaWarningCore.cpp XcaWarningCore.h
3838
PwDialogCore.cpp PwDialogCore.h digest.h
39-
digest.cpp pki_export.cpp
40-
func_base.cpp func_base.h
39+
digest.cpp pki_export.cpp debug_info.h
40+
func_base.cpp func_base.h debug_info.cpp
4141
)
4242

4343
macro(ExpandSources target)

lib/debug_info.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/* vi: set sw=4 ts=4:
2+
*
3+
* Copyright (C) 2023 Christian Hohnstaedt.
4+
*
5+
* All rights reserved.
6+
*/
7+
8+
#include <QStringList>
9+
#include <QDebug>
10+
#include <stdlib.h>
11+
12+
#include "debug_info.h"
13+
14+
QList<dbg_pattern> debug_info::patternlist;
15+
bool debug_info::all = false;
16+
17+
dbg_pattern::dbg_pattern(QString part)
18+
: first(0), last(INT_MAX), inv(false)
19+
{
20+
bool ok;
21+
if (part[0] == '-') {
22+
inv = true;
23+
part.remove(0, 1);
24+
}
25+
file = func = part;
26+
QStringList file_num = part.split(":");
27+
if (file_num.size() == 2) {
28+
file = file_num[0];
29+
file_num = file_num[1].split("-");
30+
if (file_num.size() == 1) {
31+
first = last = file_num[0].toUInt();
32+
} else {
33+
if (!file_num[0].isEmpty()) {
34+
first = file_num[0].toUInt(&ok);
35+
Q_ASSERT(ok);
36+
}
37+
if (!file_num[1].isEmpty()) {
38+
last = file_num[1].toUInt(&ok);
39+
Q_ASSERT(ok);
40+
}
41+
}
42+
}
43+
qDebug() << "New debug match" << (inv ? "Not" : "") << file << func << first << last;
44+
}
45+
46+
bool dbg_pattern::match(const QString &curr_file, const QString &curr_func,
47+
unsigned line) const
48+
{
49+
// QTextStream out(stdout);
50+
// out << QString("MATCH %1:%2(%3)\n").arg(curr_file).arg(curr_func).arg(line);
51+
if (curr_func == func)
52+
return true;
53+
if (curr_func.endsWith(QString("::%1").arg(func)))
54+
return true;
55+
if (curr_file != file && !file.endsWith(QString("/%1").arg(curr_file)))
56+
return false;
57+
if (line >= first && line <= last)
58+
return true;
59+
return false;
60+
}
61+
62+
void debug_info::set_debug(const QString &dbg)
63+
{
64+
bool local_all = false;
65+
all = true;
66+
if (isEmpty()) {
67+
foreach(QString part, dbg.split(",")) {
68+
if (part.toLower() == "all") {
69+
local_all = true;
70+
continue;
71+
}
72+
dbg_pattern d(part);
73+
patternlist.insert(d.invert() ? 0 : patternlist.size(), d);
74+
}
75+
}
76+
all = local_all;
77+
}
78+
79+
debug_info::debug_info(const QMessageLogContext &ctx)
80+
: line(0)
81+
{
82+
line = ctx.line;
83+
if (ctx.file && ctx.line) {
84+
int pos;
85+
short_file = ctx.file, short_func = ctx.function;
86+
pos = short_file.lastIndexOf("/");
87+
short_file.remove(0, pos +1);
88+
pos = short_func.indexOf("(");
89+
short_func.remove(pos, short_func.size());
90+
pos = short_func.lastIndexOf(" ");
91+
short_func.remove(0, pos +1);
92+
}
93+
//std::cerr << "DBG '" << (ctx.function ?: "(NULL)" )<< "' '" << CCHAR(short_func) << "' " << std::endl;
94+
}
95+
96+
QString debug_info::log_prefix() const
97+
{
98+
if (short_file == nullptr && line == 0)
99+
return QString();
100+
return QString(" " COL_MAGENTA "%1" COL_GREEN COL_BOLD ":%2 " COL_BLUE "%3")
101+
.arg(short_file).arg(line).arg(short_func);
102+
}
103+
104+
bool debug_info::do_debug() const
105+
{
106+
foreach(dbg_pattern pattern, patternlist) {
107+
if (pattern.match(short_file, short_func, line))
108+
return !pattern.invert();
109+
}
110+
return all;
111+
}
112+
113+
static void myMessageOutput(QtMsgType type, const QMessageLogContext &ctx,
114+
const QString &msg)
115+
{
116+
static QElapsedTimer *t;
117+
static int abort_on_warning = -1;
118+
const char *severity = "Unknown", *warn_msg = NULL;
119+
int el;
120+
121+
if (!t) {
122+
t = new QElapsedTimer();
123+
t->start();
124+
}
125+
if (abort_on_warning == -1) {
126+
char *a = getenv("XCA_ABORT_ON_WARNING");
127+
abort_on_warning = a && *a;
128+
}
129+
debug_info dinfo(ctx);
130+
el = t->elapsed();
131+
switch (type) {
132+
case QtDebugMsg:
133+
if (!dinfo.do_debug())
134+
return;
135+
severity = COL_CYAN "Debug";
136+
break;
137+
case QtWarningMsg: warn_msg = "WARNING"; severity = COL_LRED "Warning"; break;
138+
case QtCriticalMsg: warn_msg = "CRITICAL"; severity = COL_RED "Critical"; break;
139+
case QtFatalMsg: warn_msg = "FATAL"; severity = COL_RED "Fatal"; break;
140+
case QtInfoMsg: severity = COL_CYAN "Info"; break;
141+
default: severity = COL_CYAN "Default"; break;
142+
}
143+
console_write(stderr, QString(COL_YELL "%1%2 %3:%5" COL_RESET " %4\n")
144+
.arg(el/1000, 4)
145+
.arg((el%1000)/100, 2, 10, QChar('0'))
146+
.arg(severity).arg(msg)
147+
.arg(dinfo.log_prefix()).toUtf8());
148+
149+
if (abort_on_warning == 1 && warn_msg) {
150+
qFatal("Abort on %s", warn_msg);
151+
}
152+
}
153+
154+
void debug_info::init()
155+
{
156+
qInstallMessageHandler(myMessageOutput);
157+
const char *d = getenv("XCA_DEBUG");
158+
if (d && *d)
159+
debug_info::set_debug(QString(d));
160+
}

lib/debug_info.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* vi: set sw=4 ts=4:
2+
*
3+
* Copyright (C) 2023 Christian Hohnstaedt.
4+
*
5+
* All rights reserved.
6+
*/
7+
8+
#ifndef __DEBUG_INFO_H
9+
#define __DEBUG_INFO_H
10+
11+
#include <QString>
12+
#include <QList>
13+
14+
class dbg_pattern
15+
{
16+
QString file, func;
17+
unsigned first, last;
18+
bool inv;
19+
public:
20+
bool invert() const { return inv; }
21+
dbg_pattern(QString);
22+
bool match(const QString &curr_file, const QString &curr_func,
23+
unsigned line) const;
24+
};
25+
26+
class debug_info
27+
{
28+
private:
29+
QString short_file;
30+
QString short_func;
31+
unsigned line;
32+
33+
static QList<dbg_pattern> patternlist;
34+
public:
35+
static bool all;
36+
static void set_debug(const QString &dbg);
37+
static void init();
38+
debug_info(const QMessageLogContext &c);
39+
QString log_prefix() const;
40+
bool do_debug() const;
41+
static bool isEmpty()
42+
{
43+
return patternlist.size() == 0;
44+
}
45+
};
46+
47+
#endif

0 commit comments

Comments
 (0)