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