Skip to content

Commit 1a274bc

Browse files
author
MarcoFalke
committed
Merge #16205: Refactor: Replace fprintf with tfm::format
fa8f195 Replace remaining fprintf with tfm::format manually (MarcoFalke) fac03ec scripted-diff: Replace fprintf with tfm::format (MarcoFalke) fa72a64 tinyformat: Add doc to Bitcoin Core specific strprintf (MarcoFalke) Pull request description: This should be a refactor except in the cases where we use the wrong format specifier [1], in which case this patch is a bug fix. [1] : e.g. depends: Add libevent compatibility patch for windows #8730 ACKs for commit fa8f19: promag: ACK fa8f195. Ideally this should be rebased before merge. practicalswift: utACK fa8f195 Empact: ACK bitcoin/bitcoin@fa8f195 laanwj: code review and lightly tested ACK fa8f195 jonatack: ACK fa8f195 from light code review, building, and running linter/unit tests/extended functional tests. Tree-SHA512: 65f648b0bc383e3266a5bdb4ad8c8a1908a719635d49e1cd321b91254be24dbc7e22290370178e29b98ddcb3fec0889de9cbae273c7140abc9793d849534a743
2 parents f385578 + fa8f195 commit 1a274bc

14 files changed

+67
-68
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char** argv)
3636
SetupBenchArgs();
3737
std::string error;
3838
if (!gArgs.ParseParameters(argc, argv, error)) {
39-
fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
39+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
4040
return EXIT_FAILURE;
4141
}
4242

@@ -53,7 +53,7 @@ int main(int argc, char** argv)
5353

5454
double scaling_factor;
5555
if (!ParseDouble(scaling_str, &scaling_factor)) {
56-
fprintf(stderr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
56+
tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", scaling_str.c_str());
5757
return EXIT_FAILURE;
5858
}
5959

src/bitcoin-cli.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ static int AppInitRPC(int argc, char* argv[])
101101
SetupCliArgs();
102102
std::string error;
103103
if (!gArgs.ParseParameters(argc, argv, error)) {
104-
fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
104+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
105105
return EXIT_FAILURE;
106106
}
107107
if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
@@ -115,26 +115,26 @@ static int AppInitRPC(int argc, char* argv[])
115115
strUsage += "\n" + gArgs.GetHelpMessage();
116116
}
117117

118-
fprintf(stdout, "%s", strUsage.c_str());
118+
tfm::format(std::cout, "%s", strUsage.c_str());
119119
if (argc < 2) {
120-
fprintf(stderr, "Error: too few parameters\n");
120+
tfm::format(std::cerr, "Error: too few parameters\n");
121121
return EXIT_FAILURE;
122122
}
123123
return EXIT_SUCCESS;
124124
}
125125
if (!fs::is_directory(GetDataDir(false))) {
126-
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
126+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
127127
return EXIT_FAILURE;
128128
}
129129
if (!gArgs.ReadConfigFiles(error, true)) {
130-
fprintf(stderr, "Error reading configuration file: %s\n", error.c_str());
130+
tfm::format(std::cerr, "Error reading configuration file: %s\n", error.c_str());
131131
return EXIT_FAILURE;
132132
}
133133
// Check for -testnet or -regtest parameter (BaseParams() calls are only valid after this clause)
134134
try {
135135
SelectBaseParams(gArgs.GetChainName());
136136
} catch (const std::exception& e) {
137-
fprintf(stderr, "Error: %s\n", e.what());
137+
tfm::format(std::cerr, "Error: %s\n", e.what());
138138
return EXIT_FAILURE;
139139
}
140140
return CONTINUE_EXECUTION;
@@ -495,7 +495,7 @@ static int CommandLineRPC(int argc, char *argv[])
495495
}
496496

497497
if (strPrint != "") {
498-
fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
498+
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint.c_str());
499499
}
500500
return nRet;
501501
}
@@ -508,7 +508,7 @@ int main(int argc, char* argv[])
508508
#endif
509509
SetupEnvironment();
510510
if (!SetupNetworking()) {
511-
fprintf(stderr, "Error: Initializing networking failed\n");
511+
tfm::format(std::cerr, "Error: Initializing networking failed\n");
512512
return EXIT_FAILURE;
513513
}
514514
event_set_log_callback(&libevent_log_cb);

src/bitcoin-tx.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ static int AppInitRawTx(int argc, char* argv[])
8282
SetupBitcoinTxArgs();
8383
std::string error;
8484
if (!gArgs.ParseParameters(argc, argv, error)) {
85-
fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
85+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
8686
return EXIT_FAILURE;
8787
}
8888

8989
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
9090
try {
9191
SelectParams(gArgs.GetChainName());
9292
} catch (const std::exception& e) {
93-
fprintf(stderr, "Error: %s\n", e.what());
93+
tfm::format(std::cerr, "Error: %s\n", e.what());
9494
return EXIT_FAILURE;
9595
}
9696

@@ -104,10 +104,10 @@ static int AppInitRawTx(int argc, char* argv[])
104104
"\n";
105105
strUsage += gArgs.GetHelpMessage();
106106

107-
fprintf(stdout, "%s", strUsage.c_str());
107+
tfm::format(std::cout, "%s", strUsage.c_str());
108108

109109
if (argc < 2) {
110-
fprintf(stderr, "Error: too few parameters\n");
110+
tfm::format(std::cerr, "Error: too few parameters\n");
111111
return EXIT_FAILURE;
112112
}
113113
return EXIT_SUCCESS;
@@ -723,21 +723,21 @@ static void OutputTxJSON(const CTransaction& tx)
723723
TxToUniv(tx, uint256(), entry);
724724

725725
std::string jsonOutput = entry.write(4);
726-
fprintf(stdout, "%s\n", jsonOutput.c_str());
726+
tfm::format(std::cout, "%s\n", jsonOutput.c_str());
727727
}
728728

729729
static void OutputTxHash(const CTransaction& tx)
730730
{
731731
std::string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
732732

733-
fprintf(stdout, "%s\n", strHexHash.c_str());
733+
tfm::format(std::cout, "%s\n", strHexHash.c_str());
734734
}
735735

736736
static void OutputTxHex(const CTransaction& tx)
737737
{
738738
std::string strHex = EncodeHexTx(tx);
739739

740-
fprintf(stdout, "%s\n", strHex.c_str());
740+
tfm::format(std::cout, "%s\n", strHex.c_str());
741741
}
742742

743743
static void OutputTx(const CTransaction& tx)
@@ -828,7 +828,7 @@ static int CommandLineRawTx(int argc, char* argv[])
828828
}
829829

830830
if (strPrint != "") {
831-
fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str());
831+
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint.c_str());
832832
}
833833
return nRet;
834834
}

src/bitcoin-wallet.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static bool WalletAppInit(int argc, char* argv[])
3636
SetupWalletToolArgs();
3737
std::string error_message;
3838
if (!gArgs.ParseParameters(argc, argv, error_message)) {
39-
fprintf(stderr, "Error parsing command line arguments: %s\n", error_message.c_str());
39+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error_message.c_str());
4040
return false;
4141
}
4242
if (argc < 2 || HelpRequested(gArgs)) {
@@ -48,15 +48,15 @@ static bool WalletAppInit(int argc, char* argv[])
4848
" bitcoin-wallet [options] <command>\n\n" +
4949
gArgs.GetHelpMessage();
5050

51-
fprintf(stdout, "%s", usage.c_str());
51+
tfm::format(std::cout, "%s", usage.c_str());
5252
return false;
5353
}
5454

5555
// check for printtoconsole, allow -debug
5656
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
5757

5858
if (!fs::is_directory(GetDataDir(false))) {
59-
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
59+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
6060
return false;
6161
}
6262
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
@@ -87,21 +87,21 @@ int main(int argc, char* argv[])
8787
for(int i = 1; i < argc; ++i) {
8888
if (!IsSwitchChar(argv[i][0])) {
8989
if (!method.empty()) {
90-
fprintf(stderr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method.c_str(), argv[i]);
90+
tfm::format(std::cerr, "Error: two methods provided (%s and %s). Only one method should be provided.\n", method.c_str(), argv[i]);
9191
return EXIT_FAILURE;
9292
}
9393
method = argv[i];
9494
}
9595
}
9696

9797
if (method.empty()) {
98-
fprintf(stderr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
98+
tfm::format(std::cerr, "No method provided. Run `bitcoin-wallet -help` for valid methods.\n");
9999
return EXIT_FAILURE;
100100
}
101101

102102
// A name must be provided when creating a file
103103
if (method == "create" && !gArgs.IsArgSet("-wallet")) {
104-
fprintf(stderr, "Wallet name must be provided when creating a new wallet.\n");
104+
tfm::format(std::cerr, "Wallet name must be provided when creating a new wallet.\n");
105105
return EXIT_FAILURE;
106106
}
107107

src/bitcoind.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ static bool AppInit(int argc, char* argv[])
7070
SetupServerArgs();
7171
std::string error;
7272
if (!gArgs.ParseParameters(argc, argv, error)) {
73-
fprintf(stderr, "Error parsing command line arguments: %s\n", error.c_str());
73+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error.c_str());
7474
return false;
7575
}
7676

@@ -88,33 +88,33 @@ static bool AppInit(int argc, char* argv[])
8888
strUsage += "\n" + gArgs.GetHelpMessage();
8989
}
9090

91-
fprintf(stdout, "%s", strUsage.c_str());
91+
tfm::format(std::cout, "%s", strUsage.c_str());
9292
return true;
9393
}
9494

9595
try
9696
{
9797
if (!fs::is_directory(GetDataDir(false)))
9898
{
99-
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
99+
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
100100
return false;
101101
}
102102
if (!gArgs.ReadConfigFiles(error, true)) {
103-
fprintf(stderr, "Error reading configuration file: %s\n", error.c_str());
103+
tfm::format(std::cerr, "Error reading configuration file: %s\n", error.c_str());
104104
return false;
105105
}
106106
// Check for -testnet or -regtest parameter (Params() calls are only valid after this clause)
107107
try {
108108
SelectParams(gArgs.GetChainName());
109109
} catch (const std::exception& e) {
110-
fprintf(stderr, "Error: %s\n", e.what());
110+
tfm::format(std::cerr, "Error: %s\n", e.what());
111111
return false;
112112
}
113113

114114
// Error out when loose non-argument tokens are encountered on command line
115115
for (int i = 1; i < argc; i++) {
116116
if (!IsSwitchChar(argv[i][0])) {
117-
fprintf(stderr, "Error: Command line contains unexpected token '%s', see bitcoind -h for a list of options.\n", argv[i]);
117+
tfm::format(std::cerr, "Error: Command line contains unexpected token '%s', see bitcoind -h for a list of options.\n", argv[i]);
118118
return false;
119119
}
120120
}
@@ -146,18 +146,18 @@ static bool AppInit(int argc, char* argv[])
146146
#pragma GCC diagnostic push
147147
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
148148
#endif
149-
fprintf(stdout, "Bitcoin server starting\n");
149+
tfm::format(std::cout, "Bitcoin server starting\n");
150150

151151
// Daemonize
152152
if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
153-
fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
153+
tfm::format(std::cerr, "Error: daemon() failed: %s\n", strerror(errno));
154154
return false;
155155
}
156156
#if defined(MAC_OSX)
157157
#pragma GCC diagnostic pop
158158
#endif
159159
#else
160-
fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
160+
tfm::format(std::cerr, "Error: -daemon is not supported on this operating system\n");
161161
return false;
162162
#endif // HAVE_DECL_DAEMON
163163
}

src/init.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,13 @@ static fs::path GetPidFile()
108108

109109
NODISCARD static bool CreatePidFile()
110110
{
111-
FILE* file = fsbridge::fopen(GetPidFile(), "w");
111+
fsbridge::ofstream file{GetPidFile()};
112112
if (file) {
113113
#ifdef WIN32
114-
fprintf(file, "%d\n", GetCurrentProcessId());
114+
tfm::format(file, "%d\n", GetCurrentProcessId());
115115
#else
116-
fprintf(file, "%d\n", getpid());
116+
tfm::format(file, "%d\n", getpid());
117117
#endif
118-
fclose(file);
119118
return true;
120119
} else {
121120
return InitError(strprintf(_("Unable to create the PID file '%s': %s"), GetPidFile().string(), std::strerror(errno)));

src/noui.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool noui_ThreadSafeMessageBox(const std::string& message, const std::string& ca
3737

3838
if (!fSecure)
3939
LogPrintf("%s: %s\n", strCaption, message);
40-
fprintf(stderr, "%s: %s\n", strCaption.c_str(), message.c_str());
40+
tfm::format(std::cerr, "%s: %s\n", strCaption.c_str(), message.c_str());
4141
return false;
4242
}
4343

src/qt/utilitydialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ HelpMessageDialog::~HelpMessageDialog()
124124
void HelpMessageDialog::printToConsole()
125125
{
126126
// On other operating systems, the expected action is to print the message to the console.
127-
fprintf(stdout, "%s\n", qPrintable(text));
127+
tfm::format(std::cout, "%s\n", qPrintable(text));
128128
}
129129

130130
void HelpMessageDialog::showOrPrint()

src/sync.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct CLockLocation {
5757

5858
std::string ToString() const
5959
{
60-
return tfm::format(
60+
return strprintf(
6161
"%s %s:%s%s (in thread %s)",
6262
mutexName, sourceFile, itostr(sourceLine), (fTry ? " (TRY)" : ""), m_thread_name);
6363
}
@@ -118,7 +118,7 @@ static void potential_deadlock_detected(const std::pair<void*, void*>& mismatch,
118118
LogPrintf(" %s\n", i.second.ToString());
119119
}
120120
if (g_debug_lockorder_abort) {
121-
fprintf(stderr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
121+
tfm::format(std::cerr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
122122
abort();
123123
}
124124
throw std::logic_error("potential deadlock detected");
@@ -175,15 +175,15 @@ void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine,
175175
for (const std::pair<void*, CLockLocation>& i : g_lockstack)
176176
if (i.first == cs)
177177
return;
178-
fprintf(stderr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
178+
tfm::format(std::cerr, "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
179179
abort();
180180
}
181181

182182
void AssertLockNotHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs)
183183
{
184184
for (const std::pair<void*, CLockLocation>& i : g_lockstack) {
185185
if (i.first == cs) {
186-
fprintf(stderr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
186+
tfm::format(std::cerr, "Assertion failed: lock %s held in %s:%i; locks held:\n%s", pszName, pszFile, nLine, LocksHeld().c_str());
187187
abort();
188188
}
189189
}

src/tinyformat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ std::string format(const std::string &fmt, const Args&... args)
10631063

10641064
} // namespace tinyformat
10651065

1066+
/** Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for details) */
10661067
#define strprintf tfm::format
10671068

10681069
#endif // TINYFORMAT_H_INCLUDED

0 commit comments

Comments
 (0)