From 5ca8cb51ba95e21800c985998337e00a70132562 Mon Sep 17 00:00:00 2001 From: NuclearW Date: Tue, 30 Sep 2014 14:33:42 -0400 Subject: [PATCH 1/2] log: Add -timestamp option Allows users to define a new timestamp as formatted by strftime through CUtils::FormatTime Additionally -sanitize is no longer necessarily the first argument, but -timestamp and the actual timestamp must be last as it can contain spaces. --- modules/log.cpp | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/modules/log.cpp b/modules/log.cpp index 2f3124a3..b7f8cc1e 100644 --- a/modules/log.cpp +++ b/modules/log.cpp @@ -106,6 +106,7 @@ public: private: CString m_sLogPath; + CString m_sTimestamp; bool m_bSanitize; vector m_vRules; }; @@ -268,15 +269,35 @@ 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; + VCString vsArgs; + sArgs.Split(" ", vsArgs); + + bool bHaveTimestamp = false; + bool bHaveLogPath = false; + for (CString& sArg : vsArgs) { + if (sArg.Equals("-sanitize")) { + m_bSanitize = true; + } else if (sArg.Equals("-timestamp")) { + // Everything after this must be timestamp + bHaveTimestamp = true; + } else { + if (bHaveTimestamp) { + m_sTimestamp += sArg + " "; + } else { + // Only one arg may be LogPath + if (bHaveLogPath) { + sMessage = "Invalid args [" + sArgs + "]. Only one log path allowed. Check that there are no spaces in the path."; + return false; + } + m_sLogPath = sArg; + bHaveLogPath = true; + } + } } - // Use load parameter as save path - m_sLogPath = sArgs.Token(uIndex); + if (m_sTimestamp.empty()) { + m_sTimestamp = "[%H:%M:%S] "; + } // Add default filename to path if it's a folder if (GetType() == CModInfo::UserModule) { @@ -313,7 +334,7 @@ bool CLogMod::OnLoad(const CString& sArgs, CString& sMessage) sMessage = "Invalid log path ["+m_sLogPath+"]."; return false; } else { - sMessage = "Logging to ["+m_sLogPath+"]."; + sMessage = "Logging to ["+m_sLogPath+"]. Using timestamp ["+m_sTimestamp+"]"; return true; } } From c2201a59d62a61d1815dfcf5f01f6f8f77717d6f Mon Sep 17 00:00:00 2001 From: Andreas Lutro Date: Sun, 21 Jun 2015 14:20:25 +0200 Subject: [PATCH 2/2] work on log module argument parsing --- modules/log.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/modules/log.cpp b/modules/log.cpp index b7f8cc1e..24c1cfb2 100644 --- a/modules/log.cpp +++ b/modules/log.cpp @@ -239,7 +239,7 @@ void CLogMod::PutLog(const CString& sLine, const CString& sWindow /*= "Status"*/ if (!CFile::Exists(sLogDir)) CDir::MakeDir(sLogDir, ModDirInfo.st_mode); if (LogFile.Open(O_WRONLY | O_APPEND | O_CREAT)) { - LogFile.Write(CUtils::FormatTime(curtime, "[%H:%M:%S] ", GetUser()->GetTimezone()) + (m_bSanitize ? sLine.StripControls_n() : sLine) + "\n"); + LogFile.Write(CUtils::FormatTime(curtime, m_sTimestamp, GetUser()->GetTimezone()) + " " + (m_bSanitize ? sLine.StripControls_n() : sLine) + "\n"); } else DEBUG("Could not open log file [" << sPath << "]: " << strerror(errno)); } @@ -270,33 +270,32 @@ CString CLogMod::GetServer() bool CLogMod::OnLoad(const CString& sArgs, CString& sMessage) { VCString vsArgs; - sArgs.Split(" ", vsArgs); + sArgs.QuoteSplit(vsArgs); - bool bHaveTimestamp = false; + bool bReadingTimestamp = false; bool bHaveLogPath = false; + for (CString& sArg : vsArgs) { - if (sArg.Equals("-sanitize")) { + if (bReadingTimestamp) { + m_sTimestamp = sArg; + bReadingTimestamp = false; + } else if (sArg.Equals("-sanitize")) { m_bSanitize = true; } else if (sArg.Equals("-timestamp")) { - // Everything after this must be timestamp - bHaveTimestamp = true; + bReadingTimestamp = true; } else { - if (bHaveTimestamp) { - m_sTimestamp += sArg + " "; - } else { - // Only one arg may be LogPath - if (bHaveLogPath) { - sMessage = "Invalid args [" + sArgs + "]. Only one log path allowed. Check that there are no spaces in the path."; - return false; - } - m_sLogPath = sArg; - bHaveLogPath = true; + // Only one arg may be LogPath + if (bHaveLogPath) { + sMessage = "Invalid args [" + sArgs + "]. Only one log path allowed. Check that there are no spaces in the path."; + return false; } + m_sLogPath = sArg; + bHaveLogPath = true; } } if (m_sTimestamp.empty()) { - m_sTimestamp = "[%H:%M:%S] "; + m_sTimestamp = "[%H:%M:%S]"; } // Add default filename to path if it's a folder @@ -329,12 +328,11 @@ bool CLogMod::OnLoad(const CString& sArgs, CString& sMessage) // Check if it's allowed to write in this path in general m_sLogPath = CDir::CheckPathPrefix(GetSavePath(), m_sLogPath); - if (m_sLogPath.empty()) - { + if (m_sLogPath.empty()) { sMessage = "Invalid log path ["+m_sLogPath+"]."; return false; } else { - sMessage = "Logging to ["+m_sLogPath+"]. Using timestamp ["+m_sTimestamp+"]"; + sMessage = "Logging to ["+m_sLogPath+"]. Using timestamp format '"+m_sTimestamp+"'"; return true; } }