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]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog

Returns a new instance of Log


20
21
22
23
24
# File 'lib/origen/log.rb', line 20

def initialize
  @log_time_0 = @t0 = Time.new
  self.level = :normal
  @msg_hash = init_msg_hash
end

Instance Attribute Details

#msg_hashObject Also known as: messages

Returns the value of attribute msg_hash


15
16
17
# File 'lib/origen/log.rb', line 15

def msg_hash
  @msg_hash
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

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

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

.console_only=(val) ⇒ Object


54
55
56
# File 'lib/origen/log.rb', line 54

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

.console_only?Boolean

Returns:

  • (Boolean)

58
59
60
# File 'lib/origen/log.rb', line 58

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)


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

def self.log_file
  "#{log_file_directory}/last.txt"
end

.log_file_directoryObject


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

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

.rolling_log_fileObject


162
163
164
# File 'lib/origen/log.rb', line 162

def self.rolling_log_file
  "#{log_file_directory}/rolling.txt"
end

Instance Method Details

#console_only?Boolean

Returns:

  • (Boolean)

37
38
39
# File 'lib/origen/log.rb', line 37

def console_only?
  self.class.console_only? || !Origen.app || Origen.running_globally?
end

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


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

def debug(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('DEBUG', string)
  log_files.debug msg unless console_only?
  console.debug msg
  @msg_hash[:debug][msg_type] << msg
  nil
end

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


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

def deprecate(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('DEPRECATED', string)
  log_files.warn msg unless console_only?
  console.warn msg.yellow
  @msg_hash[:deprecate][msg_type] << msg
  nil
end

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


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

def error(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('ERROR', string)
  log_files.error msg unless console_only?
  console.error msg.red
  @msg_hash[:error][msg_type] << msg
  nil
end

#flushObject

Force logger to write any buffered output


183
184
185
186
187
188
# File 'lib/origen/log.rb', line 183

def flush
  if Origen.app
    log_files.outputters.each(&:flush)
  end
  console.outputters.each(&:flush)
end

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


106
107
108
109
110
111
112
113
# File 'lib/origen/log.rb', line 106

def info(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('INFO', string)
  log_files.info msg unless console_only?
  console.info msg
  @msg_hash[:info][msg_type] << msg
  nil
end

#init_msg_hashObject


26
27
28
29
30
31
32
33
34
35
# File 'lib/origen/log.rb', line 26

def init_msg_hash
  msg_types = [:info, :warn, :error, :deprecate, :debug, :success]
  msg_hash = {}
  msg_types.each do |m|
    msg_hash[m] = Hash.new do |h, k|
      h[k] = []
    end
  end
  msg_hash
end

#levelObject

Returns the current logger level


88
89
90
# File 'lib/origen/log.rb', line 88

def level
  @level
end

#level=(val) ⇒ Object

Set the logger level, for valid values see LEVELS


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/origen/log.rb', line 63

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 = Log4r::INFO
    # Output everything
    log_files.level = Log4r::DEBUG unless console_only?
  when :verbose
    console.level = Log4r::DEBUG
    log_files.level = Log4r::DEBUG unless console_only?
  when :silent
    # We don't use any fatal messages, so this is effectively OFF
    console.level = Log4r::FATAL
    log_files.level = Log4r::DEBUG unless console_only?
  end

  @level = val
end

#silent?Boolean

Returns:

  • (Boolean)

174
175
176
# File 'lib/origen/log.rb', line 174

def silent?
  level == :silent
end

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


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

def success(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('SUCCESS', string)
  log_files.info msg unless console_only?
  console.info msg.green
  @msg_hash[:success][msg_type] << msg
  nil
end

#validate_args(string, msg_type) ⇒ Object


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

def validate_args(string, msg_type)
  return string, msg_type unless string.is_a? Symbol
  ['', string]
end

#verbose?Boolean

Returns:

  • (Boolean)

178
179
180
# File 'lib/origen/log.rb', line 178

def verbose?
  level == :verbose
end

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


137
138
139
140
141
142
143
144
# File 'lib/origen/log.rb', line 137

def warn(string = '', msg_type = nil)
  string, msg_type = validate_args(string, msg_type)
  msg = format_msg('WARNING', string)
  log_files.warn msg unless console_only?
  console.warn msg.yellow
  @msg_hash[:warn][msg_type] << msg
  nil
end