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.



17
18
19
20
# File 'lib/origen/log.rb', line 17

def initialize
  @log_time_0 = @t0 = Time.new
  self.level = :normal
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


33
34
35
36
37
# File 'lib/origen/log.rb', line 33

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

.console_only=(val) ⇒ Object



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

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

.console_only?Boolean

Returns:

  • (Boolean)


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

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)



126
127
128
# File 'lib/origen/log.rb', line 126

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

.log_file_directoryObject



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

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



130
131
132
# File 'lib/origen/log.rb', line 130

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

Instance Method Details

#console_only?Boolean

Returns:

  • (Boolean)


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

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

#debug(string = '') ⇒ Object



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

def debug(string = '')
  msg = format_msg('DEBUG', string)
  log_files.debug msg unless console_only?
  console.debug msg
  nil
end

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



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

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

#error(string = '') ⇒ Object



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

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

#flushObject

Force logger to write any buffered output



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

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

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



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

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

#levelObject

Returns the current logger level



73
74
75
# File 'lib/origen/log.rb', line 73

def level
  @level
end

#level=(val) ⇒ Object

Set the logger level, for valid values see LEVELS



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/origen/log.rb', line 48

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)


142
143
144
# File 'lib/origen/log.rb', line 142

def silent?
  level == :silent
end

#success(string = '') ⇒ Object



94
95
96
97
98
99
# File 'lib/origen/log.rb', line 94

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

#verbose?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/origen/log.rb', line 146

def verbose?
  level == :verbose
end

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



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

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