Bitcoin ABC 0.30.13
P2P Digital Currency
common.cpp
Go to the documentation of this file.
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 <common/args.h>
11#include <logging.h>
12#include <node/miner.h>
13#include <node/ui_interface.h>
14#include <util/fs_helpers.h>
15#include <util/time.h>
16#include <util/translation.h>
17
19
20namespace init {
22 argsman.AddArg(
23 "-debuglogfile=<file>",
24 strprintf("Specify location of debug log file. Relative paths will be "
25 "prefixed by a net-specific datadir location. "
26 "(-nodebuglogfile to disable; default: %s)",
29 argsman.AddArg("-debug=<category>",
30 "Output debugging information (default: -nodebug, supplying "
31 "<category> is optional). "
32 "If <category> is not supplied or if <category> = 1, output "
33 "all debugging information. <category> can be: " +
34 LogInstance().LogCategoriesString() +
35 ". This option can be specified multiple times to "
36 "output multiple categories.",
38 argsman.AddArg(
39 "-debugexclude=<category>",
41 "Exclude debugging information for a category. Can be used in "
42 "conjunction with -debug=1 to output debug logs for all categories "
43 "except the specified category. This option can be specified "
44 "multiple times to exclude multiple categories."),
46 argsman.AddArg(
47 "-logips",
48 strprintf("Include IP addresses in debug output (default: %u)",
51 argsman.AddArg(
52 "-logtimestamps",
53 strprintf("Prepend debug output with timestamp (default: %u)",
56#ifdef HAVE_THREAD_LOCAL
57 argsman.AddArg(
58 "-logthreadnames",
60 "Prepend debug output with name of the originating thread (only "
61 "available on platforms supporting thread_local) (default: %u)",
64#else
65 argsman.AddHiddenArgs({"-logthreadnames"});
66#endif
67 argsman.AddArg(
68 "-logsourcelocations",
70 "Prepend debug output with name of the originating source location "
71 "(source file, line number and function name) (default: %u)",
74 argsman.AddArg(
75 "-logtimemicros",
76 strprintf("Add microsecond precision to debug timestamps (default: %u)",
80 argsman.AddArg("-printtoconsole",
81 "Send trace/debug info to console (default: 1 when no "
82 "-daemon. To disable logging to file, set -nodebuglogfile)",
84 argsman.AddArg("-printpriority",
85 strprintf("Log transaction priority and fee per kB when "
86 "mining blocks (default: %d)",
90 argsman.AddArg(
91 "-shrinkdebugfile",
92 "Shrink debug.log file on client startup (default: 1 when no -debug)",
94}
95
96void SetLoggingOptions(const ArgsManager &args) {
97 LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
99 args, args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
100
102 args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
104 args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
106 args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
107#ifdef HAVE_THREAD_LOCAL
109 args.GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
110#endif
112 args.GetBoolArg("-logsourcelocations", DEFAULT_LOGSOURCELOCATIONS);
113
114 fLogIPs = args.GetBoolArg("-logips", DEFAULT_LOGIPS);
115}
116
118 if (args.IsArgSet("-debug")) {
119 // Special-case: if -debug=0/-nodebug is set, turn off debugging
120 // messages
121 const std::vector<std::string> categories = args.GetArgs("-debug");
122
123 if (std::none_of(
124 categories.begin(), categories.end(),
125 [](std::string cat) { return cat == "0" || cat == "none"; })) {
126 for (const auto &cat : categories) {
127 if (!LogInstance().EnableCategory(cat)) {
129 strprintf(_("Unsupported logging category %s=%s."),
130 "-debug", cat));
131 }
132 }
133 }
134 }
135
136 // Now remove the logging categories which were explicitly excluded
137 for (const std::string &cat : args.GetArgs("-debugexclude")) {
138 if (!LogInstance().DisableCategory(cat)) {
139 InitWarning(strprintf(_("Unsupported logging category %s=%s."),
140 "-debugexclude", cat));
141 }
142 }
143}
144
145bool StartLogging(const ArgsManager &args) {
146 BCLog::Logger &logger = LogInstance();
147 if (logger.m_print_to_file) {
148 if (args.GetBoolArg("-shrinkdebugfile",
149 logger.DefaultShrinkDebugFile())) {
150 // Do this first since it both loads a bunch of debug.log into
151 // memory, and because this needs to happen before any other
152 // debug.log printing.
153 logger.ShrinkDebugFile();
154 }
155 }
156
157 if (!logger.StartLogging()) {
158 return InitError(
159 strprintf(Untranslated("Could not open debug log file %s"),
161 }
162
163 if (!logger.m_log_timestamps) {
164 LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
165 }
166 LogPrintf("Default data directory %s\n",
168 LogPrintf("Using data directory %s\n",
170
171 // Only log conf file usage message if conf file actually exists.
172 fs::path config_file_path = args.GetConfigFilePath();
173 if (fs::exists(config_file_path)) {
174 LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
175 } else if (args.IsArgSet("-conf")) {
176 // Warn if no conf file exists at path provided by user
178 strprintf(_("The specified config file %s does not exist\n"),
179 fs::PathToString(config_file_path)));
180 } else {
181 // Not categorizing as "Warning" because it's the default behavior
182 LogPrintf("Config file: %s (not found, skipping)\n",
183 fs::PathToString(config_file_path));
184 }
185
186 // Log the config arguments to debug.log
187 args.LogArgs();
188
189 return true;
190}
191
193 std::string version_string = FormatFullVersion();
194#ifdef DEBUG
195 version_string += " (debug build)";
196#else
197 version_string += " (release build)";
198#endif
199 LogPrintf("%s version %s\n", CLIENT_NAME, version_string);
200}
201} // namespace init
fs::path GetDefaultDataDir()
Definition: args.cpp:759
ArgsManager gArgs
Definition: args.cpp:38
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition: configfile.cpp:236
bool IsArgNegated(const std::string &strArg) const
Return true if the argument was originally passed as a negated option, i.e.
Definition: args.cpp:490
@ ALLOW_ANY
Definition: args.h:103
@ DEBUG_ONLY
Definition: args.h:104
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:371
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:215
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:381
fs::path GetConfigFilePath() const
Return config file path (read-only)
Definition: args.cpp:789
void LogArgs() const
Log the config file options and the command line arguments, useful for troubleshooting.
Definition: args.cpp:860
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:556
void AddHiddenArgs(const std::vector< std::string > &args)
Add many hidden arguments.
Definition: args.cpp:642
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:620
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: args.cpp:275
bool DefaultShrinkDebugFile() const
Default for whether ShrinkDebugFile should be run.
Definition: logging.cpp:412
bool m_log_sourcelocations
Definition: logging.h:117
fs::path m_file_path
Definition: logging.h:119
bool m_log_time_micros
Definition: logging.h:115
bool m_log_threadnames
Definition: logging.h:116
bool StartLogging()
Start logging (and flush all buffered messages)
Definition: logging.cpp:45
bool m_log_timestamps
Definition: logging.h:114
void ShrinkDebugFile()
Definition: logging.cpp:335
bool m_print_to_file
Definition: logging.h:112
bool m_print_to_console
Definition: logging.h:111
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
std::string FormatFullVersion()
const std::string CLIENT_NAME
BCLog::Logger & LogInstance()
Definition: logging.cpp:21
bool fLogIPs
Definition: logging.cpp:18
const char *const DEFAULT_DEBUGLOGFILE
Definition: logging.cpp:19
static const bool DEFAULT_LOGTIMESTAMPS
Definition: logging.h:24
static const bool DEFAULT_LOGIPS
Definition: logging.h:23
static const bool DEFAULT_LOGTHREADNAMES
Definition: logging.h:25
static const bool DEFAULT_LOGSOURCELOCATIONS
Definition: logging.h:26
static const bool DEFAULT_LOGTIMEMICROS
Definition: logging.h:22
#define LogPrintf(...)
Definition: logging.h:227
static bool exists(const path &p)
Definition: fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:142
Definition: common.cpp:20
void AddLoggingArgs(ArgsManager &argsman)
Definition: common.cpp:21
void SetLoggingCategories(const ArgsManager &args)
Definition: common.cpp:117
bool StartLogging(const ArgsManager &args)
Definition: common.cpp:145
void SetLoggingOptions(const ArgsManager &args)
Definition: common.cpp:96
void LogPackageVersion()
Definition: common.cpp:192
static const bool DEFAULT_PRINTPRIORITY
Definition: miner.h:36
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:109
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:113
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
void InitWarning(const bilingual_str &str)
Show warning message.
bool InitError(const bilingual_str &str)
Show error message.