diff --git a/.arclint b/.arclint --- a/.arclint +++ b/.arclint @@ -63,7 +63,7 @@ "type": "lint-locale-dependence", "include": "(^src/.*\\.(h|cpp)$)", "exclude": [ - "(^src/(crypto/ctaes/|leveldb/|secp256k1/|seeder/|tinyformat.h|univalue/))" + "(^src/(crypto/ctaes/|leveldb/|secp256k1/|tinyformat.h|univalue/))" ] }, "lint-cheader": { diff --git a/arcanist/linter/LocaleDependenceLinter.php b/arcanist/linter/LocaleDependenceLinter.php --- a/arcanist/linter/LocaleDependenceLinter.php +++ b/arcanist/linter/LocaleDependenceLinter.php @@ -32,6 +32,12 @@ "split", "is_space", ], + "src/seeder/main.cpp" => [ + "strtoull", + "strcasecmp", + "strftime", + ], + "src/seeder/dns.cpp" => ["strcasecmp"], "src/torcontrol.cpp" => [ "atoi", "strtol", @@ -64,7 +70,7 @@ "fgetwc", "fgetws", "fold_case", // boost::locale::fold_case - //"fprintf" // (via vfprintf) + "fprintf", // (via vfprintf) "fputwc", "fputws", "fscanf", // (via __vfscanf) @@ -111,7 +117,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/seeder/bitcoin.cpp b/src/seeder/bitcoin.cpp --- a/src/seeder/bitcoin.cpp +++ b/src/seeder/bitcoin.cpp @@ -26,7 +26,8 @@ nHeaderStart = vSend.size(); vSend << CMessageHeader(netMagic, pszCommand, 0); nMessageStart = vSend.size(); - // fprintf(stdout, "%s: SEND %s\n", ToString(you).c_str(), pszCommand); + // tfm::format(std::cout, "%s: SEND %s\n", ToString(you).c_str(), + // pszCommand); } void CSeederNode::AbortMessage() { @@ -92,7 +93,7 @@ PeerMessagingState CSeederNode::ProcessMessage(std::string strCommand, CDataStream &recv) { - // fprintf(stdout, "%s: RECV %s\n", ToString(you).c_str(), + // tfm::format(std::cout, "%s: RECV %s\n", ToString(you).c_str(), // strCommand.c_str()); if (strCommand == "version") { int64_t nTime; @@ -114,7 +115,7 @@ if (strCommand == "verack") { vRecv.SetVersion(std::min(nVersion, PROTOCOL_VERSION)); - // fprintf(stdout, "\n%s: version %i\n", ToString(you).c_str(), + // tfm::format(std::cout, "\n%s: version %i\n", ToString(you).c_str(), // nVersion); if (vAddr) { BeginMessage("getaddr"); @@ -129,7 +130,8 @@ if (strCommand == "addr" && vAddr) { std::vector vAddrNew; recv >> vAddrNew; - // fprintf(stdout, "%s: got %i addresses\n", ToString(you).c_str(), + // tfm::format(std::cout, "%s: got %i addresses\n", + // ToString(you).c_str(), // (int)vAddrNew.size()); int64_t now = time(nullptr); std::vector::iterator it = vAddrNew.begin(); @@ -140,7 +142,7 @@ } while (it != vAddrNew.end()) { CAddress &addr = *it; - // fprintf(stdout, "%s: got address %s\n", + // tfm::format(std::cout, "%s: got address %s\n", // ToString(you).c_str(), // addr.ToString().c_str(), (int)(vAddr->size())); it++; @@ -150,7 +152,7 @@ if (addr.nTime > now - 604800) { vAddr->push_back(addr); } - // fprintf(stdout, "%s: added address %s (#%i)\n", + // tfm::format(std::cout, "%s: added address %s (#%i)\n", // ToString(you).c_str(), // addr.ToString().c_str(), (int)(vAddr->size())); if (vAddr->size() > ADDR_SOFT_CAP) { @@ -186,7 +188,7 @@ CMessageHeader hdr(netMagic); vRecv >> hdr; if (!hdr.IsValidWithoutConfig(netMagic)) { - // fprintf(stdout, "%s: BAD (invalid header)\n", + // tfm::format(std::cout, "%s: BAD (invalid header)\n", // ToString(you).c_str()); ban = 100000; return true; @@ -194,7 +196,7 @@ std::string strCommand = hdr.GetCommand(); unsigned int nMessageSize = hdr.nMessageSize; if (nMessageSize > MAX_SIZE) { - // fprintf(stdout, "%s: BAD (message too large)\n", + // tfm::format(std::cout, "%s: BAD (message too large)\n", // ToString(you).c_str()); ban = 100000; return true; @@ -216,7 +218,7 @@ if (ProcessMessage(strCommand, vMsg) == PeerMessagingState::Finished) { return true; } - // fprintf(stdout, "%s: done processing %s\n", + // tfm::format(std::cout, "%s: done processing %s\n", // ToString(you).c_str(), // strCommand.c_str()); } while (1); @@ -262,7 +264,8 @@ } if (!connected) { - // fprintf(stdout, "Cannot connect to %s\n", ToString(you).c_str()); + // tfm::format(std::cout, "Cannot connect to %s\n", + // ToString(you).c_str()); CloseSocket(sock); return false; } @@ -303,12 +306,13 @@ vRecv.resize(nPos + nBytes); memcpy(&vRecv[nPos], pchBuf, nBytes); } else if (nBytes == 0) { - // fprintf(stdout, "%s: BAD (connection closed prematurely)\n", + // tfm::format(std::cout, "%s: BAD (connection closed + // prematurely)\n", // ToString(you).c_str()); res = false; break; } else { - // fprintf(stdout, "%s: BAD (connection error)\n", + // tfm::format(std::cout, "%s: BAD (connection error)\n", // ToString(you).c_str()); res = false; break; @@ -338,8 +342,8 @@ clientV = node.GetClientVersion(); clientSV = node.GetClientSubVersion(); blocks = node.GetStartingHeight(); - // fprintf(stdout, "%s: %s!!!\n", cip.ToString().c_str(), ret ? "GOOD" : - // "BAD"); + // tfm::format(std::cout, "%s: %s!!!\n", cip.ToString().c_str(), ret ? + // "GOOD" : "BAD"); return ret; } catch (std::ios_base::failure &e) { ban = 0; diff --git a/src/seeder/db.cpp b/src/seeder/db.cpp --- a/src/seeder/db.cpp +++ b/src/seeder/db.cpp @@ -28,7 +28,7 @@ if (ign && (ignoreTill == 0 || ignoreTill < ign + now)) { ignoreTill = ign + now; } - // fprintf(stdout, "%s: got %s result: success=%i/%i; + // tfm::format(std::cout, "%s: got %s result: success=%i/%i; // 2H:%.2f%%-%.2f%%(%.2f) 8H:%.2f%%-%.2f%%(%.2f) 1D:%.2f%%-%.2f%%(%.2f) // 1W:%.2f%%-%.2f%%(%.2f) \n", ToString(ip).c_str(), good ? "good" : "bad", // success, total, 100.0 * stat2H.reliability, 100.0 * (stat2H.reliability @@ -99,7 +99,7 @@ info.Update(true); if (info.IsGood() && goodId.count(id) == 0) { goodId.insert(id); - // fprintf(stdout, "%s: good; %i good nodes now\n", + // tfm::format(std::cout, "%s: good; %i good nodes now\n", // ToString(addr).c_str(), (int)goodId.size()); } nDirty++; @@ -117,13 +117,13 @@ uint32_t now = time(nullptr); int ter = info.GetBanTime(); if (ter) { - // fprintf(stdout, "%s: terrible\n", ToString(addr).c_str()); + // tfm::format(std::cout, "%s: terrible\n", ToString(addr).c_str()); if (ban < ter) { ban = ter; } } if (ban > 0) { - // fprintf(stdout, "%s: ban for %i seconds\n", + // tfm::format(std::cout, "%s: ban for %i seconds\n", // ToString(addr).c_str(), ban); banned[info.ip] = ban + now; ipToId.erase(info.ip); @@ -132,7 +132,7 @@ } else { if (/*!info.IsGood() && */ goodId.count(id) == 1) { goodId.erase(id); - // fprintf(stdout, "%s: not good; %i good nodes left\n", + // tfm::format(std::cout, "%s: not good; %i good nodes left\n", // ToString(addr).c_str(), (int)goodId.size()); } ourId.push_back(id); @@ -158,7 +158,8 @@ if (addr.nTime > ai.lastTry || ai.services != addr.nServices) { ai.lastTry = addr.nTime; ai.services |= addr.nServices; - // fprintf(stdout, "%s: updated\n", ToString(addr).c_str()); + // tfm::format(std::cout, "%s: updated\n", + // ToString(addr).c_str()); } if (force) { ai.ignoreTill = 0; @@ -176,7 +177,8 @@ int id = nId++; idToInfo[id] = ai; ipToId[ipp] = id; - // fprintf(stdout, "%s: added\n", ToString(ipp).c_str(), ipToId[ipp]); + // tfm::format(std::cout, "%s: added\n", ToString(ipp).c_str(), + // ipToId[ipp]); unkId.insert(id); nDirty++; } diff --git a/src/seeder/dns.cpp b/src/seeder/dns.cpp --- a/src/seeder/dns.cpp +++ b/src/seeder/dns.cpp @@ -385,14 +385,14 @@ outbuf[3] &= ~15; // check qr if (inbuf[2] & 128) { - /* fprintf(stdout, "Got response?\n"); */ + /* tfm::format(std::cout, "Got response?\n"); */ responseCode = DNSResponseCode::FORMAT_ERROR; goto error; } // check opcode if (((inbuf[2] & 120) >> 3) != 0) { - /* fprintf(stdout, "Opcode nonzero?\n"); */ + /* tfm::format(std::cout, "Opcode nonzero?\n"); */ responseCode = DNSResponseCode::NOT_IMPLEMENTED; goto error; } @@ -404,13 +404,13 @@ // check questions nquestion = (inbuf[4] << 8) + inbuf[5]; if (nquestion == 0) { - /* fprintf(stdout, "No questions?\n"); */ + /* tfm::format(std::cout, "No questions?\n"); */ responseCode = DNSResponseCode::OK; goto error; } if (nquestion > 1) { - /* fprintf(stdout, "Multiple questions %i?\n", nquestion); */ + /* tfm::format(std::cout, "Multiple questions %i?\n", nquestion); */ responseCode = DNSResponseCode::NOT_IMPLEMENTED; goto error; } @@ -466,8 +466,8 @@ uint8_t *outpos = outbuf + (inpos - inbuf); uint8_t *outend = outbuf + BUFLEN; - // fprintf(stdout, "DNS: Request host='%s' type=%i class=%i\n", name, - // typ, cls); + // tfm::format(std::cout, "DNS: Request host='%s' type=%i class=%i\n", + // name, typ, cls); // calculate max size of authority section @@ -485,8 +485,8 @@ if (max_auth_size < newpos - outpos) { max_auth_size = newpos - outpos; } - // fprintf(stdout, "Authority section will claim %i bytes max\n", - // max_auth_size); + // tfm::format(std::cout, "Authority section will claim %i bytes + // max\n", max_auth_size); } // Answer section @@ -496,7 +496,7 @@ (cls == CLASS_IN || cls == QCLASS_ANY)) { int ret2 = write_record_ns(&outpos, outend - max_auth_size, "", offset, CLASS_IN, opt->nsttl, opt->ns); - // fprintf(stdout, "wrote NS record: %i\n", ret2); + // tfm::format(std::cout, "wrote NS record: %i\n", ret2); if (!ret2) { outbuf[7]++; have_ns++; @@ -510,7 +510,7 @@ write_record_soa(&outpos, outend - max_auth_size, "", offset, CLASS_IN, opt->nsttl, opt->ns, opt->mbox, time(NULL), 604800, 86400, 2592000, 604800); - // fprintf(stdout, "wrote SOA record: %i\n", ret2); + // tfm::format(std::cout, "wrote SOA record: %i\n", ret2); if (!ret2) { outbuf[7]++; } @@ -536,7 +536,8 @@ opt->datattl, &addr[n]); } - // fprintf(stdout, "wrote A record: %i\n", mustbreak); + // tfm::format(std::cout, "wrote A record: %i\n", + // mustbreak); if (mustbreak) { break; } @@ -550,7 +551,7 @@ if (!have_ns && outbuf[7]) { int ret2 = write_record_ns(&outpos, outend, "", offset, CLASS_IN, opt->nsttl, opt->ns); - // fprintf(stdout, "wrote NS record: %i\n", ret2); + // tfm::format(std::cout, "wrote NS record: %i\n", ret2); if (!ret2) { outbuf[9]++; } @@ -562,7 +563,7 @@ int ret2 = write_record_soa( &outpos, outend, "", offset, CLASS_IN, opt->nsttl, opt->ns, opt->mbox, time(NULL), 604800, 86400, 2592000, 604800); - // fprintf(stdout, "wrote SOA record: %i\n", ret2); + // tfm::format(std::cout, "wrote SOA record: %i\n", ret2); if (!ret2) { outbuf[9]++; } @@ -644,9 +645,9 @@ for (; 1; ++(opt->nRequests)) { ssize_t insize = recvmsg(listenSocket, &msg, 0); // uint8_t *addr = (uint8_t*)&si_other.sin_addr.s_addr; - // fprintf(stdout, "DNS: Request %llu from %i.%i.%i.%i:%i of %i - // bytes\n", (unsigned long long)(opt->nRequests), addr[0], addr[1], - // addr[2], addr[3], ntohs(si_other.sin_port), (int)insize); + // tfm::format(std::cout, "DNS: Request %llu from %i.%i.%i.%i:%i of + // %i bytes\n", (unsigned long long)(opt->nRequests), addr[0], + // addr[1], addr[2], addr[3], ntohs(si_other.sin_port), (int)insize); if (insize <= 0) { continue; } diff --git a/src/seeder/main.cpp b/src/seeder/main.cpp --- a/src/seeder/main.cpp +++ b/src/seeder/main.cpp @@ -63,8 +63,8 @@ SetupSeederArgs(); 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 (HelpRequested(gArgs) || gArgs.IsArgSet("-version")) { @@ -77,7 +77,7 @@ gArgs.GetHelpMessage(); } - fprintf(stdout, "%s", strUsage.c_str()); + tfm::format(std::cout, "%s", strUsage.c_str()); return EXIT_SUCCESS; } @@ -377,13 +377,14 @@ } rename("dnsseed.dat.new", "dnsseed.dat"); } - FILE *d = fsbridge::fopen("dnsseed.dump", "w"); - fprintf(d, "# address good " - "lastSuccess %%(2h) %%(8h) %%(1d) %%(7d) " - "%%(30d) blocks svcs version\n"); + fsbridge::ofstream d{"dnsseed.dump"}; + tfm::format( + d, "# address good " + "lastSuccess %%(2h) %%(8h) %%(1d) %%(7d) " + "%%(30d) blocks svcs version\n"); double stat[5] = {0, 0, 0, 0, 0}; for (CAddrReport rep : v) { - fprintf( + tfm::format( d, "%-47s %4d %11" PRId64 " %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6.2f%% %6i %08" PRIx64 @@ -399,12 +400,10 @@ stat[3] += rep.uptime[3]; stat[4] += rep.uptime[4]; } - fclose(d); - FILE *ff = fsbridge::fopen("dnsstats.log", "a"); - fprintf(ff, "%llu %g %g %g %g %g\n", - (unsigned long long)(time(nullptr)), stat[0], stat[1], - stat[2], stat[3], stat[4]); - fclose(ff); + fsbridge::ofstream ff{"dnsstats.log", std::ios_base::app}; + tfm::format(ff, "%llu %g %g %g %g %g\n", + (unsigned long long)(time(nullptr)), stat[0], stat[1], + stat[2], stat[3], stat[4]); } } while (1); return nullptr; @@ -421,24 +420,25 @@ db.GetStats(stats); if (first) { first = false; - fprintf(stdout, "\n\n\n\x1b[3A"); + tfm::format(std::cout, "\n\n\n\x1b[3A"); } else { - fprintf(stdout, "\x1b[2K\x1b[u"); + tfm::format(std::cout, "\x1b[2K\x1b[u"); } - fprintf(stdout, "\x1b[s"); + tfm::format(std::cout, "\x1b[s"); uint64_t requests = 0; uint64_t queries = 0; for (unsigned int i = 0; i < dnsThread.size(); i++) { requests += dnsThread[i]->dns_opt.nRequests; queries += dnsThread[i]->dbQueries; } - fprintf(stdout, - "%s %i/%i available (%i tried in %is, %i new, %i active), %i " - "banned; %llu DNS requests, %llu db queries\n", - c, stats.nGood, stats.nAvail, stats.nTracked, stats.nAge, - stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, - stats.nBanned, (unsigned long long)requests, - (unsigned long long)queries); + tfm::format( + std::cout, + "%s %i/%i available (%i tried in %is, %i new, %i active), %i " + "banned; %llu DNS requests, %llu db queries\n", + c, stats.nGood, stats.nAvail, stats.nTracked, stats.nAge, + stats.nNew, stats.nAvail - stats.nTracked - stats.nNew, + stats.nBanned, (unsigned long long)requests, + (unsigned long long)queries); Sleep(1000); } while (1); return nullptr; @@ -473,87 +473,87 @@ return parseResults; } - fprintf(stdout, "Supporting whitelisted filters: "); + tfm::format(std::cout, "Supporting whitelisted filters: "); for (std::set::const_iterator it = opts.filter_whitelist.begin(); it != opts.filter_whitelist.end(); it++) { if (it != opts.filter_whitelist.begin()) { - fprintf(stdout, ","); + tfm::format(std::cout, ","); } - fprintf(stdout, "0x%lx", (unsigned long)*it); + tfm::format(std::cout, "0x%lx", (unsigned long)*it); } - fprintf(stdout, "\n"); + tfm::format(std::cout, "\n"); if (!opts.tor.empty()) { CService service(LookupNumeric(opts.tor.c_str(), 9050)); if (service.IsValid()) { - fprintf(stdout, "Using Tor proxy at %s\n", - service.ToStringIPPort().c_str()); + tfm::format(std::cout, "Using Tor proxy at %s\n", + service.ToStringIPPort().c_str()); SetProxy(NET_ONION, proxyType(service)); } } if (!opts.ipv4_proxy.empty()) { CService service(LookupNumeric(opts.ipv4_proxy.c_str(), 9050)); if (service.IsValid()) { - fprintf(stdout, "Using IPv4 proxy at %s\n", - service.ToStringIPPort().c_str()); + tfm::format(std::cout, "Using IPv4 proxy at %s\n", + service.ToStringIPPort().c_str()); SetProxy(NET_IPV4, proxyType(service)); } } if (!opts.ipv6_proxy.empty()) { CService service(LookupNumeric(opts.ipv6_proxy.c_str(), 9050)); if (service.IsValid()) { - fprintf(stdout, "Using IPv6 proxy at %s\n", - service.ToStringIPPort().c_str()); + tfm::format(std::cout, "Using IPv6 proxy at %s\n", + service.ToStringIPPort().c_str()); SetProxy(NET_IPV6, proxyType(service)); } } bool fDNS = true; - fprintf(stdout, "Using %s.\n", gArgs.GetChainName().c_str()); + tfm::format(std::cout, "Using %s.\n", gArgs.GetChainName().c_str()); netMagic = Params().NetMagic(); if (opts.ns.empty()) { - fprintf(stdout, "No nameserver set. Not starting DNS server.\n"); + tfm::format(std::cout, "No nameserver set. Not starting DNS server.\n"); fDNS = false; } if (fDNS && opts.host.empty()) { - fprintf(stderr, "No hostname set. Please use -h.\n"); + tfm::format(std::cerr, "No hostname set. Please use -h.\n"); return EXIT_FAILURE; } if (fDNS && opts.mbox.empty()) { - fprintf(stderr, "No e-mail address set. Please use -m.\n"); + tfm::format(std::cerr, "No e-mail address set. Please use -m.\n"); return EXIT_FAILURE; } FILE *f = fsbridge::fopen("dnsseed.dat", "r"); if (f) { - fprintf(stdout, "Loading dnsseed.dat..."); + tfm::format(std::cout, "Loading dnsseed.dat..."); CAutoFile cf(f, SER_DISK, CLIENT_VERSION); cf >> db; if (opts.fWipeBan) { db.banned.clear(); - fprintf(stdout, "Ban list wiped..."); + tfm::format(std::cout, "Ban list wiped..."); } if (opts.fWipeIgnore) { db.ResetIgnores(); - fprintf(stdout, "Ignore list wiped..."); + tfm::format(std::cout, "Ignore list wiped..."); } - fprintf(stdout, "done\n"); + tfm::format(std::cout, "done\n"); } pthread_t threadDns, threadSeed, threadDump, threadStats; if (fDNS) { - fprintf(stdout, "Starting %i DNS threads for %s on %s (port %i)...", - opts.nDnsThreads, opts.host.c_str(), opts.ns.c_str(), - opts.nPort); + tfm::format( + std::cout, "Starting %i DNS threads for %s on %s (port %i)...", + opts.nDnsThreads, opts.host.c_str(), opts.ns.c_str(), opts.nPort); dnsThread.clear(); for (int i = 0; i < opts.nDnsThreads; i++) { dnsThread.push_back(new CDnsThread(&opts, i)); pthread_create(&threadDns, nullptr, ThreadDNS, dnsThread[i]); - fprintf(stdout, "."); + tfm::format(std::cout, "."); Sleep(20); } - fprintf(stdout, "done\n"); + tfm::format(std::cout, "done\n"); } - fprintf(stdout, "Starting seeder..."); + tfm::format(std::cout, "Starting seeder..."); pthread_create(&threadSeed, nullptr, ThreadSeeder, nullptr); - fprintf(stdout, "done\n"); - fprintf(stdout, "Starting %i crawler threads...", opts.nThreads); + tfm::format(std::cout, "done\n"); + tfm::format(std::cout, "Starting %i crawler threads...", opts.nThreads); pthread_attr_t attr_crawler; pthread_attr_init(&attr_crawler); pthread_attr_setstacksize(&attr_crawler, 0x20000); @@ -562,7 +562,7 @@ pthread_create(&thread, &attr_crawler, ThreadCrawler, &opts.nThreads); } pthread_attr_destroy(&attr_crawler); - fprintf(stdout, "done\n"); + tfm::format(std::cout, "done\n"); pthread_create(&threadStats, nullptr, ThreadStats, nullptr); pthread_create(&threadDump, nullptr, ThreadDumper, nullptr); void *res; 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,10 @@ } // 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 @@ -19,7 +19,6 @@ ("src/netbase.cpp", "LogConnectFailure(bool manual_connection, const char *fmt, const Args &... args)"), ("src/util/system.cpp", "strprintf(_(COPYRIGHT_HOLDERS), _(COPYRIGHT_HOLDERS_SUBSTITUTION))"), - ("src/seeder/main.cpp", "fprintf(stderr, help, argv[0])"), ("src/tinyformat.h", "printf(const char *fmt, const Args &... args)"), ("src/tinyformat.h", "printf(const char *fmt, TINYFORMAT_VARARGS(n))"), ("src/wallet/wallet.h", @@ -37,6 +36,7 @@ ("snprintf", 2), ("sprintf", 1), ("strprintf", 0), + ("tfm::format", 1), # Assuming tfm::::format(std::ostream&, ... ("vfprintf", 1), ("vprintf", 1), ("vsnprintf", 1),