Class: Participle::LogQueue

Inherits:
Object
  • Object
show all
Includes:
Colors
Defined in:
lib/logger.rb

Overview

Runs a thread that slurps the latest log message off of a blocking queue and prints it out. This makes logging detached from the rest of the bot process, preventing slowdowns under heavy load.

Constant Summary

Constant Summary

Constants included from Colors

Colors::ANSI_COLORS_HEX, Colors::ANSI_COLORS_NAMED, Colors::ANSI_STYLES

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Colors

#ansi_colorize, #ansi_stylize, #chromaticied, #closest_to, #color_difference, #colorize, #embolden, #format_html, #format_user, #get_term_size, #italicize, #loggerify, #room_name, #timestamp, #to_hex, #unchrome, #underscore

Constructor Details

- (LogQueue) initialize(bot)

Start up the logger loop. Log messages are constantly popped off @queue and printed out; this allows messages to be added much faster than the bot can write logs, so no lag occurs.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/logger.rb', line 33

def initialize(bot)
  @bot = bot
  @loggers  = {}
  @queue    = Queue.new
  @shutdown = false
  @th       = Thread.new {
    loop {
      break if @shutdown && @queue.size.zero?
      msg = @queue.pop
      if String === msg
        puts(colorize("!!", :red) + msg)
      else
        loggerify(msg.type, msg.content, msg.room, msg.user).word_wrap(get_term_size).split(/\n/).each{|l|puts l}
        begin
          @loggers[msg.room] << loggerify(msg.type, msg.content, msg.room, msg.user).remove_ansi_colors
        rescue
          # no logger is set up, do something here i suppose
        end
      end
    }
  }
end

Instance Attribute Details

- (Boolean) pretty_display

Returns whether we are using colors or not

Returns:

  • (Boolean)

    whether we are using colors or not



16
17
18
# File 'lib/logger.rb', line 16

def pretty_display
  @pretty_display
end

- (Boolean) shutdown=(value) (writeonly)

Set to true to terminate the logging loop once all logs have been flushed.

Returns:

  • (Boolean)


14
15
16
# File 'lib/logger.rb', line 14

def shutdown=(value)
  @shutdown = value
end

Instance Method Details

- (void) add_logger(room)

This method returns an undefined value.

Add a logger to the loop.

Parameters:

  • room (String)

    the room for which we shall add a logger



59
60
61
62
63
# File 'lib/logger.rb', line 59

def add_logger room
  Dir.mkdir("log") unless File.directory?("log")
  open("log/#{room}.txt", "a").close
  @loggers[room] = Logger.new("log/#{room}.txt")
end

- (void) error(line)

This method returns an undefined value.

Push an error message onto the queue.

Parameters:

  • line (String)

    the error message to display



83
84
85
# File 'lib/logger.rb', line 83

def error line
  @queue << line
end

- (void) flush

This method returns an undefined value.

Log waiting messages and shut down.



89
90
91
92
# File 'lib/logger.rb', line 89

def flush
  @shutdown = true
  @th.join
end

- (void) log(type, line, room, username = nil)

This method returns an undefined value.

Push a log message onto the queue.

Parameters:

  • line (String)

    the message to queue

  • room (String)

    the room for which to queue the line



76
77
78
# File 'lib/logger.rb', line 76

def log type, line, room, username = nil
  @queue << LogMessage.new(type, line, room, username)
end

- (void) puts(line)

This method returns an undefined value.

Replacement for Kernel.puts to replace the prompt after each line

Parameters:

  • line (String)

    the line to print



22
23
24
25
26
27
28
29
30
# File 'lib/logger.rb', line 22

def puts line
  if $opts[:prompt] && RUBY_PLATFORM =~ /linux|darwin/
    print "\e[1K\e[200D"
    Kernel.puts line
    print "\e[1000D\e[K##{@bot.current_room}> #{@bot.__cli_buffer}"
  else
    Kernel.puts line
  end
end

- (void) remove_logger(room)

This method returns an undefined value.

Remove a logger from the loop.

Parameters:

  • room (String)

    the room to remove



68
69
70
# File 'lib/logger.rb', line 68

def remove_logger room
  @loggers.delete(room)
end