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