LCOV - code coverage report
Current view: top level - src/init - common.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 88 105 83.8 %
Date: 2026-06-25 07:23:43 Functions: 10 10 100.0 %

          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 <bls/bls.h>
      10             : #include <clientversion.h>
      11             : #include <crypto/sha256.h>
      12             : #include <crypto/x11/dispatch.h>
      13             : #include <fs.h>
      14             : #include <key.h>
      15             : #include <logging.h>
      16             : #include <node/interface_ui.h>
      17             : #include <random.h>
      18             : #include <tinyformat.h>
      19             : #include <util/time.h>
      20             : #include <util/string.h>
      21             : #include <util/system.h>
      22             : #include <util/translation.h>
      23             : 
      24             : #include <algorithm>
      25             : #include <string>
      26             : #include <vector>
      27             : 
      28             : namespace init {
      29        3032 : void SetGlobals()
      30             : {
      31        3032 :     SapphireAutoDetect();
      32        3032 :     std::string sha256_algo = SHA256AutoDetect();
      33        3032 :     LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
      34        3032 :     RandomInit();
      35        3032 :     ECC_Start();
      36        3032 : }
      37             : 
      38        3030 : void UnsetGlobals()
      39             : {
      40        3030 :     ECC_Stop();
      41        3030 : }
      42             : 
      43        3032 : bool SanityChecks()
      44             : {
      45        3032 :     if (!ECC_InitSanityCheck()) {
      46           0 :         return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
      47             :     }
      48             : 
      49        3032 :     if (!BLSInit()) {
      50           0 :         return false;
      51             :     }
      52             : 
      53        3032 :     if (!Random_SanityCheck()) {
      54           0 :         return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
      55             :     }
      56             : 
      57        3032 :     return true;
      58        3032 : }
      59             : 
      60        3789 : void AddLoggingArgs(ArgsManager& argsman)
      61             : {
      62        3789 :     argsman.AddArg("-debuglogfile=<file>", strprintf("Specify location of debug log file (default: %s). Relative paths will be prefixed by a net-specific datadir location. Pass -nodebuglogfile to disable writing the log to a file.", DEFAULT_DEBUGLOGFILE), ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
      63        7578 :     argsman.AddArg("-debug=<category>", "Output debug and trace logging (default: -nodebug, supplying <category> is optional). "
      64        3789 :         "If <category> is not supplied or if <category> = 1, output all debug and trace logging. <category> can be: " + LogInstance().LogCategoriesString() + ". This option can be specified multiple times to output multiple categories.",
      65        3789 :         ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      66        3789 :     argsman.AddArg("-debugexclude=<category>", "Exclude debug and trace logging for a category. Can be used in conjunction with -debug=1 to output debug and trace logging for all categories except the specified category. This option can be specified multiple times to exclude multiple categories.", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      67        3789 :     argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      68        3789 :     argsman.AddArg("-loglevel=<level>|<category>:<level>", strprintf("Set the global or per-category severity level for logging categories enabled with the -debug configuration option or the logging RPC. Possible values are %s (default=%s). The following levels are always logged: error, warning, info. If <category>:<level> is supplied, the setting will override the global one and may be specified multiple times to set multiple category-specific levels. <category> can be: %s.", LogInstance().LogLevelsString(), LogInstance().LogLevelToStr(BCLog::DEFAULT_LOG_LEVEL), LogInstance().LogCategoriesString()), ArgsManager::DISALLOW_NEGATION | ArgsManager::DISALLOW_ELISION | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
      69        3789 :     argsman.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      70        3789 :     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 | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
      71        3789 :     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);
      72        3789 :     argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
      73        3789 :     argsman.AddArg("-loglevelalways", strprintf("Always prepend a category and level (default: %u)", DEFAULT_LOGLEVELALWAYS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      74        3789 :     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);
      75        3789 :     argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
      76        3789 : }
      77             : 
      78        3735 : void SetLoggingOptions(const ArgsManager& args)
      79             : {
      80        3735 :     LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
      81        3735 :     LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
      82        3735 :     LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
      83        3735 :     LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
      84        3735 :     LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
      85        3735 :     LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
      86        3735 :     LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
      87        3735 :     LogInstance().m_always_print_category_level = args.GetBoolArg("-loglevelalways", DEFAULT_LOGLEVELALWAYS);
      88             : 
      89        3735 :     fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
      90        3735 : }
      91             : 
      92        3724 : void SetLoggingLevel(const ArgsManager& args)
      93             : {
      94        3724 :     if (args.IsArgSet("-loglevel")) {
      95        7450 :         for (const std::string& level_str : args.GetArgs("-loglevel")) {
      96        3726 :             if (level_str.find_first_of(':', 3) == std::string::npos) {
      97             :                 // user passed a global log level, i.e. -loglevel=<level>
      98        3723 :                 if (!LogInstance().SetLogLevel(level_str)) {
      99           0 :                     InitWarning(strprintf(_("Unsupported global logging level -loglevel=%s. Valid values: %s."), level_str, LogInstance().LogLevelsString()));
     100           0 :                 }
     101        3723 :             } else {
     102             :                 // user passed a category-specific log level, i.e. -loglevel=<category>:<level>
     103           3 :                 const auto& toks = SplitString(level_str, ':');
     104           3 :                 if (!(toks.size() == 2 && LogInstance().SetCategoryLogLevel(toks[0], toks[1]))) {
     105           0 :                     InitWarning(strprintf(_("Unsupported category-specific logging level -loglevel=%s. Expected -loglevel=<category>:<loglevel>. Valid categories: %s. Valid loglevels: %s."), level_str, LogInstance().LogCategoriesString(), LogInstance().LogLevelsString()));
     106           0 :                 }
     107           3 :             }
     108             :         }
     109        3724 :     }
     110        3724 : }
     111             : 
     112        3721 : void SetLoggingCategories(const ArgsManager& args)
     113             : {
     114        3721 :     if (args.IsArgSet("-debug")) {
     115             :         // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
     116        3721 :         const std::vector<std::string> categories = args.GetArgs("-debug");
     117             : 
     118        3721 :         if (std::none_of(categories.begin(), categories.end(),
     119        3729 :             [](std::string cat){return cat == "0" || cat == "none";})) {
     120        7450 :             for (const auto& cat : categories) {
     121        3729 :                 if (!LogInstance().EnableCategory(cat)) {
     122           0 :                     InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
     123           0 :                 }
     124             :             }
     125        3721 :         }
     126        3721 :     }
     127             : 
     128             :     // Now remove the logging categories which were explicitly excluded
     129       14257 :     for (const std::string& cat : args.GetArgs("-debugexclude")) {
     130       10536 :         if (!LogInstance().DisableCategory(cat)) {
     131           0 :             InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
     132           0 :         }
     133             :     }
     134        3721 : }
     135             : 
     136        3027 : bool StartLogging(const ArgsManager& args)
     137             : {
     138        3027 :     if (LogInstance().m_print_to_file) {
     139        3025 :         if (args.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
     140             :             // Do this first since it both loads a bunch of debug.log into memory,
     141             :             // and because this needs to happen before any other debug.log printing
     142           0 :             LogInstance().ShrinkDebugFile();
     143           0 :         }
     144        3025 :     }
     145        3027 :     if (!LogInstance().StartLogging()) {
     146           8 :             return InitError(strprintf(Untranslated("Could not open debug log file %s"),
     147           4 :                 fs::PathToString(LogInstance().m_file_path)));
     148             :     }
     149             : 
     150        3023 :     if (!LogInstance().m_log_timestamps)
     151           0 :         LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
     152        3023 :     LogPrintf("Default data directory %s\n", fs::PathToString(GetDefaultDataDir()));
     153        3023 :     LogPrintf("Using data directory %s\n", fs::PathToString(gArgs.GetDataDirNet()));
     154             : 
     155             :     // Only log conf file usage message if conf file actually exists.
     156        3023 :     fs::path config_file_path = GetConfigFile(args.GetPathArg("-conf", BITCOIN_CONF_FILENAME));
     157        3023 :     if (fs::exists(config_file_path)) {
     158        3023 :         LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
     159        3023 :     } else if (args.IsArgSet("-conf")) {
     160             :         // Warn if no conf file exists at path provided by user
     161           0 :         InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path)));
     162           0 :     } else {
     163             :         // Not categorizing as "Warning" because it's the default behavior
     164           0 :         LogPrintf("Config file: %s (not found, skipping)\n", fs::PathToString(config_file_path));
     165             :     }
     166             : 
     167             :     // Log the config arguments to debug.log
     168        3023 :     args.LogArgs();
     169             : 
     170        3023 :     return true;
     171        3027 : }
     172             : 
     173        3735 : void LogPackageVersion()
     174             : {
     175        3735 :     std::string version_string = FormatFullVersion();
     176             : #ifdef DEBUG_CORE
     177             :     version_string += " (debug build)";
     178             : #else
     179        3735 :     version_string += " (release build)";
     180             : #endif
     181        3735 :     LogPrintf(PACKAGE_NAME " version %s\n", version_string);
     182        3735 : }
     183             : } // namespace init

Generated by: LCOV version 1.16