LCOV - code coverage report
Current view: top level - src/init - common.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 83 98 84.7 %
Date: 2022-04-21 14:51:19 Functions: 9 9 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 23 36 63.9 %

           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                 :        795 : {
#      28                 :        795 :     std::string sha256_algo = SHA256AutoDetect();
#      29                 :        795 :     LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
#      30                 :        795 :     RandomInit();
#      31                 :        795 :     ECC_Start();
#      32                 :        795 :     globalVerifyHandle.reset(new ECCVerifyHandle());
#      33                 :        795 : }
#      34                 :            : 
#      35                 :            : void UnsetGlobals()
#      36                 :        794 : {
#      37                 :        794 :     globalVerifyHandle.reset();
#      38                 :        794 :     ECC_Stop();
#      39                 :        794 : }
#      40                 :            : 
#      41                 :            : bool SanityChecks()
#      42                 :        795 : {
#      43         [ -  + ]:        795 :     if (!ECC_InitSanityCheck()) {
#      44                 :          0 :         return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
#      45                 :          0 :     }
#      46                 :            : 
#      47         [ -  + ]:        795 :     if (!glibcxx_sanity_test())
#      48                 :          0 :         return false;
#      49                 :            : 
#      50         [ -  + ]:        795 :     if (!Random_SanityCheck()) {
#      51                 :          0 :         return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
#      52                 :          0 :     }
#      53                 :            : 
#      54         [ -  + ]:        795 :     if (!ChronoSanityCheck()) {
#      55                 :          0 :         return InitError(Untranslated("Clock epoch mismatch. Aborting."));
#      56                 :          0 :     }
#      57                 :            : 
#      58                 :        795 :     return true;
#      59                 :        795 : }
#      60                 :            : 
#      61                 :            : void AddLoggingArgs(ArgsManager& argsman)
#      62                 :       1662 : {
#      63                 :       1662 :     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                 :       1662 :     argsman.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
#      65                 :       1662 :         "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                 :       1662 :         ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
#      67                 :       1662 :     argsman.AddArg("-debugexclude=<category>", "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                 :       1662 :     argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
#      69                 :       1662 :     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                 :       1662 :     argsman.AddHiddenArgs({"-logthreadnames"});
#      74                 :       1662 : #endif
#      75                 :       1662 :     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                 :       1662 :     argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
#      77                 :       1662 :     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                 :       1662 :     argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
#      79                 :       1662 : }
#      80                 :            : 
#      81                 :            : void SetLoggingOptions(const ArgsManager& args)
#      82                 :       1639 : {
#      83                 :       1639 :     LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
#      84                 :       1639 :     LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
#      85                 :       1639 :     LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
#      86                 :       1639 :     LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
#      87                 :       1639 :     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                 :       1639 :     LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
#      92                 :            : 
#      93                 :       1639 :     fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
#      94                 :       1639 : }
#      95                 :            : 
#      96                 :            : void SetLoggingCategories(const ArgsManager& args)
#      97                 :       1635 : {
#      98         [ +  - ]:       1635 :     if (args.IsArgSet("-debug")) {
#      99                 :            :         // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
#     100                 :       1635 :         const std::vector<std::string> categories = args.GetArgs("-debug");
#     101                 :            : 
#     102         [ +  - ]:       1635 :         if (std::none_of(categories.begin(), categories.end(),
#     103 [ -  + ][ -  + ]:       1635 :             [](std::string cat){return cat == "0" || cat == "none";})) {
#     104         [ +  + ]:       1635 :             for (const auto& cat : categories) {
#     105         [ -  + ]:       1635 :                 if (!LogInstance().EnableCategory(cat)) {
#     106                 :          0 :                     InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
#     107                 :          0 :                 }
#     108                 :       1635 :             }
#     109                 :       1635 :         }
#     110                 :       1635 :     }
#     111                 :            : 
#     112                 :            :     // Now remove the logging categories which were explicitly excluded
#     113         [ +  + ]:       3270 :     for (const std::string& cat : args.GetArgs("-debugexclude")) {
#     114         [ -  + ]:       3270 :         if (!LogInstance().DisableCategory(cat)) {
#     115                 :          0 :             InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
#     116                 :          0 :         }
#     117                 :       3270 :     }
#     118                 :       1635 : }
#     119                 :            : 
#     120                 :            : bool StartLogging(const ArgsManager& args)
#     121                 :        794 : {
#     122         [ +  + ]:        794 :     if (LogInstance().m_print_to_file) {
#     123         [ -  + ]:        793 :         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                 :        793 :     }
#     129         [ +  + ]:        794 :     if (!LogInstance().StartLogging()) {
#     130                 :          2 :             return InitError(strprintf(Untranslated("Could not open debug log file %s"),
#     131                 :          2 :                 fs::PathToString(LogInstance().m_file_path)));
#     132                 :          2 :     }
#     133                 :            : 
#     134         [ -  + ]:        792 :     if (!LogInstance().m_log_timestamps)
#     135                 :          0 :         LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
#     136                 :        792 :     LogPrintf("Default data directory %s\n", fs::PathToString(GetDefaultDataDir()));
#     137                 :        792 :     LogPrintf("Using data directory %s\n", fs::PathToString(gArgs.GetDataDirNet()));
#     138                 :            : 
#     139                 :            :     // Only log conf file usage message if conf file actually exists.
#     140                 :        792 :     fs::path config_file_path = GetConfigFile(args.GetArg("-conf", BITCOIN_CONF_FILENAME));
#     141         [ +  + ]:        792 :     if (fs::exists(config_file_path)) {
#     142                 :        791 :         LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
#     143         [ -  + ]:        791 :     } 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"), fs::PathToString(config_file_path)));
#     146                 :          1 :     } else {
#     147                 :            :         // Not categorizing as "Warning" because it's the default behavior
#     148                 :          1 :         LogPrintf("Config file: %s (not found, skipping)\n", fs::PathToString(config_file_path));
#     149                 :          1 :     }
#     150                 :            : 
#     151                 :            :     // Log the config arguments to debug.log
#     152                 :        792 :     args.LogArgs();
#     153                 :            : 
#     154                 :        792 :     return true;
#     155                 :        794 : }
#     156                 :            : 
#     157                 :            : void LogPackageVersion()
#     158                 :       1639 : {
#     159                 :       1639 :     std::string version_string = FormatFullVersion();
#     160                 :       1639 : #ifdef DEBUG
#     161                 :       1639 :     version_string += " (debug build)";
#     162                 :            : #else
#     163                 :            :     version_string += " (release build)";
#     164                 :            : #endif
#     165                 :       1639 :     LogPrintf(PACKAGE_NAME " version %s\n", version_string);
#     166                 :       1639 : }
#     167                 :            : } // namespace init

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a