From 0c37fb18582cf87e0b1487a4899a023b5de59fdc Mon Sep 17 00:00:00 2001 From: Ravomavain Date: Sun, 12 May 2013 12:22:26 +0200 Subject: [PATCH] Add EDEBUG to CString::Escape function to escape debug output. --- include/znc/ZNCDebug.h | 4 +++- include/znc/ZNCString.h | 3 ++- src/ZNCString.cpp | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/include/znc/ZNCDebug.h b/include/znc/ZNCDebug.h index 46ff5e65..a8094492 100644 --- a/include/znc/ZNCDebug.h +++ b/include/znc/ZNCDebug.h @@ -30,7 +30,9 @@ */ #define DEBUG(f) do { \ if (CDebug::Debug()) { \ - std::cout << CDebug::GetTimestamp() << f << std::endl; \ + std::stringstream sDebug;\ + sDebug << f;\ + std::cout << CDebug::GetTimestamp() << CString(sDebug.str()).Escape_n(CString::EDEBUG) << std::endl; \ } \ } while (0) diff --git a/include/znc/ZNCString.h b/include/znc/ZNCString.h index d93b2090..2c210d5e 100644 --- a/include/znc/ZNCString.h +++ b/include/znc/ZNCString.h @@ -62,7 +62,8 @@ public: EURL, EHTML, ESQL, - ENAMEDFMT + ENAMEDFMT, + EDEBUG } EEscape; explicit CString(bool b) : std::string(b ? "true" : "false") {} diff --git a/src/ZNCString.cpp b/src/ZNCString.cpp index 1ac49da8..f2317e5d 100644 --- a/src/ZNCString.cpp +++ b/src/ZNCString.cpp @@ -164,6 +164,8 @@ CString::EEscape CString::ToEscape(const CString& sEsc) { return ESQL; } else if (sEsc.Equals("NAMEDFMT")) { return ENAMEDFMT; + } else if (sEsc.Equals("DEBUG")) { + return EDEBUG; } return EASCII; @@ -280,6 +282,26 @@ CString CString::Escape_n(EEscape eFrom, EEscape eTo) const { } break; + case EDEBUG: + if (*p == '\\' && (a +3) < iLength && *(p +1) == 'x' && isxdigit(*(p +2)) && isxdigit(*(p +3))) { + p += 2; + if (isdigit(*p)) { + ch = (unsigned char)((*p - '0') << 4); + } else { + ch = (unsigned char)((tolower(*p) - 'a' +10) << 4); + } + + p++; + if (isdigit(*p)) { + ch |= (unsigned char)(*p - '0'); + } else { + ch |= (unsigned char)(tolower(*p) - 'a' +10); + } + + a += 3; + } else { + ch = *p; + } } switch (eTo) { @@ -326,6 +348,16 @@ CString CString::Escape_n(EEscape eFrom, EEscape eTo) const { } else if (ch == '}') { sRet += '\\'; sRet += '}'; } else { sRet += ch; } + break; + case EDEBUG: + if (ch < 0x20 || ch == 0x7F) { + sRet += "\\x"; + sRet += szHex[ch >> 4]; + sRet += szHex[ch & 0xf]; + } else { + sRet += ch; + } + break; } }