3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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.
29 std::list<ILogOutput*> log_outputs[LMT_NUM_VALUES];
30 std::map<threadid_t, std::string> log_threadnames;
32 void log_add_output(ILogOutput *out, enum LogMessageLevel lev)
34 log_outputs[lev].push_back(out);
37 void log_add_output_maxlev(ILogOutput *out, enum LogMessageLevel lev)
39 for(int i=0; i<=lev; i++)
40 log_outputs[i].push_back(out);
43 void log_add_output_all_levs(ILogOutput *out)
45 for(int i=0; i<LMT_NUM_VALUES; i++)
46 log_outputs[i].push_back(out);
49 void log_register_thread(const std::string &name)
51 threadid_t id = get_current_thread_id();
52 log_threadnames[id] = name;
55 static std::string get_lev_string(enum LogMessageLevel lev)
69 return "(unknown level)";
72 void log_printline(enum LogMessageLevel lev, const std::string &text)
74 std::string threadname = "(unknown thread)";
75 std::map<threadid_t, std::string>::const_iterator i;
76 i = log_threadnames.find(get_current_thread_id());
77 if(i != log_threadnames.end())
78 threadname = i->second;
79 std::string levelname = get_lev_string(lev);
80 std::ostringstream os(std::ios_base::binary);
81 os<<getTimestamp()<<": "<<levelname<<"["<<threadname<<"]: "<<text;
82 for(std::list<ILogOutput*>::iterator i = log_outputs[lev].begin();
83 i != log_outputs[lev].end(); i++){
85 out->printLog(os.str());
86 out->printLog(lev, text);
90 class Logbuf : public std::streambuf
93 Logbuf(enum LogMessageLevel lev):
107 std::streamsize xsputn(const char *s, std::streamsize n)
109 for(int i=0; i<n; i++)
116 log_printline(m_lev, m_buf);
121 if(c == '\n' || c == '\r'){
131 enum LogMessageLevel m_lev;
135 Logbuf errorbuf(LMT_ERROR);
136 Logbuf actionbuf(LMT_ACTION);
137 Logbuf infobuf(LMT_INFO);
138 Logbuf verbosebuf(LMT_VERBOSE);
139 std::ostream errorstream(&errorbuf);
140 std::ostream actionstream(&actionbuf);
141 std::ostream infostream(&infobuf);
142 std::ostream verbosestream(&verbosebuf);