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]
{
  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',
  #opc: 'http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-376,%20Fourth%20Edition,%20Part%202%20-%20Open%20Packaging%20Conventions.zip'
  opc: 'http://3mf.io/wp-content/uploads/2016/03/3MFcoreSpec_1.1.pdf'
}.freeze

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.



43
44
45
46
47
48
49
# File 'lib/ruby3mf/log3mf.rb', line 43

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby3mf/log3mf.rb', line 71

def method_missing(name, *args, &block)
  if LOG_LEVELS.include? name.to_sym
    if [:fatal_error, :error, :debug].include? name.to_sym
      linenumber = caller_locations[0].to_s.split('/')[-1].split(':')[-2].to_s
      filename = caller_locations[0].to_s.split('/')[-1].split(':')[0].to_s
      options = {linenumber: linenumber, filename: filename}
      # Mike: do not call error or fatal_error without an entry in errors.yml
      raise "{fatal_}error called WITHOUT using error symbol from: #{filename}:#{linenumber}" if ( !(args[0].is_a? Symbol) && (name.to_sym != :debug) )

      puts "***** Log3mf.#{name} called from #{filename}:#{linenumber} *****" if $DEBUG

      options = options.merge(args[1]) if args[1]
      log(name.to_sym, args[0], options)
    else
      log(name.to_sym, *args)
    end
  else
    super
  end
end

Class Method Details

.context(context_description, &block) ⇒ Object



67
68
69
# File 'lib/ruby3mf/log3mf.rb', line 67

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

.count_entries(*l) ⇒ Object



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

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

.entries(*l) ⇒ Object



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

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

.reset_logObject



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

def self.reset_log
  Log3mf.instance.reset_log
end

.to_jsonObject



132
133
134
# File 'lib/ruby3mf/log3mf.rb', line 132

def self.to_json
  Log3mf.instance.to_json
end

Instance Method Details

#context(context_description, &block) ⇒ Object



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

def context (context_description, &block)
  @context_stack.push(context_description)
  retval = block.call(Log3mf.instance)
  @context_stack.pop
  retval
end

#count_entries(*levels) ⇒ Object



106
107
108
# File 'lib/ruby3mf/log3mf.rb', line 106

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

#entries(*levels) ⇒ Object



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

def entries(*levels)
  return @log_list if levels.size == 0
  @log_list.select { |i| levels.include? i[:severity] }
end

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

Raises:



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

def log(severity, message, options = {})
  error = @errormap.fetch(message.to_s) { {"msg" => message.to_s, "page" => nil} }
  options[:page] = error["page"] unless options[:page]
  options[:spec] = error["spec"] unless options[:spec]
  entry = {id: message,
           context: "#{@context_stack.join("/")}",
           severity: severity,
           message: interpolate(error["msg"], options)}
  entry[:spec_ref] = spec_link(options[:spec], options[:page]) if (options && options[:page])
  entry[:caller] = "#{options[:filename]}:#{options[:linenumber]}" if (options && options[:filename] && options[:linenumber])
  @log_list << entry
  raise FatalError if severity == :fatal_error
end

#reset_logObject



51
52
53
54
# File 'lib/ruby3mf/log3mf.rb', line 51

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


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

def spec_link(spec, page)
  spec = :core unless spec
  "#{SPEC_LINKS[spec]}#page=#{page}"
end

#to_jsonObject



128
129
130
# File 'lib/ruby3mf/log3mf.rb', line 128

def to_json
  @log_list.to_json
end