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 <fs.h>
11
#include <logging.h>
12
#include <node/interface_ui.h>
13
#include <tinyformat.h>
14
#include <util/system.h>
15
#include <util/time.h>
16
#include <util/translation.h>
17

            
18
#include <algorithm>
19
#include <string>
20
#include <vector>
21

            
22
namespace init {
23
void AddLoggingArgs(ArgsManager& argsman)
24
{
25
    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);
26
    argsman.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
27
        "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.",
28
        ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
29
    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);
30
    argsman.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
31
    argsman.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
32
#ifdef HAVE_THREAD_LOCAL
33
    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);
34
#else
35
    argsman.AddHiddenArgs({"-logthreadnames"});
36
#endif
37
    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);
38
    argsman.AddArg("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
39
    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);
40
    argsman.AddArg("-shrinkdebugfile", "Shrink debug.log file on client startup (default: 1 when no -debug)", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
41
}
42

            
43
void SetLoggingOptions(const ArgsManager& args)
44
{
45
    LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
46
    LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
47
    LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
48
    LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
49
    LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
50
#ifdef HAVE_THREAD_LOCAL
51
    LogInstance().m_log_threadnames = args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
52
#endif
53
    LogInstance().m_log_sourcelocations = args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
54

            
55
    fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
56
}
57

            
58
void SetLoggingCategories(const ArgsManager& args)
59
{
60
    if (args.IsArgSet("-debug")) {
61
        // Special-case: if -debug=0/-nodebug is set, turn off debugging messages
62
        const std::vector<std::string> categories = args.GetArgs("-debug");
63

            
64
        if (std::none_of(categories.begin(), categories.end(),
65
            [](std::string cat){return cat == "0" || cat == "none";})) {
66
            for (const auto& cat : categories) {
67
                if (!LogInstance().EnableCategory(cat)) {
68
                    InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
69
                }
70
            }
71
        }
72
    }
73

            
74
    // Now remove the logging categories which were explicitly excluded
75
    for (const std::string& cat : args.GetArgs("-debugexclude")) {
76
        if (!LogInstance().DisableCategory(cat)) {
77
            InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
78
        }
79
    }
80
}
81

            
82
bool StartLogging(const ArgsManager& args)
83
{
84
    if (LogInstance().m_print_to_file) {
85
        if (args.GetBoolArg("-shrinkdebugfile", LogInstance().DefaultShrinkDebugFile())) {
86
            // Do this first since it both loads a bunch of debug.log into memory,
87
            // and because this needs to happen before any other debug.log printing
88
            LogInstance().ShrinkDebugFile();
89
        }
90
    }
91
    if (!LogInstance().StartLogging()) {
92
            return InitError(strprintf(Untranslated("Could not open debug log file %s"),
93
                fs::PathToString(LogInstance().m_file_path)));
94
    }
95

            
96
    if (!LogInstance().m_log_timestamps)
97
        LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
98
    LogPrintf("Default data directory %s\n", fs::PathToString(GetDefaultDataDir()));
99
    LogPrintf("Using data directory %s\n", fs::PathToString(gArgs.GetDataDirNet()));
100

            
101
    // Only log conf file usage message if conf file actually exists.
102
    fs::path config_file_path = GetConfigFile(args.GetArg("-conf", BITCOIN_CONF_FILENAME));
103
    if (fs::exists(config_file_path)) {
104
        LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
105
    } else if (args.IsArgSet("-conf")) {
106
        // Warn if no conf file exists at path provided by user
107
        InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path)));
108
    } else {
109
        // Not categorizing as "Warning" because it's the default behavior
110
        LogPrintf("Config file: %s (not found, skipping)\n", fs::PathToString(config_file_path));
111
    }
112

            
113
    // Log the config arguments to debug.log
114
    args.LogArgs();
115

            
116
    return true;
117
}
118

            
119
void LogPackageVersion()
120
{
121
    std::string version_string = FormatFullVersion();
122
#ifdef DEBUG
123
    version_string += " (debug build)";
124
#else
125
    version_string += " (release build)";
126
#endif
127
    LogPrintf(PACKAGE_NAME " version %s\n", version_string);
128
}
129
} // namespace init