File: logger.rb

package info (click to toggle)
ruby-logify 0.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 128 kB
  • sloc: ruby: 408; makefile: 5
file content (165 lines) | stat: -rw-r--r-- 3,548 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require 'monitor'

module Logify
  class Logger
    class << self
      #
      # @private
      #
      # @macro level
      #   @method $1(message = nil, &block)
      #     Write a new $1 message to the current IO object.
      #
      #     @example Write a +:$1+ message
      #       log.$1 'This is a message'
      #
      #     @example Write a lazy evaluated +:$1+ message
      #       log.$1 { perform_complex_operation }
      #
      #     @param [String] message
      #       the message to log
      #     @param [Proc] block
      #       the block to call that returns a string to write
      #
      #     @return [String]
      #       the compiled log message
      #
      def level(name)
        constant = name.to_s.upcase

        class_eval <<-EOH, __FILE__, __LINE__ + 1
          def #{name}(message = nil, &block)
            if Logify.level <= #{constant}
              buffer = ''

              if Logify.level == #{DEBUG}
                buffer << formatted_id
                buffer << SEPARATOR
                buffer << PREFIX_LONG_#{constant}
              else
                buffer << PREFIX_#{constant}
              end

              buffer << filter(message) if message
              buffer << filter(yield)   if block_given?
              buffer << "#{NEWLINE}"

              MONITOR.synchronize { Logify.io.write(buffer) }

              buffer
            end
          end
        EOH
      end
    end

    ANONYMOUS  = '(Anonymous)'
    FILTERED   = '[FILTERED]'
    MAX_LENGTH = 32
    NEWLINE    = "\n"
    SEPARATOR  = ' | '
    MONITOR    = Monitor.new

    NONE  = 5
    FATAL = 4
    ERROR = 3
    WARN  = 2
    INFO  = 1
    DEBUG = 0

    DEFAULT = WARN

    LEVEL_MAP = {
      none:  NONE,
      fatal: FATAL,
      error: ERROR,
      warn:  WARN,
      info:  INFO,
      debug: DEBUG,
    }.freeze

    PREFIX_FATAL = 'F: '
    PREFIX_ERROR = 'E: '
    PREFIX_WARN  = 'W: '
    PREFIX_INFO  = 'I: '
    PREFIX_DEBUG = 'D: '

    PREFIX_LONG_FATAL = '!!!! '
    PREFIX_LONG_ERROR = '>>>> '
    PREFIX_LONG_WARN  = '**** '
    PREFIX_LONG_INFO  = '===> '
    PREFIX_LONG_DEBUG = '     '

    level :fatal
    level :error
    level :warn
    level :info
    level :debug

    #
    # Create a new logger object.
    #
    # @param [String, nil] id
    #   the ID of the logger object to create
    #
    def initialize(id)
      @id = id
    end

    private

    #
    # @private
    #
    # The truncated id (for debug only).
    #
    # @return [String]
    #   the formatted id
    #
    def formatted_id
      return @formatted_id if @formatted_id

      # Account for anonymous classes
      id = @id ? @id.to_s : ANONYMOUS

      if id.length == MAX_LENGTH
        @formatted_id = id
      elsif id.length < MAX_LENGTH
        @formatted_id = id.rjust(MAX_LENGTH)
      else
        temp = id

        until temp.length <= MAX_LENGTH
          if temp.include?('::')
            temp = temp.split('::')[1..-1].join('::')
          else
            temp = id[-MAX_LENGTH..-1]
          end
        end

        @formatted_id = temp.rjust(MAX_LENGTH)
      end
    end

    #
    # @private
    #
    # Filter the given string of any filtered parameters.
    #
    # @see Logify.filter
    #
    # @param [String]
    #   the string to filter
    #
    # @return [String]
    #   the filtered string
    #
    def filter(string)
      string.dup.tap do |copy|
        Logify.filters.each do |param, _|
          copy.gsub!(param, FILTERED)
        end
      end
    end
  end
end