-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
log.cpp
230 lines (196 loc) · 5.75 KB
/
log.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
2004 - 2014 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Standard logging facilities (implementation).
* See also the command line switches --logdomains and --log-@<level@>="domain".
*/
#include "game_errors.hpp"
#include "global.hpp"
#include "SDL.h"
#include "log.hpp"
#include <boost/foreach.hpp>
#include <boost/date_time.hpp>
#include <map>
#include <sstream>
#include <ctime>
namespace {
class null_streambuf : public std::streambuf
{
virtual int overflow(int c) { return std::char_traits< char >::not_eof(c); }
public:
null_streambuf() {}
};
} // end anonymous namespace
static std::ostream null_ostream(new null_streambuf);
static int indent = 0;
static bool timestamp = true;
static bool precise_timestamp = false;
static boost::posix_time::time_facet facet("%Y%m%d %H:%M:%S%F ");
static std::ostream *output_stream = NULL;
static std::ostream& output()
{
if(output_stream) {
return *output_stream;
}
return std::cerr;
}
namespace lg {
tredirect_output_setter::tredirect_output_setter(std::ostream& stream)
: old_stream_(output_stream)
{
output_stream = &stream;
}
tredirect_output_setter::~tredirect_output_setter()
{
output_stream = old_stream_;
}
typedef std::map<std::string, int> domain_map;
static domain_map *domains;
static int strict_level_ = -1;
void timestamps(bool t) { timestamp = t; }
void precise_timestamps(bool pt) { precise_timestamp = pt; }
logger err("error", 0), warn("warning", 1), info("info", 2), debug("debug", 3);
log_domain general("general");
log_domain::log_domain(char const *name)
: domain_(NULL)
{
// Indirection to prevent initialization depending on link order.
if (!domains) domains = new domain_map;
domain_ = &*domains->insert(logd(name, 1)).first;
}
bool set_log_domain_severity(std::string const &name, int severity)
{
std::string::size_type s = name.size();
if (name == "all") {
BOOST_FOREACH(logd &l, *domains) {
l.second = severity;
}
} else if (s > 2 && name.compare(s - 2, 2, "/*") == 0) {
BOOST_FOREACH(logd &l, *domains) {
if (l.first.compare(0, s - 1, name, 0, s - 1) == 0)
l.second = severity;
}
} else {
domain_map::iterator it = domains->find(name);
if (it == domains->end())
return false;
it->second = severity;
}
return true;
}
bool set_log_domain_severity(std::string const &name, const logger &lg) {
return set_log_domain_severity(name, lg.get_severity());
}
std::string list_logdomains(const std::string& filter)
{
std::ostringstream res;
BOOST_FOREACH(logd &l, *domains) {
if(l.first.find(filter) != std::string::npos)
res << l.first << "\n";
}
return res.str();
}
void set_strict_severity(int severity) {
strict_level_ = severity;
}
void set_strict_severity(const logger &lg) {
set_strict_severity(lg.get_severity());
}
std::string get_timestamp(const time_t& t, const std::string& format) {
char buf[100] = {0};
tm* lt = localtime(&t);
if (lt) {
strftime(buf, 100, format.c_str(), lt);
}
return buf;
}
std::string get_timespan(const time_t& t) {
char buf[100];
// There doesn't seem to be any library function for this
const time_t minutes = t / 60;
const time_t days = minutes / 60 / 24;
if(t <= 0) {
strncpy(buf, "expired", 100);
} else if(minutes == 0) {
snprintf(buf, 100, "00:00:%02ld", t);
} else if(days == 0) {
snprintf(buf, 100, "%02ld:%02ld", minutes / 60, minutes % 60);
} else {
snprintf(buf, 100, "%ld %02ld:%02ld", days, (minutes / 60) % 24, minutes % 60);
}
return buf;
}
static void print_precise_timestamp(std::ostream & out)
{
facet.put(
std::ostreambuf_iterator<char>(out),
out,
' ',
boost::posix_time::microsec_clock::local_time());
}
std::ostream &logger::operator()(log_domain const &domain, bool show_names, bool do_indent) const
{
static bool strict_threw_ = false;
if (severity_ > domain.domain_->second) {
return null_ostream;
} else if (!strict_threw_ && (severity_ <= strict_level_)) {
std::stringstream ss;
ss << "Error (strict mode, strict_level = " << strict_level_ << "): wesnoth reported on channel " << name_ << " " << domain.domain_->first;
std::cerr << ss.str() << std::endl;
//TODO: Would be nice to actually get whatever message they were going to log...
//this would likely require refactor to pass back a wrapper instead of ostream & though.
strict_threw_ = true;
throw game::game_error(ss.str());
} else {
std::ostream& stream = output();
if(do_indent) {
for(int i = 0; i != indent; ++i)
stream << " ";
}
if (timestamp) {
if(precise_timestamp) {
print_precise_timestamp(stream);
} else {
stream << get_timestamp(time(NULL));
}
}
if (show_names) {
stream << name_ << ' ' << domain.domain_->first << ": ";
}
return stream;
}
}
void scope_logger::do_log_entry(log_domain const &domain, const std::string& str)
{
output_ = &debug(domain, false, true);
str_ = str;
ticks_ = SDL_GetTicks();
(*output_) << "{ BEGIN: " << str_ << "\n";
++indent;
}
void scope_logger::do_log_exit()
{
const int ticks = SDL_GetTicks() - ticks_;
--indent;
do_indent();
if (timestamp) (*output_) << get_timestamp(time(NULL));
(*output_) << "} END: " << str_ << " (took " << ticks << "ms)\n";
}
void scope_logger::do_indent() const
{
for(int i = 0; i != indent; ++i)
(*output_) << " ";
}
std::stringstream wml_error;
} // end namespace lg