Skip to content

Commit b7e8d04

Browse files
committed
Allow logging by env variables
1 parent 10894b0 commit b7e8d04

File tree

2 files changed

+65
-124
lines changed

2 files changed

+65
-124
lines changed

src/irisnet/corelib/netnames.cpp

Lines changed: 35 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,19 @@
2828
#endif
2929
#include <limits>
3030

31-
// #define NETNAMES_DEBUG
32-
#ifdef NETNAMES_DEBUG
3331
#define NNDEBUG (qDebug() << this << "#" << __FUNCTION__ << ":")
34-
#endif
32+
static std::optional<bool> enable_logs;
33+
#define NNLOG(msg) \
34+
{ \
35+
if (!enable_logs.has_value()) { \
36+
enable_logs = qgetenv("NN_DEBUG") == "1"; \
37+
} \
38+
if (*enable_logs) { \
39+
msg; \
40+
} \
41+
} \
42+
while (false) \
43+
;
3544

3645
namespace XMPP {
3746
//----------------------------------------------------------------------------
@@ -482,9 +491,7 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
482491
}
483492
/* There are no priority groups left, return failure */
484493
if (currentPriorityGroup == priorityGroups.end()) {
485-
#ifdef NETNAMES_DEBUG
486-
NNDEBUG << "No more SRV records left";
487-
#endif
494+
NNLOG(NNDEBUG << "No more SRV records left");
488495
return {};
489496
}
490497

@@ -494,16 +501,12 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
494501
totalWeight += record.record.weight();
495502
}
496503

497-
#ifdef NETNAMES_DEBUG
498-
NNDEBUG << "Total weight:" << totalWeight;
499-
#endif
504+
NNLOG(NNDEBUG << "Total weight:" << totalWeight);
500505

501506
/* Pick a random entry */
502507
int randomWeight = totalWeight ? QRandomGenerator::global()->bounded(totalWeight) : 0;
503508

504-
#ifdef NETNAMES_DEBUG
505-
NNDEBUG << "Picked weight:" << randomWeight;
506-
#endif
509+
NNLOG(NNDEBUG << "Picked weight:" << randomWeight);
507510

508511
/* Iterate through the priority group until we found the randomly selected entry */
509512
WeightedNameRecordPriorityGroup::iterator it(currentPriorityGroup->second.begin());
@@ -514,9 +517,7 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
514517
/* We are going to delete the entry in the list, so save it */
515518
auto result { *it };
516519

517-
#ifdef NETNAMES_DEBUG
518-
NNDEBUG << "Picked record:" << result;
519-
#endif
520+
NNLOG(NNDEBUG << "Picked record:" << result);
520521

521522
/* Delete the entry from list, to prevent it from being tried multiple times */
522523
currentPriorityGroup->second.remove(it->record.weight(), *it);
@@ -1093,9 +1094,7 @@ void ServiceBrowser::stop() { }
10931094
//----------------------------------------------------------------------------
10941095
ServiceResolver::ServiceResolver(QObject *parent) : QObject(parent)
10951096
{
1096-
#ifdef NETNAMES_DEBUG
1097-
NNDEBUG;
1098-
#endif
1097+
NNLOG(NNDEBUG);
10991098

11001099
d = new Private(this);
11011100
}
@@ -1104,9 +1103,7 @@ ServiceResolver::~ServiceResolver() { delete d; }
11041103

11051104
void ServiceResolver::clear_resolvers()
11061105
{
1107-
#ifdef NETNAMES_DEBUG
1108-
NNDEBUG;
1109-
#endif
1106+
NNLOG(NNDEBUG);
11101107

11111108
/* cleanup all resolvers */
11121109
for (XMPP::NameResolver *resolver : std::as_const(d->resolverList)) {
@@ -1116,9 +1113,7 @@ void ServiceResolver::clear_resolvers()
11161113

11171114
void ServiceResolver::cleanup_resolver(XMPP::NameResolver *resolver)
11181115
{
1119-
#ifdef NETNAMES_DEBUG
1120-
NNDEBUG << "r:" << resolver;
1121-
#endif
1116+
NNLOG(NNDEBUG << "r:" << resolver);
11221117

11231118
if (resolver) {
11241119
/*
@@ -1144,9 +1139,7 @@ void ServiceResolver::start(const QByteArray &name) { NameManager::instance()->r
11441139
/* normal host lookup */
11451140
void ServiceResolver::start(const QString &host, quint16 port, const QString &service)
11461141
{
1147-
#ifdef NETNAMES_DEBUG
1148-
NNDEBUG << "h:" << host << "p:" << port;
1149-
#endif
1142+
NNLOG(NNDEBUG << "h:" << host << "p:" << port);
11501143

11511144
/* clear host list */
11521145
d->hostList.clear();
@@ -1156,9 +1149,7 @@ void ServiceResolver::start(const QString &host, quint16 port, const QString &se
11561149
d->host = host;
11571150
d->port = port;
11581151

1159-
#ifdef NETNAMES_DEBUG
1160-
NNDEBUG << "d->p:" << d->protocol;
1161-
#endif
1152+
NNLOG(NNDEBUG << "d->p:" << d->protocol);
11621153

11631154
/* initiate the host lookup */
11641155
XMPP::NameRecord::Type querytype
@@ -1175,9 +1166,7 @@ void ServiceResolver::start(const QString &host, quint16 port, const QString &se
11751166
/* SRV lookup */
11761167
void ServiceResolver::start(const QStringList &services, const QString &transport, const QString &domain, int port)
11771168
{
1178-
#ifdef NETNAMES_DEBUG
1179-
NNDEBUG << "s:" << services << "t:" << transport << "d:" << domain << "p:" << port;
1180-
#endif
1169+
NNLOG(NNDEBUG << "s:" << services << "t:" << transport << "d:" << domain << "p:" << port);
11811170
/* clear SRV list */
11821171
d->srvList.clear();
11831172
d->domain = domain;
@@ -1207,9 +1196,11 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
12071196
auto stats = std::make_shared<SrvStats>(
12081197
[this](bool success) {
12091198
if (success) {
1199+
NNLOG(NNDEBUG << "emit srvReady");
12101200
emit srvReady();
12111201
} else {
12121202
/* srvList already contains a failsafe host, try that */
1203+
NNLOG(NNDEBUG << "emit srvFailed");
12131204
emit srvFailed();
12141205
}
12151206
if (d->requestedProtocol != HappyEyeballs) {
@@ -1225,9 +1216,7 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
12251216
auto resolver = new XMPP::NameResolver;
12261217
connect(resolver, &XMPP::NameResolver::resultsReady, this,
12271218
[this, resolver, stats, service](const QList<XMPP::NameRecord> &r) {
1228-
#ifdef NETNAMES_DEBUG
1229-
NNDEBUG << "sl:" << r;
1230-
#endif
1219+
NNLOG(NNDEBUG << "sl:" << r);
12311220
QList<ServiceBoundRecord> sbr;
12321221
std::transform(r.begin(), r.end(), std::back_inserter(sbr),
12331222
[service](auto const &r) { return ServiceBoundRecord { service, r }; });
@@ -1236,12 +1225,8 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
12361225
cleanup_resolver(resolver);
12371226
});
12381227
connect(resolver, &XMPP::NameResolver::error, this, [this, resolver, stats](XMPP::NameResolver::Error e) {
1239-
/* failed the srv lookup, but we might have a fallback host in the srvList */
1240-
#ifdef NETNAMES_DEBUG
1241-
NNDEBUG << "e:" << e;
1242-
#else
1243-
Q_UNUSED(e)
1244-
#endif
1228+
/* failed the srv lookup, but we might have a fallback host in the srvList */
1229+
NNLOG(NNDEBUG << "e:" << e);
12451230
stats->finishOne(false);
12461231
cleanup_resolver(resolver);
12471232
});
@@ -1253,9 +1238,7 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
12531238
/* hosts resolved, now try to connect to them */
12541239
void ServiceResolver::handle_host_ready(const QString &service, const QList<XMPP::NameRecord> &rl)
12551240
{
1256-
#ifdef NETNAMES_DEBUG
1257-
NNDEBUG << "hl:" << rl;
1258-
#endif
1241+
NNLOG(NNDEBUG << "hl:" << rl);
12591242

12601243
/* cleanup resolver */
12611244
cleanup_resolver(static_cast<XMPP::NameResolver *>(sender()));
@@ -1270,9 +1253,7 @@ void ServiceResolver::handle_host_ready(const QString &service, const QList<XMPP
12701253
/* failed to lookup the primary record (A or AAAA, depending on user choice) */
12711254
void ServiceResolver::handle_host_error(XMPP::NameResolver::Error e)
12721255
{
1273-
#ifdef NETNAMES_DEBUG
1274-
NNDEBUG << "e:" << e;
1275-
#endif
1256+
NNLOG(NNDEBUG << "e:" << e);
12761257

12771258
/* cleanup resolver */
12781259
cleanup_resolver(static_cast<XMPP::NameResolver *>(sender()));
@@ -1287,11 +1268,7 @@ void ServiceResolver::handle_host_error(XMPP::NameResolver::Error e)
12871268
/* failed to lookup the fallback record (A or AAAA, depending on user choice) */
12881269
void ServiceResolver::handle_host_fallback_error(XMPP::NameResolver::Error e)
12891270
{
1290-
#ifdef NETNAMES_DEBUG
1291-
NNDEBUG << "e:" << e;
1292-
#else
1293-
Q_UNUSED(e)
1294-
#endif
1271+
NNLOG(NNDEBUG << "e:" << e);
12951272

12961273
/* cleanup resolver */
12971274
cleanup_resolver(static_cast<XMPP::NameResolver *>(sender()));
@@ -1310,9 +1287,7 @@ bool ServiceResolver::check_protocol_fallback()
13101287
/* lookup the fallback host */
13111288
bool ServiceResolver::lookup_host_fallback()
13121289
{
1313-
#ifdef NETNAMES_DEBUG
1314-
NNDEBUG;
1315-
#endif
1290+
NNLOG(NNDEBUG);
13161291

13171292
/* if a fallback is desired, otherwise we must fail immediately */
13181293
if (!check_protocol_fallback()) {
@@ -1322,9 +1297,7 @@ bool ServiceResolver::lookup_host_fallback()
13221297
d->protocol = (d->protocol == QAbstractSocket::IPv6Protocol ? QAbstractSocket::IPv4Protocol
13231298
: QAbstractSocket::IPv6Protocol);
13241299

1325-
#ifdef NETNAMES_DEBUG
1326-
NNDEBUG << "d->p:" << d->protocol;
1327-
#endif
1300+
NNLOG(NNDEBUG << "d->p:" << d->protocol);
13281301

13291302
/* initiate the fallback host lookup */
13301303
XMPP::NameRecord::Type querytype
@@ -1343,9 +1316,7 @@ bool ServiceResolver::lookup_host_fallback()
13431316
/* notify user about next host */
13441317
bool ServiceResolver::try_next_host()
13451318
{
1346-
#ifdef NETNAMES_DEBUG
1347-
NNDEBUG << "hl:" << d->hostList;
1348-
#endif
1319+
NNLOG(NNDEBUG << "hl:" << d->hostList);
13491320

13501321
/* if there is a host left for current protocol (AAAA or A) */
13511322
if (!d->hostList.empty()) {
@@ -1362,19 +1333,15 @@ bool ServiceResolver::try_next_host()
13621333
/* lookup the next SRV record in line */
13631334
void ServiceResolver::try_next_srv()
13641335
{
1365-
#ifdef NETNAMES_DEBUG
1366-
NNDEBUG << "sl:" << d->srvList;
1367-
#endif
1336+
NNLOG(NNDEBUG << "sl:" << d->srvList);
13681337

13691338
/* if there are still hosts we did not try */
13701339
auto record = d->srvList.takeNext();
13711340
if (!record.record.isNull()) {
13721341
/* lookup host by name and specify port for later use */
13731342
start(record.record.name(), quint16(record.record.port()), record.service);
13741343
} else {
1375-
#ifdef NETNAMES_DEBUG
1376-
NNDEBUG << "SRV list empty, failing";
1377-
#endif
1344+
NNLOG(NNDEBUG << "SRV list empty, failing");
13781345
/* no more SRV hosts to try, fail */
13791346
emit error(NoHostLeft);
13801347
}

0 commit comments

Comments
 (0)