core/loggingmanager.lua

changeset 1117
360ec48ea780
parent 1101
fb096ca4b296
child 1343
a0bee511d144
equal deleted inserted replaced
1116:507702cf44f8 1117:360ec48ea780
8 local math_max, rep = math.max, string.rep; 8 local math_max, rep = math.max, string.rep;
9 local os_date, os_getenv = os.date, os.getenv; 9 local os_date, os_getenv = os.date, os.getenv;
10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; 10 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
11 11
12 local config = require "core.configmanager"; 12 local config = require "core.configmanager";
13 13 local eventmanager = require "core.eventmanager";
14 local logger = require "util.logger"; 14 local logger = require "util.logger";
15 15
16 _G.log = logger.init("general"); 16 _G.log = logger.init("general");
17 17
18 module "loggingmanager" 18 module "loggingmanager"
188 end 188 end
189 end 189 end
190 end 190 end
191 end 191 end
192 192
193 local empty_function = function () end;
193 function log_sink_types.file(config) 194 function log_sink_types.file(config)
194 local log = config.filename; 195 local log = config.filename;
195 local logfile = io_open(log, "a+"); 196 local logfile = io_open(log, "a+");
196 if not logfile then 197 if not logfile then
197 return function () end 198 return empty_function;
198 end 199 end
200 local write, flush = logfile.write, logfile.flush;
201
202 eventmanager.add_event_hook("reopen-log-files", function ()
203 if logfile then
204 logfile:close();
205 end
206 logfile = io_open(log, "a+");
207 if not logfile then
208 write, flush = empty_function, empty_function;
209 else
210 write, flush = logfile.write, logfile.flush;
211 end
212 end);
199 213
200 local timestamps = config.timestamps; 214 local timestamps = config.timestamps;
201 215
202 if timestamps == true then 216 if timestamps == true then
203 timestamps = default_timestamp; -- Default format 217 timestamps = default_timestamp; -- Default format
204 end 218 end
205 219
206 local write, format, flush = logfile.write, format, logfile.flush;
207 return function (name, level, message, ...) 220 return function (name, level, message, ...)
208 if timestamps then 221 if timestamps then
209 write(logfile, os_date(timestamps), " "); 222 write(logfile, os_date(timestamps), " ");
210 end 223 end
211 if ... then 224 if ... then

mercurial