Streamline Log with print() and out()

This commit is contained in:
Vinnie Falco
2013-06-30 12:11:42 -07:00
parent c35c52ff08
commit b52bbccd8a
25 changed files with 174 additions and 81 deletions

View File

@@ -62,13 +62,13 @@ Section ParseSection (const std::string& strInput, const bool bTrim)
void SectionEntriesPrint (std::vector<std::string>* vspEntries, const std::string& strSection)
{
std::cerr << "[" << strSection << "]" << std::endl;
Log::out() << "[" << strSection << "]";
if (vspEntries)
{
BOOST_FOREACH (std::string & strValue, *vspEntries)
{
std::cerr << strValue << std::endl;
Log::out() << strValue;
}
}
}

View File

@@ -139,13 +139,33 @@ Log::~Log ()
logMsg += "...";
}
print (logMsg, mSeverity >= sMinSeverity);
}
void Log::print (std::string const& text, bool toStdErr)
{
boost::recursive_mutex::scoped_lock sl (sLock);
if (mSeverity >= sMinSeverity)
std::cerr << logMsg << std::endl;
// Always write to the log file if it is open.
//
if (outStream != NULL)
(*outStream) << logMsg << std::endl;
{
(*outStream) << text << std::endl;
}
if (toStdErr)
{
if (beast_isRunningUnderDebugger ())
{
// Send it to the attached debugger's Output window
//
Logger::outputDebugString (text);
}
else
{
std::cerr << text << std::endl;
}
}
}
std::string Log::rotateLog (void)

View File

@@ -117,6 +117,63 @@ public:
static std::string rotateLog ();
public:
/** Write to log output.
All logging eventually goes through this function. If a
debugger is attached, the string goes to the debugging console,
else it goes to the standard error output. If a log file is
open, then the message is additionally written to the open log
file.
The text should not contain a newline, it will be automatically
added as needed.
@note This acquires a global mutex.
@param text The text to write.
@param toStdErr `true` to also write to std::cerr
*/
static void print (std::string const& text,
bool toStdErr = true);
/** Output stream for logging
This is a convenient replacement for writing to `std::cerr`.
Usage:
@code
Log::out () << "item1" << 2;
@endcode
It is not necessary to append a newline.
*/
class out
{
public:
out ()
{
}
~out ()
{
Log::print (m_ss.str ());
}
template <class T>
out& operator<< (T t)
{
m_ss << t;
return *this;
}
private:
std::stringstream m_ss;
};
private:
enum
{

View File

@@ -37,7 +37,7 @@ void RandomNumbers::fillBytes (void* destinationBuffer, int numberOfBytes)
if (! initialize ())
{
char const* message = "Unable to add system entropy";
std::cerr << message << std::endl;
Log::out() << message;
throw std::runtime_error (message);
}
}
@@ -75,7 +75,7 @@ bool RandomNumbers::platformAddEntropy ()
if (!CryptGetDefaultProviderA (PROV_RSA_FULL, NULL, CRYPT_MACHINE_DEFAULT, name, &count))
{
#ifdef BEAST_DEBUG
std::cerr << "Unable to get default crypto provider" << std::endl;
Log::out() << "Unable to get default crypto provider";
#endif
return false;
}
@@ -83,7 +83,7 @@ bool RandomNumbers::platformAddEntropy ()
if (!CryptAcquireContextA (&cryptoHandle, NULL, name, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT))
{
#ifdef BEAST_DEBUG
std::cerr << "Unable to acquire crypto provider" << std::endl;
Log::out() << "Unable to acquire crypto provider";
#endif
return false;
}
@@ -91,7 +91,7 @@ bool RandomNumbers::platformAddEntropy ()
if (!CryptGenRandom (cryptoHandle, 128, reinterpret_cast<BYTE*> (rand)))
{
#ifdef BEAST_DEBUG
std::cerr << "Unable to get entropy from crypto provider" << std::endl;
Log::out() << "Unable to get entropy from crypto provider";
#endif
CryptReleaseContext (cryptoHandle, 0);
return false;
@@ -115,7 +115,7 @@ bool RandomNumbers::platformAddEntropy ()
if (!reader.is_open ())
{
#ifdef BEAST_DEBUG
std::cerr << "Unable to open random source" << std::endl;
Log::out() << "Unable to open random source";
#endif
return false;
}
@@ -127,7 +127,7 @@ bool RandomNumbers::platformAddEntropy ()
if (bytesRead == 0)
{
#ifdef BEAST_DEBUG
std::cerr << "Unable to read from random source" << std::endl;
Log::out() << "Unable to read from random source";
#endif
return false;
}

View File

@@ -188,6 +188,7 @@ extern std::string urlEncode (const std::string& strSrc)
// IP Port parsing
//
// <-- iPort: "" = -1
// VFALCO TODO Make this not require boost... and especially boost::asio
bool parseIpPort (const std::string& strSource, std::string& strIP, int& iPort)
{
boost::smatch smMatch;
@@ -235,10 +236,10 @@ bool parseUrl (const std::string& strUrl, std::string& strScheme, std::string& s
boost::algorithm::to_lower (strScheme);
iPort = strPort.empty () ? -1 : lexical_cast_s<int> (strPort);
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'" << std::endl;
// Log::out() << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPort << "' : " << iPort << " : '" << strPath << "'";
}
// std::cerr << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'" << std::endl;
// Log::out() << strUrl << " : " << bMatch << " : '" << strDomain << "' : '" << strPath << "'";
return bMatch;
}
@@ -261,3 +262,12 @@ bool parseQuality (const std::string& strSource, uint32& uQuality)
return !!uQuality;
}
std::string addressToString (void const* address)
{
// VFALCO TODO Clean this up, use uintptr_t and only produce a 32 bit
// output on 32 bit platforms
//
return strHex (static_cast <char const*> (address) - static_cast <char const*> (0));
}

View File

@@ -208,6 +208,10 @@ template<typename T> std::string lexical_cast_it (const T& t)
bool parseUrl (const std::string& strUrl, std::string& strScheme, std::string& strDomain, int& iPort, std::string& strPath);
#endif
#define ADDRESS(p) strHex(uint64( ((char*) p) - ((char*) 0)))
// vim:ts=4
/** Convert a pointer address to a string for display purposes.
*/
extern std::string addressToString (void const* address);
#endif

View File

@@ -197,9 +197,9 @@ void Config::setup (const std::string& strConf, bool bTestNet, bool bQuiet)
// Update default values
load ();
// std::cerr << "CONFIG FILE: " << CONFIG_FILE << std::endl;
// std::cerr << "CONFIG DIR: " << CONFIG_DIR << std::endl;
// std::cerr << "DATA DIR: " << DATA_DIR << std::endl;
// Log::out() << "CONFIG FILE: " << CONFIG_FILE;
// Log::out() << "CONFIG DIR: " << CONFIG_DIR;
// Log::out() << "DATA DIR: " << DATA_DIR;
boost::filesystem::create_directories (DATA_DIR, ec);
@@ -274,13 +274,13 @@ Config::Config ()
void Config::load ()
{
if (!QUIET)
std::cerr << "Loading: " << CONFIG_FILE << std::endl;
Log::out() << "Loading: " << CONFIG_FILE;
std::ifstream ifsConfig (CONFIG_FILE.c_str (), std::ios::in);
if (!ifsConfig)
{
std::cerr << "Failed to open '" << CONFIG_FILE << "'." << std::endl;
Log::out() << "Failed to open '" << CONFIG_FILE << "'.";
}
else
{
@@ -291,7 +291,7 @@ void Config::load ()
if (ifsConfig.bad ())
{
std::cerr << "Failed to read '" << CONFIG_FILE << "'." << std::endl;
Log::out() << "Failed to read '" << CONFIG_FILE << "'.";
}
else
{

View File

@@ -277,7 +277,7 @@ bool checkECIES (void)
if ((i % 100) == 0)
{
// generate new keys every 100 times
// std::cerr << "new keys" << std::endl;
// Log::out() << "new keys";
senderPriv.MakeNewKey ();
recipientPriv.MakeNewKey ();
@@ -307,7 +307,7 @@ bool checkECIES (void)
return false;
}
// std::cerr << "Msg(" << msglen << ") ok " << ciphertext.size() << std::endl;
//Log::out() << "Msg(" << msglen << ") ok " << ciphertext.size();
}
return true;

View File

@@ -19,7 +19,7 @@ PackedMessage::PackedMessage (::google::protobuf::Message const& message, int ty
message.SerializeToArray (&mBuffer [PackedMessage::kHeaderBytes], messageBytes);
#ifdef BEAST_DEBUG
// std::cerr << "PackedMessage: type=" << type << ", datalen=" << msg_size << std::endl;
//Log::out() << "PackedMessage: type=" << type << ", datalen=" << msg_size;
#endif
}
}

View File

@@ -814,17 +814,17 @@ bool RippleAddress::setSeedGeneric (const std::string& strText)
}
else if (setSeed (strText))
{
// std::cerr << "Recognized seed." << std::endl;
// Log::out() << "Recognized seed.";
nothing ();
}
else if (1 == setSeed1751 (strText))
{
// std::cerr << "Recognized 1751 seed." << std::endl;
// Log::out() << "Recognized 1751 seed.";
nothing ();
}
else
{
// std::cerr << "Creating seed from pass phrase." << std::endl;
// Log::out() << "Creating seed from pass phrase.";
setSeed (CKey::PassPhraseToKey (strText));
}

View File

@@ -50,7 +50,7 @@ bool STAmount::currencyFromString (uint160& uDstCurrency, const std::string& sCu
// std::string sIso;
// sIso.assign(vucIso.begin(), vucIso.end());
// std::cerr << "currency: " << sIso << std::endl;
// Log::out() << "currency: " << sIso;
Serializer s;

View File

@@ -25,6 +25,8 @@ SerializedType& SerializedType::operator= (const SerializedType& t)
void STPathSet::printDebug ()
{
// VFALCO NOTE Can't use Log::out() because of std::endl
//
for (int i = 0; i < value.size (); i++)
{
std::cerr << i << ": ";
@@ -45,13 +47,13 @@ void STPathSet::printDebug ()
void STPath::printDebug ()
{
std::cerr << "STPath:" << std::endl;
Log::out() << "STPath:";
for (int i = 0; i < mPath.size (); i++)
{
RippleAddress nad;
nad.setAccountID (mPath[i].mAccountID);
std::cerr << " " << i << ": " << nad.humanAccountID () << std::endl;
Log::out() << " " << i << ": " << nad.humanAccountID ();
}
}