Sha256: 03004f7ad2683aa054d6606ca416a230e33b1bb044ad3a4d5f499ba7bd770966

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

class Wagn::Log  
  TAB_SIZE = 2
  @@log = []
  @@last_toplevel_card = nil
  
  class << self
    
    # args: 
    # :method  => :view|:event|:fetch|:search
    # :cardname, :message, :details
    def start_block args  
      level = @@log.last ? @@log.last[:level] + 1 : 1
      @@log << args.merge( :start => Time.now, :level=>level, :subtree =>[] )
    end
    
    def finish_block
      log = @@log.pop
      duration = (Time.now - log[:start]) * 1000
      return if limit = Wagn.config.performance_logger[:limit] and limit > 0 and duration < limit
        
      log_msg = "#{ indent log[:level] }(%d.2ms) #{ log[:method] }: #{ log[:message] }" % duration
      log_msg += details log if Wagn.config.performance_logger[:details] 
      log_msg += subtree log
      
      if log_parent = @@log.last
        if sibling = log_parent[:subtree].last and ( sibling[:card] == log[:cardname] or not log[:cardname] )
          sibling[:lines] << log_msg
        else
          log_parent[:subtree] << {:card=>log[:cardname], :lines=>[log_msg]}
        end
      else
        if log[:cardname] and @@last_toplevel_card != log[:cardname]
          @@last_toplevel_card = log[:cardname]
          Rails.logger.wagn log[:cardname]
        end
        Rails.logger.wagn log_msg
      end
    end
    
    
    def indent level, args={}
       res = (' '*TAB_SIZE + '|') * level
       res += args[:no_link] ? '  ' : '--'
    end
    
    def details log
      if log[:details]
        ", " + log[:details].to_s.gsub( "\n", "\n#{ indent( log[:level]+1, :no_link=>true) }" )  
      else
        ''
      end
    end
    
    def subtree log
      if log[:subtree].present?
        "\n" + log[:subtree].map do |subentry| 
            msg = subentry[:card] ? "#{ indent(log[:level]) }#{ subentry[:card] }\n" : ''        
            msg += subentry[:lines].join("\n")
          end.join("\n")
      else
        ''
      end
    end
    
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wagn-1.14.6 lib/wagn/log.rb
wagn-1.14.5 lib/wagn/log.rb
wagn-1.14.4 lib/wagn/log.rb
wagn-1.14.3 lib/wagn/log.rb
wagn-1.14.2 lib/wagn/log.rb