Translation to Portuguese of Brazil for Minetest
[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
33 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
34 {
35         log_outputs[lev].push_back(out);
36 }
37
38 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
39 {
40         for(int i=0; i<=lev; i++)
41                 log_outputs[i].push_back(out);
42 }
43
44 void log_add_output_all_levs(ILogOutput *out)
45 {
46         for(int i=0; i<LMT_NUM_VALUES; i++)
47                 log_outputs[i].push_back(out);
48 }
49
50 void log_remove_output(ILogOutput *out)
51 {
52         for(int i=0; i<LMT_NUM_VALUES; i++){
53                 std::list<ILogOutput*>::iterator it =
54                                 std::find(log_outputs[i].begin(), log_outputs[i].end(), out);
55                 if(it != log_outputs[i].end())
56                         log_outputs[i].erase(it);
57         }
58 }
59
60 void log_register_thread(const std::string &name)
61 {
62         threadid_t id = get_current_thread_id();
63         log_threadnames[id] = name;
64 }
65
66 void log_deregister_thread()
67 {
68         threadid_t id = get_current_thread_id();
69         log_threadnames.erase(id);
70 }
71
72 static std::string get_lev_string(enum LogMessageLevel lev)
73 {
74         switch(lev){
75         case LMT_ERROR:
76                 return "ERROR";
77         case LMT_ACTION:
78                 return "ACTION";
79         case LMT_INFO:
80                 return "INFO";
81         case LMT_VERBOSE:
82                 return "VERBOSE";
83         case LMT_NUM_VALUES:
84                 break;
85         }
86         return "(unknown level)";
87 }
88
89 void log_printline(enum LogMessageLevel lev, const std::string &text)
90 {
91         std::string threadname = "(unknown thread)";
92         std::map<threadid_t, std::string>::const_iterator i;
93         i = log_threadnames.find(get_current_thread_id());
94         if(i != log_threadnames.end())
95                 threadname = i->second;
96         std::string levelname = get_lev_string(lev);
97         std::ostringstream os(std::ios_base::binary);
98         os<<getTimestamp()<<": "<<levelname<<"["<<threadname<<"]: "<<text;
99         for(std::list<ILogOutput*>::iterator i = log_outputs[lev].begin();
100                         i != log_outputs[lev].end(); i++){
101                 ILogOutput *out = *i;
102                 out->printLog(os.str());
103                 out->printLog(os.str(), lev);
104                 out->printLog(lev, text);
105         }
106 }
107
108 class Logbuf : public std::streambuf
109 {
110 public:
111         Logbuf(enum LogMessageLevel lev):
112                 m_lev(lev)
113         {
114         }
115
116         ~Logbuf()
117         {
118         }
119
120         int overflow(int c)
121         {
122                 bufchar(c);
123                 return c;
124         }
125         std::streamsize xsputn(const char *s, std::streamsize n)
126         {
127                 for(int i=0; i<n; i++)
128                         bufchar(s[i]);
129                 return n;
130         }
131
132         void printbuf()
133         {
134                 log_printline(m_lev, m_buf);
135         }
136
137         void bufchar(char c)
138         {
139                 if(c == '\n' || c == '\r'){
140                         if(m_buf != "")
141                                 printbuf();
142                         m_buf = "";
143                         return;
144                 }
145                 m_buf += c;
146         }
147         
148 private:
149         enum LogMessageLevel m_lev;
150         std::string m_buf;
151 };
152
153 Logbuf errorbuf(LMT_ERROR);
154 Logbuf actionbuf(LMT_ACTION);
155 Logbuf infobuf(LMT_INFO);
156 Logbuf verbosebuf(LMT_VERBOSE);
157 std::ostream errorstream(&errorbuf);
158 std::ostream actionstream(&actionbuf);
159 std::ostream infostream(&infobuf);
160 std::ostream verbosestream(&verbosebuf);
161
162 bool log_trace_level_enabled = false;
163