3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
27 #include "threading/mutex_auto_lock.h"
34 std::ostream *dout_con_ptr = &dummyout;
35 std::ostream *derr_con_ptr = &verbosestream;
38 std::ostream *dout_server_ptr = &infostream;
39 std::ostream *derr_server_ptr = &errorstream;
43 std::ostream *dout_client_ptr = &infostream;
44 std::ostream *derr_client_ptr = &errorstream;
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
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;
60 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
62 log_outputs[lev].push_back(out);
65 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
67 for(int i=0; i<=lev; i++)
68 log_outputs[i].push_back(out);
71 void log_add_output_all_levs(ILogOutput *out)
73 for(int i=0; i<LMT_NUM_VALUES; i++)
74 log_outputs[i].push_back(out);
77 void log_remove_output(ILogOutput *out)
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);
87 void log_set_lev_silence(enum LogMessageLevel lev, bool silence)
89 MutexAutoLock lock(log_thread_name_mutex);
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;
98 void log_register_thread(const std::string &name)
100 threadid_t id = get_current_thread_id();
101 MutexAutoLock lock(log_thread_name_mutex);
103 log_thread_names[id] = name;
106 void log_deregister_thread()
108 threadid_t id = get_current_thread_id();
109 MutexAutoLock lock(log_thread_name_mutex);
111 log_thread_names.erase(id);
114 static std::string get_lev_string(enum LogMessageLevel lev)
128 return "(unknown level)";
131 void log_printline(enum LogMessageLevel lev, const std::string &text)
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;
143 for(std::vector<ILogOutput*>::iterator i = log_outputs[lev].begin();
144 i != log_outputs[lev].end(); ++i) {
145 ILogOutput *out = *i;
149 out->printLog(os.str());
150 out->printLog(os.str(), lev);
151 out->printLog(lev, text);
155 class Logbuf : public std::streambuf
158 Logbuf(enum LogMessageLevel lev):
172 std::streamsize xsputn(const char *s, std::streamsize n)
174 for(int i=0; i<n; i++)
181 log_printline(m_lev, m_buf);
183 __android_log_print(android_log_level_mapping[m_lev], PROJECT_NAME, "%s", m_buf.c_str());
189 if(c == '\n' || c == '\r'){
199 enum LogMessageLevel m_lev;
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);
212 bool log_trace_level_enabled = false;