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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/origen/log.rb', line 262

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
  @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)



181
182
183
# File 'lib/origen/log.rb', line 181

def self.log_file
  File.join(log_file_directory, 'last.txt')
end

.log_file_directoryObject



185
186
187
188
189
190
191
# File 'lib/origen/log.rb', line 185

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



108
109
110
111
112
113
114
115
116
117
# File 'lib/origen/log.rb', line 108

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



144
145
146
147
148
149
150
151
152
153
# File 'lib/origen/log.rb', line 144

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



168
169
170
171
172
173
174
175
176
177
# File 'lib/origen/log.rb', line 168

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



202
203
204
205
206
207
# File 'lib/origen/log.rb', line 202

def flush
  @open_logs.each do |logger, file|
    file.flush
  end
  nil
end

#info(string = '', options = {}) ⇒ Object Also known as: lputs, lprint



119
120
121
122
123
124
125
126
127
128
# File 'lib/origen/log.rb', line 119

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



79
80
81
# File 'lib/origen/log.rb', line 79

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
# 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)



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/origen/log.rb', line 211

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)


193
194
195
# File 'lib/origen/log.rb', line 193

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


97
98
99
100
101
# File 'lib/origen/log.rb', line 97

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.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/origen/log.rb', line 225

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.



104
105
106
# File 'lib/origen/log.rb', line 104

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.



249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/origen/log.rb', line 249

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



133
134
135
136
137
138
139
140
141
142
# File 'lib/origen/log.rb', line 133

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)


197
198
199
# File 'lib/origen/log.rb', line 197

def verbose?
  level == :verbose
end

#warn(string = '', options = {}) ⇒ Object Also known as: warning



156
157
158
159
160
161
162
163
164
165
# File 'lib/origen/log.rb', line 156

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