Permalink
Browse files

Merge pull request #355 from ravomavain/pull_logstrip

Add -sanitize option to log module. (v3)
  • Loading branch information...
DarthGandalf committed May 26, 2013
2 parents 40fa544 + 3ba4598 commit def14c2b992f1c77b46d28b6dfe5bbc2693fdec1
Showing with 80 additions and 4 deletions.
  1. +13 −0 include/znc/ZNCString.h
  2. +15 −4 modules/log.cpp
  3. +52 −0 src/ZNCString.cpp
View
@@ -457,6 +457,19 @@ class CString : public std::string {
* @return The result of the conversion.
*/
CString RightChomp_n(size_type uLen = 1) const;
+ /** Remove controls characters from this string.
+ * Controls characters are color codes, and those in C0 set
+ * See https://en.wikipedia.org/wiki/C0_and_C1_control_codes
+ * @return The result of the conversion.
+ */
+ CString& StripControls();
+ /** Remove controls characters from this string.
+ * Controls characters are color codes, and those in C0 set
+ * See https://en.wikipedia.org/wiki/C0_and_C1_control_codes
+ * This string object isn't modified.
+ * @return The result of the conversion.
+ */
+ CString StripControls_n() const;
private:
protected:
View
@@ -17,7 +17,10 @@ using std::vector;
class CLogMod: public CModule {
public:
- MODCONSTRUCTOR(CLogMod) {}
+ MODCONSTRUCTOR(CLogMod)
+ {
+ m_bSanitize = false;
+ }
void PutLog(const CString& sLine, const CString& sWindow = "status");
void PutLog(const CString& sLine, const CChan& Channel);
@@ -54,6 +57,7 @@ class CLogMod: public CModule {
private:
CString m_sLogPath;
+ bool m_bSanitize;
};
void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/)
@@ -88,7 +92,7 @@ void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/
if (!CFile::Exists(sLogDir)) CDir::MakeDir(sLogDir);
if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT))
{
- LogFile.Write(CUtils::FormatTime(curtime, "[%H:%M:%S] ", m_pUser->GetTimezone()) + sLine + "\n");
+ LogFile.Write(CUtils::FormatTime(curtime, "[%H:%M:%S] ", m_pUser->GetTimezone()) + (m_bSanitize ? sLine.StripControls_n() : sLine) + "\n");
} else
DEBUG("Could not open log file [" << sPath << "]: " << strerror(errno));
}
@@ -118,8 +122,15 @@ CString CLogMod::GetServer()
bool CLogMod::OnLoad(const CString& sArgs, CString& sMessage)
{
+ size_t uIndex = 0;
+ if (sArgs.Token(0).Equals("-sanitize"))
+ {
+ m_bSanitize = true;
+ ++uIndex;
+ }
+
// Use load parameter as save path
- m_sLogPath = sArgs;
+ m_sLogPath = sArgs.Token(uIndex);
// Add default filename to path if it's a folder
if (GetType() == CModInfo::UserModule) {
@@ -282,7 +293,7 @@ template<> void TModInfo<CLogMod>(CModInfo& Info) {
Info.AddType(CModInfo::NetworkModule);
Info.AddType(CModInfo::GlobalModule);
Info.SetHasArgs(true);
- Info.SetArgsHelpText("Optional path where to store logs.");
+ Info.SetArgsHelpText("[-sanitize] Optional path where to store logs.");
Info.SetWikiPage("log");
}
View
@@ -1131,6 +1131,58 @@ bool CString::RightChomp(size_type uLen) {
return bRet;
}
+CString CString::StripControls_n() const {
+ CString sRet;
+ const unsigned char *pStart = (const unsigned char*) data();
+ unsigned char ch = *pStart;
+ size_type iLength = length();
+ sRet.reserve(iLength);
+ bool colorCode = false;
+ unsigned int digits = 0;
+ bool comma = false;
+
+ for (unsigned int a = 0; a < iLength; a++, ch = pStart[a]) {
+ // Color code. Format: \x03([0-9]{1,2}(,[0-9]{1,2})?)?
+ if (ch == 0x03) {
+ colorCode = true;
+ digits = 0;
+ comma = false;
+ continue;
+ }
+ if (colorCode) {
+ if (isdigit(ch) && digits < 2) {
+ digits++;
+ continue;
+ }
+ if (ch == ',' && !comma && digits > 0) {
+ comma = true;
+ digits = 0;
+ continue;
+ }
+
+ colorCode = false;
+
+ if (digits == 0 && comma) { // There was a ',' which wasn't followed by digits, we should print it.
+ sRet += ',';
+ }
+ }
+ // CO controls codes
+ if (ch < 0x20 || ch == 0x7F)
+ continue;
+ sRet += ch;
+ }
+ if (colorCode && digits == 0 && comma) {
+ sRet += ',';
+ }
+
+ sRet.reserve(0);
+ return sRet;
+}
+
+CString& CString::StripControls() {
+ return (*this = StripControls_n());
+}
+
//////////////// MCString ////////////////
const MCString MCString::EmptyMap;

0 comments on commit def14c2

Please sign in to comment.