core/loggingmanager.lua
author Matthew Wild <mwild1@gmail.com>
Thu, 15 Mar 2012 19:09:24 +0000
changeset 4627 b046cafc81a8
parent 4427 ae71ae5ddcfc
child 5001 78a3d275715a
permissions -rw-r--r--
loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     1
-- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2922
diff changeset
     2
-- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2922
diff changeset
     3
-- Copyright (C) 2008-2010 Waqas Hussain
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     4
-- 
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     5
-- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     6
-- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     7
--
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1344
diff changeset
     8
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local format, rep = string.format, string.rep;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
local pcall = pcall;
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local debug = debug;
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3514
diff changeset
    13
local tostring, setmetatable, rawset, pairs, ipairs, type =
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    14
	tostring, setmetatable, rawset, pairs, ipairs, type;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    15
local io_open, io_write = io.open, io.write;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    16
local math_max, rep = math.max, string.rep;
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
    17
local os_date, os_getenv = os.date, os.getenv;
3750
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
    18
local getstyle, setstyle = require "util.termcolours".getstyle, require "util.termcolours".setstyle;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    19
2139
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    20
if os.getenv("__FLUSH_LOG") then
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    21
	local io_flush = io.flush;
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    22
	local _io_write = io_write;
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    23
	io_write = function(...) _io_write(...); io_flush(); end
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    24
end
625b2d3e8900 loggingmanager: Explicitly flush log messages if the __FLUSH_LOG environment variable is defined (workaround for MSVCRT buffering piped output).
Waqas Hussain <waqas20@gmail.com>
parents: 1892
diff changeset
    25
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    26
local config = require "core.configmanager";
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
local logger = require "util.logger";
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    28
local prosody = prosody;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    29
1046
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
    30
_G.log = logger.init("general");
6fef969ff307 core.loggingmanager: Reinstating global log() function
Matthew Wild <mwild1@gmail.com>
parents: 1031
diff changeset
    31
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    32
module "loggingmanager"
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    33
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    34
-- The log config used if none specified in the config file (see reload_logging for initialization)
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    35
local default_logging;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    36
local default_file_logging;
2922
0ea2ed371fb2 loggingmanager: Don't use non-standard format specifier to format the timestamp.
Waqas Hussain <waqas20@gmail.com>
parents: 2139
diff changeset
    37
local default_timestamp = "%b %d %H:%M:%S";
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    38
-- The actual config loggingmanager is using
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
    39
local logging_config;
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    41
local apply_sink_rules;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    42
local log_sink_types = setmetatable({}, { __newindex = function (t, k, v) rawset(t, k, v); apply_sink_rules(k); end; });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    43
local get_levels;
4427
ae71ae5ddcfc loggingmanager: Remove unused 'critical' level
Matthew Wild <mwild1@gmail.com>
parents: 4141
diff changeset
    44
local logging_levels = { "debug", "info", "warn", "error" }
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    46
-- Put a rule into action. Requires that the sink type has already been registered.
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    47
-- This function is called automatically when a new sink type is added [see apply_sink_rules()]
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    48
local function add_rule(sink_config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    49
	local sink_maker = log_sink_types[sink_config.to];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    50
	if sink_maker then
4627
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    51
		-- Create sink
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    52
		local sink = sink_maker(sink_config);
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    53
		
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    54
		-- Set sink for all chosen levels
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    55
		for level in pairs(get_levels(sink_config.levels or logging_levels)) do
b046cafc81a8 loggingmanager, util.logger: Remove name sinks and the ability to filter logs by source name (lots of code, hardly used if at all, and possibly broken)
Matthew Wild <mwild1@gmail.com>
parents: 4427
diff changeset
    56
			logger.add_level_sink(level, sink);
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
		end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    58
	else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    59
		-- No such sink type
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
	end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    63
-- Search for all rules using a particular sink type, and apply
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    64
-- them. Called automatically when a new sink type is added to
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    65
-- the log_sink_types table.
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    66
function apply_sink_rules(sink_type)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    67
	if type(logging_config) == "table" then
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    68
		
4141
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    69
		for _, level in ipairs(logging_levels) do
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    70
			if type(logging_config[level]) == "string" then
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    71
				local value = logging_config[level];
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    72
				if sink_type == "file" then
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    73
					add_rule({
4141
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    74
						to = sink_type;
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    75
						filename = value;
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    76
						timestamps = true;
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    77
						levels = { min = level };
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    78
					});
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    79
				elseif value == "*"..sink_type then
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    80
					add_rule({
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    81
						to = sink_type;
ef94984c44a0 loggingmanager: Allow specifying a sink type in per-level logging config (thanks ruskie)
Matthew Wild <mwild1@gmail.com>
parents: 4140
diff changeset
    82
						levels = { min = level };
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    83
					});
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    84
				end
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    85
			end
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    86
		end
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    87
		
4140
e463e1df1bda loggingmanager: Iterate over logging config rules using ipairs rather than pairs
Matthew Wild <mwild1@gmail.com>
parents: 4124
diff changeset
    88
		for _, sink_config in ipairs(logging_config) do
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    89
			if (type(sink_config) == "table" and sink_config.to == sink_type) then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    90
				add_rule(sink_config);
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    91
			elseif (type(sink_config) == "string" and sink_config:match("^%*(.+)") == sink_type) then
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
    92
				add_rule({ levels = { min = "debug" }, to = sink_type });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    93
			end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    94
		end
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    95
	elseif type(logging_config) == "string" and (not logging_config:match("^%*")) and sink_type == "file" then
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
    96
		-- User specified simply a filename, and the "file" sink type
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
    97
		-- was just added
1067
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    98
		for _, sink_config in pairs(default_file_logging) do
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
    99
			sink_config.filename = logging_config;
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   100
			add_rule(sink_config);
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   101
			sink_config.filename = nil;
21f41b06f1d2 loggingmanager: Add ability to set 'log' config option to a filename, which causes all levels >= info to be logged to that file
Matthew Wild <mwild1@gmail.com>
parents: 1046
diff changeset
   102
		end
1101
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   103
	elseif type(logging_config) == "string" and logging_config:match("^%*(.+)") == sink_type then
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   104
		-- Log all levels (debug+) to this sink
fb096ca4b296 loggingmanager: Support for specifying a single sink with *sinkname (*syslog should now work)
Matthew Wild <mwild1@gmail.com>
parents: 1080
diff changeset
   105
		add_rule({ levels = { min = "debug" }, to = sink_type });
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   106
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   107
end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   109
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   110
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   111
--- Helper function to get a set of levels given a "criteria" table
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   112
function get_levels(criteria, set)
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   113
	set = set or {};
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   114
	if type(criteria) == "string" then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   115
		set[criteria] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   116
		return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   117
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   118
	local min, max = criteria.min, criteria.max;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   119
	if min or max then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   120
		local in_range;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   121
		for _, level in ipairs(logging_levels) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   122
			if min == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   123
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   124
				in_range = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   125
			elseif max == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   126
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   127
				return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   128
			elseif in_range then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   129
				set[level] = true;
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   130
			end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   131
		end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   132
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   133
	
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   134
	for _, level in ipairs(criteria) do
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   135
		set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   136
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   137
	return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   138
end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   139
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   140
-- Initialize config, etc. --
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   141
function reload_logging()
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   142
	local old_sink_types = {};
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   143
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   144
	for name, sink_maker in pairs(log_sink_types) do
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   145
		old_sink_types[name] = sink_maker;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   146
		log_sink_types[name] = nil;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   147
	end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   148
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   149
	logger.reset();
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   150
4123
0c9399a66b17 loggingmanager: Re-read 'debug' option on reload.
Waqas Hussain <waqas20@gmail.com>
parents: 4117
diff changeset
   151
	local debug_mode = config.get("*", "core", "debug");
0c9399a66b17 loggingmanager: Re-read 'debug' option on reload.
Waqas Hussain <waqas20@gmail.com>
parents: 4117
diff changeset
   152
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   153
	default_logging = { { to = "console" , levels = { min = (debug_mode and "debug") or "info" } } };
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
   154
	default_file_logging = {
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3514
diff changeset
   155
		{ to = "file", levels = { min = (debug_mode and "debug") or "info" }, timestamps = true }
3514
fdae08713c67 core.loggingmanager: Logging config simplification - allow [level] = filename and *sink to appear in the config table
Matthew Wild <mwild1@gmail.com>
parents: 3438
diff changeset
   156
	};
3358
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   157
	default_timestamp = "%b %d %H:%M:%S";
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   158
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   159
	logging_config = config.get("*", "core", "log") or default_logging;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   160
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   161
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   162
	for name, sink_maker in pairs(old_sink_types) do
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   163
		log_sink_types[name] = sink_maker;
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   164
	end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   165
	
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   166
	prosody.events.fire_event("logging-reloaded");
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   167
end
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   168
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   169
reload_logging();
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   170
prosody.events.add_handler("config-reloaded", reload_logging);
b5a812cf698c loggingmanager: Add reload_logging() method, which gets called on any config reload, to reset util.logger and remove and re-add all sink types to perform a full reload of the logging system without a restart.
Matthew Wild <mwild1@gmail.com>
parents: 3045
diff changeset
   171
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   172
--- Definition of built-in logging sinks ---
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   173
1080
b02290fd8a75 loggingmanager: Add a comment about 'nowhere' sink type
Matthew Wild <mwild1@gmail.com>
parents: 1078
diff changeset
   174
-- Null sink, must enter log_sink_types *first*
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   175
function log_sink_types.nowhere()
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   176
	return function () return false; end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   177
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   178
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   179
-- Column width for "source" (used by stdout and console)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   180
local sourcewidth = 20;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   181
4124
3761aa844d65 loggingmanager: Fix reading configuration of timestamps for console output.
Waqas Hussain <waqas20@gmail.com>
parents: 4123
diff changeset
   182
function log_sink_types.stdout(config)
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   183
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   184
	
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   185
	if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   186
		timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   187
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   188
	
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   189
	return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   190
		sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   191
		local namelen = #name;
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   192
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   193
			io_write(os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   194
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   195
		if ... then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   196
			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   197
		else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   198
			io_write(name, rep(" ", sourcewidth-namelen), level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   199
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   200
	end
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   201
end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   202
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   203
do
3750
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   204
	local do_pretty_printing = true;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   205
	
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   206
	local logstyles = {};
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   207
	if do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   208
		logstyles["info"] = getstyle("bold");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   209
		logstyles["warn"] = getstyle("bold", "yellow");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   210
		logstyles["error"] = getstyle("bold", "red");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   211
	end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   212
	function log_sink_types.console(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   213
		-- Really if we don't want pretty colours then just use plain stdout
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   214
		if not do_pretty_printing then
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   215
			return log_sink_types.stdout(config);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   216
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   217
		
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   218
		local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   219
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   220
		if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   221
			timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   222
		end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   223
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   224
		return function (name, level, message, ...)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   225
			sourcewidth = math_max(#name+2, sourcewidth);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   226
			local namelen = #name;
1892
adc0c80413ee loggingmanager: Whitespace fix
Matthew Wild <mwild1@gmail.com>
parents: 1613
diff changeset
   227
			
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   228
			if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   229
				io_write(os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   230
			end
3750
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   231
			io_write(name, rep(" ", sourcewidth-namelen));
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   232
			setstyle(logstyles[level]);
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   233
			io_write(level);
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   234
			setstyle();
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   235
			if ... then
3750
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   236
				io_write("\t", format(message, ...), "\n");
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   237
			else
3750
e076e4bf70bf core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   238
				io_write("\t", message, "\n");
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   239
			end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   240
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   241
	end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   242
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   243
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   244
local empty_function = function () end;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   245
function log_sink_types.file(config)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   246
	local log = config.filename;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   247
	local logfile = io_open(log, "a+");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   248
	if not logfile then
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   249
		return empty_function;
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   250
	end
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   251
	local write, flush = logfile.write, logfile.flush;
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   252
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   253
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   254
1613
ebf0813a81f6 core.loggingmanager: Enable timestamps by default for file log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   255
	if timestamps == nil or timestamps == true then
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   256
		timestamps = default_timestamp; -- Default format
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   257
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   258
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   259
	return function (name, level, message, ...)
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   260
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   261
			write(logfile, os_date(timestamps), " ");
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   262
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   263
		if ... then
1031
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   264
			write(logfile, name, "\t", level, "\t", format(message, ...), "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   265
		else
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   266
			write(logfile, name, "\t" , level, "\t", message, "\n");
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   267
		end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   268
		flush(logfile);
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   269
	end;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   270
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   271
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   272
function register_sink_type(name, sink_maker)
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   273
	local old_sink_maker = log_sink_types[name];
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   274
	log_sink_types[name] = sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   275
	return old_sink_maker;
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   276
end
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   277
ec013f93de81 core.loggingmanager: Refactoring, converted to a module. Now possible to register additional sink types (think syslog) from other modules
Matthew Wild <mwild1@gmail.com>
parents: 1024
diff changeset
   278
return _M;