diff --git a/include/znc/ZNCDebug.h b/include/znc/ZNCDebug.h index a8094492..310505f8 100644 --- a/include/znc/ZNCDebug.h +++ b/include/znc/ZNCDebug.h @@ -11,11 +11,7 @@ #include #include -#include -#include -#include #include -#include /** Output a debug info if debugging is enabled. * If ZNC was compiled with --enable-debug or was started with @@ -30,9 +26,8 @@ */ #define DEBUG(f) do { \ if (CDebug::Debug()) { \ - std::stringstream sDebug;\ + CDebugStream sDebug;\ sDebug << f;\ - std::cout << CDebug::GetTimestamp() << CString(sDebug.str()).Escape_n(CString::EDEBUG) << std::endl; \ } \ } while (0) @@ -42,21 +37,15 @@ public: static bool StdoutIsTTY() { return stdoutIsTTY; } static void SetDebug(bool b) { debug = b; } static bool Debug() { return debug; } - static CString GetTimestamp() { - char buf[64]; - timeval time_now; - gettimeofday(&time_now, NULL); - time_t currentSec = (time_t) time_now.tv_sec; // cast from long int - tm* time_info = localtime(¤tSec); - strftime(buf, sizeof(buf), "[%Y-%m-%d %H:%M:%S.", time_info); - std::ostringstream buffer; - buffer << buf << std::setw(6) << std::setfill('0') << (time_now.tv_usec) << "] "; - return buffer.str(); - } protected: static bool stdoutIsTTY; static bool debug; }; +class CDebugStream : public std::ostringstream { +public: + ~CDebugStream(); +}; + #endif // !ZNCDEBUG_H diff --git a/src/HTTPSock.cpp b/src/HTTPSock.cpp index 12a5cd0b..84618fdd 100644 --- a/src/HTTPSock.cpp +++ b/src/HTTPSock.cpp @@ -8,6 +8,7 @@ #include #include +#include #ifdef HAVE_ZLIB diff --git a/src/ZNCDebug.cpp b/src/ZNCDebug.cpp index 36a6efc5..d457928a 100644 --- a/src/ZNCDebug.cpp +++ b/src/ZNCDebug.cpp @@ -7,6 +7,9 @@ */ #include +#include +#include +#include bool CDebug::stdoutIsTTY = true; bool CDebug::debug = @@ -15,3 +18,17 @@ bool CDebug::debug = #else false; #endif + +CDebugStream::~CDebugStream() { + timeval tTime; + gettimeofday(&tTime, NULL); + time_t tSec = (time_t)tTime.tv_sec; // some systems (e.g. openbsd) define tv_sec as long int instead of time_t + tm tM; + tzset();// localtime_r requires this + localtime_r(&tSec, &tM); + char sTime[20] = {}; + strftime(sTime, sizeof(sTime), "%Y-%m-%d %H:%M:%S", &tM); + char sUsec[7] = {}; + snprintf(sUsec, sizeof(sUsec), "%06lu", (unsigned long int)tTime.tv_usec); + std::cout << "[" << sTime << "." << sUsec << "] " << CString(this->str()).Escape_n(CString::EDEBUG) << std::endl; +}