Class: Log3mf

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

Defined Under Namespace

Classes: FatalError

Constant Summary collapse

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

Constants included from Interpolation

Interpolation::INTERPOLATION_PATTERN

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interpolation

#interpolate, #map_value, #symbolize_recursive

Constructor Details

#initializeLog3mf

Returns a new instance of Log3mf.



34
35
36
37
38
39
40
# File 'lib/ruby3mf/log3mf.rb', line 34

def initialize()
  @log_list = []
  @context_stack = []
  @ledger = []
  errormap_path = File.join(File.dirname(__FILE__),"errors.yml")
  @errormap = YAML.load_file(errormap_path)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



65
66
67
68
69
70
71
# File 'lib/ruby3mf/log3mf.rb', line 65

def method_missing(name, *args, &block)
  if LOG_LEVELS.include? name.to_sym
    log(name.to_sym, *args)
  else
    super
  end
end

Class Method Details

.context(context_description, &block) ⇒ Object



61
62
63
# File 'lib/ruby3mf/log3mf.rb', line 61

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

.count_entries(*l) ⇒ Object



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

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

.entries(*l) ⇒ Object



93
94
95
# File 'lib/ruby3mf/log3mf.rb', line 93

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

.reset_logObject



47
48
49
# File 'lib/ruby3mf/log3mf.rb', line 47

def self.reset_log
  Log3mf.instance.reset_log
end

.to_hashObject



116
117
118
# File 'lib/ruby3mf/log3mf.rb', line 116

def self.to_hash
  Log3mf.instance.to_hash
end

.to_jsonObject



124
125
126
# File 'lib/ruby3mf/log3mf.rb', line 124

def self.to_json
  Log3mf.instance.to_json
end

Instance Method Details

#context(context_description, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/ruby3mf/log3mf.rb', line 51

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



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

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

#entries(*levels) ⇒ Object



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

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

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

Raises:



73
74
75
76
77
78
79
# File 'lib/ruby3mf/log3mf.rb', line 73

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

#reset_logObject



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

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


97
98
99
100
101
102
103
104
105
106
# File 'lib/ruby3mf/log3mf.rb', line 97

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_hashObject



108
109
110
111
112
113
114
# File 'lib/ruby3mf/log3mf.rb', line 108

def to_hash
  @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
  }
end

#to_jsonObject



120
121
122
# File 'lib/ruby3mf/log3mf.rb', line 120

def to_json
  to_hash.to_json
end