diff --git a/modules/adminlog.cpp b/modules/adminlog.cpp index 16367994..6c8b8b50 100644 --- a/modules/adminlog.cpp +++ b/modules/adminlog.cpp @@ -27,7 +27,7 @@ public: MODCONSTRUCTOR(CAdminLogMod) { AddHelpCommand(); AddCommand("Show", static_cast(&CAdminLogMod::OnShowCommand), "", "Show the logging target"); - AddCommand("Target", static_cast(&CAdminLogMod::OnTargetCommand), "", "Set the logging target"); + AddCommand("Target", static_cast(&CAdminLogMod::OnTargetCommand), " [path]", "Set the logging target"); openlog("znc", LOG_PID, LOG_DAEMON); } @@ -47,7 +47,7 @@ public: else m_eLogMode = LOG_TO_FILE; - m_sLogFile = GetSavePath() + "/znc.log"; + SetLogFilePath(GetNV("path")); Log("Logging started. ZNC PID[" + CString(getpid()) + "] UID/GID[" + CString(getuid()) + ":" + CString(getgid()) + "]"); return true; @@ -86,6 +86,23 @@ public: Log("[" + sUsername + "] failed to login from " + sRemoteIP, LOG_WARNING); } + void SetLogFilePath(CString sPath) { + if (sPath.empty()) { + sPath = GetSavePath() + "/znc.log"; + } + + CFile LogFile(sPath); + CString sLogDir = LogFile.GetDir(); + struct stat ModDirInfo; + CFile::GetInfo(GetSavePath(), ModDirInfo); + if (!CFile::Exists(sLogDir)) { + CDir::MakeDir(sLogDir, ModDirInfo.st_mode); + } + + m_sLogFile = sPath; + SetNV("path", sPath); + } + void Log(CString sLine, int iPrio = LOG_INFO) { if (m_eLogMode & LOG_TO_SYSLOG) syslog(iPrio, "%s", sLine.c_str()); @@ -117,14 +134,14 @@ public: } void OnTargetCommand(const CString& sCommand) { - CString sArg = sCommand.Token(1, true); + CString sArg = sCommand.Token(1, false); CString sTarget; CString sMessage; LogMode mode; if (sArg.Equals("file")) { sTarget = "file"; - sMessage = "Now only logging to file"; + sMessage = "Now logging to file"; mode = LOG_TO_FILE; } else if (sArg.Equals("syslog")) { sTarget = "syslog"; @@ -132,17 +149,23 @@ public: mode = LOG_TO_SYSLOG; } else if (sArg.Equals("both")) { sTarget = "both"; - sMessage = "Now logging to file and syslog"; + sMessage = "Now logging to syslog and file"; mode = LOG_TO_BOTH; } else { if (sArg.empty()) { - PutModule("Usage: Target "); + PutModule("Usage: Target [path]"); } else { PutModule("Unknown target"); } return; } + if (mode != LOG_TO_SYSLOG) { + CString sPath = sCommand.Token(2, true); + SetLogFilePath(sPath); + sMessage += " [" + sPath + "]"; + } + Log(sMessage); SetNV("target", sTarget); m_eLogMode = mode;