Class: Origen::Log

Inherits:
Object show all
Defined in:
lib/origen/log.rb

Overview

An instance of this class is instantiated as Origen.log and provides the following API

Examples:

log.error "Blah"     # Error message, always shown
log.debug "Blah"     # Debug message, only shown when in verbose mode
log.info  "Blah"     # Info message, always shown
log.warn  "Blah"     # Warning message, always shown
log.deprecate "Blah" # Deprecate message, always shown

Constant Summary collapse

LEVELS =
[:normal, :verbose, :silent]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

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_onlyObject

Anything executed within the given block will log to the console only

Examples:


Origen::Log.console_only do
  Origen.log.info "This will not appear in the log file!"
end


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

Returns:

  • (Boolean)


49
50
51
# File 'lib/origen/log.rb', line 49

def self.console_only?
  @console_only
end

.log_fileObject

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_directoryObject



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

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# File 'lib/origen/log.rb', line 23

def console_only?(options = {})
  if options.key?(:console_only)
    option = options[:console_only]
  else
    option = self.class.console_only?
  end
  option || !Origen.app || Origen.running_globally?
end

#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

#flushObject

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

#levelObject

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

  # Map the log4r levels to our simplified 3 level system
  # log4r level order is DEBUG < INFO < WARN < ERROR < FATAL
  case val
  when :normal
    # Output everything except debug statements
    console.level = Logger::INFO
    # Output everything
    log_files(:level=, Logger::DEBUG) unless console_only?
  when :verbose
    console.level = Logger::DEBUG
    log_files(:level=, Logger::DEBUG) unless console_only?
  when :silent
    # We don't use any fatal messages, so this is effectively OFF
    console.level = Logger::FATAL
    log_files(:level=, Logger::DEBUG) unless console_only?
  end

  @level = val
end

#resetObject

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

Returns:

  • (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.

Examples:

of an interceptor:


# An interceptor ID is returned, this should be given to stop_intercepting
@log_intercept_id = Origen.log.start_intercepting do |msg, type, options, original|
  if some_condition_is_true?
    # Handling it ourselves
    my_method(msg, type)
  else
    # Call the original Origen.log method (or the next interceptor in line)
    original.call(msg, type, options)
  end
end


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)
  # Make sure the log name is unique in this run, duplication and overwrite can occur in cases where
  # a pattern is run multiple times during a simulation
  @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

#stop_jobObject

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

Returns:

  • (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