+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()
+{
+ /* Re-register the nofork stream if necessary. */
+ if (ServerInstance->Config->nofork)
+ {
+ SetupNoFork();
+ }
+ /* Skip rest of logfile opening if we are running -nolog. */
+ if (!ServerInstance->Config->writelog)
+ {
+ return;
+ }
+ ConfigReader* Conf = new ConfigReader(ServerInstance);
+ std::map<std::string, FileWriter*> logmap;
+ std::map<std::string, FileWriter*>::iterator i;
+ for (int index = 0; index < Conf->Enumerate("log"); ++index)
+ {
+ std::string method = Conf->ReadValue("log", "method", index);
+ if (method != "file")
+ {
+ continue;
+ }
+ std::string type = Conf->ReadValue("log", "type", index);
+ std::string level = Conf->ReadValue("log", "level", index);
+ int loglevel = DEFAULT;
+ if (level == "debug" || ServerInstance->Config->forcedebug)
+ {
+ loglevel = DEBUG;
+ ServerInstance->Config->debugging = true;
+ }
+ else if (level == "verbose")
+ {
+ loglevel = VERBOSE;
+ }
+ else if (level == "default")
+ {
+ loglevel = DEFAULT;
+ }
+ else if (level == "sparse")
+ {
+ loglevel = SPARSE;
+ }
+ else if (level == "none")
+ {
+ loglevel = NONE;
+ }
+ FileWriter* fw;
+ std::string target = Conf->ReadValue("log", "target", index);
+ if ((i = logmap.find(target)) == logmap.end())
+ {
+ FILE* f = fopen(target.c_str(), "a");
+ fw = new FileWriter(ServerInstance, f);
+ logmap.insert(std::make_pair(target, fw));
+ }
+ else
+ {
+ fw = i->second;
+ }
+ FileLogStream* fls = new FileLogStream(ServerInstance, loglevel, fw);
+ AddLogTypes(type, fls, true);
+ }
+}
+
+void LogManager::CloseLogs()
+{
+ std::map<std::string, std::vector<LogStream*> >().swap(LogStreams); /* Clear it */
+ std::map<LogStream*, std::vector<std::string> >().swap(GlobalLogStreams); /* Clear it */
+ for (std::map<LogStream*, int>::iterator i = AllLogStreams.begin(); i != AllLogStreams.end(); ++i)
+ {
+ delete i->first;
+ }
+ std::map<LogStream*, int>().swap(AllLogStreams); /* And clear it */
+}
+
+void LogManager::AddLogTypes(const std::string &types, LogStream* l, bool autoclose)
+{
+ irc::spacesepstream css(types);
+ std::string tok;
+ std::vector<std::string> excludes;
+ while (css.GetToken(tok))
+ {
+ if (tok.empty())
+ {
+ continue;
+ }
+ if (tok.at(0) == '-')
+ {
+ /* Exclude! */
+ excludes.push_back(tok.substr(1));
+ }
+ else
+ {
+ AddLogType(tok, l, autoclose);
+ }
+ }
+ // Handle doing things like: USERINPUT USEROUTPUT -USERINPUT should be the same as saying just USEROUTPUT.
+ // (This is so modules could, for example, inject exclusions for logtypes they can't handle.)
+ for (std::vector<std::string>::iterator i = excludes.begin(); i != excludes.end(); ++i)
+ {
+ if (*i == "*")
+ {
+ /* -* == Exclude all. Why someone would do this, I dunno. */
+ DelLogStream(l);
+ return;
+ }
+ DelLogType(*i, l);
+ }
+ // Now if it's registered as a global, add the exclusions there too.
+ std::map<LogStream *, std::vector<std::string> >::iterator gi = GlobalLogStreams.find(l);
+ if (gi != GlobalLogStreams.end())
+ {
+ gi->second.swap(excludes); // Swap with the vector in the hash.
+ }
+}
+
+bool LogManager::AddLogType(const std::string &type, LogStream *l, bool autoclose)