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
{
28
    std::string sha256_algo = SHA256AutoDetect();
29
    LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo);
30
    RandomInit();
31
    ECC_Start();
32
    globalVerifyHandle.reset(new ECCVerifyHandle());
33
}
34

            
35
void UnsetGlobals()
36
{
37
    globalVerifyHandle.reset();
38
    ECC_Stop();
39
}
40

            
41
bool SanityChecks()
42
{
43
    if (!ECC_InitSanityCheck()) {
44
        return InitError(Untranslated("Elliptic curve cryptography sanity check failure. Aborting."));
45
    }
46

            
47
    if (!glibcxx_sanity_test())
48
        return false;
49

            
50
    if (!Random_SanityCheck()) {
51
        return InitError(Untranslated("OS cryptographic RNG sanity check failure. Aborting."));
52
    }
53

            
54
    if (!ChronoSanityCheck()) {
55
        return InitError(Untranslated("Clock epoch mismatch. Aborting."));
56
    }
57

            
58
    return true;
59
}
60

            
61
void AddLoggingArgs(ArgsManager& argsman)
62
{
63
    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
    argsman.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
65
        "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
        ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
67
    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
    argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
69
    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
    argsman.AddHiddenArgs({"-logthreadnames"});
74
#endif
75
    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
    argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
77
    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
    argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
79
}
80

            
81
void SetLoggingOptions(const ArgsManager& args)
82
{
83
    LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
84
    LogInstance().m_file_path = AbsPathForConfigVal(fs::PathFromString(args.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)));
85
    LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
86
    LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
87
    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
    LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
92

            
93
    fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
94
}
95

            
96
void SetLoggingCategories(const ArgsManager& args)
97
{
98
    if (args.IsArgSet("-debug")) {
99
        // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
100
        const std::vector<std::string> categories = args.GetArgs("-debug");
101

            
102
        if (std::none_of(categories.begin(), categories.end(),
103
            [](std::string cat){return cat == "0" || cat == "none";})) {
104
            for (const auto& cat : categories) {
105
                if (!LogInstance().EnableCategory(cat)) {
106
                    InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
107
                }
108
            }
109
        }
110
    }
111

            
112
    // Now remove the logging categories which were explicitly excluded
113
    for (const std::string& cat : args.GetArgs("-debugexclude")) {
114
        if (!LogInstance().DisableCategory(cat)) {
115
            InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
116
        }
117
    }
118
}
119

            
120
bool StartLogging(const ArgsManager& args)
121
{
122
    if (LogInstance().m_print_to_file) {
123
        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
            LogInstance().ShrinkDebugFile();
127
        }
128
    }
129
    if (!LogInstance().StartLogging()) {
130
            return InitError(strprintf(Untranslated("Could not open debug log file %s"),
131
                fs::PathToString(LogInstance().m_file_path)));
132
    }
133

            
134
    if (!LogInstance().m_log_timestamps)
135
        LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
136
    LogPrintf("Default data directory %s\n", fs::PathToString(GetDefaultDataDir()));
137
    LogPrintf("Using data directory %s\n", fs::PathToString(gArgs.GetDataDirNet()));
138

            
139
    // Only log conf file usage message if conf file actually exists.
140
    fs::path config_file_path = GetConfigFile(args.GetArg("-conf", BITCOIN_CONF_FILENAME));
141
    if (fs::exists(config_file_path)) {
142
        LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
143
    } else if (args.IsArgSet("-conf")) {
144
        // Warn if no conf file exists at path provided by user
145
        InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path)));
146
    } else {
147
        // Not categorizing as "Warning" because it's the default behavior
148
        LogPrintf("Config file: %s (not found, skipping)\n", fs::PathToString(config_file_path));
149
    }
150

            
151
    // Log the config arguments to debug.log
152
    args.LogArgs();
153

            
154
    return true;
155
}
156

            
157
void LogPackageVersion()
158
{
159
    std::string version_string = FormatFullVersion();
160
#ifdef DEBUG
161
    version_string += " (debug build)";
162
#else
163
    version_string += " (release build)";
164
#endif
165
    LogPrintf(PACKAGE_NAME " version %s\n", version_string);
166
}
167
} // namespace init