diff --git a/arcanist/linter/LocaleDependenceLinter.php b/arcanist/linter/LocaleDependenceLinter.php --- a/arcanist/linter/LocaleDependenceLinter.php +++ b/arcanist/linter/LocaleDependenceLinter.php @@ -64,7 +64,7 @@ "fgetwc", "fgetws", "fold_case", // boost::locale::fold_case - //"fprintf" // (via vfprintf) + "fprintf", // (via vfprintf) "fputwc", "fputws", "fscanf", // (via __vfscanf) @@ -111,7 +111,7 @@ "mbtowc", // LC_CTYPE "mktime", "normalize", // boost::locale::normalize - //"printf" // LC_NUMERIC + "printf", // LC_NUMERIC "putwc", "putwchar", "scanf", // LC_NUMERIC diff --git a/src/bench/bench_bitcoin.cpp b/src/bench/bench_bitcoin.cpp --- a/src/bench/bench_bitcoin.cpp +++ b/src/bench/bench_bitcoin.cpp @@ -70,8 +70,8 @@ SetupBenchArgs(); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { - fprintf(stderr, "Error parsing command line arguments: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error parsing command line arguments: %s\n", + error.c_str()); return EXIT_FAILURE; } @@ -87,8 +87,8 @@ double scaling_factor; if (!ParseDouble(scaling_str, &scaling_factor)) { - fprintf(stderr, "Error parsing scaling factor as double: %s\n", - scaling_str.c_str()); + tfm::format(std::cerr, "Error parsing scaling factor as double: %s\n", + scaling_str.c_str()); return EXIT_FAILURE; } diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -153,8 +153,8 @@ SetupCliArgs(); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { - fprintf(stderr, "Error parsing command line arguments: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error parsing command line arguments: %s\n", + error.c_str()); return EXIT_FAILURE; } if (argc < 2 || HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { @@ -175,22 +175,22 @@ strUsage += "\n" + gArgs.GetHelpMessage(); } - fprintf(stdout, "%s", strUsage.c_str()); + tfm::format(std::cout, "%s", strUsage.c_str()); if (argc < 2) { - fprintf(stderr, "Error: too few parameters\n"); + tfm::format(std::cerr, "Error: too few parameters\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; } if (!fs::is_directory(GetDataDir(false))) { - fprintf(stderr, - "Error: Specified data directory \"%s\" does not exist.\n", - gArgs.GetArg("-datadir", "").c_str()); + tfm::format(std::cerr, + "Error: Specified data directory \"%s\" does not exist.\n", + gArgs.GetArg("-datadir", "").c_str()); return EXIT_FAILURE; } if (!gArgs.ReadConfigFiles(error, true)) { - fprintf(stderr, "Error reading configuration file: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error reading configuration file: %s\n", + error.c_str()); return EXIT_FAILURE; } // Check for -testnet or -regtest parameter (BaseParams() calls are only @@ -198,7 +198,7 @@ try { SelectBaseParams(gArgs.GetChainName()); } catch (const std::exception &e) { - fprintf(stderr, "Error: %s\n", e.what()); + tfm::format(std::cerr, "Error: %s\n", e.what()); return EXIT_FAILURE; } return CONTINUE_EXECUTION; @@ -615,7 +615,8 @@ } if (strPrint != "") { - fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); + tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", + strPrint.c_str()); } return nRet; } @@ -627,7 +628,7 @@ #endif SetupEnvironment(); if (!SetupNetworking()) { - fprintf(stderr, "Error: Initializing networking failed\n"); + tfm::format(std::cerr, "Error: Initializing networking failed\n"); return EXIT_FAILURE; } event_set_log_callback(&libevent_log_cb); diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp --- a/src/bitcoin-tx.cpp +++ b/src/bitcoin-tx.cpp @@ -109,8 +109,8 @@ SetupBitcoinTxArgs(); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { - fprintf(stderr, "Error parsing command line arguments: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error parsing command line arguments: %s\n", + error.c_str()); return EXIT_FAILURE; } @@ -119,7 +119,7 @@ try { SelectParams(gArgs.GetChainName()); } catch (const std::exception &e) { - fprintf(stderr, "Error: %s\n", e.what()); + tfm::format(std::cerr, "Error: %s\n", e.what()); return EXIT_FAILURE; } @@ -136,10 +136,10 @@ "hex-encoded bitcoin transaction\n" + "\n"; strUsage += gArgs.GetHelpMessage(); - fprintf(stdout, "%s", strUsage.c_str()); + tfm::format(std::cout, "%s", strUsage.c_str()); if (argc < 2) { - fprintf(stderr, "Error: too few parameters\n"); + tfm::format(std::cerr, "Error: too few parameters\n"); return EXIT_FAILURE; } @@ -763,20 +763,20 @@ TxToUniv(tx, uint256(), entry); std::string jsonOutput = entry.write(4); - fprintf(stdout, "%s\n", jsonOutput.c_str()); + tfm::format(std::cout, "%s\n", jsonOutput.c_str()); } static void OutputTxHash(const CTransaction &tx) { // the hex-encoded transaction id. std::string strHexHash = tx.GetId().GetHex(); - fprintf(stdout, "%s\n", strHexHash.c_str()); + tfm::format(std::cout, "%s\n", strHexHash.c_str()); } static void OutputTxHex(const CTransaction &tx) { std::string strHex = EncodeHexTx(tx); - fprintf(stdout, "%s\n", strHex.c_str()); + tfm::format(std::cout, "%s\n", strHex.c_str()); } static void OutputTx(const CTransaction &tx) { @@ -871,7 +871,8 @@ } if (strPrint != "") { - fprintf((nRet == 0 ? stdout : stderr), "%s\n", strPrint.c_str()); + tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", + strPrint.c_str()); } return nRet; diff --git a/src/bitcoin-wallet.cpp b/src/bitcoin-wallet.cpp --- a/src/bitcoin-wallet.cpp +++ b/src/bitcoin-wallet.cpp @@ -50,8 +50,8 @@ SetupWalletToolArgs(); std::string error_message; if (!gArgs.ParseParameters(argc, argv, error_message)) { - fprintf(stderr, "Error parsing command line arguments: %s\n", - error_message.c_str()); + tfm::format(std::cerr, "Error parsing command line arguments: %s\n", + error_message.c_str()); return false; } if (argc < 2 || HelpRequested(gArgs)) { @@ -67,7 +67,7 @@ "Usage:\n" + " bitcoin-wallet [options] \n\n" + gArgs.GetHelpMessage(); - fprintf(stdout, "%s", usage.c_str()); + tfm::format(std::cout, "%s", usage.c_str()); return false; } @@ -76,9 +76,9 @@ gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false)); if (!fs::is_directory(GetDataDir(false))) { - fprintf(stderr, - "Error: Specified data directory \"%s\" does not exist.\n", - gArgs.GetArg("-datadir", "").c_str()); + tfm::format(std::cerr, + "Error: Specified data directory \"%s\" does not exist.\n", + gArgs.GetArg("-datadir", "").c_str()); return false; } // Check for -testnet or -regtest parameter (Params() calls are only valid @@ -111,10 +111,10 @@ for (int i = 1; i < argc; ++i) { if (!IsSwitchChar(argv[i][0])) { if (!method.empty()) { - fprintf(stderr, - "Error: two methods provided (%s and %s). Only one " - "method should be provided.\n", - method.c_str(), argv[i]); + tfm::format(std::cerr, + "Error: two methods provided (%s and %s). Only one " + "method should be provided.\n", + method.c_str(), argv[i]); return EXIT_FAILURE; } method = argv[i]; @@ -122,15 +122,17 @@ } if (method.empty()) { - fprintf(stderr, "No method provided. Run `bitcoin-wallet -help` for " - "valid methods.\n"); + tfm::format(std::cerr, + "No method provided. Run `bitcoin-wallet -help` for " + "valid methods.\n"); return EXIT_FAILURE; } // A name must be provided when creating a file if (method == "create" && !gArgs.IsArgSet("-wallet")) { - fprintf(stderr, - "Wallet name must be provided when creating a new wallet.\n"); + tfm::format( + std::cerr, + "Wallet name must be provided when creating a new wallet.\n"); return EXIT_FAILURE; } diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -83,8 +83,8 @@ SetupServerArgs(); std::string error; if (!gArgs.ParseParameters(argc, argv, error)) { - fprintf(stderr, "Error parsing command line arguments: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error parsing command line arguments: %s\n", + error.c_str()); return false; } @@ -102,20 +102,21 @@ strUsage += "\n" + gArgs.GetHelpMessage(); } - fprintf(stdout, "%s", strUsage.c_str()); + tfm::format(std::cout, "%s", strUsage.c_str()); return true; } try { if (!fs::is_directory(GetDataDir(false))) { - fprintf(stderr, - "Error: Specified data directory \"%s\" does not exist.\n", - gArgs.GetArg("-datadir", "").c_str()); + tfm::format( + std::cerr, + "Error: Specified data directory \"%s\" does not exist.\n", + gArgs.GetArg("-datadir", "").c_str()); return false; } if (!gArgs.ReadConfigFiles(error, true)) { - fprintf(stderr, "Error reading configuration file: %s\n", - error.c_str()); + tfm::format(std::cerr, "Error reading configuration file: %s\n", + error.c_str()); return false; } // Check for -testnet or -regtest parameter (Params() calls are only @@ -123,7 +124,7 @@ try { SelectParams(gArgs.GetChainName()); } catch (const std::exception &e) { - fprintf(stderr, "Error: %s\n", e.what()); + tfm::format(std::cerr, "Error: %s\n", e.what()); return false; } @@ -141,10 +142,11 @@ // line for (int i = 1; i < argc; i++) { if (!IsSwitchChar(argv[i][0])) { - fprintf(stderr, - "Error: Command line contains unexpected token '%s', " - "see bitcoind -h for a list of options.\n", - argv[i]); + tfm::format( + std::cerr, + "Error: Command line contains unexpected token '%s', " + "see bitcoind -h for a list of options.\n", + argv[i]); return false; } } @@ -176,21 +178,21 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif - fprintf(stdout, "Bitcoin server starting\n"); + tfm::format(std::cout, "Bitcoin server starting\n"); // Daemonize if (daemon(1, 0)) { // don't chdir (1), do close FDs (0) - fprintf(stderr, "Error: daemon() failed: %s\n", - strerror(errno)); + tfm::format(std::cerr, "Error: daemon() failed: %s\n", + strerror(errno)); return false; } #if defined(MAC_OSX) #pragma GCC diagnostic pop #endif #else - fprintf( - stderr, + tfm::format( + std::cerr, "Error: -daemon is not supported on this operating system\n"); return false; #endif // HAVE_DECL_DAEMON diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -104,14 +104,13 @@ } NODISCARD static bool CreatePidFile() { - FILE *file = fsbridge::fopen(GetPidFile(), "w"); + fsbridge::ofstream file{GetPidFile()}; if (file) { #ifdef WIN32 - fprintf(file, "%d\n", GetCurrentProcessId()); + tfm::format(file, "%d\n", GetCurrentProcessId()); #else - fprintf(file, "%d\n", getpid()); + tfm::format(file, "%d\n", getpid()); #endif - fclose(file); return true; } else { return InitError(strprintf(_("Unable to create the PID file '%s': %s"), diff --git a/src/noui.cpp b/src/noui.cpp --- a/src/noui.cpp +++ b/src/noui.cpp @@ -45,7 +45,7 @@ if (!fSecure) { LogPrintf("%s: %s\n", strCaption, message); } - fprintf(stderr, "%s: %s\n", strCaption.c_str(), message.c_str()); + tfm::format(std::cerr, "%s: %s\n", strCaption.c_str(), message.c_str()); return false; } diff --git a/src/qt/utilitydialog.cpp b/src/qt/utilitydialog.cpp --- a/src/qt/utilitydialog.cpp +++ b/src/qt/utilitydialog.cpp @@ -127,7 +127,7 @@ void HelpMessageDialog::printToConsole() { // On other operating systems, the expected action is to print the message // to the console. - fprintf(stdout, "%s\n", qPrintable(text)); + tfm::format(std::cout, "%s\n", qPrintable(text)); } void HelpMessageDialog::showOrPrint() { diff --git a/src/sync.cpp b/src/sync.cpp --- a/src/sync.cpp +++ b/src/sync.cpp @@ -41,9 +41,9 @@ m_thread_name(thread_name), sourceLine(nLine) {} std::string ToString() const { - return tfm::format("%s %s:%s%s (in thread %s)", mutexName, sourceFile, - itostr(sourceLine), (fTry ? " (TRY)" : ""), - m_thread_name); + return strprintf("%s %s:%s%s (in thread %s)", mutexName, sourceFile, + itostr(sourceLine), (fTry ? " (TRY)" : ""), + m_thread_name); } private: @@ -103,10 +103,11 @@ LogPrintf(" %s\n", i.second.ToString()); } if (g_debug_lockorder_abort) { - fprintf(stderr, - "Assertion failed: detected inconsistent lock order at %s:%i, " - "details in debug log.\n", - __FILE__, __LINE__); + tfm::format( + std::cerr, + "Assertion failed: detected inconsistent lock order at %s:%i, " + "details in debug log.\n", + __FILE__, __LINE__); abort(); } throw std::logic_error("potential deadlock detected"); @@ -167,9 +168,9 @@ return; } } - fprintf(stderr, - "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", - pszName, pszFile, nLine, LocksHeld().c_str()); + tfm::format(std::cerr, + "Assertion failed: lock %s not held in %s:%i; locks held:\n%s", + pszName, pszFile, nLine, LocksHeld().c_str()); abort(); } @@ -177,9 +178,10 @@ int nLine, void *cs) { for (const std::pair &i : g_lockstack) { if (i.first == cs) { - fprintf(stderr, - "Assertion failed: lock %s held in %s:%i; locks held:\n%s", - pszName, pszFile, nLine, LocksHeld().c_str()); + tfm::format( + std::cerr, + "Assertion failed: lock %s held in %s:%i; locks held:\n%s", + pszName, pszFile, nLine, LocksHeld().c_str()); abort(); } } diff --git a/src/tinyformat.h b/src/tinyformat.h --- a/src/tinyformat.h +++ b/src/tinyformat.h @@ -1085,6 +1085,8 @@ } // namespace tinyformat +/** Format arguments and return the string or write to given std::ostream (see + * tinyformat::format doc for details) */ #define strprintf tfm::format #endif // TINYFORMAT_H_INCLUDED diff --git a/src/util/system.cpp b/src/util/system.cpp --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -779,7 +779,8 @@ void PrintExceptionContinue(const std::exception *pex, const char *pszThread) { std::string message = FormatException(pex, pszThread); LogPrintf("\n\n************************\n%s\n", message); - fprintf(stderr, "\n\n************************\n%s\n", message.c_str()); + tfm::format(std::cerr, "\n\n************************\n%s\n", + message.c_str()); } fs::path GetDefaultDataDir() { @@ -1072,10 +1073,11 @@ } } for (const std::string &to_include : includeconf) { - fprintf(stderr, - "warning: -includeconf cannot be used from included " - "files; ignoring -includeconf=%s\n", - to_include.c_str()); + tfm::format( + std::cerr, + "warning: -includeconf cannot be used from included " + "files; ignoring -includeconf=%s\n", + to_include.c_str()); } } } diff --git a/src/wallet/wallettool.cpp b/src/wallet/wallettool.cpp --- a/src/wallet/wallettool.cpp +++ b/src/wallet/wallettool.cpp @@ -26,7 +26,7 @@ static std::shared_ptr CreateWallet(const std::string &name, const fs::path &path) { if (fs::exists(path)) { - fprintf(stderr, "Error: File exists already\n"); + tfm::format(std::cerr, "Error: File exists already\n"); return nullptr; } // dummy chain interface @@ -38,7 +38,7 @@ bool first_run = true; DBErrors load_wallet_ret = wallet_instance->LoadWallet(first_run); if (load_wallet_ret != DBErrors::LOAD_OK) { - fprintf(stderr, "Error creating %s", name.c_str()); + tfm::format(std::cerr, "Error creating %s", name.c_str()); return nullptr; } @@ -48,7 +48,7 @@ CPubKey seed = wallet_instance->GenerateNewSeed(); wallet_instance->SetHDSeed(seed); - fprintf(stdout, "Topping up keypool...\n"); + tfm::format(std::cout, "Topping up keypool...\n"); wallet_instance->TopUpKeyPool(); return wallet_instance; } @@ -56,7 +56,7 @@ static std::shared_ptr LoadWallet(const std::string &name, const fs::path &path) { if (!fs::exists(path)) { - fprintf(stderr, "Error: Wallet files does not exist\n"); + tfm::format(std::cerr, "Error: Wallet files does not exist\n"); return nullptr; } @@ -71,35 +71,38 @@ bool first_run; load_wallet_ret = wallet_instance->LoadWallet(first_run); } catch (const std::runtime_error &) { - fprintf(stderr, - "Error loading %s. Is wallet being used by another process?\n", - name.c_str()); + tfm::format( + std::cerr, + "Error loading %s. Is wallet being used by another process?\n", + name.c_str()); return nullptr; } if (load_wallet_ret != DBErrors::LOAD_OK) { wallet_instance = nullptr; if (load_wallet_ret == DBErrors::CORRUPT) { - fprintf(stderr, "Error loading %s: Wallet corrupted", name.c_str()); + tfm::format(std::cerr, "Error loading %s: Wallet corrupted", + name.c_str()); return nullptr; } else if (load_wallet_ret == DBErrors::NONCRITICAL_ERROR) { - fprintf(stderr, - "Error reading %s! All keys read correctly, but " - "transaction data" - " or address book entries might be missing or incorrect.", - name.c_str()); + tfm::format( + std::cerr, + "Error reading %s! All keys read correctly, but " + "transaction data" + " or address book entries might be missing or incorrect.", + name.c_str()); } else if (load_wallet_ret == DBErrors::TOO_NEW) { - fprintf(stderr, - "Error loading %s: Wallet requires newer version of %s", - name.c_str(), PACKAGE_NAME); + tfm::format(std::cerr, + "Error loading %s: Wallet requires newer version of %s", + name.c_str(), PACKAGE_NAME); return nullptr; } else if (load_wallet_ret == DBErrors::NEED_REWRITE) { - fprintf(stderr, - "Wallet needed to be rewritten: restart %s to complete", - PACKAGE_NAME); + tfm::format(std::cerr, + "Wallet needed to be rewritten: restart %s to complete", + PACKAGE_NAME); return nullptr; } else { - fprintf(stderr, "Error loading %s", name.c_str()); + tfm::format(std::cerr, "Error loading %s", name.c_str()); return nullptr; } } @@ -111,15 +114,17 @@ // lock required because of some AssertLockHeld() LOCK(wallet_instance->cs_wallet); - fprintf(stdout, "Wallet info\n===========\n"); - fprintf(stdout, "Encrypted: %s\n", - wallet_instance->IsCrypted() ? "yes" : "no"); - fprintf(stdout, "HD (hd seed available): %s\n", - wallet_instance->GetHDChain().seed_id.IsNull() ? "no" : "yes"); - fprintf(stdout, "Keypool Size: %u\n", wallet_instance->GetKeyPoolSize()); - fprintf(stdout, "Transactions: %zu\n", wallet_instance->mapWallet.size()); - fprintf(stdout, "Address Book: %zu\n", - wallet_instance->mapAddressBook.size()); + tfm::format(std::cout, "Wallet info\n===========\n"); + tfm::format(std::cout, "Encrypted: %s\n", + wallet_instance->IsCrypted() ? "yes" : "no"); + tfm::format(std::cout, "HD (hd seed available): %s\n", + wallet_instance->GetHDChain().seed_id.IsNull() ? "no" : "yes"); + tfm::format(std::cout, "Keypool Size: %u\n", + wallet_instance->GetKeyPoolSize()); + tfm::format(std::cout, "Transactions: %zu\n", + wallet_instance->mapWallet.size()); + tfm::format(std::cout, "Address Book: %zu\n", + wallet_instance->mapAddressBook.size()); } bool ExecuteWalletToolFunc(const std::string &command, @@ -134,13 +139,14 @@ } } else if (command == "info") { if (!fs::exists(path)) { - fprintf(stderr, "Error: no wallet file at %s\n", name.c_str()); + tfm::format(std::cerr, "Error: no wallet file at %s\n", + name.c_str()); return false; } std::string error; if (!WalletBatch::VerifyEnvironment(path, error)) { - fprintf( - stderr, + tfm::format( + std::cerr, "Error loading %s. Is wallet being used by other process?\n", name.c_str()); return false; @@ -152,7 +158,7 @@ WalletShowInfo(wallet_instance.get()); wallet_instance->Flush(); } else { - fprintf(stderr, "Invalid command: %s\n", command.c_str()); + tfm::format(std::cerr, "Invalid command: %s\n", command.c_str()); return false; } diff --git a/test/lint/lint-format-strings.py b/test/lint/lint-format-strings.py --- a/test/lint/lint-format-strings.py +++ b/test/lint/lint-format-strings.py @@ -37,6 +37,7 @@ ("snprintf", 2), ("sprintf", 1), ("strprintf", 0), + ("tfm::format", 1), # Assuming tfm::::format(std::ostream&, ... ("vfprintf", 1), ("vprintf", 1), ("vsnprintf", 1),