From 14878d285cb753d61b2480dcdcb7ef3af14ad875 Mon Sep 17 00:00:00 2001 From: Audric Date: Sat, 18 Apr 2026 19:28:16 +0200 Subject: [PATCH] logging: migrate remaining cold paths to facade MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sweeps startup banners, config validation, HTTP /status lifecycle, TLS and certificate diagnostics, MQTT and Redis publisher lifecycle, and ReflectorMsg serialisation errors through geulog::{info,warn,error}. No behaviour change — every line retains its message content and level. After this commit, grep for cout/cerr << in src/svxlink/reflector/ returns no real call sites (only #if 0 blocks and string literals). --- src/svxlink/reflector/MqttPublisher.cpp | 56 +++-- src/svxlink/reflector/RedisStore.cpp | 42 ++-- src/svxlink/reflector/Reflector.cpp | 288 ++++++++++------------ src/svxlink/reflector/ReflectorClient.cpp | 89 +++---- src/svxlink/reflector/ReflectorMsg.h | 15 +- src/svxlink/reflector/TrunkLink.cpp | 72 +++--- src/svxlink/reflector/TwinLink.cpp | 12 +- src/svxlink/reflector/svxreflector.cpp | 104 ++++---- 8 files changed, 319 insertions(+), 359 deletions(-) diff --git a/src/svxlink/reflector/MqttPublisher.cpp b/src/svxlink/reflector/MqttPublisher.cpp index 94dfefd..6c50158 100644 --- a/src/svxlink/reflector/MqttPublisher.cpp +++ b/src/svxlink/reflector/MqttPublisher.cpp @@ -4,6 +4,7 @@ #include #include #include +#include using namespace std; @@ -12,12 +13,12 @@ static void on_connect_cb(struct mosquitto*, void*, int rc) { if (rc == 0) { - cout << "MQTT: Connected to broker" << endl; + geulog::info("mqtt", "MQTT: Connected to broker"); } else { - cerr << "*** WARNING: MQTT connection failed: " - << mosquitto_connack_string(rc) << endl; + geulog::warn("mqtt", "MQTT connection failed: ", + mosquitto_connack_string(rc)); } } @@ -26,8 +27,8 @@ static void on_disconnect_cb(struct mosquitto*, void*, int rc) { if (rc != 0) { - cerr << "*** WARNING: MQTT disconnected unexpectedly (rc=" << rc - << "), will reconnect" << endl; + geulog::warn("mqtt", "MQTT disconnected unexpectedly (rc=", rc, + "), will reconnect"); } } @@ -49,7 +50,7 @@ bool MqttPublisher::initialize(void) // Read required config values if (!m_cfg.getValue("MQTT", "HOST", m_host) || m_host.empty()) { - cerr << "*** ERROR: MQTT/HOST not configured" << endl; + geulog::error("mqtt", "MQTT/HOST not configured"); return false; } @@ -60,26 +61,26 @@ bool MqttPublisher::initialize(void) } else { - cerr << "*** ERROR: MQTT/PORT not configured" << endl; + geulog::error("mqtt", "MQTT/PORT not configured"); return false; } if (!m_cfg.getValue("MQTT", "USERNAME", m_username) || m_username.empty()) { - cerr << "*** ERROR: MQTT/USERNAME not configured" << endl; + geulog::error("mqtt", "MQTT/USERNAME not configured"); return false; } if (!m_cfg.getValue("MQTT", "PASSWORD", m_password) || m_password.empty()) { - cerr << "*** ERROR: MQTT/PASSWORD not configured" << endl; + geulog::error("mqtt", "MQTT/PASSWORD not configured"); return false; } if (!m_cfg.getValue("MQTT", "TOPIC_PREFIX", m_topic_prefix) || m_topic_prefix.empty()) { - cerr << "*** ERROR: MQTT/TOPIC_PREFIX not configured" << endl; + geulog::error("mqtt", "MQTT/TOPIC_PREFIX not configured"); return false; } @@ -107,7 +108,7 @@ bool MqttPublisher::initialize(void) if (!m_cfg.getValue("MQTT", "TLS_CA_CERT", m_tls_ca_cert) || m_tls_ca_cert.empty()) { - cerr << "*** ERROR: MQTT/TLS_CA_CERT required when TLS_ENABLED=1" << endl; + geulog::error("mqtt", "MQTT/TLS_CA_CERT required when TLS_ENABLED=1"); return false; } m_cfg.getValue("MQTT", "TLS_CLIENT_CERT", m_tls_client_cert); @@ -120,7 +121,7 @@ bool MqttPublisher::initialize(void) m_mosq = mosquitto_new(nullptr, true, nullptr); if (m_mosq == nullptr) { - cerr << "*** ERROR: mosquitto_new() failed" << endl; + geulog::error("mqtt", "mosquitto_new() failed"); return false; } @@ -141,8 +142,8 @@ bool MqttPublisher::initialize(void) client_cert, client_key, nullptr); if (rc != MOSQ_ERR_SUCCESS) { - cerr << "*** ERROR: MQTT TLS configuration failed: " - << mosquitto_strerror(rc) << endl; + geulog::error("mqtt", "MQTT TLS configuration failed: ", + mosquitto_strerror(rc)); return false; } } @@ -154,8 +155,8 @@ bool MqttPublisher::initialize(void) int rc = mosquitto_loop_start(m_mosq); if (rc != MOSQ_ERR_SUCCESS) { - cerr << "*** ERROR: mosquitto_loop_start() failed: " - << mosquitto_strerror(rc) << endl; + geulog::error("mqtt", "mosquitto_loop_start() failed: ", + mosquitto_strerror(rc)); return false; } @@ -163,13 +164,12 @@ bool MqttPublisher::initialize(void) rc = mosquitto_connect_async(m_mosq, m_host.c_str(), m_port, 60); if (rc != MOSQ_ERR_SUCCESS) { - cerr << "*** WARNING: MQTT initial connect failed: " - << mosquitto_strerror(rc) - << " -- will retry in background" << endl; + geulog::warn("mqtt", "MQTT initial connect failed: ", + mosquitto_strerror(rc), " -- will retry in background"); } - cout << "MQTT: Publisher initialized, broker=" << m_host << ":" << m_port - << " topic_prefix=" << m_topic_prefix << endl; + geulog::info("mqtt", "MQTT: Publisher initialized, broker=", m_host, ":", + m_port, " topic_prefix=", m_topic_prefix); return true; } @@ -184,7 +184,7 @@ void MqttPublisher::shutdown(void) mosquitto_destroy(m_mosq); m_mosq = nullptr; mosquitto_lib_cleanup(); - cout << "MQTT: Publisher shut down" << endl; + geulog::info("mqtt", "MQTT: Publisher shut down"); } } @@ -214,13 +214,17 @@ void MqttPublisher::publish(const std::string& topic_suffix, if (first || code_changed || cooldown_ok) { - cerr << "*** WARNING: MQTT publish failed: topic=" << topic - << " rc=" << rc << " (" << mosquitto_strerror(rc) << ")"; if (m_pub_err_suppressed > 0) { - cerr << " [suppressed " << m_pub_err_suppressed << " prior]"; + geulog::warn("mqtt", "MQTT publish failed: topic=", topic, + " rc=", rc, " (", mosquitto_strerror(rc), ")", + " [suppressed ", m_pub_err_suppressed, " prior]"); + } + else + { + geulog::warn("mqtt", "MQTT publish failed: topic=", topic, + " rc=", rc, " (", mosquitto_strerror(rc), ")"); } - cerr << endl; m_last_pub_err_code = rc; m_last_pub_err_logged = now; m_pub_err_suppressed = 0; diff --git a/src/svxlink/reflector/RedisStore.cpp b/src/svxlink/reflector/RedisStore.cpp index 70ddd04..3831930 100644 --- a/src/svxlink/reflector/RedisStore.cpp +++ b/src/svxlink/reflector/RedisStore.cpp @@ -6,6 +6,7 @@ #include "RedisLiveQueue.h" #include #include +#include namespace { @@ -52,15 +53,15 @@ bool RedisStore::connectSync(void) { m_sync = redisConnectWithTimeout(m_cfg.host.c_str(), m_cfg.port, tv); } if (!m_sync || m_sync->err) { - std::cerr << "*** ERROR: Redis sync connect failed: " - << (m_sync ? m_sync->errstr : "alloc failed") << std::endl; + geulog::error("redis", "Redis sync connect failed: ", + (m_sync ? m_sync->errstr : "alloc failed")); if (m_sync) { redisFree(m_sync); m_sync = nullptr; } return false; } if (!m_cfg.password.empty()) { redisReply* r = (redisReply*)redisCommand(m_sync, "AUTH %s", m_cfg.password.c_str()); if (!r || r->type == REDIS_REPLY_ERROR) { - std::cerr << "*** ERROR: Redis AUTH failed" << std::endl; + geulog::error("redis", "Redis AUTH failed"); if (r) freeReplyObject(r); redisFree(m_sync); m_sync = nullptr; return false; @@ -70,7 +71,7 @@ bool RedisStore::connectSync(void) { if (m_cfg.db != 0) { redisReply* r = (redisReply*)redisCommand(m_sync, "SELECT %d", m_cfg.db); if (!r || r->type == REDIS_REPLY_ERROR) { - std::cerr << "*** ERROR: Redis SELECT failed" << std::endl; + geulog::error("redis", "Redis SELECT failed"); if (r) freeReplyObject(r); redisFree(m_sync); m_sync = nullptr; return false; @@ -95,8 +96,8 @@ bool RedisStore::connect(void) { m_heartbeat_timer->expired.connect( sigc::mem_fun(*this, &RedisStore::refreshLiveExpire)); - std::cout << "RedisStore: connected sync+async, subscribed to " - << channelName() << std::endl; + geulog::info("redis", "RedisStore: connected sync+async, subscribed to ", + channelName()); return true; } @@ -122,7 +123,7 @@ std::string RedisStore::lookupUserKey(const std::string& callsign) { } freeReplyObject(r); if (!enabled) { - std::cout << "RedisStore: user " << callsign << " disabled" << std::endl; + geulog::info("redis", "RedisStore: user ", callsign, " disabled"); return ""; } @@ -130,7 +131,7 @@ std::string RedisStore::lookupUserKey(const std::string& callsign) { r = (redisReply*)redisCommand(m_sync, "HGET %s password", group_key.c_str()); if (!r || r->type != REDIS_REPLY_STRING) { if (r) freeReplyObject(r); - std::cout << "*** WARNING: group " << group << " missing password" << std::endl; + geulog::warn("redis", "group ", group, " missing password"); return ""; } std::string pw(r->str, r->len); @@ -243,9 +244,9 @@ RedisStore::loadTrunkPeers(void) // Require host + secret + remote_prefix if (cfg.host.empty() || cfg.secret.empty() || cfg.remote_prefix.empty()) { - std::cerr << "*** WARN: RedisStore: trunk peer " << section - << " missing required fields (host/secret/remote_prefix); " - << "skipping" << std::endl; + geulog::warn("redis", "RedisStore: trunk peer ", section, + " missing required fields (host/secret/remote_prefix); " + "skipping"); continue; } out[section] = std::move(cfg); @@ -446,8 +447,8 @@ bool RedisStore::connectAsync(void) { m_async = redisAsyncConnect(m_cfg.host.c_str(), m_cfg.port); } if (!m_async || m_async->err) { - std::cerr << "*** ERROR: Redis async connect failed: " - << (m_async ? m_async->errstr : "alloc failed") << std::endl; + geulog::error("redis", "Redis async connect failed: ", + (m_async ? m_async->errstr : "alloc failed")); if (m_async) { redisAsyncFree(m_async); m_async = nullptr; } return false; } @@ -474,9 +475,8 @@ bool RedisStore::connectAsyncWrite(void) { m_async_write = redisAsyncConnect(m_cfg.host.c_str(), m_cfg.port); } if (!m_async_write || m_async_write->err) { - std::cerr << "*** ERROR: Redis async-write connect failed: " - << (m_async_write ? m_async_write->errstr : "alloc failed") - << std::endl; + geulog::error("redis", "Redis async-write connect failed: ", + (m_async_write ? m_async_write->errstr : "alloc failed")); if (m_async_write) { redisAsyncFree(m_async_write); m_async_write = nullptr; } return false; } @@ -504,7 +504,7 @@ void RedisStore::subscribe(void) { } void RedisStore::onAsyncDisconnect(int /*status*/) { if (m_shutting_down) return; - std::cerr << "*** WARN: Redis async disconnect" << std::endl; + geulog::warn("redis", "Redis async disconnect"); // hiredis will free the context after this returns — just null out. m_async = nullptr; scheduleReconnect(); @@ -512,7 +512,7 @@ void RedisStore::onAsyncDisconnect(int /*status*/) { void RedisStore::onAsyncWriteDisconnect(int /*status*/) { if (m_shutting_down) return; - std::cerr << "*** WARN: Redis async-write disconnect" << std::endl; + geulog::warn("redis", "Redis async-write disconnect"); m_async_write = nullptr; scheduleReconnect(); } @@ -540,14 +540,14 @@ void RedisStore::scheduleReconnect(void) { // Rebuild sync context too — its TCP socket is likely dead. if (m_sync) { redisFree(m_sync); m_sync = nullptr; } if (!connectSync()) { - std::cerr << "*** WARN: Redis sync reconnect failed" << std::endl; + geulog::warn("redis", "Redis sync reconnect failed"); // async is up but sync isn't — schedule another retry so we fix sync. m_reconnect_backoff_s = std::min(m_reconnect_backoff_s * 2, 30); scheduleReconnect(); return; } - std::cout << "RedisStore: reconnected, re-subscribing and requesting " - "full reload" << std::endl; + geulog::info("redis", "RedisStore: reconnected, re-subscribing and requesting " + "full reload"); m_reconnect_backoff_s = 1; configChanged.emit("all"); }); diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index c6b59a3..498c41f 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -167,11 +167,10 @@ namespace { dirname += part + "/"; if (access(dirname.c_str(), F_OK) != 0) { - std::cout << "Create directory '" << dirname << "'" << std::endl; + geulog::info("core", "Create directory '", dirname, "'"); if (mkdir(dirname.c_str(), 0777) != 0) { - std::cerr << "*** ERROR: Could not create directory '" - << dirname << "'" << std::endl; + geulog::error("core", "Could not create directory '", dirname, "'"); return false; } } @@ -268,8 +267,7 @@ Reflector::Reflector(void) { if (!loadServerCertificateFiles()) { - std::cerr << "*** WARNING: Failed to renew server certificate" - << std::endl; + geulog::warn("core", "Failed to renew server certificate"); } }); m_renew_issue_ca_cert_timer.expired.connect( @@ -277,8 +275,7 @@ Reflector::Reflector(void) { if (!loadSigningCAFiles()) { - std::cerr << "*** WARNING: Failed to renew issuing CA certificate" - << std::endl; + geulog::warn("core", "Failed to renew issuing CA certificate"); } }); m_sat_cleanup_timer.setEnable(false); @@ -369,8 +366,7 @@ bool Reflector::initialize(Async::Config &cfg) (err="initialization failure", !m_udp_sock->initOk()) || (err="unsupported cipher", !m_udp_sock->setCipher(UdpCipher::NAME))) { - std::cerr << "*** ERROR: Could not initialize UDP socket due to " - << err << std::endl; + geulog::error("core", "Could not initialize UDP socket due to ", err); return false; } m_udp_sock->setCipherAADLength(UdpCipher::AADLEN); @@ -397,8 +393,7 @@ bool Reflector::initialize(Async::Config &cfg) m_random_qsy_hi = m_random_qsy_lo + random_qsy_range.second-1; if ((m_random_qsy_lo < 1) || (m_random_qsy_hi < m_random_qsy_lo)) { - cout << "*** WARNING: Illegal RANDOM_QSY_RANGE specified. Ignored." - << endl; + geulog::warn("core", "Illegal RANDOM_QSY_RANGE specified. Ignored."); m_random_qsy_hi = m_random_qsy_lo = 0; } m_random_qsy_tg = m_random_qsy_hi; @@ -422,9 +417,8 @@ bool Reflector::initialize(Async::Config &cfg) m_cmd_pty = new Pty(pty_path); if ((m_cmd_pty == nullptr) || !m_cmd_pty->open()) { - std::cerr << "*** ERROR: Could not open command PTY '" << pty_path - << "' as specified in configuration variable " - "GLOBAL/COMMAND_PTY" << std::endl; + geulog::error("core", "Could not open command PTY '", pty_path, + "' as specified in configuration variable GLOBAL/COMMAND_PTY"); return false; } m_cmd_pty->setLineBuffered(true); @@ -458,16 +452,16 @@ bool Reflector::initialize(Async::Config &cfg) m_redis = new RedisStore(rcfg); if (!m_redis->connect()) { - std::cerr << "*** ERROR: [REDIS] is configured but Redis is unreachable. " - << "Aborting startup." << std::endl; + geulog::error("redis", "[REDIS] is configured but Redis is unreachable. " + "Aborting startup."); return false; } auto warn_if_nonempty = [this](const char* section) { std::list keys = m_cfg->listSection(section); if (!keys.empty()) { - std::cerr << "WARN: [" << section << "] in svxreflector.conf is " + geulog::warn("redis", "[", section, "] in svxreflector.conf is " "ignored because [REDIS] is configured. " - << "Run --import-conf-to-redis to migrate." << std::endl; + "Run --import-conf-to-redis to migrate."); } }; warn_if_nonempty("USERS"); @@ -492,7 +486,7 @@ bool Reflector::initialize(Async::Config &cfg) m_mqtt = new MqttPublisher(cfg); if (!m_mqtt->initialize()) { - cerr << "*** WARNING: MQTT publisher failed to initialize, continuing without MQTT" << endl; + geulog::warn("mqtt", "MQTT publisher failed to initialize, continuing without MQTT"); delete m_mqtt; m_mqtt = nullptr; } @@ -522,8 +516,7 @@ bool Reflector::initialize(Async::Config &cfg) m_satellite_client = new SatelliteClient(this, cfg); if (!m_satellite_client->initialize()) { - std::cerr << "*** ERROR: Failed to initialize satellite connection" - << std::endl; + geulog::error("core", "Failed to initialize satellite connection"); return false; } // Satellites don't participate in the trunk mesh @@ -539,7 +532,7 @@ bool Reflector::initialize(Async::Config &cfg) || trunk_debug_str == "yes"); if (m_trunk_debug) { - std::cout << "Trunk debug logging enabled" << std::endl; + geulog::info("core", "Trunk debug logging enabled"); } } @@ -714,8 +707,7 @@ bool Reflector::signClientCert(Async::SslX509& cert, const std::string& ca_op) auto cn = cert.commonName(); if (!cert.sign(m_issue_ca_pkey)) { - std::cerr << "*** ERROR: Certificate signing failed for client " - << cn << std::endl; + geulog::error("core", "Certificate signing failed for client ", cn); return false; } auto crtfile = m_certs_dir + "/" + cn + ".crt"; @@ -728,8 +720,8 @@ bool Reflector::signClientCert(Async::SslX509& cert, const std::string& ca_op) } else { - std::cerr << "*** WARNING: Failed to write client certificate file '" - << crtfile << "'" << std::endl; + geulog::warn("core", "Failed to write client certificate file '", + crtfile, "'"); } return true; } /* Reflector::signClientCert */ @@ -744,8 +736,7 @@ Async::SslX509 Reflector::signClientCsr(const std::string& cn) auto req = loadClientPendingCsr(cn); if (req.isNull()) { - std::cerr << "*** ERROR: Cannot find CSR to sign '" << req.filePath() - << "'" << std::endl; + geulog::error("core", "Cannot find CSR to sign '", req.filePath(), "'"); return cert; } @@ -773,9 +764,8 @@ Async::SslX509 Reflector::signClientCsr(const std::string& cn) if (rename(req.filePath().c_str(), csr_path.c_str()) != 0) { auto errstr = SvxLink::strError(errno); - std::cerr << "*** WARNING: Failed to move signed CSR from '" - << req.filePath() << "' to '" << csr_path << "': " - << errstr << std::endl; + geulog::warn("core", "Failed to move signed CSR from '", + req.filePath(), "' to '", csr_path, "': ", errstr); } auto client = ReflectorClient::lookup(cn); @@ -1967,10 +1957,9 @@ void Reflector::ctrlPtyDataReceived(const void *buf, size_t count) m_cmd_pty->write("---------- Signed Client Certificate ----------\n"); m_cmd_pty->write(cert.toString()); m_cmd_pty->write("-----------------------------------------------\n"); - std::cout << "---------- Signed Client Certificate ----------\n" - << cert.toString() - << "-----------------------------------------------" - << std::endl; + geulog::info("core", "---------- Signed Client Certificate ----------"); + geulog::info("core", cert.toString()); + geulog::info("core", "-----------------------------------------------"); } else { @@ -1990,7 +1979,7 @@ void Reflector::ctrlPtyDataReceived(const void *buf, size_t count) { std::string msg(cn + ": Removed client certificate and CSR"); m_cmd_pty->write(msg + "\n"); - std::cout << msg << std::endl; + geulog::info("core", msg); } else { @@ -2261,9 +2250,9 @@ void Reflector::initTrunkLinks(void) if (s.size() >= 6 && s.substr(0, 6) == "TRUNK_") { conf_has_trunk = true; break; } } if (conf_has_trunk) { - std::cerr << "WARN: [TRUNK_*] sections in svxreflector.conf are " + geulog::warn("core", "[TRUNK_*] sections in svxreflector.conf are " "ignored because [REDIS] is configured. Run " - "--import-conf-to-redis to migrate." << std::endl; + "--import-conf-to-redis to migrate."); } auto peers = m_redis->loadTrunkPeers(); @@ -2316,8 +2305,8 @@ void Reflector::initTrunkLinks(void) } else { - std::cerr << "*** ERROR: Failed to initialize trunk link '" - << section << "'" << std::endl; + geulog::error("core", "Failed to initialize trunk link '", + section, "'"); delete link; } } @@ -2331,10 +2320,9 @@ void Reflector::initTrunkLinks(void) if (s.size() >= prefix.size() && s.compare(0, prefix.size(), prefix) == 0) { - std::cerr << "*** WARNING: Cluster TG " << tg - << " conflicts with prefix " << prefix - << " — this TG will be routed as cluster (broadcast to all)" - << std::endl; + geulog::warn("core", "Cluster TG ", tg, " conflicts with prefix ", + prefix, + " — this TG will be routed as cluster (broadcast to all)"); } } } @@ -2357,7 +2345,7 @@ void Reflector::initTrunkServer(void) m_trunk_srv->clientDisconnected.connect( sigc::mem_fun(*this, &Reflector::trunkClientDisconnected)); - std::cout << "Trunk server listening on port " << trunk_port << std::endl; + geulog::info("core", "Trunk server listening on port ", trunk_port); } /* Reflector::initTrunkServer */ @@ -2645,7 +2633,7 @@ void Reflector::initSatelliteServer(void) m_sat_srv->clientDisconnected.connect( sigc::mem_fun(*this, &Reflector::satelliteDisconnected)); - std::cout << "Satellite server listening on port " << sat_port << std::endl; + geulog::info("core", "Satellite server listening on port ", sat_port); } /* Reflector::initSatelliteServer */ @@ -3275,15 +3263,15 @@ bool Reflector::loadCertificateFiles(void) if (!ensureDirectoryExist(m_ca_bundle_file) || !m_ca_cert.writePemFile(m_ca_bundle_file)) { - std::cout << "*** ERROR: Failed to write CA bundle file '" - << m_ca_bundle_file << "'" << std::endl; + geulog::error("core", "Failed to write CA bundle file '", + m_ca_bundle_file, "'"); return false; } } if (!m_ssl_ctx.setCaCertificateFile(m_ca_bundle_file)) { - std::cout << "*** ERROR: Failed to read CA certificate bundle '" - << m_ca_bundle_file << "'" << std::endl; + geulog::error("core", "Failed to read CA certificate bundle '", + m_ca_bundle_file, "'"); return false; } @@ -3291,8 +3279,8 @@ bool Reflector::loadCertificateFiles(void) if (stat(m_ca_bundle_file.c_str(), &st) != 0) { auto errstr = SvxLink::strError(errno); - std::cerr << "*** ERROR: Failed to read CA file from '" - << m_ca_bundle_file << "': " << errstr << std::endl; + geulog::error("core", "Failed to read CA file from '", + m_ca_bundle_file, "': ", errstr); return false; } auto bundle = caBundlePem(); @@ -3300,8 +3288,7 @@ bool Reflector::loadCertificateFiles(void) Async::Digest ca_dgst; if (!ca_dgst.md(m_ca_md, MsgCABundle::MD_ALG, bundle)) { - std::cerr << "*** ERROR: CA bundle checksumming failed" - << std::endl; + geulog::error("core", "CA bundle checksumming failed"); return false; } ca_dgst.signInit(MsgCABundle::MD_ALG, m_issue_ca_pkey); @@ -3319,9 +3306,9 @@ bool Reflector::loadServerCertificateFiles(void) if (!m_cfg->getValue("SERVER_CERT", "COMMON_NAME", cert_cn) || cert_cn.empty()) { - std::cerr << "*** ERROR: The 'SERVER_CERT/COMMON_NAME' variable is " - "unset which is needed for certificate signing request " - "generation." << std::endl; + geulog::error("core", "The 'SERVER_CERT/COMMON_NAME' variable is " + "unset which is needed for certificate signing request " + "generation."); return false; } @@ -3333,8 +3320,8 @@ bool Reflector::loadServerCertificateFiles(void) Async::SslKeypair pkey; if (access(keyfile.c_str(), F_OK) != 0) { - std::cout << "Server private key file not found. Generating '" - << keyfile << "'" << std::endl; + geulog::info("core", "Server private key file not found. Generating '", + keyfile, "'"); if (!generateKeyFile(pkey, keyfile)) { return false; @@ -3342,8 +3329,8 @@ bool Reflector::loadServerCertificateFiles(void) } else if (!pkey.readPrivateKeyFile(keyfile)) { - std::cerr << "*** ERROR: Failed to read private key file from '" - << keyfile << "'" << std::endl; + geulog::error("core", "Failed to read private key file from '", + keyfile, "'"); return false; } @@ -3359,9 +3346,9 @@ bool Reflector::loadServerCertificateFiles(void) !cert.verify(m_issue_ca_pkey); if (generate_cert) { - std::cerr << "*** WARNING: Failed to read server certificate " - "from '" << m_crtfile << "' or the cert is invalid. " - "Generating new certificate." << std::endl; + geulog::warn("core", "Failed to read server certificate from '", + m_crtfile, "' or the cert is invalid. " + "Generating new certificate."); cert.clear(); } else @@ -3374,9 +3361,9 @@ bool Reflector::loadServerCertificateFiles(void) time_t renew_time = tnow + (days*24*3600 + seconds)*RENEW_AFTER; if (!cert.timeIsWithinRange(tnow, renew_time)) { - std::cerr << "Time to renew the server certificate '" << m_crtfile - << "'. It's valid until " - << cert.notAfterLocaltimeString() << "." << std::endl; + geulog::info("core", "Time to renew the server certificate '", + m_crtfile, "'. It's valid until ", + cert.notAfterLocaltimeString(), "."); cert.clear(); generate_cert = true; } @@ -3395,8 +3382,8 @@ bool Reflector::loadServerCertificateFiles(void) csrfile = m_csrs_dir + "/" + cert_cn + ".csr"; } Async::SslCertSigningReq req; - std::cout << "Generating server certificate signing request file '" - << csrfile << "'" << std::endl; + geulog::info("core", "Generating server certificate signing request file '", + csrfile, "'"); req.setVersion(Async::SslCertSigningReq::VERSION_1); req.addSubjectName("CN", cert_cn); Async::SslX509Extensions req_exts; @@ -3426,17 +3413,14 @@ bool Reflector::loadServerCertificateFiles(void) { // FIXME: Read SSL error stack - std::cerr << "*** WARNING: Failed to write server certificate " - "signing request file to '" << csrfile << "'" - << std::endl; + geulog::warn("core", "Failed to write server certificate " + "signing request file to '", csrfile, "'"); //return false; } - std::cout << "-------- Certificate Signing Request -------" << std::endl; + geulog::info("core", "-------- Certificate Signing Request -------"); req.print(); - std::cout << "--------------------------------------------" << std::endl; - - std::cout << "Generating server certificate file '" << m_crtfile << "'" - << std::endl; + geulog::info("core", "--------------------------------------------"); + geulog::info("core", "Generating server certificate file '", m_crtfile, "'"); cert.setSerialNumber(); cert.setVersion(Async::SslX509::VERSION_3); cert.setIssuerName(m_issue_ca_cert.subjectName()); @@ -3449,27 +3433,25 @@ bool Reflector::loadServerCertificateFiles(void) if (!ensureDirectoryExist(m_crtfile) || !cert.writePemFile(m_crtfile) || !m_issue_ca_cert.appendPemFile(m_crtfile)) { - std::cout << "*** ERROR: Failed to write server certificate file '" - << m_crtfile << "'" << std::endl; + geulog::error("core", "Failed to write server certificate file '", + m_crtfile, "'"); return false; } } - std::cout << "------------ Server Certificate ------------" << std::endl; + geulog::info("core", "------------ Server Certificate ------------"); cert.print(); - std::cout << "--------------------------------------------" << std::endl; + geulog::info("core", "--------------------------------------------"); if (!m_ssl_ctx.setCertificateFiles(keyfile, m_crtfile)) { - std::cout << "*** ERROR: Failed to read and verify key ('" - << keyfile << "') and certificate ('" - << m_crtfile << "') files. " - << "If key- and cert-file does not match, the certificate " - "is invalid for any other reason, you need " - "to remove the cert file in order to trigger the " - "generation of a new certificate signing request." - "Then the CSR need to be signed by the CA which creates a " - "valid certificate." - << std::endl; + geulog::error("core", "Failed to read and verify key ('", + keyfile, "') and certificate ('", m_crtfile, "') files. " + "If key- and cert-file does not match, the certificate " + "is invalid for any other reason, you need " + "to remove the cert file in order to trigger the " + "generation of a new certificate signing request." + "Then the CSR need to be signed by the CA which creates a " + "valid certificate."); return false; } @@ -3485,8 +3467,8 @@ bool Reflector::generateKeyFile(Async::SslKeypair& pkey, pkey.generate(2048); if (!ensureDirectoryExist(keyfile) || !pkey.writePrivateKeyFile(keyfile)) { - std::cerr << "*** ERROR: Failed to write private key file to '" - << keyfile << "'" << std::endl; + geulog::error("core", "Failed to write private key file to '", + keyfile, "'"); return false; } return true; @@ -3503,25 +3485,25 @@ bool Reflector::loadRootCAFiles(void) } if (access(ca_keyfile.c_str(), F_OK) != 0) { - std::cout << "Root CA private key file not found. Generating '" - << ca_keyfile << "'" << std::endl; + geulog::info("core", "Root CA private key file not found. Generating '", + ca_keyfile, "'"); if (!m_ca_pkey.generate(4096)) { - std::cout << "*** ERROR: Failed to generate root CA key" << std::endl; + geulog::error("core", "Failed to generate root CA key"); return false; } if (!ensureDirectoryExist(ca_keyfile) || !m_ca_pkey.writePrivateKeyFile(ca_keyfile)) { - std::cerr << "*** ERROR: Failed to write root CA private key file to '" - << ca_keyfile << "'" << std::endl; + geulog::error("core", "Failed to write root CA private key file to '", + ca_keyfile, "'"); return false; } } else if (!m_ca_pkey.readPrivateKeyFile(ca_keyfile)) { - std::cerr << "*** ERROR: Failed to read root CA private key file from '" - << ca_keyfile << "'" << std::endl; + geulog::error("core", "Failed to read root CA private key file from '", + ca_keyfile, "'"); return false; } @@ -3538,16 +3520,15 @@ bool Reflector::loadRootCAFiles(void) !m_ca_cert.verify(m_ca_pkey) || !m_ca_cert.timeIsWithinRange()) { - std::cerr << "*** ERROR: Failed to read root CA certificate file " - "from '" << ca_crtfile << "' or the cert is invalid." - << std::endl; + geulog::error("core", "Failed to read root CA certificate file from '", + ca_crtfile, "' or the cert is invalid."); return false; } } if (generate_ca_cert) { - std::cout << "Generating root CA certificate file '" << ca_crtfile << "'" - << std::endl; + geulog::info("core", "Generating root CA certificate file '", + ca_crtfile, "'"); m_ca_cert.setSerialNumber(); m_ca_cert.setVersion(Async::SslX509::VERSION_3); @@ -3556,9 +3537,8 @@ bool Reflector::loadRootCAFiles(void) (void)m_cfg->getValue("ROOT_CA", "COMMON_NAME", value); if (value.empty()) { - std::cerr << "*** ERROR: The 'ROOT_CA/COMMON_NAME' variable is " - "unset which is needed for root CA certificate generation." - << std::endl; + geulog::error("core", "The 'ROOT_CA/COMMON_NAME' variable is " + "unset which is needed for root CA certificate generation."); return false; } m_ca_cert.addIssuerName("CN", value); @@ -3599,14 +3579,14 @@ bool Reflector::loadRootCAFiles(void) m_ca_cert.sign(m_ca_pkey); if (!m_ca_cert.writePemFile(ca_crtfile)) { - std::cout << "*** ERROR: Failed to write root CA certificate file '" - << ca_crtfile << "'" << std::endl; + geulog::error("core", "Failed to write root CA certificate file '", + ca_crtfile, "'"); return false; } } - std::cout << "----------- Root CA Certificate ------------" << std::endl; + geulog::info("core", "----------- Root CA Certificate ------------"); m_ca_cert.print(); - std::cout << "--------------------------------------------" << std::endl; + geulog::info("core", "--------------------------------------------"); return true; } /* Reflector::loadRootCAFiles */ @@ -3622,25 +3602,25 @@ bool Reflector::loadSigningCAFiles(void) } if (access(ca_keyfile.c_str(), F_OK) != 0) { - std::cout << "Issuing CA private key file not found. Generating '" - << ca_keyfile << "'" << std::endl; + geulog::info("core", "Issuing CA private key file not found. Generating '", + ca_keyfile, "'"); if (!m_issue_ca_pkey.generate(2048)) { - std::cout << "*** ERROR: Failed to generate CA key" << std::endl; + geulog::error("core", "Failed to generate CA key"); return false; } if (!ensureDirectoryExist(ca_keyfile) || !m_issue_ca_pkey.writePrivateKeyFile(ca_keyfile)) { - std::cerr << "*** ERROR: Failed to write issuing CA private key file " - "to '" << ca_keyfile << "'" << std::endl; + geulog::error("core", "Failed to write issuing CA private key file to '", + ca_keyfile, "'"); return false; } } else if (!m_issue_ca_pkey.readPrivateKeyFile(ca_keyfile)) { - std::cerr << "*** ERROR: Failed to read issuing CA private key file " - "from '" << ca_keyfile << "'" << std::endl; + geulog::error("core", "Failed to read issuing CA private key file from '", + ca_keyfile, "'"); return false; } @@ -3658,9 +3638,9 @@ bool Reflector::loadSigningCAFiles(void) !m_issue_ca_cert.timeIsWithinRange(); if (generate_ca_cert) { - std::cerr << "*** WARNING: Failed to read issuing CA certificate " - "from '" << ca_crtfile << "' or the cert is invalid. " - "Generating new certificate." << std::endl; + geulog::warn("core", "Failed to read issuing CA certificate from '", + ca_crtfile, "' or the cert is invalid. " + "Generating new certificate."); m_issue_ca_cert.clear(); } else @@ -3671,10 +3651,9 @@ bool Reflector::loadSigningCAFiles(void) time_t renew_time = tnow + (days*24*3600 + seconds)*RENEW_AFTER; if (!m_issue_ca_cert.timeIsWithinRange(tnow, renew_time)) { - std::cerr << "Time to renew the issuing CA certificate '" - << ca_crtfile << "'. It's valid until " - << m_issue_ca_cert.notAfterLocaltimeString() << "." - << std::endl; + geulog::info("core", "Time to renew the issuing CA certificate '", + ca_crtfile, "'. It's valid until ", + m_issue_ca_cert.notAfterLocaltimeString(), "."); m_issue_ca_cert.clear(); generate_ca_cert = true; } @@ -3688,8 +3667,8 @@ bool Reflector::loadSigningCAFiles(void) { ca_csrfile = m_csrs_dir + "/svxreflector_issuing_ca.csr"; } - std::cout << "Generating issuing CA CSR file '" << ca_csrfile - << "'" << std::endl; + geulog::info("core", "Generating issuing CA CSR file '", + ca_csrfile, "'"); Async::SslCertSigningReq csr; csr.setVersion(Async::SslCertSigningReq::VERSION_1); std::string value; @@ -3697,9 +3676,9 @@ bool Reflector::loadSigningCAFiles(void) (void)m_cfg->getValue("ISSUING_CA", "COMMON_NAME", value); if (value.empty()) { - std::cerr << "*** ERROR: The 'ISSUING_CA/COMMON_NAME' variable is " - "unset which is needed for issuing CA certificate " - "generation." << std::endl; + geulog::error("core", "The 'ISSUING_CA/COMMON_NAME' variable is " + "unset which is needed for issuing CA certificate " + "generation."); return false; } csr.addSubjectName("CN", value); @@ -3738,13 +3717,13 @@ bool Reflector::loadSigningCAFiles(void) //csr.print(); if (!csr.writePemFile(ca_csrfile)) { - std::cout << "*** ERROR: Failed to write issuing CA CSR file '" - << ca_csrfile << "'" << std::endl; + geulog::error("core", "Failed to write issuing CA CSR file '", + ca_csrfile, "'"); return false; } - std::cout << "Generating issuing CA certificate file '" << ca_crtfile - << "'" << std::endl; + geulog::info("core", "Generating issuing CA certificate file '", + ca_crtfile, "'"); m_issue_ca_cert.setSerialNumber(); m_issue_ca_cert.setVersion(Async::SslX509::VERSION_3); m_issue_ca_cert.setSubjectName(csr.subjectName()); @@ -3755,14 +3734,14 @@ bool Reflector::loadSigningCAFiles(void) m_issue_ca_cert.sign(m_ca_pkey); if (!m_issue_ca_cert.writePemFile(ca_crtfile)) { - std::cout << "*** ERROR: Failed to write issuing CA certificate file '" - << ca_crtfile << "'" << std::endl; + geulog::error("core", "Failed to write issuing CA certificate file '", + ca_crtfile, "'"); return false; } } - std::cout << "---------- Issuing CA Certificate ----------" << std::endl; + geulog::info("core", "---------- Issuing CA Certificate ----------"); m_issue_ca_cert.print(); - std::cout << "--------------------------------------------" << std::endl; + geulog::info("core", "--------------------------------------------"); startCertRenewTimer(m_issue_ca_cert, m_renew_issue_ca_cert_timer); @@ -3781,11 +3760,10 @@ bool Reflector::onVerifyPeer(TcpConnection *con, bool preverify_ok, preverify_ok = preverify_ok && !cert.commonName().empty(); if (!preverify_ok) { - std::cout << "*** ERROR: Certificate verification failed for client" - << std::endl; - std::cout << "------------ Client Certificate -------------" << std::endl; + geulog::error("client", "Certificate verification failed for client"); + geulog::info("client", "------------ Client Certificate -------------"); cert.print(); - std::cout << "---------------------------------------------" << std::endl; + geulog::info("client", "---------------------------------------------"); } return preverify_ok; @@ -3827,7 +3805,7 @@ bool Reflector::removeClientCertFiles(const std::string& cn) { if (!isCallsignPathSafe(cn)) { - std::cerr << "*** ERROR: Unsafe callsign in removeClientCertFiles" << std::endl; + geulog::error("core", "Unsafe callsign in removeClientCertFiles"); return false; } @@ -3849,8 +3827,7 @@ bool Reflector::removeClientCertFiles(const std::string& cn) { success = false; auto errstr = SvxLink::strError(errno); - std::cerr << "*** WARNING: Failed to remove file '" << path << "': " - << errstr << std::endl; + geulog::warn("core", "Failed to remove file '", path, "': ", errstr); } } @@ -3867,14 +3844,14 @@ void Reflector::runCAHook(const Async::Exec::Environment& env) ca_hook->addEnvironmentVars(env); ca_hook->setTimeout(300); // Five minutes timeout ca_hook->stdoutData.connect( - [=](const char* buf, int cnt) + [=](const char* buf, int /*cnt*/) { - std::cout << buf; + geulog::info("core", buf); }); ca_hook->stderrData.connect( - [=](const char* buf, int cnt) + [=](const char* buf, int /*cnt*/) { - std::cerr << buf; + geulog::warn("core", buf); }); ca_hook->exited.connect( [=](void) { @@ -3882,14 +3859,14 @@ void Reflector::runCAHook(const Async::Exec::Environment& env) { if (ca_hook->exitStatus() != 0) { - std::cerr << "*** ERROR: CA hook exited with exit status " - << ca_hook->exitStatus() << std::endl; + geulog::error("core", "CA hook exited with exit status ", + ca_hook->exitStatus()); } } else if (ca_hook->ifSignaled()) { - std::cerr << "*** ERROR: CA hook exited with signal " - << ca_hook->termSig() << std::endl; + geulog::error("core", "CA hook exited with signal ", + ca_hook->termSig()); } Async::Application::app().runTask([=]{ delete ca_hook; }); }); @@ -4143,8 +4120,7 @@ void Reflector::initTwinServer(void) m_twin_srv->clientDisconnected.connect( sigc::mem_fun(*this, &Reflector::twinClientDisconnected)); - std::cout << "TWIN: server listening on port " << m_twin_listen_port - << std::endl; + geulog::info("twin", "TWIN: server listening on port ", m_twin_listen_port); } /* Reflector::initTwinServer */ diff --git a/src/svxlink/reflector/ReflectorClient.cpp b/src/svxlink/reflector/ReflectorClient.cpp index 28eb157..23a50c0 100644 --- a/src/svxlink/reflector/ReflectorClient.cpp +++ b/src/svxlink/reflector/ReflectorClient.cpp @@ -411,9 +411,8 @@ void ReflectorClient::onSslConnectionReady(TcpConnection *con) if (m_con_state != STATE_EXPECT_SSL_CON_READY) { - std::cout << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: SSL connection ready event unexpected" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] SSL connection ready event unexpected"); disconnect(); return; } @@ -423,9 +422,9 @@ void ReflectorClient::onSslConnectionReady(TcpConnection *con) Async::SslX509 peer_cert(con->sslPeerCertificate()); if (peer_cert.isNull()) { - std::cout << m_con->remoteHost() << ":" << m_con->remotePort() - << ": No client certificate. Requesting Certificate " - "Signing Request from client." << std::endl; + geulog::info("client", m_con->remoteHost(), ":", m_con->remotePort(), + ": No client certificate. Requesting Certificate " + "Signing Request from client."); sendMsg(MsgClientCsrRequest()); m_con_state = STATE_EXPECT_CSR; return; @@ -442,17 +441,15 @@ void ReflectorClient::onSslConnectionReady(TcpConnection *con) //return; } - std::cout << "------------- Client Certificate --------------" << std::endl; + geulog::info("client", "------------- Client Certificate --------------"); peer_cert.print(); - std::cout << "-----------------------------------------------" << std::endl; + geulog::info("client", "-----------------------------------------------"); std::string callsign = peer_cert.commonName(); if (!m_reflector->callsignOk(callsign)) { - std::cout << "*** WARNING[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: client certificate has invalid CN (callsign)" - << std::endl; + geulog::warn("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] client certificate has invalid CN (callsign)"); disconnect(); return; } @@ -471,7 +468,7 @@ void ReflectorClient::onSslConnectionReady(TcpConnection *con) m_renew_cert_timer.setTimeout(renew_time); m_renew_cert_timer.start(); - std::cout << callsign << ": " << peer_cert.subjectNameString() << std::endl; + geulog::info("client", callsign, ": ", peer_cert.subjectNameString()); connectionAuthenticated(callsign); } /* ReflectorClient::onSslConnectionReady */ @@ -662,9 +659,8 @@ void ReflectorClient::handleMsgCABundleRequest(std::istream& is) if (m_con_state != STATE_EXPECT_START_ENCRYPTION) { - std::cout << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() << "]: Unexpected MsgCABundleRequest" - << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Unexpected MsgCABundleRequest"); disconnect(); return; } @@ -683,9 +679,8 @@ void ReflectorClient::handleMsgStartEncryptionRequest(std::istream& is) if (m_con_state != STATE_EXPECT_START_ENCRYPTION) { - std::cout << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: Unexpected MsgStartEncryptionRequest" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Unexpected MsgStartEncryptionRequest"); disconnect(); return; } @@ -693,15 +688,14 @@ void ReflectorClient::handleMsgStartEncryptionRequest(std::istream& is) MsgStartEncryptionRequest msg; if (!msg.unpack(is)) { - std::cerr << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: Could not unpack MsgStartEncryptionRequest" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Could not unpack MsgStartEncryptionRequest"); disconnect(); return; } - std::cout << m_con->remoteHost() << ":" << m_con->remotePort() - << ": Starting encryption" << std::endl; + geulog::info("client", m_con->remoteHost(), ":", m_con->remotePort(), + ": Starting encryption"); m_con->setMaxRxFrameSize(ReflectorMsg::MAX_SSL_SETUP_FRAME_SIZE); sendMsg(MsgStartEncryption()); @@ -714,9 +708,8 @@ void ReflectorClient::handleMsgAuthResponse(std::istream& is) { if (m_con_state != STATE_EXPECT_AUTH_RESPONSE) { - std::cerr << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: Authentication response unexpected" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Authentication response unexpected"); sendError("Authentication response unexpected"); return; } @@ -724,20 +717,17 @@ void ReflectorClient::handleMsgAuthResponse(std::istream& is) MsgAuthResponse msg; if (!msg.unpack(is)) { - std::cerr << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: Could not unpack MsgAuthResponse" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Could not unpack MsgAuthResponse"); sendError("Illegal MsgAuthResponse protocol message received"); return; } if (!m_reflector->callsignOk(msg.callsign())) { - std::cerr << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() - << "]: Invalid node callsign '" << msg.callsign() - << "' in MsgAuthResponse" - << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Invalid node callsign '", msg.callsign(), + "' in MsgAuthResponse"); sendError("Invalid callsign"); return; } @@ -764,14 +754,13 @@ void ReflectorClient::handleMsgAuthResponse(std::istream& is) string auth_key = lookupUserKey(msg.callsign()); if (!auth_key.empty() && msg.verify(auth_key, m_auth_challenge)) { - std::cout << msg.callsign() << ": Received valid auth key" << std::endl; + geulog::info("client", msg.callsign(), ": Received valid auth key"); connectionAuthenticated(msg.callsign()); } else { - std::cerr << "*** ERROR[" << m_con->remoteHost() << ":" - << m_con->remotePort() << "]: Authentication failed for user '" - << msg.callsign() << "'" << std::endl; + geulog::error("client", "[", m_con->remoteHost(), ":", m_con->remotePort(), + "] Authentication failed for user '", msg.callsign(), "'"); sendError("Access denied"); } } /* ReflectorClient::handleMsgAuthResponse */ @@ -791,8 +780,8 @@ void ReflectorClient::handleMsgClientCsr(std::istream& is) if ((m_con_state != STATE_CONNECTED) && (m_con_state != STATE_EXPECT_CSR)) { - std::cerr << "*** ERROR[" << idss.str() - << "]: Certificate Signing Request unexpected" << std::endl; + geulog::error("client", "[", idss.str(), + "] Certificate Signing Request unexpected"); sendError("Certificate Signing Request unexpected"); return; } @@ -800,19 +789,19 @@ void ReflectorClient::handleMsgClientCsr(std::istream& is) MsgClientCsr msg; if (!msg.unpack(is)) { - std::cout << "*** ERROR[" << idss.str() - << "]: Could not unpack MsgClientCsr" << std::endl; + geulog::error("client", "[", idss.str(), + "] Could not unpack MsgClientCsr"); sendError("Illegal MsgClientCsr protocol message received"); return; } - std::cerr << idss.str() << ": Received CSR" << std::endl; + geulog::info("client", idss.str(), ": Received CSR"); Async::SslCertSigningReq req; if (!req.readPem(msg.csrPem()) || req.isNull()) { - std::cerr << "*** ERROR[" << idss.str() << "]: Invalid CSR received:\n" - << msg.csrPem() << std::endl; + geulog::error("client", "[", idss.str(), "] Invalid CSR received:\n", + msg.csrPem()); sendError("Invalid CSR received"); return; } @@ -820,7 +809,7 @@ void ReflectorClient::handleMsgClientCsr(std::istream& is) auto errstr = m_reflector->checkCsr(req); if (!errstr.empty()) { - std::cerr << "*** ERROR[" << idss.str() << "]: " << errstr << std::endl; + geulog::error("client", "[", idss.str(), "] ", errstr); sendError(errstr); return; } @@ -833,14 +822,14 @@ void ReflectorClient::handleMsgClientCsr(std::istream& is) ) && sendClientCert(cert)) { - std::cout << idss.str() << ": Sent certificate to peer" << std::endl; + geulog::info("client", idss.str(), ": Sent certificate to peer"); //cert.print(); m_con_state = STATE_EXPECT_DISCONNECT; } else if (m_con_state == STATE_EXPECT_CSR) { - std::cout << idss.str() << ": No valid certificate found matching CSR. " - "Sending authentication challenge." << std::endl; + geulog::info("client", idss.str(), ": No valid certificate found matching CSR. " + "Sending authentication challenge."); sendAuthChallenge(); m_con_state = STATE_EXPECT_AUTH_RESPONSE; } diff --git a/src/svxlink/reflector/ReflectorMsg.h b/src/svxlink/reflector/ReflectorMsg.h index 7aabffc..8f3d194 100644 --- a/src/svxlink/reflector/ReflectorMsg.h +++ b/src/svxlink/reflector/ReflectorMsg.h @@ -37,6 +37,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include #include +#include /**************************************************************************** @@ -373,9 +374,8 @@ class MsgAuthChallenge : public ReflectorMsgBase<10> if (rc != 1) { unsigned long err = ERR_get_error(); - std::cerr << "*** WARNING: Failed to generate challenge. " - "RAND_bytes failed with error code " << err - << std::endl; + geulog::warn("core", "Failed to generate challenge. " + "RAND_bytes failed with error code ", err); m_challenge.clear(); } } @@ -427,8 +427,7 @@ class MsgAuthResponse : public ReflectorMsgBase<11> { if (!calcHMAC(m_digest, key, challenge)) { - std::cerr << "*** ERROR: Digest calculation failed in MsgAuthResponse" - << std::endl; + geulog::error("core", "Digest calculation failed in MsgAuthResponse"); abort(); } } @@ -1440,15 +1439,13 @@ class MsgTrunkHello : public ReflectorMsgBase<115> int rc = RAND_bytes(&m_nonce.front(), NONCE_LEN); if (rc != 1) { - std::cerr << "*** WARNING: RAND_bytes failed in MsgTrunkHello" - << std::endl; + geulog::warn("core", "RAND_bytes failed in MsgTrunkHello"); m_nonce.clear(); return; } if (!calcHMAC(m_digest, secret, m_nonce.data(), m_nonce.size())) { - std::cerr << "*** ERROR: HMAC calculation failed in MsgTrunkHello" - << std::endl; + geulog::error("core", "HMAC calculation failed in MsgTrunkHello"); } } diff --git a/src/svxlink/reflector/TrunkLink.cpp b/src/svxlink/reflector/TrunkLink.cpp index f71e832..46edd65 100644 --- a/src/svxlink/reflector/TrunkLink.cpp +++ b/src/svxlink/reflector/TrunkLink.cpp @@ -203,7 +203,7 @@ bool TrunkLink::initialize(void) std::string host_str; if (!m_cfg.getValue(m_section, "HOST", host_str) || host_str.empty()) { - cerr << "*** ERROR[" << m_section << "]: Missing HOST" << endl; + geulog::error("trunk", "[", m_section, "] Missing HOST"); return false; } @@ -220,9 +220,9 @@ bool TrunkLink::initialize(void) } if (m_peer_hosts.size() < 2) { - cerr << "*** ERROR[" << m_section - << "]: PAIRED=1 requires HOST to list at least 2 hosts, got '" - << host_str << "'" << endl; + geulog::error("trunk", "[", m_section, + "] PAIRED=1 requires HOST to list at least 2 hosts, got '", + host_str, "'"); return false; } } @@ -237,7 +237,7 @@ bool TrunkLink::initialize(void) // SECRET if (!m_cfg.getValue(m_section, "SECRET", m_secret) || m_secret.empty()) { - cerr << "*** ERROR[" << m_section << "]: Missing SECRET" << endl; + geulog::error("trunk", "[", m_section, "] Missing SECRET"); return false; } @@ -247,7 +247,7 @@ bool TrunkLink::initialize(void) m_local_prefix = splitPrefixes(local_prefix_str); if (m_local_prefix.empty()) { - cerr << "*** ERROR: Missing or empty LOCAL_PREFIX in [GLOBAL]" << endl; + geulog::error("trunk", "Missing or empty LOCAL_PREFIX in [GLOBAL]"); return false; } @@ -256,13 +256,13 @@ bool TrunkLink::initialize(void) if (!m_cfg.getValue(m_section, "REMOTE_PREFIX", remote_prefix_str) || remote_prefix_str.empty()) { - cerr << "*** ERROR[" << m_section << "]: Missing REMOTE_PREFIX" << endl; + geulog::error("trunk", "[", m_section, "] Missing REMOTE_PREFIX"); return false; } m_remote_prefix = splitPrefixes(remote_prefix_str); if (m_remote_prefix.empty()) { - cerr << "*** ERROR[" << m_section << "]: Invalid REMOTE_PREFIX" << endl; + geulog::error("trunk", "[", m_section, "] Invalid REMOTE_PREFIX"); return false; } @@ -281,8 +281,8 @@ bool TrunkLink::initialize(void) { m_blacklist_filter = TgFilter::parse(blacklist_str); if (!m_blacklist_filter.empty()) - cout << m_section << ": Blacklisted TGs: " - << m_blacklist_filter.toString() << endl; + geulog::info("trunk", m_section, ": Blacklisted TGs: ", + m_blacklist_filter.toString()); } // ALLOW_TGS — flexible whitelist (empty = allow all) @@ -291,8 +291,8 @@ bool TrunkLink::initialize(void) { m_allow_filter = TgFilter::parse(allow_str); if (!m_allow_filter.empty()) - cout << m_section << ": Allowed TGs: " - << m_allow_filter.toString() << endl; + geulog::info("trunk", m_section, ": Allowed TGs: ", + m_allow_filter.toString()); } // TG_MAP — bidirectional TG remap, syntax "peer:local,peer2:local2" @@ -314,25 +314,29 @@ bool TrunkLink::initialize(void) } if (!m_tg_map_in.empty()) { - cout << m_section << ": TG mappings (peer<->local):"; + std::ostringstream _tgmap_oss; + _tgmap_oss << m_section << ": TG mappings (peer<->local):"; for (const auto& kv : m_tg_map_in) - cout << " " << kv.first << "<->" << kv.second; - cout << endl; + _tgmap_oss << " " << kv.first << "<->" << kv.second; + geulog::info("trunk", _tgmap_oss.str()); } } if (m_paired) { - cout << m_section << ": PAIRED=1, " << m_peer_hosts.size() - << " partner hosts:"; - for (const auto& x : m_peer_hosts) cout << " " << x; - cout << " port=" << m_peer_port - << " local_prefix=" << joinPrefixes(m_local_prefix) - << " remote_prefix=" << joinPrefixes(m_remote_prefix) - << (m_blacklist_filter.empty() ? "" : " [blacklist]") - << (m_allow_filter.empty() ? "" : " [whitelist]") - << (m_tg_map_in.empty() ? "" : " [tg_map]") - << endl; + { + std::ostringstream _paired_oss; + _paired_oss << m_section << ": PAIRED=1, " << m_peer_hosts.size() + << " partner hosts:"; + for (const auto& x : m_peer_hosts) _paired_oss << " " << x; + _paired_oss << " port=" << m_peer_port + << " local_prefix=" << joinPrefixes(m_local_prefix) + << " remote_prefix=" << joinPrefixes(m_remote_prefix) + << (m_blacklist_filter.empty() ? "" : " [blacklist]") + << (m_allow_filter.empty() ? "" : " [whitelist]") + << (m_tg_map_in.empty() ? "" : " [tg_map]"); + geulog::info("trunk", _paired_oss.str()); + } // Create one outbound client per host m_ob_cons.reserve(m_peer_hosts.size()); @@ -359,20 +363,18 @@ bool TrunkLink::initialize(void) client->connect(); m_ob_cons.push_back(client); - cout << m_section << ": paired outbound #" << i - << " connecting to " << m_peer_hosts[i] << ":" << m_peer_port - << endl; + geulog::info("trunk", m_section, ": paired outbound #", i, + " connecting to ", m_peer_hosts[i], ":", m_peer_port); } return true; } - cout << m_section << ": Trunk to " << m_peer_host << ":" << m_peer_port - << " local_prefix=" << joinPrefixes(m_local_prefix) - << " remote_prefix=" << joinPrefixes(m_remote_prefix) - << (m_blacklist_filter.empty() ? "" : " [blacklist]") - << (m_allow_filter.empty() ? "" : " [whitelist]") - << (m_tg_map_in.empty() ? "" : " [tg_map]") - << endl; + geulog::info("trunk", m_section, ": Trunk to ", m_peer_host, ":", m_peer_port, + " local_prefix=", joinPrefixes(m_local_prefix), + " remote_prefix=", joinPrefixes(m_remote_prefix), + (m_blacklist_filter.empty() ? "" : " [blacklist]"), + (m_allow_filter.empty() ? "" : " [whitelist]"), + (m_tg_map_in.empty() ? "" : " [tg_map]")); m_con.addStaticSRVRecord(0, 0, 0, m_peer_port, m_peer_host); m_con.setReconnectMinTime(2000); diff --git a/src/svxlink/reflector/TwinLink.cpp b/src/svxlink/reflector/TwinLink.cpp index 25d9f07..8dc208f 100644 --- a/src/svxlink/reflector/TwinLink.cpp +++ b/src/svxlink/reflector/TwinLink.cpp @@ -122,7 +122,7 @@ bool TwinLink::initialize(void) if (!m_cfg.getValue(m_peer_id_config, "HOST", m_peer_host) || m_peer_host.empty()) { - cerr << "*** ERROR[TWIN]: Missing HOST in [TWIN] section" << endl; + geulog::error("twin", "[TWIN] Missing HOST in [TWIN] section"); return false; } @@ -137,7 +137,7 @@ bool TwinLink::initialize(void) if (!m_cfg.getValue(m_peer_id_config, "SECRET", m_secret) || m_secret.empty()) { - cerr << "*** ERROR[TWIN]: Missing SECRET in [TWIN] section" << endl; + geulog::error("twin", "[TWIN] Missing SECRET in [TWIN] section"); return false; } @@ -145,13 +145,13 @@ bool TwinLink::initialize(void) if (!m_cfg.getValue("GLOBAL", "LOCAL_PREFIX", m_local_prefix) || m_local_prefix.empty()) { - cerr << "*** ERROR[TWIN]: Missing LOCAL_PREFIX in [GLOBAL]" << endl; + geulog::error("twin", "[TWIN] Missing LOCAL_PREFIX in [GLOBAL]"); return false; } - cout << "TWIN: partner=" << m_peer_host << ":" << m_peer_port - << " local_prefix=" << m_local_prefix - << " priority=" << m_priority << endl; + geulog::info("twin", "TWIN: partner=", m_peer_host, ":", m_peer_port, + " local_prefix=", m_local_prefix, + " priority=", m_priority); m_con.addStaticSRVRecord(0, 0, 0, m_peer_port, m_peer_host); m_con.setReconnectMinTime(2000); diff --git a/src/svxlink/reflector/svxreflector.cpp b/src/svxlink/reflector/svxreflector.cpp index bf1512a..7dc0ff8 100644 --- a/src/svxlink/reflector/svxreflector.cpp +++ b/src/svxlink/reflector/svxreflector.cpp @@ -172,8 +172,7 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) cfg.getValue("REDIS", "KEY_PREFIX", rcfg.key_prefix); cfg.getValue("REDIS", "UNIX_SOCKET", rcfg.unix_socket); if (rcfg.host.empty() && rcfg.unix_socket.empty()) { - std::cerr << "*** ERROR: --import-conf-to-redis requires [REDIS] in conf" - << std::endl; + geulog::error("core", "--import-conf-to-redis requires [REDIS] in conf"); return false; } @@ -184,8 +183,8 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) ? redisConnectWithTimeout(rcfg.host.c_str(), rcfg.port, tv) : redisConnectUnixWithTimeout(rcfg.unix_socket.c_str(), tv); if (!ctx || ctx->err) { - std::cerr << "*** ERROR: import: Redis connect failed: " - << (ctx ? ctx->errstr : "alloc failed") << std::endl; + geulog::error("core", "import: Redis connect failed: ", + (ctx ? ctx->errstr : "alloc failed")); if (ctx) redisFree(ctx); return false; } @@ -210,8 +209,8 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) if (!cfg.getValue("USERS", cs, group) || group.empty()) continue; std::string ukey = kf("user:" + cs); if (dry_run) { - std::cout << "[dry-run] HSET " << ukey << " group " << group - << " enabled 1" << std::endl; + geulog::info("core", "[dry-run] HSET ", ukey, " group ", group, + " enabled 1"); } else { redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s group %s enabled 1", ukey.c_str(), group.c_str()); @@ -225,8 +224,7 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) if (!cfg.getValue("PASSWORDS", g, pw) || pw.empty()) continue; std::string gkey = kf("group:" + g); if (dry_run) { - std::cout << "[dry-run] HSET " << gkey << " password " - << std::endl; + geulog::info("core", "[dry-run] HSET ", gkey, " password "); } else { redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s password %s", gkey.c_str(), pw.c_str()); @@ -248,7 +246,7 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) tok.erase(tok.find_last_not_of(" \t") + 1); if (tok.empty()) continue; if (dry_run) { - std::cout << "[dry-run] SADD " << ck << " " << tok << std::endl; + geulog::info("core", "[dry-run] SADD ", ck, " ", tok); } else { redisReply* r = (redisReply*)redisCommand(ctx, "SADD %s %s", ck.c_str(), tok.c_str()); @@ -279,9 +277,8 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) auto setField = [&](const char* field, const std::string& value) { if (value.empty()) return; if (dry_run) { - std::cout << "[dry-run] HSET " << pk << " " << field - << " " << (std::string(field) == "secret" ? "" : value) - << std::endl; + geulog::info("core", "[dry-run] HSET ", pk, " ", field, " ", + (std::string(field) == "secret" ? "" : value)); } else { redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s %s %s", pk.c_str(), field, value.c_str()); @@ -307,7 +304,7 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) tok.erase(tok.find_last_not_of(" \t") + 1); if (tok.empty()) continue; if (dry_run) { - std::cout << "[dry-run] SADD " << k << " " << tok << std::endl; + geulog::info("core", "[dry-run] SADD ", k, " ", tok); } else { redisReply* r = (redisReply*)redisCommand(ctx, "SADD %s %s", k.c_str(), tok.c_str()); @@ -336,8 +333,7 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) local.erase(0, local.find_first_not_of(" \t")); local.erase(local.find_last_not_of(" \t") + 1); if (dry_run) { - std::cout << "[dry-run] HSET " << k << " " << peer - << " " << local << std::endl; + geulog::info("core", "[dry-run] HSET ", k, " ", peer, " ", local); } else { redisReply* r = (redisReply*)redisCommand(ctx, "HSET %s %s %s", k.c_str(), peer.c_str(), local.c_str()); @@ -347,9 +343,8 @@ static bool runImportConfToRedis(Async::Config& cfg, bool dry_run) } } - std::cout << "Imported " << users << " users, " << groups << " groups, " - << cluster_n << " cluster TGs, " << trunks_n << " trunks." - << std::endl; + geulog::info("core", "Imported ", users, " users, ", groups, " groups, ", + cluster_n, " cluster TGs, ", trunks_n, " trunks."); if (ctx) redisFree(ctx); return true; @@ -491,8 +486,7 @@ int main(int argc, const char *argv[]) cfg_filename = string(config); if (!cfg.open(cfg_filename)) { - cerr << "*** ERROR: Could not open configuration file: " - << config << endl; + geulog::error("core", "Could not open configuration file: ", config); exit(1); } } @@ -505,18 +499,19 @@ int main(int argc, const char *argv[]) cfg_filename = SVX_SYSCONF_INSTALL_DIR "/svxreflector.conf"; if (!cfg.open(cfg_filename)) { - cerr << "*** ERROR: Could not open configuration file"; - if (errno != 0) { - cerr << " (" << strerror(errno) << ")"; + std::ostringstream _cfg_oss; + _cfg_oss << "Could not open configuration file"; + if (errno != 0) _cfg_oss << " (" << strerror(errno) << ")"; + _cfg_oss << ".\n" + << "Tried the following paths:\n" + << "\t" << home_dir << "/.svxlink/svxreflector.conf\n" + << "\t" SVX_SYSCONF_INSTALL_DIR "/svxreflector.conf\n" + << "Possible reasons for failure are: None of the files exist,\n" + << "you do not have permission to read the file or there was a\n" + << "syntax error in the file."; + geulog::error("core", _cfg_oss.str()); } - cerr << ".\n"; - cerr << "Tried the following paths:\n" - << "\t" << home_dir << "/.svxlink/svxreflector.conf\n" - << "\t" SVX_SYSCONF_INSTALL_DIR "/svxreflector.conf\n" - << "Possible reasons for failure are: None of the files exist,\n" - << "you do not have permission to read the file or there was a\n" - << "syntax error in the file.\n"; exit(1); } } @@ -541,8 +536,8 @@ int main(int argc, const char *argv[]) DIR *dir = opendir(cfg_dir.c_str()); if (dir == NULL) { - cerr << "*** ERROR: Could not read from directory spcified by " - << "configuration variable GLOBAL/CFG_DIR=" << cfg_dir << endl; + geulog::error("core", "Could not read from directory specified by " + "configuration variable GLOBAL/CFG_DIR=", cfg_dir); exit(1); } @@ -558,16 +553,16 @@ int main(int argc, const char *argv[]) cfg_filename = cfg_dir + "/" + dirent->d_name; if (!cfg.open(cfg_filename)) { - cerr << "*** ERROR: Could not open configuration file: " - << cfg_filename << endl; + geulog::error("core", "Could not open configuration file: ", + cfg_filename); exit(1); } } if (closedir(dir) == -1) { - cerr << "*** ERROR: Error closing directory specified by" - << "configuration variable GLOBAL/CFG_DIR=" << cfg_dir << endl; + geulog::error("core", "Error closing directory specified by " + "configuration variable GLOBAL/CFG_DIR=", cfg_dir); exit(1); } } @@ -578,20 +573,19 @@ int main(int argc, const char *argv[]) if (!geulog::configure(cfg)) { - cerr << "*** ERROR: logging facade configuration failed" << endl; + geulog::error("core", "logging facade configuration failed"); exit(1); } - cout << PROGRAM_NAME " v" SVXREFLECTOR_VERSION - " Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX\n"; - cout << "Modified with server-to-server trunk support by IW1GEU.\n\n"; - cout << PROGRAM_NAME " comes with ABSOLUTELY NO WARRANTY. " - "This is free software, and you are\n"; - cout << "welcome to redistribute it in accordance with the " - "terms and conditions in the\n"; - cout << "GNU GPL (General Public License) version 2 or later.\n"; - - cout << "\nUsing configuration file: " << main_cfg_filename << endl; + geulog::info("core", PROGRAM_NAME " v" SVXREFLECTOR_VERSION + " Copyright (C) 2003-2025 Tobias Blomberg / SM0SVX"); + geulog::info("core", "Modified with server-to-server trunk support by IW1GEU."); + geulog::info("core", PROGRAM_NAME " comes with ABSOLUTELY NO WARRANTY. " + "This is free software, and you are"); + geulog::info("core", "welcome to redistribute it in accordance with the " + "terms and conditions in the"); + geulog::info("core", "GNU GPL (General Public License) version 2 or later."); + geulog::info("core", "Using configuration file: ", main_cfg_filename); struct termios org_termios = {0}; if (logfile_name == 0) @@ -615,13 +609,12 @@ int main(int argc, const char *argv[]) Reflector ref; if (ref.initialize(cfg)) { - std::cout << "NOTICE: Initialization done. Starting main application." - << std::endl; + geulog::info("core", "NOTICE: Initialization done. Starting main application."); app.exec(); } else { - cerr << ":-(" << endl; + geulog::error("core", ":-( Initialization failed"); } if (stdin_watch != 0) @@ -722,7 +715,7 @@ static void parse_arguments(int argc, const char **argv) if (print_version) { - std::cout << SVXREFLECTOR_VERSION << std::endl; + geulog::info("core", SVXREFLECTOR_VERSION); exit(0); } } /* parse_arguments */ @@ -766,10 +759,10 @@ static void sighup_handler(int signal) { if (logfile_name == 0) { - cout << "Ignoring SIGHUP\n"; + geulog::info("core", "Ignoring SIGHUP"); return; } - std::cout << "SIGHUP received" << std::endl; + geulog::info("core", "SIGHUP received"); logwriter.reopenLogfile(); } /* sighup_handler */ @@ -790,9 +783,8 @@ static void sigterm_handler(int signal) break; } - std::cout << "\nNOTICE: " << signame - << " received. Shutting down application..." - << std::endl; + geulog::info("core", "NOTICE: ", signame, + " received. Shutting down application..."); Application::app().quit(); } /* sigterm_handler */