]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/filelogger.cpp
Remove more unnecessary header traffic
[user/henk/code/inspircd.git] / src / filelogger.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <fstream>
16 #include "socketengine.h"
17 #include "inspircd_se_config.h"
18 #include "filelogger.h"
19
20 FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile)
21 : ServerInstance(Instance), log(logfile), writeops(0)
22 {
23         if (log)
24         {
25                 Instance->SE->NonBlocking(fileno(log));
26                 SetFd(fileno(log));
27                 buffer.clear();
28         }
29 }
30
31 bool FileLogger::Readable()
32 {
33         return false;
34 }
35     
36 void FileLogger::HandleEvent(EventType et, int errornum)
37 {
38         WriteLogLine("");
39         if (log)
40                 ServerInstance->SE->DelFd(this);
41 }
42
43 void FileLogger::WriteLogLine(const std::string &line)
44 {
45         if (line.length())
46                 buffer.append(line);
47
48         if (log)
49         {
50                 int written = fprintf(log,"%s",buffer.c_str());
51 #ifdef WINDOWS
52                 buffer.clear();
53 #else
54                 if ((written >= 0) && (written < (int)buffer.length()))
55                 {
56                         buffer.erase(0, buffer.length());
57                         ServerInstance->SE->AddFd(this);
58                 }
59                 else if (written == -1)
60                 {
61                         if (errno == EAGAIN)
62                                 ServerInstance->SE->AddFd(this);
63                 }
64                 else
65                 {
66                         /* Wrote the whole buffer, and no need for write callback */
67                         buffer.clear();
68                 }
69 #endif
70                 if (writeops++ % 20)
71                 {
72                         fflush(log);
73                 }
74         }
75 }
76
77 void FileLogger::Close()
78 {
79         if (log)
80         {
81                 ServerInstance->SE->Blocking(fileno(log));
82
83                 if (buffer.size())
84                         fprintf(log,"%s",buffer.c_str());
85
86 #ifndef WINDOWS
87                 ServerInstance->SE->DelFd(this);
88 #endif
89
90                 fflush(log);
91                 fclose(log);
92         }
93
94         buffer.clear();
95 }
96
97 FileLogger::~FileLogger()
98 {
99         this->Close();
100 }
101