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
# File 'lib/origen/log.rb', line 16

def initialize
  @log_time_0 = @t0 = Time.new
  self.level = :normal
  @custom_logs = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/origen/log.rb', line 201

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


38
39
40
41
42
# File 'lib/origen/log.rb', line 38

def self.console_only
  @console_only = true
  yield
  @console_only = false
end

.console_only=(val) ⇒ Object



44
45
46
# File 'lib/origen/log.rb', line 44

def self.console_only=(val)
  @console_only = val
end

.console_only?Boolean

Returns:

  • (Boolean)


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

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)



137
138
139
# File 'lib/origen/log.rb', line 137

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

.log_file_directoryObject



141
142
143
144
145
146
147
# File 'lib/origen/log.rb', line 141

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)


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

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



82
83
84
85
86
87
88
# File 'lib/origen/log.rb', line 82

def debug(string = '', options = {})
  string, options = sanitize_args(string, options)
  msg = format_msg('DEBUG', string)
  log_files(:debug, msg) unless console_only?(options)
  console.debug msg
  nil
end

#deprecate(string = '', msg_type = nil) ⇒ Object Also known as: deprecated



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

def deprecate(string = '', msg_type = nil)
  string, options = sanitize_args(string, options)
  msg = format_msg('DEPRECATED', string)
  log_files(:warn, msg) unless console_only?(options)
  console.warn color_unless_remote(msg, :yellow)
  nil
end

#error(string = '', msg_type = nil) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/origen/log.rb', line 127

def error(string = '', msg_type = nil)
  string, options = sanitize_args(string, options)
  msg = format_msg('ERROR', string)
  log_files(:error, msg) unless console_only?(options)
  console.error color_unless_remote(msg, :red)
  nil
end

#flushObject

Used to force logger to write any buffered output under an earlier implementation, now does nothing



158
159
160
161
# File 'lib/origen/log.rb', line 158

def flush
  # No such API provided by the underlying logger, method kept around for compatibility with application
  # code which was built for a previous version of this logger where flushing was required
end

#info(string = '', msg_type = nil) ⇒ Object Also known as: lputs, lprint



90
91
92
93
94
95
96
# File 'lib/origen/log.rb', line 90

def info(string = '', msg_type = nil)
  string, options = sanitize_args(string, options)
  msg = format_msg('INFO', string)
  log_files(:info, msg) unless console_only?(options)
  console.info msg
  nil
end

#levelObject

Returns the current logger level



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

def level
  @level
end

#level=(val) ⇒ Object

Set the logger level, for valid values see LEVELS



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/origen/log.rb', line 53

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)



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/origen/log.rb', line 165

def reset
  self.level = :normal
  @last_file.close if @last_file
  @last_file = nil
  @job_file.close if @job_file
  @job_file = nil
  @custom_logs.each do |name, log|
    log.close
  end
  @custom_logs = {}
end

#silent?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/origen/log.rb', line 149

def silent?
  level == :silent
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.



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/origen/log.rb', line 178

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_path = File.join(dir, "#{name}.txt")
  FileUtils.rm_f(@job_file_path) if File.exist?(@job_file_path)
  @job_file = open_log(@job_file_path)
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.



193
194
195
196
197
198
199
# File 'lib/origen/log.rb', line 193

def stop_job
  if @job_file
    Origen.log.info "Log file written to: #{@job_file_path}"
    @job_file.close
    @job_file = nil
  end
end

#success(string = '', msg_type = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/origen/log.rb', line 101

def success(string = '', msg_type = nil)
  string, options = sanitize_args(string, options)
  msg = format_msg('SUCCESS', string)
  log_files(:info, msg) unless console_only?(options)
  console.info color_unless_remote(msg, :green)
  nil
end

#verbose?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/origen/log.rb', line 153

def verbose?
  level == :verbose
end

#warn(string = '', msg_type = nil) ⇒ Object Also known as: warning



118
119
120
121
122
123
124
# File 'lib/origen/log.rb', line 118

def warn(string = '', msg_type = nil)
  string, options = sanitize_args(string, options)
  msg = format_msg('WARNING', string)
  log_files(:warn, msg) unless console_only?(options)
  console.warn color_unless_remote(msg, :yellow)
  nil
end