pantheios-ruby 0.13.4 → 0.20.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+
2
+ Dir[File.join(File.dirname(__FILE__), 'services', '*log_service.rb')].each { |f| require f }
3
+
@@ -0,0 +1,239 @@
1
+
2
+ # ######################################################################## #
3
+ # File: lib/pantheios/services/multiplexing_log_service.rb
4
+ #
5
+ # Purpose: Definition of the
6
+ # ::Pantheios::Services::MultiplexingLogService class
7
+ #
8
+ # Created: 14th June 2015
9
+ # Updated: 8th February 2018
10
+ #
11
+ # Home: http://github.com/synesissoftware/Pantheios-Ruby
12
+ #
13
+ # Author: Matthew Wilson
14
+ #
15
+ # Copyright (c) 2015-2018, Matthew Wilson and Synesis Software
16
+ # All rights reserved.
17
+ #
18
+ # Redistribution and use in source and binary forms, with or without
19
+ # modification, are permitted provided that the following conditions are
20
+ # met:
21
+ #
22
+ # * Redistributions of source code must retain the above copyright
23
+ # notice, this list of conditions and the following disclaimer.
24
+ #
25
+ # * Redistributions in binary form must reproduce the above copyright
26
+ # notice, this list of conditions and the following disclaimer in the
27
+ # documentation and/or other materials provided with the distribution.
28
+ #
29
+ # * Neither the names of the copyright holder nor the names of its
30
+ # contributors may be used to endorse or promote products derived from
31
+ # this software without specific prior written permission.
32
+ #
33
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
+ #
45
+ # ######################################################################## #
46
+
47
+
48
+ =begin
49
+ =end
50
+
51
+ module Pantheios
52
+ module Services
53
+
54
+ # A class that fulfils the Pantheios *LogService* protocol by multiplexing
55
+ # its responsibilities to a number of (concrete) log service instances
56
+ #
57
+ # NOTE: The *LogService* protocol is implemented by a class that provides
58
+ # the instance methods +severity_logged?(severity : Object) : boolean+ and
59
+ # +log(severity : Object, t : Time, prefix : String, msg : String)+
60
+ class MultiplexingLogService
61
+
62
+ module MultiplexingLogService_Internals_
63
+
64
+ class ServiceManagementInfo
65
+
66
+ def initialize svc
67
+
68
+ @service = svc
69
+ end
70
+
71
+ attr_reader :service
72
+ end
73
+ end
74
+
75
+ # Initializes the instance with an array of log services, according to
76
+ # the given options
77
+ #
78
+ # ===Signature
79
+ #
80
+ # * *Parameters:*
81
+ # - +services+:: [ ::Array ] An array of instances that observe the
82
+ # Log Service protocol
83
+ # - +options+:: [ ::Hash] options
84
+ #
85
+ # * *Options:*
86
+ # - +:level_cache_mode+:: [ ::Symbol ] Specifies the mode of severity
87
+ # level caching, and must be one of the following values:
88
+ # * +:none+:: no severity level caching is performed. This is the
89
+ # default because it is completely thread-safe, but it is the
90
+ # slowest mode, and users are advised to specify another mode
91
+ # suitable to their use
92
+ # * +:thread_fixed+:: remembers the response of each multiplexed log
93
+ # service to each severity level on a thread-specific basis
94
+ # * +:process_fixed+:: remembers the response of each multiplexed
95
+ # log service to each severity level and then remembers that for
96
+ # the duration of the lifetime of the instance
97
+ # - +:unsync_process_lcm+:: [ boolean ] If truey, causes
98
+ # +:process_fixed+ +:level_cache_mode+ to NOT be synchronised; the
99
+ # default is for it to be synchronised using an internal +Mutex+
100
+ # instance
101
+ def initialize services, **options
102
+
103
+ @tss_sym = self.to_s.to_sym
104
+ @services = services.map { |svc| MultiplexingLogService_Internals_::ServiceManagementInfo.new svc }
105
+ @options = options.dup
106
+ @mode = options[:level_cache_mode]
107
+ @unsync_pf = options[:unsync_process_lcm]
108
+
109
+ @process_m = {}
110
+ @mx = Mutex.new unless @unsync_pf
111
+ end
112
+
113
+ private
114
+
115
+ # { svc => { sev => flag } }
116
+ def get_tss_svc_sev_map_
117
+
118
+ sym = @tss_sym
119
+ tc = Thread.current
120
+ m = tc.thread_variable_get sym
121
+
122
+ unless m
123
+
124
+ tc.thread_variable_set sym, (m = {})
125
+ end
126
+
127
+ m
128
+ end
129
+
130
+ def svc_sev_logged_tf_ m, svc, severity
131
+
132
+ m[svc.object_id] ||= {}
133
+
134
+ unless m[svc.object_id].has_key? severity
135
+
136
+ r = svc.severity_logged? severity
137
+
138
+ m[svc.object_id][severity] = r
139
+ else
140
+
141
+ r = m[svc.object_id][severity]
142
+ end
143
+
144
+ r
145
+ end
146
+
147
+ def svc_sev_logged_pf_ m, svc, severity
148
+
149
+ m[svc.object_id] ||= {}
150
+
151
+ unless m[svc.object_id].has_key? severity
152
+
153
+ r = svc.severity_logged? severity
154
+
155
+ m[svc.object_id][severity] = r
156
+ else
157
+
158
+ r = m[svc.object_id][severity]
159
+ end
160
+
161
+ r
162
+ end
163
+
164
+ def sev_logged_pf_ m, severity
165
+
166
+ @services.any? { |smi| svc_sev_logged_pf_ m, smi.service, severity }
167
+ end
168
+ public
169
+
170
+ # Indicates whether the given severity is to be logged by any of the
171
+ # multiplexed log services
172
+ def severity_logged? severity
173
+
174
+ case @mode
175
+ when :process_fixed
176
+
177
+ if @unsync_pf
178
+
179
+ sev_logged_pf_ @process_m, severity
180
+ else
181
+
182
+ @mx.synchronize { sev_logged_pf_ @process_m, severity }
183
+ end
184
+ when :thread_fixed
185
+
186
+ m = get_tss_svc_sev_map_
187
+
188
+ @services.any? do |smi|
189
+
190
+ svc = smi.service
191
+
192
+ svc_sev_logged_tf_ m, svc, severity
193
+ end
194
+ else # :none
195
+
196
+ @services.any? { |smi| smi.service.severity_logged? severity }
197
+ end
198
+ end
199
+
200
+ def log sev, t, pref, msg
201
+
202
+ tss_m = :thread_fixed == @mode ? get_tss_svc_sev_map_ : nil
203
+
204
+ @services.each do |smi|
205
+
206
+ svc = smi.service
207
+
208
+ case @mode
209
+ when :process_fixed
210
+
211
+ if @unsync_pf
212
+
213
+ isl = svc_sev_logged_pf_ @process_m, svc, sev
214
+ else
215
+
216
+ isl = @mx.synchronize { svc_sev_logged_pf_ @process_m, svc, sev }
217
+ end
218
+ when :thread_fixed
219
+
220
+ m = tss_m
221
+
222
+ isl = svc_sev_logged_tf_ m, svc, sev
223
+ else # :none
224
+
225
+ isl = svc.severity_logged? sev
226
+ end
227
+
228
+
229
+ svc.log sev, t, pref, msg if isl
230
+ end
231
+ end
232
+ end
233
+
234
+ end # module Services
235
+ end # module Pantheios
236
+
237
+ # ############################## end of file ############################# #
238
+
239
+
@@ -1,12 +1,12 @@
1
1
 
2
2
  # ######################################################################## #
3
- # File: lib/pantheios/services/simple_console_log_service.rb
3
+ # File: lib/pantheios/services/null_log_service.rb
4
4
  #
5
5
  # Purpose: Definition of the ::Pantheios::Services::NullLogService
6
6
  # class
7
7
  #
8
8
  # Created: 14th June 2015
9
- # Updated: 22nd January 2018
9
+ # Updated: 8th February 2018
10
10
  #
11
11
  # Home: http://github.com/synesissoftware/Pantheios-Ruby
12
12
  #
@@ -6,7 +6,7 @@
6
6
  # ::Pantheios::Services::SimpleConsoleLogService class
7
7
  #
8
8
  # Created: 14th June 2015
9
- # Updated: 6th January 2018
9
+ # Updated: 12th March 2018
10
10
  #
11
11
  # Home: http://github.com/synesissoftware/Pantheios-Ruby
12
12
  #
@@ -45,8 +45,6 @@
45
45
  # ######################################################################## #
46
46
 
47
47
 
48
- require 'pantheios/application_layer/stock_severity_levels'
49
-
50
48
  =begin
51
49
  =end
52
50
 
@@ -73,11 +71,14 @@ class SimpleConsoleLogService
73
71
  stm.puts "#{pref}#{msg}"
74
72
  end
75
73
 
74
+ # Overrideable method that determines which stream to write, based on a
75
+ # severity. This implementation always returns +$stderr+
76
+ #
77
+ # Overrides must return an object that supports the +puts(String)+
78
+ # method
76
79
  def infer_stream sev
77
80
 
78
- sev = ::Pantheios::ApplicationLayer::StockSeverityLevels::STOCK_SEVERITY_LEVEL_VALUES[sev] if ::Symbol === sev
79
-
80
- (sev.to_i < 6) ? $stderr : $stdout
81
+ $stderr
81
82
  end
82
83
  end
83
84
 
@@ -0,0 +1,230 @@
1
+
2
+ # ######################################################################## #
3
+ # File: lib/pantheios/services/simple_file_log_service.rb
4
+ #
5
+ # Purpose: Definition of the
6
+ # ::Pantheios::Services::SimpleFileLogService class
7
+ #
8
+ # Created: 17th June 2015
9
+ # Updated: 4th February 2018
10
+ #
11
+ # Home: http://github.com/synesissoftware/Pantheios-Ruby
12
+ #
13
+ # Author: Matthew Wilson
14
+ #
15
+ # Copyright (c) 2015-2018, Matthew Wilson and Synesis Software
16
+ # All rights reserved.
17
+ #
18
+ # Redistribution and use in source and binary forms, with or without
19
+ # modification, are permitted provided that the following conditions are
20
+ # met:
21
+ #
22
+ # * Redistributions of source code must retain the above copyright
23
+ # notice, this list of conditions and the following disclaimer.
24
+ #
25
+ # * Redistributions in binary form must reproduce the above copyright
26
+ # notice, this list of conditions and the following disclaimer in the
27
+ # documentation and/or other materials provided with the distribution.
28
+ #
29
+ # * Neither the names of the copyright holder nor the names of its
30
+ # contributors may be used to endorse or promote products derived from
31
+ # this software without specific prior written permission.
32
+ #
33
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34
+ # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36
+ # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
37
+ # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38
+ # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39
+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40
+ # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41
+ # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42
+ # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43
+ # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44
+ #
45
+ # ######################################################################## #
46
+
47
+
48
+ require 'pantheios/application_layer/stock_severity_levels'
49
+
50
+ require 'logger'
51
+
52
+ =begin
53
+ =end
54
+
55
+ module Pantheios
56
+ module Services
57
+
58
+ # A class that fulfils the Pantheios *LogService* protocol that allows all
59
+ # severities and logs to a file
60
+ #
61
+ # NOTE: The *LogService* protocol is implemented by a class that provides
62
+ # the instance methods +severity_logged?(severity : Object) : boolean+ and
63
+ # +log(severity : Object, t : Time, prefix : String, msg : String)+
64
+ class SimpleFileLogService
65
+
66
+ module SimpleFileLogService_Constants
67
+
68
+ DEFAULT_ROLL_DEPTH = 7
69
+ DEFAULT_ROLL_SIZE = 1024 * 1024
70
+
71
+ RECOGNISED_OPTIONS = %w{ roll_depth roll_period roll_size }.map { |s| s.to_sym }
72
+
73
+ end # module SimpleFileLogService_Constants
74
+
75
+ #
76
+ # === Signature
77
+ #
78
+ # * *Parameters:*
79
+ # - +log_file_or_path+:: [ +::File+, +::IO+, +::String+ ] A file or an
80
+ # IO that will be used as the target of the log statements, or a
81
+ # string specifying the path of the file to be used as the target
82
+ # - +options+:: [ ::Hash ] Options. Options other than those listed
83
+ # are ignored silently (except if +$DEBUG+, in which case a
84
+ # +warn+ing will be issued)
85
+ #
86
+ # * *Options:*
87
+ # - +:roll_period+:: ( +:daily+, +:weekly+, +:monthly+ ) The
88
+ # periodicity of the log-file rolling. Ignored unless
89
+ # +log_file_or_path+ is a +::String+. Ignored if either +:roll_size+
90
+ # or +:roll_depth+ is specified
91
+ # - +:roll_size+:: [ ::Integer, [ ::Integer, ::Integer ] ] An integer
92
+ # specifying the size of the log file, or an array containing the
93
+ # size of the log file and the depth of the log roll
94
+ # - +:roll_depth+:: [ ::Integer ] The depth of the size-based log
95
+ # rolling. Overrides the second element in an array specified for
96
+ # +:roll_size+
97
+
98
+
99
+ def initialize log_file_or_path, **options
100
+
101
+ roll_period = options[:roll_period]
102
+ roll_size = options[:roll_size]
103
+ roll_depth = options[:roll_depth]
104
+
105
+ if $DEBUG
106
+
107
+ options.each do |k, v|
108
+
109
+ warn "#{self.class}##{__method__}(): ignoring unrecognised option '#{k}'" unless SimpleFileLogService_Constants::RECOGNISED_OPTIONS.include?(:k)
110
+ end
111
+ end
112
+
113
+ if roll_period && (roll_size || roll_depth)
114
+
115
+ warn "#{self.class}##{__method__}(): caller specified :roll_depth/:roll_period with :roll_size to #{self.class}##{__method__}() - ignoring :roll_period" if $DEBUG
116
+
117
+ roll_period = nil
118
+ end
119
+
120
+ if roll_size || roll_depth
121
+
122
+ if ::Array === roll_size
123
+
124
+ roll_size, d = roll_size
125
+
126
+ roll_depth ||= d
127
+ end
128
+
129
+ roll_size ||= SimpleFileLogService_Constants::DEFAULT_ROLL_SIZE
130
+ roll_depth ||= SimpleFileLogService_Constants::DEFAULT_ROLL_DEPTH
131
+ end
132
+
133
+ logger_init_args = []
134
+ logger_init_options = {}
135
+
136
+ if false;
137
+ elsif roll_depth
138
+
139
+ logger_init_args << roll_depth
140
+ logger_init_args << roll_size
141
+ elsif roll_period
142
+
143
+ roll_period = roll_period.downcase.to_sym if ::String === roll_period
144
+
145
+ case roll_period
146
+ when :daily, :weekly, :monthly
147
+
148
+ ;
149
+ else
150
+
151
+ raise ArgumentError, ":roll_period value must be one of :daily, :weekly, :monthly"
152
+ end
153
+
154
+ logger_init_args << roll_period.to_s
155
+ logger_init_args << 0
156
+ end
157
+
158
+ raise ArgumentError, ":roll_depth must be a non-negative integer" unless roll_depth.nil? || (::Integer === roll_depth && roll_depth >= 0)
159
+ raise ArgumentError, ":roll_size must be a non-negative integer" unless roll_size.nil? || (::Integer === roll_size && roll_size >= 0)
160
+
161
+ file_proc = lambda do
162
+
163
+ @logger = ::Logger.new log_file_or_path, *logger_init_args
164
+ @log_file_path = log_file_or_path.path
165
+ end
166
+
167
+ io_proc = lambda do
168
+
169
+ @logger = ::Logger.new log_file_or_path, *logger_init_args
170
+ @log_file_path = log_file_or_path.respond_to?(:path) ? log_file_or_path.path : nil
171
+ end
172
+
173
+ case log_file_or_path
174
+ when nil
175
+
176
+ raise ArgumentError, 'log_file_or_path may not be nil'
177
+ when ::IO#, ::StringIO
178
+
179
+ io_proc.call
180
+ when ::File#, ::Tempfile
181
+
182
+ file_proc.call
183
+ when ::String
184
+
185
+ @logger = ::Logger.new log_file_or_path, *logger_init_args
186
+ @log_file_path = log_file_or_path
187
+ else
188
+
189
+ ancestor_names = log_file_or_path.class.ancestors.map(&:to_s)
190
+
191
+ if false
192
+
193
+ ;
194
+ elsif ancestor_names.include?('StringIO')
195
+
196
+ io_proc.call
197
+ elsif ancestor_names.include?('Tempfile')
198
+
199
+ file_proc.call
200
+ else
201
+
202
+ raise TypeError, "log_file_or_path type must be one of #{::File}, #{::IO}, #{::String}, #{::StringIO}"
203
+ end
204
+ end
205
+
206
+ self
207
+ end
208
+
209
+ # The path of the underlying log file. May return +nil+ if the path
210
+ # cannot be determined
211
+ attr_reader :log_file_path
212
+
213
+ def severity_logged? severity
214
+
215
+ true
216
+ end
217
+
218
+ def log sev, t, pref, msg
219
+
220
+ @logger << "#{pref}#{msg}\n"
221
+ end
222
+ end
223
+
224
+
225
+ end # module Services
226
+ end # module Pantheios
227
+
228
+ # ############################## end of file ############################# #
229
+
230
+