Class: Origen::Log
Overview
An instance of this class is instantiated as Origen.log and provides the following API
Constant Summary
collapse
- LEVELS =
[:normal, :verbose, :silent]
Class Method Summary
collapse
Instance Method Summary
collapse
-
#console_only?(options = {}) ⇒ Boolean
-
#debug(string = '', options = {}) ⇒ Object
-
#deprecate(string = '', options = {}) ⇒ Object
(also: #deprecated)
-
#error(string = '', options = {}) ⇒ Object
-
#flush ⇒ Object
Force the logger to write any buffered output to the log files.
-
#info(string = '', options = {}) ⇒ Object
(also: #lputs, #lprint)
-
#initialize ⇒ Log
constructor
-
#level ⇒ Object
Returns the current logger level.
-
#level=(val) ⇒ Object
Set the logger level, for valid values see LEVELS.
-
#method_missing(method, *args, &block) ⇒ Object
-
#reset ⇒ Object
Mainly intended for testing the logger, this will return the log level to the default (:normal) and close all log files, such that any further logging will be done to a new file(s).
-
#silent? ⇒ Boolean
-
#start_intercepting(&block) ⇒ Object
private
-
#start_job(name, type) ⇒ Object
private
-
#stop_intercepting(id) ⇒ Object
private
-
#stop_job ⇒ Object
private
-
#success(string = '', options = {}) ⇒ Object
-
#verbose? ⇒ Boolean
-
#warn(string = '', options = {}) ⇒ Object
(also: #warning)
Constructor Details
#initialize ⇒ Log
Returns a new instance of Log.
16
17
18
19
20
21
|
# File 'lib/origen/log.rb', line 16
def initialize
@log_time_0 = @t0 = Time.new
self.level = :normal
@custom_logs = {}
@interceptors = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/origen/log.rb', line 269
def method_missing(method, *args, &block)
@custom_logs[method.to_sym] ||= begin
log_file = File.join(Log.log_file_directory, "#{method}.txt")
unless Origen.running_remotely?
FileUtils.mv log_file, "#{log_file}.old" if File.exist?(log_file)
end
open_log(log_file)
end
msg = args.shift
options = args.shift || {}
if options.key?(:format) && !options[:format]
msg = "#{msg}\n"
else
msg = format_msg(method.to_s.upcase, msg)
end
console.info msg if options[:verbose]
@custom_logs[method.to_sym].info(msg)
end
|
Class Method Details
.console_only ⇒ Object
Anything executed within the given block will log to the console only
39
40
41
42
43
|
# File 'lib/origen/log.rb', line 39
def self.console_only
@console_only = true
yield
@console_only = false
end
|
.console_only=(val) ⇒ Object
45
46
47
|
# File 'lib/origen/log.rb', line 45
def self.console_only=(val)
@console_only = val
end
|
.console_only? ⇒ Boolean
49
50
51
|
# File 'lib/origen/log.rb', line 49
def self.console_only?
@console_only
end
|
Made these all class methods so that they can be read without instantiating a new logger (mainly for use by the origen save command)
182
183
184
|
# File 'lib/origen/log.rb', line 182
def self.log_file
File.join(log_file_directory, 'last.txt')
end
|
.log_file_directory ⇒ Object
186
187
188
189
190
191
192
|
# File 'lib/origen/log.rb', line 186
def self.log_file_directory
@log_file_directory ||= begin
dir = Origen.config.log_directory
FileUtils.mkdir_p dir unless File.exist?(dir)
dir
end
end
|
Instance Method Details
#console_only?(options = {}) ⇒ Boolean
#debug(string = '', options = {}) ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/origen/log.rb', line 109
def debug(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :debug, options) do |msg, type, options|
msg = format_msg('DEBUG', msg)
log_files(:debug, msg) unless console_only?(options)
console.debug msg
nil
end
end
|
#deprecate(string = '', options = {}) ⇒ Object
Also known as:
deprecated
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/origen/log.rb', line 145
def deprecate(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :deprecate, options) do |msg, type, options|
msg = format_msg('DEPRECATED', msg)
log_files(:warn, msg) unless console_only?(options)
console.warn color_unless_remote(msg, :yellow)
nil
end
end
|
#error(string = '', options = {}) ⇒ Object
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/origen/log.rb', line 169
def error(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :error, options) do |msg, type, options|
msg = format_msg('ERROR', msg)
log_files(:error, msg) unless console_only?(options)
console.error color_unless_remote(msg, :red)
nil
end
end
|
Force the logger to write any buffered output to the log files
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/origen/log.rb', line 203
def flush
@open_logs.each do |logger, file|
file.flush
rescue => e
if file.is_a?(File)
Origen.log.warning "Could not flush log file #{file.path}: #{e.message}"
else
Origen.log.warning "Could not flush IO buffer: #{e.message}"
end
end
nil
end
|
#info(string = '', options = {}) ⇒ Object
Also known as:
lputs, lprint
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/origen/log.rb', line 120
def info(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :info, options) do |msg, type, options|
msg = format_msg('INFO', msg)
log_files(:info, msg) unless console_only?(options)
console.info msg
nil
end
end
|
Returns the current logger level
80
81
82
|
# File 'lib/origen/log.rb', line 80
def level
@level
end
|
#level=(val) ⇒ Object
Set the logger level, for valid values see LEVELS
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/origen/log.rb', line 54
def level=(val)
unless LEVELS.include?(val)
fail "Unknown log level, valid values are: #{LEVELS}"
end
case val
when :normal
console.level = Logger::INFO
log_files(:level=, Logger::DEBUG) unless console_only?
when :verbose
console.level = Logger::DEBUG
log_files(:level=, Logger::DEBUG) unless console_only?
when :silent
console.level = Logger::FATAL
log_files(:level=, Logger::DEBUG) unless console_only?
end
@level = val
end
|
Mainly intended for testing the logger, this will return the log level to the default (:normal) and close all log files, such that any further logging will be done to a new file(s)
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/origen/log.rb', line 218
def reset
self.level = :normal
flush
close_log(@last_file)
@last_file = nil
close_log(@job_file)
@job_file = nil
@custom_logs.each do |name, log|
close_log(log)
end
@custom_logs = {}
end
|
#silent? ⇒ Boolean
194
195
196
|
# File 'lib/origen/log.rb', line 194
def silent?
level == :silent
end
|
#start_intercepting(&block) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
98
99
100
101
102
|
# File 'lib/origen/log.rb', line 98
def start_intercepting(&block)
id = block.object_id
@interceptors[id] = block
id
end
|
#start_job(name, type) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/origen/log.rb', line 232
def start_job(name, type)
dir = File.join(Origen.config.log_directory, type.to_s)
if target = Origen.try(:target).try(:name)
dir = File.join(dir, target)
end
if env = Origen.try(:environment).try(:name)
dir = File.join(dir, env)
end
FileUtils.mkdir_p dir unless File.exist?(dir)
@@job_file_paths = {} unless defined?(@@job_file_paths)
@job_file_path = File.join(dir, "#{name}.txt")
if n = @@job_file_paths[@job_file_path]
@@job_file_paths[@job_file_path] += 1
@job_file_path = File.join(dir, "#{name}_#{n}.txt")
else
@@job_file_paths[@job_file_path] = 1
end
FileUtils.rm_f(@job_file_path) if File.exist?(@job_file_path)
@job_file = open_log(@job_file_path)
end
|
#stop_intercepting(id) ⇒ Object
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
105
106
107
|
# File 'lib/origen/log.rb', line 105
def stop_intercepting(id)
@interceptors.delete(id)
end
|
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/origen/log.rb', line 256
def stop_job
if @job_file
if tester && tester.respond_to?(:log_file_written)
tester.log_file_written @job_file_path
else
Origen.log.info "Log file written to: #{@job_file_path}"
end
flush
close_log(@job_file)
@job_file = nil
end
end
|
#success(string = '', options = {}) ⇒ Object
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/origen/log.rb', line 134
def success(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :success, options) do |msg, type, options|
msg = format_msg('SUCCESS', msg)
log_files(:info, msg) unless console_only?(options)
console.info color_unless_remote(msg, :green)
nil
end
end
|
#verbose? ⇒ Boolean
198
199
200
|
# File 'lib/origen/log.rb', line 198
def verbose?
level == :verbose
end
|
#warn(string = '', options = {}) ⇒ Object
Also known as:
warning
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/origen/log.rb', line 157
def warn(string = '', options = {})
string, options = sanitize_args(string, options)
PatSeq.add_thread(string) unless options[:no_thread_id]
intercept(string, :warn, options) do |msg, type, options|
msg = format_msg('WARNING', msg)
log_files(:warn, msg) unless console_only?(options)
console.warn color_unless_remote(msg, :yellow)
nil
end
end
|