File: config.rb

package info (click to toggle)
ruby-console 1.34.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 272 kB
  • sloc: ruby: 1,509; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 2,816 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2025, by Samuel Williams.

require_relative "filter"
require_relative "event"
require_relative "resolver"
require_relative "output"
require_relative "logger"

module Console
	# Represents a configuration for the traces library.
	class Config
		PATH = ENV.fetch("CONSOLE_CONFIG_PATH", "config/console.rb")
		
		# Load the configuration from the given path.
		# @parameter path [String] The path to the configuration file.
		# @returns [Config] The loaded configuration.
		def self.load(path)
			config = self.new
			
			if File.exist?(path)
				config.instance_eval(File.read(path), path)
			end
			
			return config
		end
		
		# Load the default configuration.
		# @returns [Config] The default configuration.
		def self.default
			@default ||= self.load(PATH).freeze
		end
		
		# Set the default log level based on `$DEBUG` and `$VERBOSE`.
		# You can also specify CONSOLE_LEVEL=debug or CONSOLE_LEVEL=info in environment.
		# https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together.
		#
		# @parameter env [Hash] The environment to read the log level from.
		# @returns [Integer | Symbol] The default log level.
		def log_level(env = ENV)
			Logger.default_log_level(env)
		end
		
		# Controls verbose output using `$VERBOSE`.
		def verbose?(env = ENV)
			!$VERBOSE.nil? || env["CONSOLE_VERBOSE"]
		end
		
		# Create an output with the given output and options.
		#
		# @parameter output [IO] The output to write log messages to.
		# @parameter env [Hash] The environment to read configuration from.
		# @parameter options [Hash] Additional options to pass to the output.
		# @returns [Output] The created output.
		def make_output(io = nil, env = ENV, **options)
			Output.new(io, env, **options)
		end
		
		# Create a resolver with the given logger.
		#
		# @parameter logger [Logger] The logger to set the log levels on.
		# @returns [Resolver | Nil] The created resolver.
		def make_resolver(logger)
			Resolver.default_resolver(logger)
		end
		
		# Create a logger with the given output and options.
		#
		# @parameter output [IO] The output to write log messages to.
		# @parameter env [Hash] The environment to read configuration from.
		# @parameter options [Hash] Additional options to pass to the logger.
		# @returns [Logger] The created logger.
		def make_logger(io = $stderr, env = ENV, **options)
			if options[:verbose].nil?
				options[:verbose] = self.verbose?(env)
			end
			
			if options[:level].nil?
				options[:level] = self.log_level(env)
			end
			
			output = self.make_output(io, env, **options)
			
			logger = Logger.new(output, **options)
			
			make_resolver(logger)
			
			return logger
		end
		
		# Load the default configuration.
		DEFAULT = self.default
	end
end