Branch data Line data Source code
# 1 : : // Copyright (c) 2021 The Bitcoin Core developers
# 2 : : // Distributed under the MIT software license, see the accompanying
# 3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 4 : :
# 5 : : #if defined(HAVE_CONFIG_H)
# 6 : : #include <config/bitcoin-config.h>
# 7 : : #endif
# 8 : :
# 9 : : #include <clientversion.h>
# 10 : : #include <compat/sanity.h>
# 11 : : #include <crypto/sha256.h>
# 12 : : #include <key.h>
# 13 : : #include <logging.h>
# 14 : : #include <node/ui_interface.h>
# 15 : : #include <pubkey.h>
# 16 : : #include <random.h>
# 17 : : #include <util/system.h>
# 18 : : #include <util/time.h>
# 19 : : #include <util/translation.h>
# 20 : :
# 21 : : #include <memory>
# 22 : :
# 23 : : static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
# 24 : :
# 25 : : namespace init {
# 26 : : void SetGlobals()
# 27 : 664 : {
# 28 : 664 : std::string sha256_algo = SHA256AutoDetect();
# 29 : 664 : LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
# 30 : 664 : RandomInit();
# 31 : 664 : ECC_Start();
# 32 : 664 : globalVerifyHandle.reset(new ECCVerifyHandle());
# 33 : 664 : }
# 34 : :
# 35 : : void UnsetGlobals()
# 36 : 663 : {
# 37 : 663 : globalVerifyHandle.reset();
# 38 : 663 : ECC_Stop();
# 39 : 663 : }
# 40 : :
# 41 : : bool SanityChecks()
# 42 : 664 : {
# 43 [ - + ]: 664 : if (!ECC_InitSanityCheck()) {
# 44 : 0 : return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
# 45 : 0 : }
# 46 : :
# 47 [ - + ]: 664 : if (!glibcxx_sanity_test())
# 48 : 0 : return false;
# 49 : :
# 50 [ - + ]: 664 : if (!Random_SanityCheck()) {
# 51 : 0 : return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
# 52 : 0 : }
# 53 : :
# 54 [ - + ]: 664 : if (!ChronoSanityCheck()) {
# 55 : 0 : return InitError(Untranslated("Clock epoch mismatch. Aborting."));
# 56 : 0 : }
# 57 : :
# 58 : 664 : return true;
# 59 : 664 : }
# 60 : :
# 61 : : void AddLoggingArgs(ArgsManager& argsman)
# 62 : 1536 : {
# 63 : 1536 : argsman.AddArg("-debuglogfile=<file>", strprintf("Specify location of debug log file. Relative paths will be prefixed by a net-specific datadir location. (-nodebuglogfile to disable; default: %s)", DEFAULT_DEBUGLOGFILE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
# 64 : 1536 : argsman.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
# 65 : 1536 : "If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + LogInstance().LogCategoriesString() + ". This option can be specified multiple times to output multiple categories.",
# 66 : 1536 : ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 67 : 1536 : argsman.AddArg("-debugexclude=<category>", strprintf("Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except the specified category. This option can be specified multiple times to exclude multiple categories."), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 68 : 1536 : argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 69 : 1536 : argsman.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 70 : : #ifdef HAVE_THREAD_LOCAL
# 71 : : argsman.AddArg("-logthreadnames", strprintf("Prepend debug output with name of the originating thread (only available on platforms supporting thread_local) (default: %u)", DEFAULT_LOGTHREADNAMES), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 72 : : #else
# 73 : 1536 : argsman.AddHiddenArgs({"-logthreadnames"});
# 74 : 1536 : #endif
# 75 : 1536 : argsman.AddArg("-logsourcelocations", strprintf("Prepend debug output with name of the originating source location (source file, line number and function name) (default: %u)", DEFAULT_LOGSOURCELOCATIONS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 76 : 1536 : argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
# 77 : 1536 : argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -daemon. To disable logging to file, set -nodebuglogfile)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 78 : 1536 : argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
# 79 : 1536 : }
# 80 : :
# 81 : : void SetLoggingOptions(const ArgsManager& args)
# 82 : 1516 : {
# 83 : 1516 : LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
# 84 : 1516 : LogInstance().m_file_path = AbsPathForConfigVal(args.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
# 85 : 1516 : LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
# 86 : 1516 : LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
# 87 : 1516 : LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
# 88 : : #ifdef HAVE_THREAD_LOCAL
# 89 : : LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
# 90 : : #endif
# 91 : 1516 : LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
# 92 : :
# 93 : 1516 : fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
# 94 : 1516 : }
# 95 : :
# 96 : : void SetLoggingCategories(const ArgsManager& args)
# 97 : 1514 : {
# 98 [ + - ]: 1514 : if (args.IsArgSet("-debug")) {
# 99 : : // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
# 100 : 1514 : const std::vector<std::string> categories = args.GetArgs("-debug");
# 101 : :
# 102 [ + - ]: 1514 : if (std::none_of(categories.begin(), categories.end(),
# 103 [ - + ][ - + ]: 1514 : [](std::string cat){return cat == "0" || cat == "none";})) {
# 104 [ + + ]: 1514 : for (const auto& cat : categories) {
# 105 [ - + ]: 1514 : if (!LogInstance().EnableCategory(cat)) {
# 106 : 0 : InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
# 107 : 0 : }
# 108 : 1514 : }
# 109 : 1514 : }
# 110 : 1514 : }
# 111 : :
# 112 : : // Now remove the logging categories which were explicitly excluded
# 113 [ + + ]: 3028 : for (const std::string& cat : args.GetArgs("-debugexclude")) {
# 114 [ - + ]: 3028 : if (!LogInstance().DisableCategory(cat)) {
# 115 : 0 : InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
# 116 : 0 : }
# 117 : 3028 : }
# 118 : 1514 : }
# 119 : :
# 120 : : bool StartLogging(const ArgsManager& args)
# 121 : 663 : {
# 122 [ + + ]: 663 : if (LogInstance().m_print_to_file) {
# 123 [ - + ]: 662 : if (args.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
# 124 : : // Do this first since it both loads a bunch of debug.log into memory,
# 125 : : // and because this needs to happen before any other debug.log printing
# 126 : 0 : LogInstance().ShrinkDebugFile();
# 127 : 0 : }
# 128 : 662 : }
# 129 [ + + ]: 663 : if (!LogInstance().StartLogging()) {
# 130 : 2 : return InitError(strprintf(Untranslated("Could not open debug log file %s"),
# 131 : 2 : LogInstance().m_file_path.string()));
# 132 : 2 : }
# 133 : :
# 134 [ - + ]: 661 : if (!LogInstance().m_log_timestamps)
# 135 : 661 : LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
# 136 : 661 : LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
# 137 : 661 : LogPrintf("Using data directory %s\n", gArgs.GetDataDirNet().string());
# 138 : :
# 139 : : // Only log conf file usage message if conf file actually exists.
# 140 : 661 : fs::path config_file_path = GetConfigFile(args.GetArg("-conf", BITCOIN_CONF_FILENAME));
# 141 [ + - ]: 661 : if (fs::exists(config_file_path)) {
# 142 : 661 : LogPrintf("Config file: %s\n", config_file_path.string());
# 143 [ # # ]: 661 : } else if (args.IsArgSet("-conf")) {
# 144 : : // Warn if no conf file exists at path provided by user
# 145 : 0 : InitWarning(strprintf(_("The specified config file %s does not exist"), config_file_path.string()));
# 146 : 0 : } else {
# 147 : : // Not categorizing as "Warning" because it's the default behavior
# 148 : 0 : LogPrintf("Config file: %s (not found, skipping)\n", config_file_path.string());
# 149 : 0 : }
# 150 : :
# 151 : : // Log the config arguments to debug.log
# 152 : 661 : args.LogArgs();
# 153 : :
# 154 : 661 : return true;
# 155 : 661 : }
# 156 : :
# 157 : : void LogPackageVersion()
# 158 : 1516 : {
# 159 : 1516 : std::string version_string = FormatFullVersion();
# 160 : 1516 : #ifdef DEBUG
# 161 : 1516 : version_string += " (debug build)";
# 162 : : #else
# 163 : : version_string += " (release build)";
# 164 : : #endif
# 165 : 1516 : LogPrintf(PACKAGE_NAME " version %s\n", version_string);
# 166 : 1516 : }
# 167 : : } // namespace init
|