]> git.lizzy.rs Git - minetest.git/blob - src/log.cpp
Handle the newly added TOCLIENT_ACCESS_DENIED and TOCLIENT_DELETE_PARTICLESPAWNER
[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 "jthread/jmutexautolock.h"
28 #include "debug.h"
29 #include "gettime.h"
30 #include "porting.h"
31 #include "config.h"
32
33 #ifdef __ANDROID__
34 unsigned int android_log_level_mapping[] = {
35                 /* LMT_ERROR */   ANDROID_LOG_ERROR,
36                 /* LMT_ACTION */  ANDROID_LOG_WARN,
37                 /* LMT_INFO */    ANDROID_LOG_INFO,
38                 /* LMT_VERBOSE */ ANDROID_LOG_VERBOSE
39         };
40 #endif
41
42 std::vector<ILogOutput*> log_outputs[LMT_NUM_VALUES];
43 std::map<threadid_t, std::string> log_threadnames;
44 JMutex                            log_threadnamemutex;
45
46 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
47 {
48         log_outputs[lev].push_back(out);
49 }
50
51 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
52 {
53         for(int i=0; i<=lev; i++)
54                 log_outputs[i].push_back(out);
55 }
56
57 void log_add_output_all_levs(ILogOutput *out)
58 {
59         for(int i=0; i<LMT_NUM_VALUES; i++)
60                 log_outputs[i].push_back(out);
61 }
62
63 void log_remove_output(ILogOutput *out)
64 {
65         for(int i=0; i<LMT_NUM_VALUES; i++){
66                 std::vector<ILogOutput*>::iterator it =
67                                 std::find(log_outputs[i].begin(), log_outputs[i].end(), out);
68                 if(it != log_outputs[i].end())
69                         log_outputs[i].erase(it);
70         }
71 }
72
73 void log_set_lev_silence(enum LogMessageLevel lev, bool silence)
74 {
75         JMutexAutoLock lock(log_threadnamemutex);
76
77         for (std::vector<ILogOutput *>::iterator it = log_outputs[lev].begin();
78                         it != log_outputs[lev].end(); ++it) {
79                 ILogOutput *out = *it;
80                 out->silence = silence;
81         }
82 }
83
84 void log_register_thread(const std::string &name)
85 {
86         threadid_t id = get_current_thread_id();
87         JMutexAutoLock lock(log_threadnamemutex);
88
89         log_threadnames[id] = name;
90 }
91
92 void log_deregister_thread()
93 {
94         threadid_t id = get_current_thread_id();
95         JMutexAutoLock lock(log_threadnamemutex);
96
97         log_threadnames.erase(id);
98 }
99
100 static std::string get_lev_string(enum LogMessageLevel lev)
101 {
102         switch(lev){
103         case LMT_ERROR:
104                 return "ERROR";
105         case LMT_ACTION:
106                 return "ACTION";
107         case LMT_INFO:
108                 return "INFO";
109         case LMT_VERBOSE:
110                 return "VERBOSE";
111         case LMT_NUM_VALUES:
112                 break;
113         }
114         return "(unknown level)";
115 }
116
117 void log_printline(enum LogMessageLevel lev, const std::string &text)
118 {
119         JMutexAutoLock lock(log_threadnamemutex);
120         std::string threadname = "(unknown thread)";
121         std::map<threadid_t, std::string>::const_iterator i;
122         i = log_threadnames.find(get_current_thread_id());
123         if(i != log_threadnames.end())
124                 threadname = i->second;
125         std::string levelname = get_lev_string(lev);
126         std::ostringstream os(std::ios_base::binary);
127         os << getTimestamp() << ": " << levelname << "["<<threadname<<"]: " << text;
128
129         for(std::vector<ILogOutput*>::iterator i = log_outputs[lev].begin();
130                         i != log_outputs[lev].end(); i++) {
131                 ILogOutput *out = *i;
132                 if (out->silence)
133                         continue;
134
135                 out->printLog(os.str());
136                 out->printLog(os.str(), lev);
137                 out->printLog(lev, text);
138         }
139 }
140
141 class Logbuf : public std::streambuf
142 {
143 public:
144         Logbuf(enum LogMessageLevel lev):
145                 m_lev(lev)
146         {
147         }
148
149         ~Logbuf()
150         {
151         }
152
153         int overflow(int c)
154         {
155                 bufchar(c);
156                 return c;
157         }
158         std::streamsize xsputn(const char *s, std::streamsize n)
159         {
160                 for(int i=0; i<n; i++)
161                         bufchar(s[i]);
162                 return n;
163         }
164
165         void printbuf()
166         {
167                 log_printline(m_lev, m_buf);
168 #ifdef __ANDROID__
169                 __android_log_print(android_log_level_mapping[m_lev], PROJECT_NAME, "%s", m_buf.c_str());
170 #endif
171         }
172
173         void bufchar(char c)
174         {
175                 if(c == '\n' || c == '\r'){
176                         if(m_buf != "")
177                                 printbuf();
178                         m_buf = "";
179                         return;
180                 }
181                 m_buf += c;
182         }
183
184 private:
185         enum LogMessageLevel m_lev;
186         std::string m_buf;
187 };
188
189 Logbuf errorbuf(LMT_ERROR);
190 Logbuf actionbuf(LMT_ACTION);
191 Logbuf infobuf(LMT_INFO);
192 Logbuf verbosebuf(LMT_VERBOSE);
193 std::ostream errorstream(&errorbuf);
194 std::ostream actionstream(&actionbuf);
195 std::ostream infostream(&infobuf);
196 std::ostream verbosestream(&verbosebuf);
197
198 bool log_trace_level_enabled = false;
199