Skip to content

Commit

Permalink
Refactor code so we can choose to send the version and build info to …
Browse files Browse the repository at this point in the history
…a specific stream
  • Loading branch information
omoerbeek committed Jul 23, 2024
1 parent b4fd9d0 commit e021a9b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 26 deletions.
8 changes: 5 additions & 3 deletions pdns/auth-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1226,8 +1226,8 @@ int main(int argc, char** argv)
::arg().laxParse(argc, argv); // do a lax parse

if (::arg().mustDo("version")) {
showProductVersion();
showBuildConfiguration();
cout << getProductVersion();
cout << getBuildConfiguration();
return 0;
}

Expand Down Expand Up @@ -1501,7 +1501,9 @@ int main(int argc, char** argv)

DLOG(g_log << Logger::Warning << "Verbose logging in effect" << endl);

showProductVersion();
for (const string& line : getProductVersionLines()) {
g_log << Logger::Info << line << endl;
}

try {
mainthread();
Expand Down
9 changes: 6 additions & 3 deletions pdns/recursordist/rec-main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3153,8 +3153,8 @@ int main(int argc, char** argv)
::arg().laxParse(argc, argv); // do a lax parse

if (::arg().mustDo("version")) {
showProductVersion();
showBuildConfiguration();
cout << getProductVersion();
cout << getBuildConfiguration();
return 0;
}
if (::arg().mustDo("help")) {
Expand All @@ -3174,7 +3174,10 @@ int main(int argc, char** argv)
}
g_log.setLoglevel(s_logUrgency);
g_log.toConsole(s_logUrgency);
showProductVersion();

for (const string& line : getProductVersionLines()) {
g_log << Logger::Info << line << endl;
}
if (!::arg().mustDo("structured-logging")) {
g_log << Logger::Error << "Disabling structured logging is not supported anymore" << endl;
}
Expand Down
39 changes: 28 additions & 11 deletions pdns/version.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
*/

#include "config.h"

#include "logger.hh"
#include "version.hh"
#include "namespaces.hh"

#ifdef PDNS_MODULES
#include "dnsbackend.hh"
#endif

#include <sstream>
#include <boost/algorithm/string/join.hpp>

static ProductType productType;
Expand Down Expand Up @@ -70,27 +73,40 @@ string productTypeApiType()
return "unknown";
}

void showProductVersion()
vector<string> getProductVersionLines()
{
g_log << Logger::Warning << productName() << " " << VERSION << " (C) "
vector<string> ret;
std::istringstream istr(getProductVersion());
for (string line; std::getline(istr, line);) {
ret.emplace_back(line);
}
return ret;
}

string getProductVersion()
{
ostringstream ret;
ret << productName() << " " << VERSION << " (C) "
"PowerDNS.COM BV"
<< endl;
g_log << Logger::Warning << "Using " << (sizeof(unsigned long) * 8) << "-bits mode. "
ret << "Using " << (sizeof(unsigned long) * 8) << "-bits mode. "
"Built using "
<< compilerVersion()
#ifndef REPRODUCIBLE
<< " on " __DATE__ " " __TIME__ " by " BUILD_HOST
#endif
<< "." << endl;
g_log << Logger::Warning << "PowerDNS comes with ABSOLUTELY NO WARRANTY. "
ret << "PowerDNS comes with ABSOLUTELY NO WARRANTY. "
"This is free software, and you are welcome to redistribute it "
"according to the terms of the GPL version 2."
<< endl;
return ret.str();
}

void showBuildConfiguration()
string getBuildConfiguration()
{
g_log << Logger::Warning << "Features: "
ostringstream ret;
ret << "Features: "
<<
#ifdef HAVE_LIBDECAF
"decaf "
Expand Down Expand Up @@ -184,19 +200,20 @@ void showBuildConfiguration()
endl;
#ifdef PDNS_MODULES
// Auth only
g_log << Logger::Warning << "Built-in modules: " << PDNS_MODULES << endl;
ret << "Built-in modules: " << PDNS_MODULES << endl;
const auto& modules = BackendMakers().getModules();
g_log << Logger::Warning << "Loaded modules: " << boost::join(modules, " ") << endl;
ret << "Loaded modules: " << boost::join(modules, " ") << endl;
#endif
// NOLINTBEGIN(cppcoreguidelines-macro-usage)
#ifdef PDNS_CONFIG_ARGS
#define double_escape(s) #s
#define escape_quotes(s) double_escape(s)
// NOLINTEND(cppcoreguidelines-macro-usage)
g_log << Logger::Warning << "Configured with: " << escape_quotes(PDNS_CONFIG_ARGS) << endl;
ret << "Configured with: " << escape_quotes(PDNS_CONFIG_ARGS) << endl;
#undef escape_quotes
#undef double_escape
#endif
return ret.str();
}

string fullVersionString()
Expand Down
19 changes: 10 additions & 9 deletions pdns/version.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,21 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <string>
#include "namespaces.hh"

enum ProductType
{
ProductAuthoritative,
ProductRecursor
};

std::string compilerVersion();
void showProductVersion();
void showBuildConfiguration();
std::string fullVersionString();
std::string getPDNSVersion();
std::string productName();
std::string productTypeApiType();
[[nodiscard]] std::string compilerVersion();
[[nodiscard]] std::vector<std::string> getProductVersionLines();
[[nodiscard]] std::string getProductVersion();
[[nodiscard]] std::string getBuildConfiguration();
[[nodiscard]] std::string fullVersionString();
[[nodiscard]] std::string getPDNSVersion();
[[nodiscard]] std::string productName();
[[nodiscard]] std::string productTypeApiType();
void versionSetProduct(ProductType productType_);
ProductType versionGetProduct();
[[nodiscard]] ProductType versionGetProduct();

0 comments on commit e021a9b

Please sign in to comment.