Module: RCS::Tracer

Constant Summary collapse

TRACE_YAML_NAME =
'trace.yaml'

Instance Method Summary collapse

Instance Method Details

#thread_nameObject



94
95
96
# File 'lib/rcs-common/trace.rb', line 94

def thread_name
  (Thread.current[:name] || Thread.current.object_id.to_s(32)).to_s
end

#trace(level, msg) ⇒ Object

the actual method used to output a trace



99
100
101
102
103
104
105
106
107
108
# File 'lib/rcs-common/trace.rb', line 99

def trace(level, msg)
  thread_info = "[Thread:#{thread_name}] " if ENV['TRACE_SHOW_THREAD_NAME']
  msg = "#{thread_info}#{msg}"

  log = Logger['rcslogger']
  log.send(level, "#{msg}") unless log.nil?

  # fallback if the logger is not initialized
  puts "#{Time.now} [#{level.to_s.upcase}]: #{msg}" if log.nil? and not ENV['no_trace']
end

#trace_ensure_log_foldersObject

Ensures that the log directories exists



17
18
19
20
21
22
23
# File 'lib/rcs-common/trace.rb', line 17

def trace_ensure_log_folders
  pwd = File.expand_path(Dir.pwd)

  ["#{pwd}/log", "#{pwd}/log/err"].each do |path|
    Dir.mkdir(path) unless File.directory?(path)
  end
end

#trace_init(path = '.', file = 'trace.yaml') ⇒ Object

Deprecated.

Please use #trace_setup instead

needed to initialize the trace subsystem. the path provided is the path where the ‘trace.yaml’ resides the configuration inside the yaml can be changed at will and the trace system will reflect it



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rcs-common/trace.rb', line 52

def trace_init(path = '.', file = 'trace.yaml')
  # the configuration file is YAML
  # the file must be called trace.yaml and put in the working directory
  # of the module that wants to use the 'trace' function
  trace_cfg = YamlConfigurator 
  
  # the only parameter in the YAML, our HOME directory
  trace_cfg['HOME'] = path

  # load the YAML file with this
  begin
    trace_cfg.load_yaml_file(file)
  rescue
    raise "FATAL: cannot find trace.yaml file. aborting."
  end

end

#trace_named_put(name, msg) ⇒ Object



83
84
85
86
87
# File 'lib/rcs-common/trace.rb', line 83

def trace_named_put(name, msg)
  # create the named context
  # the %X{name} tag should appear in the YAML file if you want to see this traces
  MDC.put(name, msg)
end

#trace_named_remove(name) ⇒ Object



89
90
91
92
# File 'lib/rcs-common/trace.rb', line 89

def trace_named_remove(name)
  # remove the specified context
  MDC.remove(name)
end

#trace_nested_popObject



77
78
79
80
# File 'lib/rcs-common/trace.rb', line 77

def trace_nested_pop
  # pop the last context
  NDC.pop
end

#trace_nested_push(msg) ⇒ Object



71
72
73
74
75
# File 'lib/rcs-common/trace.rb', line 71

def trace_nested_push(msg)
  # push the context
  # the %x tag should appear in the YAML file if you want to see this traces
  NDC.push(msg)
end

#trace_setupObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rcs-common/trace.rb', line 25

def trace_setup
  pwd = File.expand_path(Dir.pwd)

  raise "FATAL: Invalid execution directory." unless File.directory?("#{pwd}/lib")

  trace_file_path = File.exist?(TRACE_YAML_NAME) ? TRACE_YAML_NAME : "#{pwd}/config/#{TRACE_YAML_NAME}"
  raise "FATAL: Unable to find #{TRACE_YAML_NAME} file" unless File.exist?(trace_file_path)

  trace_ensure_log_folders

  trace_cfg = YamlConfigurator

  # the only parameter in the YAML, our HOME directory
  trace_cfg['HOME'] = pwd

  # load the YAML file with this
  trace_cfg.load_yaml_file(trace_file_path)
rescue Exception => ex
  raise "FATAL: Unable to load #{TRACE_YAML_NAME}: #{ex.message}"
end