28
28
#endif
29
29
#include < limits>
30
30
31
- // #define NETNAMES_DEBUG
32
- #ifdef NETNAMES_DEBUG
33
31
#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
+ ;
35
44
36
45
namespace XMPP {
37
46
// ----------------------------------------------------------------------------
@@ -482,9 +491,7 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
482
491
}
483
492
/* There are no priority groups left, return failure */
484
493
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" );
488
495
return {};
489
496
}
490
497
@@ -494,16 +501,12 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
494
501
totalWeight += record.record .weight ();
495
502
}
496
503
497
- #ifdef NETNAMES_DEBUG
498
- NNDEBUG << " Total weight:" << totalWeight;
499
- #endif
504
+ NNLOG (NNDEBUG << " Total weight:" << totalWeight);
500
505
501
506
/* Pick a random entry */
502
507
int randomWeight = totalWeight ? QRandomGenerator::global ()->bounded (totalWeight) : 0 ;
503
508
504
- #ifdef NETNAMES_DEBUG
505
- NNDEBUG << " Picked weight:" << randomWeight;
506
- #endif
509
+ NNLOG (NNDEBUG << " Picked weight:" << randomWeight);
507
510
508
511
/* Iterate through the priority group until we found the randomly selected entry */
509
512
WeightedNameRecordPriorityGroup::iterator it (currentPriorityGroup->second .begin ());
@@ -514,9 +517,7 @@ ServiceBoundRecord WeightedNameRecordList::takeNext()
514
517
/* We are going to delete the entry in the list, so save it */
515
518
auto result { *it };
516
519
517
- #ifdef NETNAMES_DEBUG
518
- NNDEBUG << " Picked record:" << result;
519
- #endif
520
+ NNLOG (NNDEBUG << " Picked record:" << result);
520
521
521
522
/* Delete the entry from list, to prevent it from being tried multiple times */
522
523
currentPriorityGroup->second .remove (it->record .weight (), *it);
@@ -1093,9 +1094,7 @@ void ServiceBrowser::stop() { }
1093
1094
// ----------------------------------------------------------------------------
1094
1095
ServiceResolver::ServiceResolver (QObject *parent) : QObject(parent)
1095
1096
{
1096
- #ifdef NETNAMES_DEBUG
1097
- NNDEBUG;
1098
- #endif
1097
+ NNLOG (NNDEBUG);
1099
1098
1100
1099
d = new Private (this );
1101
1100
}
@@ -1104,9 +1103,7 @@ ServiceResolver::~ServiceResolver() { delete d; }
1104
1103
1105
1104
void ServiceResolver::clear_resolvers ()
1106
1105
{
1107
- #ifdef NETNAMES_DEBUG
1108
- NNDEBUG;
1109
- #endif
1106
+ NNLOG (NNDEBUG);
1110
1107
1111
1108
/* cleanup all resolvers */
1112
1109
for (XMPP::NameResolver *resolver : std::as_const (d->resolverList )) {
@@ -1116,9 +1113,7 @@ void ServiceResolver::clear_resolvers()
1116
1113
1117
1114
void ServiceResolver::cleanup_resolver (XMPP::NameResolver *resolver)
1118
1115
{
1119
- #ifdef NETNAMES_DEBUG
1120
- NNDEBUG << " r:" << resolver;
1121
- #endif
1116
+ NNLOG (NNDEBUG << " r:" << resolver);
1122
1117
1123
1118
if (resolver) {
1124
1119
/*
@@ -1144,9 +1139,7 @@ void ServiceResolver::start(const QByteArray &name) { NameManager::instance()->r
1144
1139
/* normal host lookup */
1145
1140
void ServiceResolver::start (const QString &host, quint16 port, const QString &service)
1146
1141
{
1147
- #ifdef NETNAMES_DEBUG
1148
- NNDEBUG << " h:" << host << " p:" << port;
1149
- #endif
1142
+ NNLOG (NNDEBUG << " h:" << host << " p:" << port);
1150
1143
1151
1144
/* clear host list */
1152
1145
d->hostList .clear ();
@@ -1156,9 +1149,7 @@ void ServiceResolver::start(const QString &host, quint16 port, const QString &se
1156
1149
d->host = host;
1157
1150
d->port = port;
1158
1151
1159
- #ifdef NETNAMES_DEBUG
1160
- NNDEBUG << " d->p:" << d->protocol ;
1161
- #endif
1152
+ NNLOG (NNDEBUG << " d->p:" << d->protocol );
1162
1153
1163
1154
/* initiate the host lookup */
1164
1155
XMPP::NameRecord::Type querytype
@@ -1175,9 +1166,7 @@ void ServiceResolver::start(const QString &host, quint16 port, const QString &se
1175
1166
/* SRV lookup */
1176
1167
void ServiceResolver::start (const QStringList &services, const QString &transport, const QString &domain, int port)
1177
1168
{
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);
1181
1170
/* clear SRV list */
1182
1171
d->srvList .clear ();
1183
1172
d->domain = domain;
@@ -1207,9 +1196,11 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
1207
1196
auto stats = std::make_shared<SrvStats>(
1208
1197
[this ](bool success) {
1209
1198
if (success) {
1199
+ NNLOG (NNDEBUG << " emit srvReady" );
1210
1200
emit srvReady ();
1211
1201
} else {
1212
1202
/* srvList already contains a failsafe host, try that */
1203
+ NNLOG (NNDEBUG << " emit srvFailed" );
1213
1204
emit srvFailed ();
1214
1205
}
1215
1206
if (d->requestedProtocol != HappyEyeballs) {
@@ -1225,9 +1216,7 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
1225
1216
auto resolver = new XMPP::NameResolver;
1226
1217
connect (resolver, &XMPP::NameResolver::resultsReady, this ,
1227
1218
[this , resolver, stats, service](const QList<XMPP::NameRecord> &r) {
1228
- #ifdef NETNAMES_DEBUG
1229
- NNDEBUG << " sl:" << r;
1230
- #endif
1219
+ NNLOG (NNDEBUG << " sl:" << r);
1231
1220
QList<ServiceBoundRecord> sbr;
1232
1221
std::transform (r.begin (), r.end (), std::back_inserter (sbr),
1233
1222
[service](auto const &r) { return ServiceBoundRecord { service, r }; });
@@ -1236,12 +1225,8 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
1236
1225
cleanup_resolver (resolver);
1237
1226
});
1238
1227
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);
1245
1230
stats->finishOne (false );
1246
1231
cleanup_resolver (resolver);
1247
1232
});
@@ -1253,9 +1238,7 @@ void ServiceResolver::start(const QStringList &services, const QString &transpor
1253
1238
/* hosts resolved, now try to connect to them */
1254
1239
void ServiceResolver::handle_host_ready (const QString &service, const QList<XMPP::NameRecord> &rl)
1255
1240
{
1256
- #ifdef NETNAMES_DEBUG
1257
- NNDEBUG << " hl:" << rl;
1258
- #endif
1241
+ NNLOG (NNDEBUG << " hl:" << rl);
1259
1242
1260
1243
/* cleanup resolver */
1261
1244
cleanup_resolver (static_cast <XMPP::NameResolver *>(sender ()));
@@ -1270,9 +1253,7 @@ void ServiceResolver::handle_host_ready(const QString &service, const QList<XMPP
1270
1253
/* failed to lookup the primary record (A or AAAA, depending on user choice) */
1271
1254
void ServiceResolver::handle_host_error (XMPP::NameResolver::Error e)
1272
1255
{
1273
- #ifdef NETNAMES_DEBUG
1274
- NNDEBUG << " e:" << e;
1275
- #endif
1256
+ NNLOG (NNDEBUG << " e:" << e);
1276
1257
1277
1258
/* cleanup resolver */
1278
1259
cleanup_resolver (static_cast <XMPP::NameResolver *>(sender ()));
@@ -1287,11 +1268,7 @@ void ServiceResolver::handle_host_error(XMPP::NameResolver::Error e)
1287
1268
/* failed to lookup the fallback record (A or AAAA, depending on user choice) */
1288
1269
void ServiceResolver::handle_host_fallback_error (XMPP::NameResolver::Error e)
1289
1270
{
1290
- #ifdef NETNAMES_DEBUG
1291
- NNDEBUG << " e:" << e;
1292
- #else
1293
- Q_UNUSED (e)
1294
- #endif
1271
+ NNLOG (NNDEBUG << " e:" << e);
1295
1272
1296
1273
/* cleanup resolver */
1297
1274
cleanup_resolver (static_cast <XMPP::NameResolver *>(sender ()));
@@ -1310,9 +1287,7 @@ bool ServiceResolver::check_protocol_fallback()
1310
1287
/* lookup the fallback host */
1311
1288
bool ServiceResolver::lookup_host_fallback ()
1312
1289
{
1313
- #ifdef NETNAMES_DEBUG
1314
- NNDEBUG;
1315
- #endif
1290
+ NNLOG (NNDEBUG);
1316
1291
1317
1292
/* if a fallback is desired, otherwise we must fail immediately */
1318
1293
if (!check_protocol_fallback ()) {
@@ -1322,9 +1297,7 @@ bool ServiceResolver::lookup_host_fallback()
1322
1297
d->protocol = (d->protocol == QAbstractSocket::IPv6Protocol ? QAbstractSocket::IPv4Protocol
1323
1298
: QAbstractSocket::IPv6Protocol);
1324
1299
1325
- #ifdef NETNAMES_DEBUG
1326
- NNDEBUG << " d->p:" << d->protocol ;
1327
- #endif
1300
+ NNLOG (NNDEBUG << " d->p:" << d->protocol );
1328
1301
1329
1302
/* initiate the fallback host lookup */
1330
1303
XMPP::NameRecord::Type querytype
@@ -1343,9 +1316,7 @@ bool ServiceResolver::lookup_host_fallback()
1343
1316
/* notify user about next host */
1344
1317
bool ServiceResolver::try_next_host ()
1345
1318
{
1346
- #ifdef NETNAMES_DEBUG
1347
- NNDEBUG << " hl:" << d->hostList ;
1348
- #endif
1319
+ NNLOG (NNDEBUG << " hl:" << d->hostList );
1349
1320
1350
1321
/* if there is a host left for current protocol (AAAA or A) */
1351
1322
if (!d->hostList .empty ()) {
@@ -1362,19 +1333,15 @@ bool ServiceResolver::try_next_host()
1362
1333
/* lookup the next SRV record in line */
1363
1334
void ServiceResolver::try_next_srv ()
1364
1335
{
1365
- #ifdef NETNAMES_DEBUG
1366
- NNDEBUG << " sl:" << d->srvList ;
1367
- #endif
1336
+ NNLOG (NNDEBUG << " sl:" << d->srvList );
1368
1337
1369
1338
/* if there are still hosts we did not try */
1370
1339
auto record = d->srvList .takeNext ();
1371
1340
if (!record.record .isNull ()) {
1372
1341
/* lookup host by name and specify port for later use */
1373
1342
start (record.record .name (), quint16 (record.record .port ()), record.service );
1374
1343
} else {
1375
- #ifdef NETNAMES_DEBUG
1376
- NNDEBUG << " SRV list empty, failing" ;
1377
- #endif
1344
+ NNLOG (NNDEBUG << " SRV list empty, failing" );
1378
1345
/* no more SRV hosts to try, fail */
1379
1346
emit error (NoHostLeft);
1380
1347
}
0 commit comments