From 11f1f2126c3e1f1cb91f5d6e273eba2850ca61a8 Mon Sep 17 00:00:00 2001 From: aquanight Date: Fri, 15 Feb 2008 13:30:46 +0000 Subject: Make -nofork work properly with logging now. git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8944 e03df62e-2008-0410-955e-edbf42e46eb7 --- src/filelogger.cpp | 14 ++++---------- src/helperfuncs.cpp | 11 +++++++++-- src/inspircd.cpp | 9 +++++---- src/logger.cpp | 47 +++++++++++++++++++++++++++++------------------ src/modules/m_chanlog.cpp | 2 +- src/server.cpp | 3 --- 6 files changed, 48 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/filelogger.cpp b/src/filelogger.cpp index b0eb77310..fd86e4594 100644 --- a/src/filelogger.cpp +++ b/src/filelogger.cpp @@ -42,7 +42,9 @@ void FileLogStream::OnLog(int loglevel, const std::string &type, const std::stri /* If we were given -debug we output all messages, regardless of configured loglevel */ if ((loglevel < this->loglvl) && !ServerInstance->Config->forcedebug) + { return; + } if (ServerInstance->Time() != LAST) { @@ -54,14 +56,6 @@ void FileLogStream::OnLog(int loglevel, const std::string &type, const std::stri LAST = ServerInstance->Time(); } - if (ServerInstance->Config->log_file && ServerInstance->Config->writelog) - { - std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n"; - this->f->WriteLogLine(out); - } - - if (ServerInstance->Config->nofork) - { - printf("%s %s\n", TIMESTR, text.c_str()); - } + std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n"; + this->f->WriteLogLine(out); } diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 8e9f4dfd5..8cb62e72f 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -303,6 +303,12 @@ bool InspIRCd::IsSID(const std::string &str) /* open the proper logfile */ bool InspIRCd::OpenLog(char**, int) { + /* This function only happens at startup now (log reopening is done at OnReadConfig stage now instead of rehash) */ + if (Config->nofork) + { + this->Logs->SetupNoFork(); + } + if (!Config->writelog) return true; // Skip opening default log if -nolog Config->MyDir = Config->GetFullProgDir(); if (!*this->LogFileName) @@ -325,9 +331,10 @@ bool InspIRCd::OpenLog(char**, int) } FileWriter* fw = new FileWriter(this, Config->log_file); - FileLogStream *f = new FileLogStream(this, Config->LogLevel, fw); + FileLogStream *f = new FileLogStream(this, (Config->forcedebug ? DEBUG : Config->LogLevel), fw); + + this->Logs->AddLogType("*", f, true); - this->Logs->AddLogType("*", f); return true; } diff --git a/src/inspircd.cpp b/src/inspircd.cpp index 209b7ddbc..501be3979 100644 --- a/src/inspircd.cpp +++ b/src/inspircd.cpp @@ -401,6 +401,11 @@ InspIRCd::InspIRCd(int argc, char** argv) #endif strlcpy(Config->MyExecutable,argv[0],MAXBUF); + /* Set the finished argument values */ + Config->nofork = do_nofork; + Config->forcedebug = do_debug; + Config->writelog = !do_nolog; + if (!this->OpenLog(argv, argc)) { printf("ERROR: Could not open logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno)); @@ -421,10 +426,6 @@ InspIRCd::InspIRCd(int argc, char** argv) printf_c("\t\033[1;32mpippijn, peavey, aquanight, fez\033[0m\n\n"); printf_c("Others:\t\t\t\033[1;32mSee /INFO Output\033[0m\n"); - /* Set the finished argument values */ - Config->nofork = do_nofork; - Config->forcedebug = do_debug; - Config->writelog = !do_nolog; Config->ClearStack(); this->Modes = new ModeParser(this); diff --git a/src/logger.cpp b/src/logger.cpp index 7b0fa1798..5c24a4451 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -44,8 +44,24 @@ * */ +void LogManager::SetupNoFork() +{ + if (!noforkstream) + { + FileWriter* fw = new FileWriter(ServerInstance, stdout); + noforkstream = new FileLogStream(ServerInstance, ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel, fw); + } + else + { + noforkstream->ChangeLevel(ServerInstance->Config->forcedebug ? DEBUG : ServerInstance->Config->LogLevel); + } + AddLogType("*", noforkstream, false); +} + void LogManager::OpenFileLogs() { + if (ServerInstance->Config->nofork) SetupNoFork(); // Call this to reregister the nofork stream. + if (!ServerInstance->Config->writelog) return; // Skip rest of logfile opening if we are running -nolog. ConfigReader* Conf = new ConfigReader(ServerInstance); std::map logmap; std::map::iterator i; @@ -56,7 +72,7 @@ void LogManager::OpenFileLogs() std::string type = Conf->ReadValue("log", "type", index); std::string level = Conf->ReadValue("log", "level", index); int loglevel = DEFAULT; - if (level == "debug") + if (level == "debug" || ServerInstance->Config->forcedebug) { loglevel = DEBUG; ServerInstance->Config->debugging = true; @@ -94,7 +110,7 @@ void LogManager::OpenFileLogs() std::string tok; while (css.GetToken(tok)) { - AddLogType(tok, fls); + AddLogType(tok, fls, true); } } } @@ -108,17 +124,9 @@ void LogManager::CloseLogs() delete i->first; } std::map().swap(AllLogStreams); /* And clear it */ - - /* Now close FileLoggers, for those logstreams that neglected to properly free their stuff. */ - for (FileLogMap::iterator it = FileLogs.begin(); it != FileLogs.end(); ++it) - { - delete it->first; - } - - FileLogMap().swap(FileLogs); } -bool LogManager::AddLogType(const std::string &type, LogStream *l) +bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose) { std::map >::iterator i = LogStreams.find(type); @@ -134,14 +142,17 @@ bool LogManager::AddLogType(const std::string &type, LogStream *l) if (type == "*") GlobalLogStreams.push_back(l); - std::map::iterator ai = AllLogStreams.find(l); - if (ai == AllLogStreams.end()) - { - AllLogStreams.insert(std::make_pair(l, 1)); - } - else + if (autoclose) { - ++ai->second; + std::map::iterator ai = AllLogStreams.find(l); + if (ai == AllLogStreams.end()) + { + AllLogStreams.insert(std::make_pair(l, 1)); + } + else + { + ++ai->second; + } } return true; diff --git a/src/modules/m_chanlog.cpp b/src/modules/m_chanlog.cpp index 7d066a6ac..afc8c5225 100644 --- a/src/modules/m_chanlog.cpp +++ b/src/modules/m_chanlog.cpp @@ -98,7 +98,7 @@ class ModuleChanLog : public Module std::string tok; while (css.GetToken(tok)) { - ServerInstance->Logs->AddLogType(tok, c); + ServerInstance->Logs->AddLogType(tok, c, true); } cls.push_back(c); } diff --git a/src/server.cpp b/src/server.cpp index 191c84ce0..102fb35d3 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -47,9 +47,6 @@ void InspIRCd::Exit(int status) void InspIRCd::Rehash() { this->SNO->WriteToSnoMask('A', "Rehashing config file %s due to SIGHUP",ServerConfig::CleanFilename(this->ConfigFileName)); - this->Logs->CloseLogs(); - if (!this->OpenLog(this->Config->argv, this->Config->argc)) - this->SNO->WriteToSnoMask('A', "ERROR: Could not open logfile %s: %s", Config->logpath.c_str(), strerror(errno)); this->RehashUsersAndChans(); FOREACH_MOD_I(this, I_OnGarbageCollect, OnGarbageCollect()); /*this->Config->Read(false,NULL);*/ -- cgit v1.2.3