Permalink
Browse files

Refactor log printing

Add Typesafe
replace printf with LogPrint(f)
remove .c_str() from prints
remove prints()
replace PRI64(d,u,x) with %d, %u, %x
remove ToStringShorts
replace multiple debug switches with -debug=  or just -debug for all
use qDebug() for QT
  • Loading branch information...
Tranz5 committed Oct 14, 2014
1 parent 0cbb1f9 commit de48d363c150bb6918e7d70f17f3a0e71b49182b
View
@@ -260,8 +260,6 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId, int nOrigin)
void CAddrMan::Good_(const CService &addr, int64_t nTime)
{
// printf("Good: addr=%s\n", addr.ToString().c_str());
int nId;
CAddrInfo *pinfo = Find(addr, &nId);
@@ -303,7 +301,7 @@ void CAddrMan::Good_(const CService &addr, int64_t nTime)
// TODO: maybe re-add the node, but for now, just bail out
if (nUBucket == -1) return;
printf("Moving %s to tried\n", addr.ToString().c_str());
LogPrint("addrman", "Moving %s to tried\n", addr.ToString());
// move nId to the tried tables
MakeTried(info, nId, nUBucket);
@@ -350,7 +348,6 @@ bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimeP
} else {
pinfo = Create(addr, source, &nId);
pinfo->nTime = max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
// printf("Added %s [nTime=%fhr]\n", pinfo->ToString().c_str(), (GetAdjustedTime() - pinfo->nTime) / 3600.0);
nNew++;
fNew = true;
}
View
@@ -403,7 +403,7 @@ class CAddrMan
LOCK(cs);
int err;
if ((err=Check_()))
printf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
}
#endif
}
@@ -419,7 +419,7 @@ class CAddrMan
Check();
}
if (fRet)
printf("Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort().c_str(), source.ToString().c_str(), nTried, nNew);
LogPrint("addrman", "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
return fRet;
}
@@ -435,7 +435,7 @@ class CAddrMan
Check();
}
if (nAdd)
printf("Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString().c_str(), nTried, nNew);
LogPrint("addrman", "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
return nAdd > 0;
}
View
@@ -56,8 +56,8 @@ std::string CUnsignedAlert::ToString() const
return strprintf(
"CAlert(\n"
" nVersion = %d\n"
" nRelayUntil = %"PRI64d"\n"
" nExpiration = %"PRI64d"\n"
" nRelayUntil = %d\n"
" nExpiration = %d\n"
" nID = %d\n"
" nCancel = %d\n"
" setCancel = %s\n"
@@ -73,18 +73,13 @@ std::string CUnsignedAlert::ToString() const
nExpiration,
nID,
nCancel,
strSetCancel.c_str(),
strSetCancel,
nMinVer,
nMaxVer,
strSetSubVer.c_str(),
strSetSubVer,
nPriority,
strComment.c_str(),
strStatusBar.c_str());
}
void CUnsignedAlert::print() const
{
printf("%s", ToString().c_str());
strComment,
strStatusBar);
}
void CAlert::SetNull()
@@ -210,13 +205,13 @@ bool CAlert::ProcessAlert(bool fThread)
const CAlert& alert = (*mi).second;
if (Cancels(alert))
{
printf("cancelling alert %d\n", alert.nID);
LogPrint("alert", "cancelling alert %d\n", alert.nID);
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
mapAlerts.erase(mi++);
}
else if (!alert.IsInEffect())
{
printf("expiring alert %d\n", alert.nID);
LogPrint("alert", "expiring alert %d\n", alert.nID);
uiInterface.NotifyAlertChanged((*mi).first, CT_DELETED);
mapAlerts.erase(mi++);
}
@@ -230,7 +225,7 @@ bool CAlert::ProcessAlert(bool fThread)
const CAlert& alert = item.second;
if (alert.Cancels(*this))
{
printf("alert already cancelled by %d\n", alert.nID);
LogPrint("alert", "alert already cancelled by %d\n", alert.nID);
return false;
}
}
@@ -258,6 +253,6 @@ bool CAlert::ProcessAlert(bool fThread)
}
}
printf("accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
LogPrint("alert", "accepted alert %d, AppliesToMe()=%d\n", nID, AppliesToMe());
return true;
}
View
@@ -61,7 +61,6 @@ class CUnsignedAlert
void SetNull();
std::string ToString() const;
void print() const;
};
/** An alert is a combination of a serialized CUnsignedAlert and a signature. */
View
@@ -80,12 +80,12 @@ void RPCTypeCheck(const Object& o,
{
const Value& v = find_value(o, t.first);
if (!fAllowNull && v.type() == null_type)
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first.c_str()));
throw JSONRPCError(RPC_TYPE_ERROR, strprintf("Missing %s", t.first));
if (!((v.type() == t.second) || (fAllowNull && (v.type() == null_type))))
{
string err = strprintf("Expected type %s for %s, got %s",
Value_type_name[t.second], t.first.c_str(), Value_type_name[v.type()]);
Value_type_name[t.second], t.first, Value_type_name[v.type()]);
throw JSONRPCError(RPC_TYPE_ERROR, err);
}
}
@@ -145,7 +145,7 @@ string CRPCTable::help(string strCommand) const
}
}
if (strRet == "")
strRet = strprintf("help: unknown command: %s\n", strCommand.c_str());
strRet = strprintf("help: unknown command: %s\n", strCommand);
strRet = strRet.substr(0,strRet.size()-1);
return strRet;
}
@@ -351,7 +351,7 @@ static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
"<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>\r\n"
"</HEAD>\r\n"
"<BODY><H1>401 Unauthorized.</H1></BODY>\r\n"
"</HTML>\r\n", rfc1123Time().c_str(), FormatFullVersion().c_str());
"</HTML>\r\n", rfc1123Time(), FormatFullVersion());
const char *cStatus;
if (nStatus == HTTP_OK) cStatus = "OK";
else if (nStatus == HTTP_BAD_REQUEST) cStatus = "Bad Request";
@@ -370,11 +370,11 @@ static string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
"%s",
nStatus,
cStatus,
rfc1123Time().c_str(),
rfc1123Time(),
keepalive ? "keep-alive" : "close",
strMsg.size(),
FormatFullVersion().c_str(),
strMsg.c_str());
FormatFullVersion(),
strMsg);
}
int ReadHTTPStatus(std::basic_istream<char>& stream, int &proto)
@@ -651,7 +651,7 @@ void ThreadRPCServer(void* parg)
vnThreadsRunning[THREAD_RPCLISTENER]--;
PrintException(NULL, "ThreadRPCServer()");
}
printf("ThreadRPCServer exited\n");
LogPrintf("ThreadRPCServer exited\n");
}
// Forward declaration required for RPCListen
@@ -723,7 +723,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
// start HTTP client thread
else if (!NewThread(ThreadRPCServer3, conn)) {
printf("Failed to create RPC server client thread\n");
LogPrintf("Failed to create RPC server client thread\n");
delete conn;
}
@@ -732,7 +732,7 @@ static void RPCAcceptHandler(boost::shared_ptr< basic_socket_acceptor<Protocol,
void ThreadRPCServer2(void* parg)
{
printf("ThreadRPCServer started\n");
LogPrintf("ThreadRPCServer started\n");
strRPCUserColonPass = mapArgs["-rpcuser"] + ":" + mapArgs["-rpcpassword"];
@@ -755,9 +755,9 @@ void ThreadRPCServer2(void* parg)
"If the file does not exist, create it with owner-readable-only file permissions.\n"
"It is also recommended to set alertnotify so you are notified of problems;\n"
"for example: alertnotify=echo %%s | mail -s \"HoboNickels Alert\" admin@foo.com\n"),
strWhatAmI.c_str(),
GetConfigFile().string().c_str(),
EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32).c_str()),
strWhatAmI,
GetConfigFile().string(),
EncodeBase58(&rand_pwd[0],&rand_pwd[0]+32)),
_("Error"), CClientUIInterface::MSG_ERROR);
StartShutdown();
return;
@@ -775,12 +775,12 @@ void ThreadRPCServer2(void* parg)
filesystem::path pathCertFile(GetArg("-rpcsslcertificatechainfile", "server.cert"));
if (!pathCertFile.is_complete()) pathCertFile = filesystem::path(GetDataDir()) / pathCertFile;
if (filesystem::exists(pathCertFile)) context.use_certificate_chain_file(pathCertFile.string());
else printf("ThreadRPCServer ERROR: missing server certificate file %s\n", pathCertFile.string().c_str());
else LogPrintf("ThreadRPCServer ERROR: missing server certificate file %s\n", pathCertFile.string());
filesystem::path pathPKFile(GetArg("-rpcsslprivatekeyfile", "server.pem"));
if (!pathPKFile.is_complete()) pathPKFile = filesystem::path(GetDataDir()) / pathPKFile;
if (filesystem::exists(pathPKFile)) context.use_private_key_file(pathPKFile.string(), ssl::context::pem);
else printf("ThreadRPCServer ERROR: missing server private key file %s\n", pathPKFile.string().c_str());
else LogPrintf("ThreadRPCServer ERROR: missing server private key file %s\n", pathPKFile.string());
string strCiphers = GetArg("-rpcsslciphers", "TLSv1+HIGH:!SSLv2:!aNULL:!eNULL:!AH:!3DES:@STRENGTH");
SSL_CTX_set_cipher_list(context.impl(), strCiphers.c_str());
@@ -890,7 +890,7 @@ void JSONRequest::parse(const Value& valRequest)
throw JSONRPCError(RPC_INVALID_REQUEST, "Method must be a string");
strMethod = valMethod.get_str();
if (strMethod != "getwork" && strMethod != "getblocktemplate")
printf("ThreadRPCServer method=%s\n", strMethod.c_str());
LogPrintf("ThreadRPCServer method=%s\n", strMethod);
// Parse params
Value valParams = find_value(request, "params");
@@ -973,7 +973,7 @@ void ThreadRPCServer3(void* parg)
}
if (!HTTPAuthorized(mapHeaders))
{
printf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer_address_to_string().c_str());
LogPrintf("ThreadRPCServer incorrect password attempt from %s\n", conn->peer_address_to_string());
/* Deter brute-forcing short passwords.
If this results in a DOS the user really
shouldn't have their RPC port exposed.*/
@@ -1087,7 +1087,7 @@ Object CallRPC(const string& strMethod, const Array& params)
throw runtime_error(strprintf(
_("You must set rpcpassword=<password> in the configuration file:\n%s\n"
"If the file does not exist, create it with owner-readable-only file permissions."),
GetConfigFile().string().c_str()));
GetConfigFile().string()));
// Connect to localhost
bool fUseSSL = GetBoolArg("-rpcssl");
@@ -1327,7 +1327,7 @@ int main(int argc, char *argv[])
{
if (argc >= 2 && string(argv[1]) == "-server")
{
printf("server ready\n");
LogPrintf("server ready\n");
ThreadRPCServer(NULL);
}
else
Oops, something went wrong.

0 comments on commit de48d36

Please sign in to comment.