Diff

core/loggingmanager.lua @ 1078:24c9ee99d900

loggingmanager: Support prepending timestamps in file/console/stdout log sinks
author Matthew Wild <mwild1@gmail.com>
date Thu, 30 Apr 2009 03:05:26 +0100
parent 1067:21f41b06f1d2
child 1080:b02290fd8a75
line wrap: on
line diff
--- a/core/loggingmanager.lua	Thu Apr 30 02:45:33 2009 +0100
+++ b/core/loggingmanager.lua	Thu Apr 30 03:05:26 2009 +0100
@@ -6,7 +6,7 @@
 	tostring, setmetatable, rawset, pairs, ipairs, type;
 local io_open, io_write = io.open, io.write;
 local math_max, rep = math.max, string.rep;
-local os_getenv = os.getenv;
+local os_date, os_getenv = os.date, os.getenv;
 local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
 
 local config = require "core.configmanager";
@@ -20,7 +20,7 @@
 -- The log config used if none specified in the config file
 local default_logging = { { to = "console" } };
 local default_file_logging = { { to = "file", levels = { min = "info" } } };
-
+local default_timestamp = "%b %d %T";
 -- The actual config loggingmanager is using
 local logging_config = config.get("*", "core", "log") or default_logging;
 
@@ -130,9 +130,18 @@
 local sourcewidth = 20;
 
 function log_sink_types.stdout()
+	local timestamps = config.timestamps;
+	
+	if timestamps == true then
+		timestamps = default_timestamp; -- Default format
+	end
+	
 	return function (name, level, message, ...)
 		sourcewidth = math_max(#name+2, sourcewidth);
 		local namelen = #name;
+		if timestamps then
+			io_write(os_date(timestamps), " ");
+		end
 		if ... then 
 			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
 		else
@@ -156,9 +165,18 @@
 			return log_sink_types.stdout(config);
 		end
 		
+		local timestamps = config.timestamps;
+
+		if timestamps == true then
+			timestamps = default_timestamp; -- Default format
+		end
+
 		return function (name, level, message, ...)
 			sourcewidth = math_max(#name+2, sourcewidth);
 			local namelen = #name;
+			if timestamps then
+				io_write(os_date(timestamps), " ");
+			end
 			if ... then 
 				io_write(name, rep(" ", sourcewidth-namelen), getstring(logstyles[level], level), "\t", format(message, ...), "\n");
 			else
@@ -175,8 +193,17 @@
 		return function () end
 	end
 
+	local timestamps = config.timestamps;
+
+	if timestamps == true then
+		timestamps = default_timestamp; -- Default format
+	end
+
 	local write, format, flush = logfile.write, format, logfile.flush;
 	return function (name, level, message, ...)
+		if timestamps then
+			write(logfile, os_date(timestamps), " ");
+		end
 		if ... then 
 			write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
 		else