]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/filelogger.cpp
244b627174ddf8064fdcdff7a2859383bd358544
[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 <sstream>
16 #include <fstream>
17 #include "socketengine.h"
18 #include "inspircd_se_config.h"
19 #include "filelogger.h"
20
21 FileLogger::FileLogger(InspIRCd* Instance, FILE* logfile)
22 : ServerInstance(Instance), log(logfile), writeops(0)
23 {
24         if (log)
25         {
26                 irc::sockets::NonBlocking(fileno(log));
27                 SetFd(fileno(log));
28                 buffer.clear();
29         }
30 }
31
32 bool FileLogger::Readable()
33 {
34         return false;
35 }
36     
37 void FileLogger::HandleEvent(EventType et, int errornum)
38 {
39         WriteLogLine("");
40         if (log)
41                 ServerInstance->SE->DelFd(this);
42 }
43
44 void FileLogger::WriteLogLine(const std::string &line)
45 {
46         if (line.length())
47                 buffer.append(line);
48
49         if (log)
50         {
51                 int written = fprintf(log,"%s",buffer.c_str());
52 #ifdef WINDOWS
53                 buffer.clear();
54 #else
55                 if ((written >= 0) && (written < (int)buffer.length()))
56                 {
57                         buffer.erase(0, buffer.length());
58                         ServerInstance->SE->AddFd(this);
59                 }
60                 else if (written == -1)
61                 {
62                         if (errno == EAGAIN)
63                                 ServerInstance->SE->AddFd(this);
64                 }
65                 else
66                 {
67                         /* Wrote the whole buffer, and no need for write callback */
68                         buffer.clear();
69                 }
70 #endif
71                 if (writeops++ % 20)
72                 {
73                         fflush(log);
74                 }
75         }
76 }
77
78 void FileLogger::Close()
79 {
80         if (log)
81         {
82                 /* Burlex: Windows assumes nonblocking on FILE* pointers anyway, and also "file" fd's aren't the same
83                  * as socket fd's.
84                  */
85 #ifndef WIN32
86                 int flags = fcntl(fileno(log), F_GETFL, 0);
87                 fcntl(fileno(log), F_SETFL, flags ^ O_NONBLOCK);
88 #endif
89                 if (buffer.size())
90                         fprintf(log,"%s",buffer.c_str());
91
92 #ifndef WINDOWS
93                 ServerInstance->SE->DelFd(this);
94 #endif
95
96                 fflush(log);
97                 fclose(log);
98         }
99
100         buffer.clear();
101 }
102
103 FileLogger::~FileLogger()
104 {
105         this->Close();
106 }
107