Class: Log3mf

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ruby3mf/log3mf.rb

Defined Under Namespace

Classes: FatalError

Constant Summary collapse

LOG_LEVELS =
[:fatal_error, :error, :warning, :info, :debug]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLog3mf

Returns a new instance of Log3mf.



32
33
34
35
36
# File 'lib/ruby3mf/log3mf.rb', line 32

def initialize()
  @log_list = []
  @context_stack = []
  @ledger = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



61
62
63
64
65
66
67
68
# File 'lib/ruby3mf/log3mf.rb', line 61

def method_missing(name, *args, &block)
  if LOG_LEVELS.include? name.to_sym
    #puts "***** #{name} called from #{caller[0]}"
    log(name.to_sym, *args)
  else
    super
  end
end

Class Method Details

.context(context_description, &block) ⇒ Object



57
58
59
# File 'lib/ruby3mf/log3mf.rb', line 57

def self.context(context_description, &block)
  Log3mf.instance.context(context_description, &block)
end

.count_entries(*l) ⇒ Object



80
81
82
# File 'lib/ruby3mf/log3mf.rb', line 80

def self.count_entries(*l)
  Log3mf.instance.count_entries(*l)
end

.entries(*l) ⇒ Object



88
89
90
# File 'lib/ruby3mf/log3mf.rb', line 88

def self.entries(*l)
  Log3mf.instance.entries(*l)
end

.reset_logObject



43
44
45
# File 'lib/ruby3mf/log3mf.rb', line 43

def self.reset_log
  Log3mf.instance.reset_log
end

.to_jsonObject



111
112
113
# File 'lib/ruby3mf/log3mf.rb', line 111

def self.to_json
  Log3mf.instance.to_json
end

.to_ppObject



134
135
136
# File 'lib/ruby3mf/log3mf.rb', line 134

def self.to_pp
  Log3mf.instance.to_pp
end

Instance Method Details

#context(context_description, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/ruby3mf/log3mf.rb', line 47

def context (context_description, &block)
  @context_stack.push(context_description)
  #puts "started context #{@context_stack.join("/")}"

  retval = block.call(Log3mf.instance)

  @context_stack.pop
  retval
end

#count_entries(*levels) ⇒ Object



76
77
78
# File 'lib/ruby3mf/log3mf.rb', line 76

def count_entries(*levels)
  entries(*levels).count
end

#entries(*levels) ⇒ Object



84
85
86
# File 'lib/ruby3mf/log3mf.rb', line 84

def entries(*levels)
  @log_list.select { |i| levels.include? i[1] }
end

#log(severity, message, options = {}) ⇒ Object

Raises:



70
71
72
73
74
# File 'lib/ruby3mf/log3mf.rb', line 70

def log(severity, message, options={})
  @log_list << ["#{@context_stack.join("/")}", severity, message, options] unless severity==:debug && ENV['LOGDEBUG'].nil?
  #puts "[#{@context_stack.join("/")}] #{severity.to_s.upcase} #{message}"
  raise FatalError if severity == :fatal_error
end

#reset_logObject



38
39
40
41
# File 'lib/ruby3mf/log3mf.rb', line 38

def reset_log
  @log_list = []
  @context_stack = []
end


92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby3mf/log3mf.rb', line 92

def spec_link(spec, page)
  spec = :core unless spec
  doc_urls={
    core: 'http://3mf.io/wp-content/uploads/2016/03/3MFcoreSpec_1.1.pdf',
    material: 'http://3mf.io/wp-content/uploads/2015/04/3MFmaterialsSpec_1.0.1.pdf',
    production: 'http://3mf.io/wp-content/uploads/2016/07/3MFproductionSpec.pdf',
    slice: 'http://3mf.io/wp-content/uploads/2016/07/3MFsliceSpec.pdf'
  }
  "#{doc_urls[spec]}#page=#{page}"
end

#to_jsonObject



103
104
105
106
107
108
109
# File 'lib/ruby3mf/log3mf.rb', line 103

def to_json
  @log_list.collect { |ent|
    h = { context: ent[0], severity: ent[1], message: ent[2] }
    h[:spec_ref] = spec_link(ent[3][:spec], ent[3][:page]) if (ent[3] && ent[3][:page])
    h
  }.to_json
end

#to_ppObject

Pretty print our errors!



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby3mf/log3mf.rb', line 116

def to_pp
  s = []
  s << "<i>Listing #{@log_list.size} log lines:</i>"
  longest_context = @log_list.collect { |logline| logline[0].size }.max
  longest_severity = @log_list.collect { |logline| logline[1].size }.max

  @log_list.each do |logline|
    msg = logline[2]
    if logline[3] && logline[3][:page]
      msg = "<a href=\"#{spec_link(logline[3][:spec], logline[3][:page])}\" target=\"_blank\">#{msg}</a>"
    end
    s << "[#{logline[0].ljust(longest_context)}] #{logline[1].to_s.upcase.ljust(longest_severity)} #{msg}"
  end
  s << "<br/>"
  s.map! { |row| row.include?("ERROR") ? "<b>#{row}</b>" : row }
  s.join("<br/>")
end