Merge pull request #978 from alprs/log-timestamp

log: Add -timestamp option (cont.)
This commit is contained in:
Alexey Sokolov
2015-08-23 20:38:42 +01:00

View File

@@ -107,6 +107,7 @@ public:
private:
CString m_sLogPath;
CString m_sTimestamp;
bool m_bSanitize;
vector<CLogRule> m_vRules;
};
@@ -239,7 +240,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));
}
@@ -269,15 +270,34 @@ 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.QuoteSplit(vsArgs);
bool bReadingTimestamp = false;
bool bHaveLogPath = false;
for (CString& sArg : vsArgs) {
if (bReadingTimestamp) {
m_sTimestamp = sArg;
bReadingTimestamp = false;
} else if (sArg.Equals("-sanitize")) {
m_bSanitize = true;
} else if (sArg.Equals("-timestamp")) {
bReadingTimestamp = true;
} 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) {
@@ -309,12 +329,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+"].";
sMessage = "Logging to ["+m_sLogPath+"]. Using timestamp format '"+m_sTimestamp+"'";
return true;
}
}