Class: GreeLog

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-object/gree_logger.rb

Constant Summary collapse

@@logger =
nil
@@echo_to_screen =
false

Class Method Summary collapse

Class Method Details

.closeObject



11
12
13
14
# File 'lib/rest-object/gree_logger.rb', line 11

def self.close
    @@logger.close
    @@logger = nil
end

.current_function_nameObject

Returns the name of the function from which this method was called, by looking at the call stack. If call stack is empty, returns “(anonymous)”

def foo
    puts current_function_name
end

foo -> "foo"

current_function_name -> "(anonymous)"

Used for logging and error reporting, so that generic code can record the name of the currently running function.



70
71
72
# File 'lib/rest-object/gree_logger.rb', line 70

def self.current_function_name
    return caller()[0] =~ /in `([^']+)'/ ? $1 : '(anonymous)'
end

.echo_to_screenObject



16
17
18
# File 'lib/rest-object/gree_logger.rb', line 16

def self.echo_to_screen
    return @@echo_to_screen
end

.echo_to_screen=(value) ⇒ Object



20
21
22
# File 'lib/rest-object/gree_logger.rb', line 20

def self.echo_to_screen=(value)
    @@echo_to_screen=value
end

.loggerObject



24
25
26
# File 'lib/rest-object/gree_logger.rb', line 24

def self.logger
    return @@logger
end

.logger=(logger_object) ⇒ Object



28
29
30
# File 'lib/rest-object/gree_logger.rb', line 28

def self.logger=(logger_object)
    @@logger=logger_object
end

.logging?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rest-object/gree_logger.rb', line 32

def self.logging?
    return !@@logger.nil?
end

.method_missing(method_name, *method_args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rest-object/gree_logger.rb', line 36

def self.method_missing(method_name, *method_args, &block)
    if @@echo_to_screen
        puts "[#{Time.now.strftime('%H:%M:%S')}]  #{method_name.to_s.upcase} -- #{previous_function_name}: #{method_args.inspect} #{(block ? '{' + block.to_s + '}' : '')}"
    end
    if @@logger
        @@logger.progname = previous_function_name
        @@logger.send(method_name, *method_args, &block)
    end
end

.previous_function_nameObject

Returns the name of the function two levels down in the call stack. If call stack has insufficient depth, returns “(anonymous)”

def foo
    puts previous_function_name
end

def bar
    foo
end

bar -> "bar"

foo -> "(anonymous)"

previous_function_name => "(anonymous)"

Used for setting up a centralized error handler that calls a centralized logger.



93
94
95
96
97
98
99
# File 'lib/rest-object/gree_logger.rb', line 93

def self.previous_function_name
    if caller().length < 2
        return '(anonymous)'
    else
        return caller()[1] =~ /in `([^']+)'/ ? $1 : '(anonymous)'
    end
end

.puts(message) ⇒ Object

print out a message without escaping the the quotes, newlines …



47
48
49
50
51
52
53
54
55
# File 'lib/rest-object/gree_logger.rb', line 47

def self.puts(message)
    if @@echo_to_screen
       Kernel.puts "#{message}"
    end
    if @@logger
        @@logger.progname = previous_function_name
        @@logger.send("info", message)
    end
end