97f25cc77e95a1a0cf7962b99e0af3b67557ad2a
[oweals/minetest.git] / src / log.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "log.h"
21
22 #include <map>
23 #include <list>
24 #include <sstream>
25 #include <algorithm>
26 #include "threads.h"
27 #include "debug.h"
28 #include "gettime.h"
29
30 std::list<ILogOutput*> log_outputs[LMT_NUM_VALUES];
31 std::map<threadid_t, std::string> log_threadnames;
32 JMutex                            log_threadnamemutex;
33
34 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
35 {
36         log_outputs[lev].push_back(out);
37 }
38
39 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
40 {
41         for(int i=0; i<=lev; i++)
42                 log_outputs[i].push_back(out);
43 }
44
45 void log_add_output_all_levs(ILogOutput *out)
46 {
47         for(int i=0; i<LMT_NUM_VALUES; i++)
48                 log_outputs[i].push_back(out);
49 }
50
51 void log_remove_output(ILogOutput *out)
52 {
53         for(int i=0; i<LMT_NUM_VALUES; i++){
54                 std::list<ILogOutput*>::iterator it =
55                                 std::find(log_outputs[i].begin(), log_outputs[i].end(), out);
56                 if(it != log_outputs[i].end())
57                         log_outputs[i].erase(it);
58         }
59 }
60
61 void log_register_thread(const std::string &name)
62 {
63         threadid_t id = get_current_thread_id();
64         log_threadnamemutex.Lock();
65         log_threadnames[id] = name;
66         log_threadnamemutex.Unlock();
67 }
68
69 void log_deregister_thread()
70 {
71         threadid_t id = get_current_thread_id();
72         log_threadnamemutex.Lock();
73         log_threadnames.erase(id);
74         log_threadnamemutex.Unlock();
75 }
76
77 static std::string get_lev_string(enum LogMessageLevel lev)
78 {
79         switch(lev){
80         case LMT_ERROR:
81                 return "ERROR";
82         case LMT_ACTION:
83                 return "ACTION";
84         case LMT_INFO:
85                 return "INFO";
86         case LMT_VERBOSE:
87                 return "VERBOSE";
88         case LMT_NUM_VALUES:
89                 break;
90         }
91         return "(unknown level)";
92 }
93
94 void log_printline(enum LogMessageLevel lev, const std::string &text)
95 {
96         log_threadnamemutex.Lock();
97         std::string threadname = "(unknown thread)";
98         std::map<threadid_t, std::string>::const_iterator i;
99         i = log_threadnames.find(get_current_thread_id());
100         if(i != log_threadnames.end())
101                 threadname = i->second;
102         std::string levelname = get_lev_string(lev);
103         std::ostringstream os(std::ios_base::binary);
104         os<<getTimestamp()<<": "<<levelname<<"["<<threadname<<"]: "<<text;
105         for(std::list<ILogOutput*>::iterator i = log_outputs[lev].begin();
106                         i != log_outputs[lev].end(); i++){
107                 ILogOutput *out = *i;
108                 out->printLog(os.str());
109                 out->printLog(os.str(), lev);
110                 out->printLog(lev, text);
111         }
112         log_threadnamemutex.Unlock();
113 }
114
115 class Logbuf : public std::streambuf
116 {
117 public:
118         Logbuf(enum LogMessageLevel lev):
119                 m_lev(lev)
120         {
121         }
122
123         ~Logbuf()
124         {
125         }
126
127         int overflow(int c)
128         {
129                 bufchar(c);
130                 return c;
131         }
132         std::streamsize xsputn(const char *s, std::streamsize n)
133         {
134                 for(int i=0; i<n; i++)
135                         bufchar(s[i]);
136                 return n;
137         }
138
139         void printbuf()
140         {
141                 log_printline(m_lev, m_buf);
142         }
143
144         void bufchar(char c)
145         {
146                 if(c == '\n' || c == '\r'){
147                         if(m_buf != "")
148                                 printbuf();
149                         m_buf = "";
150                         return;
151                 }
152                 m_buf += c;
153         }
154
155 private:
156         enum LogMessageLevel m_lev;
157         std::string m_buf;
158 };
159
160 Logbuf errorbuf(LMT_ERROR);
161 Logbuf actionbuf(LMT_ACTION);
162 Logbuf infobuf(LMT_INFO);
163 Logbuf verbosebuf(LMT_VERBOSE);
164 std::ostream errorstream(&errorbuf);
165 std::ostream actionstream(&actionbuf);
166 std::ostream infostream(&infobuf);
167 std::ostream verbosestream(&verbosebuf);
168
169 bool log_trace_level_enabled = false;
170