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