summaryrefslogtreecommitdiff
path: root/src/inspircd.cpp
diff options
context:
space:
mode:
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-18 22:01:26 +0000
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>2006-08-18 22:01:26 +0000
commit5adcab2223c1f64550f24c2b1d49d1299ceb69d5 (patch)
tree8614a97923ebf4f1ff2fbc3df4109928a3fe1e81 /src/inspircd.cpp
parent365fd7e1e903e99d7e3399487d1cf9204ebe64fa (diff)
NONBLOCKING LOGGER!
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4971 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'src/inspircd.cpp')
-rw-r--r--src/inspircd.cpp69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/inspircd.cpp b/src/inspircd.cpp
index 18d289381..5b2d5aac2 100644
--- a/src/inspircd.cpp
+++ b/src/inspircd.cpp
@@ -50,6 +50,7 @@
#include "command_parse.h"
using irc::sockets::NonBlocking;
+using irc::sockets::Blocking;
using irc::sockets::insp_ntoa;
using irc::sockets::insp_inaddr;
using irc::sockets::insp_sockaddr;
@@ -792,3 +793,71 @@ time_t InspIRCd::Time()
return TIME;
}
+bool FileLogger::Readable()
+{
+ return false;
+}
+
+void FileLogger::HandleEvent(EventType et)
+{
+ this->WriteLogLine("");
+ ServerInstance->SE->DelFd(this);
+}
+
+void FileLogger::WriteLogLine(const std::string &line)
+{
+ if (line.length())
+ buffer.append(line);
+
+ if (log)
+ {
+ int written = fprintf(log,"%s",buffer.c_str());
+ if ((written >= 0) && (written < (int)buffer.length()))
+ {
+ buffer.erase(0, buffer.length());
+ ServerInstance->SE->AddFd(this);
+ }
+ else if (written == -1)
+ {
+ if (errno == EAGAIN)
+ ServerInstance->SE->AddFd(this);
+ }
+ else
+ {
+ /* Wrote the whole buffer, and no need for write callback */
+ buffer = "";
+ }
+ }
+ if (writeops++ % 20)
+ {
+ fflush(log);
+ }
+}
+
+void FileLogger::Close()
+{
+ if (log)
+ {
+ int flags = fcntl(fileno(log), F_GETFL, 0);
+ fcntl(fileno(log), F_SETFL, flags ^ O_NONBLOCK);
+ if (buffer.size())
+ fprintf(log,"%s",buffer.c_str());
+ fflush(log);
+ fclose(log);
+ }
+ buffer = "";
+ ServerInstance->SE->DelFd(this);
+}
+
+FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile) : ServerInstance(Instance), log(logfile), writeops(0)
+{
+ irc::sockets::NonBlocking(fileno(log));
+ this->SetFd(fileno(log));
+ buffer = "";
+}
+
+FileLogger::~FileLogger()
+{
+ this->Close();
+}
+