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.
30 std::list<ILogOutput*> log_outputs[LMT_NUM_VALUES];
31 std::map<threadid_t, std::string> log_threadnames;
32 JMutex log_threadnamemutex;
34 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
36 log_outputs[lev].push_back(out);
39 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
41 for(int i=0; i<=lev; i++)
42 log_outputs[i].push_back(out);
45 void log_add_output_all_levs(ILogOutput *out)
47 for(int i=0; i<LMT_NUM_VALUES; i++)
48 log_outputs[i].push_back(out);
51 void log_remove_output(ILogOutput *out)
53 for(int i=0; i<LMT_NUM_VALUES; i++){
54 std::list<ILogOutput*>::iterator it =
55 std::find(log_outputs[i].begin(), log_outputs[i].end(), out);
56 if(it != log_outputs[i].end())
57 log_outputs[i].erase(it);
61 void log_register_thread(const std::string &name)
63 threadid_t id = get_current_thread_id();
64 log_threadnamemutex.Lock();
65 log_threadnames[id] = name;
66 log_threadnamemutex.Unlock();
69 void log_deregister_thread()
71 threadid_t id = get_current_thread_id();
72 log_threadnamemutex.Lock();
73 log_threadnames.erase(id);
74 log_threadnamemutex.Unlock();
77 static std::string get_lev_string(enum LogMessageLevel lev)
91 return "(unknown level)";
94 void log_printline(enum LogMessageLevel lev, const std::string &text)
96 std::string threadname = "(unknown thread)";
97 std::map<threadid_t, std::string>::const_iterator i;
98 i = log_threadnames.find(get_current_thread_id());
99 if(i != log_threadnames.end())
100 threadname = i->second;
101 std::string levelname = get_lev_string(lev);
102 std::ostringstream os(std::ios_base::binary);
103 os<<getTimestamp()<<": "<<levelname<<"["<<threadname<<"]: "<<text;
104 for(std::list<ILogOutput*>::iterator i = log_outputs[lev].begin();
105 i != log_outputs[lev].end(); i++){
106 ILogOutput *out = *i;
107 out->printLog(os.str());
108 out->printLog(os.str(), lev);
109 out->printLog(lev, text);
113 class Logbuf : public std::streambuf
116 Logbuf(enum LogMessageLevel lev):
130 std::streamsize xsputn(const char *s, std::streamsize n)
132 for(int i=0; i<n; i++)
139 log_printline(m_lev, m_buf);
144 if(c == '\n' || c == '\r'){
154 enum LogMessageLevel m_lev;
158 Logbuf errorbuf(LMT_ERROR);
159 Logbuf actionbuf(LMT_ACTION);
160 Logbuf infobuf(LMT_INFO);
161 Logbuf verbosebuf(LMT_VERBOSE);
162 std::ostream errorstream(&errorbuf);
163 std::ostream actionstream(&actionbuf);
164 std::ostream infostream(&infobuf);
165 std::ostream verbosestream(&verbosebuf);
167 bool log_trace_level_enabled = false;