1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 /* $Core: libIRCDfilelogger */
18 #include "socketengine.h"
19 #include "inspircd_se_config.h"
20 #include "filelogger.h"
22 FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile)
23 : ServerInstance(Instance), log(logfile), writeops(0)
27 Instance->SE->NonBlocking(fileno(log));
33 bool FileLogger::Readable()
38 void FileLogger::HandleEvent(EventType, int)
42 ServerInstance->SE->DelFd(this);
45 void FileLogger::WriteLogLine(const std::string &line)
52 int written = fprintf(log,"%s",buffer.c_str());
56 if ((written >= 0) && (written < (int)buffer.length()))
58 buffer.erase(0, buffer.length());
59 ServerInstance->SE->AddFd(this);
61 else if (written == -1)
64 ServerInstance->SE->AddFd(this);
68 /* Wrote the whole buffer, and no need for write callback */
79 void FileLogger::Close()
83 ServerInstance->SE->Blocking(fileno(log));
86 fprintf(log,"%s",buffer.c_str());
89 ServerInstance->SE->DelFd(this);
99 FileLogger::~FileLogger()
105 void FileLogStream::OnLog(int loglevel, const std::string &type, const std::string &text)
107 static char TIMESTR[26];
108 static time_t LAST = 0;
110 /* sanity check, just in case */
111 if (!ServerInstance->Config)
114 /* If we were given -debug we output all messages, regardless of configured loglevel */
115 if ((loglevel < ServerInstance->Config->LogLevel) && !ServerInstance->Config->forcedebug)
118 if (ServerInstance->Time() != LAST)
120 time_t local = ServerInstance->Time();
121 struct tm *timeinfo = localtime(&local);
123 strlcpy(TIMESTR,asctime(timeinfo),26);
125 LAST = ServerInstance->Time();
128 if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
130 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
131 this->f->WriteLogLine(out);
134 if (ServerInstance->Config->nofork)
136 printf("%s %s\n", TIMESTR, text.c_str());