]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/filelogger.cpp
Code for loading <log method=file> tags from config, but this still needs to be fit...
[user/henk/code/inspircd.git] / src / filelogger.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 /* $Core: libIRCDfilelogger */
15
16 #include "inspircd.h"
17 #include <fstream>
18 #include "socketengine.h"
19 #include "inspircd_se_config.h"
20 #include "filelogger.h"
21
22 FileLogStream::FileLogStream(InspIRCd *Instance, int loglevel, FileWriter *fw)
23         : LogStream(Instance, loglevel), f(fw)
24 {
25         ServerInstance->Logs->AddLoggerRef(f);
26 }
27
28 FileLogStream::~FileLogStream()
29 {
30         /* FileWriter is managed externally now */
31         ServerInstance->Logs->DelLoggerRef(f);
32 }
33
34 void FileLogStream::OnLog(int loglevel, const std::string &type, const std::string &text)
35 {
36         static char TIMESTR[26];
37         static time_t LAST = 0;
38
39         /* sanity check, just in case */
40         if (!ServerInstance->Config)
41                 return;
42
43         /* If we were given -debug we output all messages, regardless of configured loglevel */
44         if ((loglevel < this->loglvl) && !ServerInstance->Config->forcedebug)
45                 return;
46
47         if (ServerInstance->Time() != LAST)
48         {
49                 time_t local = ServerInstance->Time();
50                 struct tm *timeinfo = localtime(&local);
51
52                 strlcpy(TIMESTR,asctime(timeinfo),26);
53                 TIMESTR[24] = ':';
54                 LAST = ServerInstance->Time();
55         }
56
57         if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
58         {
59                 std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
60                 this->f->WriteLogLine(out);
61         }
62
63         if (ServerInstance->Config->nofork)
64         {
65                 printf("%s %s\n", TIMESTR, text.c_str());
66         }
67 }