core/loggingmanager.lua
author Waqas Hussain <waqas20@gmail.com>
Thu, 13 Jan 2011 02:31:10 +0500
changeset 4123 0c9399a66b17
parent 4117 b42ca82b0ea4
child 4124 3761aa844d65
permissions -rw-r--r--
loggingmanager: Re-read 'debug' option on reload.
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;
3870
84ee4a4ef7d5 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;
1024
1bcc8ca57a7c core.loggingmanager: Add default logging settings (to console) and fill out code for adding sinks which catch all sources
Matthew Wild <mwild1@gmail.com>
parents: 1021
diff changeset
    44
local logging_levels = { "debug", "info", "warn", "error", "critical" }
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
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
    51
		if sink_config.levels and not sink_config.source 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
    52
			-- Create sink
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
    53
			local sink = sink_maker(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
    54
			
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
    55
			-- Set sink for all chosen levels
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
    56
			for level in pairs(get_levels(sink_config.levels)) do
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
    57
				logger.add_level_sink(level, sink);
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
			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
    59
		elseif sink_config.source and not sink_config.levels 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
    60
			logger.add_name_sink(sink_config.source, sink_maker(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
    61
		elseif sink_config.source and sink_config.levels 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
    62
			local levels = get_levels(sink_config.levels);
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
    63
			local sink = sink_maker(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
    64
			logger.add_name_sink(sink_config.source,
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
    65
				function (name, level, ...)
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
					if levels[level] 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
    67
						return sink(name, level, ...);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    68
					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
    69
				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
    70
		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
    71
			-- All sources
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
    72
			-- Create sink
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
    73
			local sink = sink_maker(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
    74
			
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
    75
			-- Set sink for all levels
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
    76
			for _, level in pairs(logging_levels) do
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
    77
				logger.add_level_sink(level, sink);
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
    78
			end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
		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
    80
	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
    81
		-- No such sink type
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
	end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
end
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
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
    85
-- 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
    86
-- 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
    87
-- 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
    88
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
    89
	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
    90
		
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
		if sink_type == "file" 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
			for _, level in ipairs(logging_levels) do
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
    93
				if type(logging_config[level]) == "string" 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
    94
					add_rule({
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
    95
						to = "file",
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
    96
						filename = logging_config[level],
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
    97
						timestamps = true,
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
    98
						levels = { min = level },
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
    99
					});
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
   100
				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
   101
			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
   102
		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
   103
		
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
   104
		for _, sink_config in pairs(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
   105
			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
   106
				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
   107
			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
   108
				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
   109
			end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   110
		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
   111
	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
   112
		-- 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
   113
		-- 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
   114
		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
   115
			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
   116
			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
   117
			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
   118
		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
   119
	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
   120
		-- 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
   121
		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
   122
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   123
end
1016
73afe3e30e97 core.loggingmanager: A new manager (yay!) to manage log output
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   125
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   126
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   127
--- 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
   128
function get_levels(criteria, set)
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   129
	set = set or {};
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   130
	if type(criteria) == "string" then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   131
		set[criteria] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   132
		return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   133
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   134
	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
   135
	if min or max then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   136
		local in_range;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   137
		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
   138
			if min == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   139
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   140
				in_range = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   141
			elseif max == level then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   142
				set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   143
				return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   144
			elseif in_range then
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   145
				set[level] = true;
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   146
			end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   147
		end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   148
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   149
	
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   150
	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
   151
		set[level] = true;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   152
	end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   153
	return set;
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   154
end
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   155
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
   156
-- 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
   157
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
   158
	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
   159
	
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
	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
   161
		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
   162
		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
   163
	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
   164
	
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
	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
   166
4123
0c9399a66b17 loggingmanager: Re-read 'debug' option on reload.
Waqas Hussain <waqas20@gmail.com>
parents: 4117
diff changeset
   167
	local debug_mode = config.get("*", "core", "debug");
0c9399a66b17 loggingmanager: Re-read 'debug' option on reload.
Waqas Hussain <waqas20@gmail.com>
parents: 4117
diff changeset
   168
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
   169
	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
   170
	default_file_logging = {
3540
bc139431830b Monster whitespace commit (beware the whitespace monster).
Waqas Hussain <waqas20@gmail.com>
parents: 3514
diff changeset
   171
		{ 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
   172
	};
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
   173
	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
   174
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
   175
	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
   176
	
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
   177
	
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
   178
	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
   179
		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
   180
	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
   181
	
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
   182
	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
   183
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
   184
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
   185
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
   186
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
   187
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
   188
--- 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
   189
1080
b02290fd8a75 loggingmanager: Add a comment about 'nowhere' sink type
Matthew Wild <mwild1@gmail.com>
parents: 1078
diff changeset
   190
-- 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
   191
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
   192
	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
   193
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
   194
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
   195
-- 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
   196
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
   197
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
function log_sink_types.stdout()
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   199
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   200
	
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   201
	if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   202
		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
   203
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   204
	
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
	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
   206
		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
   207
		local namelen = #name;
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   208
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   209
			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
   210
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   211
		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
   212
			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
   213
		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
   214
			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
   215
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   216
	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
   217
end
1021
f9122efeaadd core.loggingmanager: Filled out most code, and cleaned up
Matthew Wild <mwild1@gmail.com>
parents: 1016
diff changeset
   218
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
   219
do
3870
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   220
	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
   221
	
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
   222
	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
   223
	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
   224
		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
   225
		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
   226
		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
   227
	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
   228
	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
   229
		-- 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
   230
		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
   231
			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
   232
		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
   233
		
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   234
		local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   235
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   236
		if timestamps == true then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   237
			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
   238
		end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   239
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
   240
		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
   241
			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
   242
			local namelen = #name;
1892
adc0c80413ee loggingmanager: Whitespace fix
Matthew Wild <mwild1@gmail.com>
parents: 1613
diff changeset
   243
			
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   244
			if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   245
				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
   246
			end
3870
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   247
			io_write(name, rep(" ", sourcewidth-namelen));
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   248
			setstyle(logstyles[level]);
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   249
			io_write(level);
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   250
			setstyle();
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   251
			if ... then
3870
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   252
				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
   253
			else
3870
84ee4a4ef7d5 core.loggingmanager: Updated to use termcolours.getstyle instead of termcolours.getstring for console logging.
Waqas Hussain <waqas20@gmail.com>
parents: 3540
diff changeset
   254
				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
   255
			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
   256
		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
   257
	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
   258
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
   259
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   260
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
   261
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
   262
	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
   263
	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
   264
	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
   265
		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
   266
	end
1117
360ec48ea780 loggingmanager: File log sinks react to reopen-log-files event
Matthew Wild <mwild1@gmail.com>
parents: 1101
diff changeset
   267
	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
   268
1078
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   269
	local timestamps = config.timestamps;
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   270
1613
ebf0813a81f6 core.loggingmanager: Enable timestamps by default for file log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1522
diff changeset
   271
	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
   272
		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
   273
	end
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   274
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
   275
	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
   276
		if timestamps then
24c9ee99d900 loggingmanager: Support prepending timestamps in file/console/stdout log sinks
Matthew Wild <mwild1@gmail.com>
parents: 1067
diff changeset
   277
			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
   278
		end
2586
26ead5e16cd3 loggingmanager: Trailing whitespace
Matthew Wild <mwild1@gmail.com>
parents: 2139
diff changeset
   279
		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
   280
			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
   281
		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
   282
			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
   283
		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
   284
		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
   285
	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
   286
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
   287
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
   288
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
   289
	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
   290
	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
   291
	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
   292
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
   293
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
   294
return _M;