Class: Judges::AsciiLoog

Inherits:
Object show all
Defined in:
lib/judges/ascii_loog.rb

Overview

ASCII wrapper for Loog logging facility.

This class wraps any Loog logger and converts Unicode symbols to ASCII equivalents when the –ascii option is enabled.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024-2026 Yegor Bugayenko

License

MIT

Constant Summary collapse

UNICODE_TO_ASCII =

Unicode to ASCII symbol mapping

{
  '👍' => '+',
  '👎' => '-',
  '' => '!',
  '👉' => '>',
  '' => '+',
  '' => '!',
  '' => '>',
  '' => '<',
  '' => 'v',
  '' => '^'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(loog) ⇒ AsciiLoog

Initialize the ASCII wrapper.

Parameters:

  • loog (Loog)

    The original logging facility to wrap



33
34
35
# File 'lib/judges/ascii_loog.rb', line 33

def initialize(loog)
  @loog = loog
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

Delegate all other methods to the original logger.



80
81
82
# File 'lib/judges/ascii_loog.rb', line 80

def method_missing(method, *, &)
  @loog.send(method, *, &)
end

Instance Method Details

#debug(message) ⇒ Object

Log a debug message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



75
76
77
# File 'lib/judges/ascii_loog.rb', line 75

def debug(message)
  @loog.debug(to_ascii(message))
end

#error(message) ⇒ Object

Log an error message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



69
70
71
# File 'lib/judges/ascii_loog.rb', line 69

def error(message)
  @loog.error(to_ascii(message))
end

#info(message) ⇒ Object

Log an info message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



57
58
59
# File 'lib/judges/ascii_loog.rb', line 57

def info(message)
  @loog.info(to_ascii(message))
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Check if the original logger responds to a method.

Returns:

  • (Boolean)


85
86
87
# File 'lib/judges/ascii_loog.rb', line 85

def respond_to_missing?(method, include_private = false)
  @loog.respond_to?(method, include_private) || super
end

#to_ascii(message) ⇒ String

Convert Unicode symbols to ASCII equivalents.

Parameters:

  • message (String)

    The message to convert

Returns:

  • (String)

    The converted message with ASCII symbols



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/judges/ascii_loog.rb', line 40

def to_ascii(message)
  result = message.to_s
  if result.encoding != Encoding::UTF_8
    begin
      result = result.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
    rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError
      result = result.force_encoding(Encoding::UTF_8).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
    end
  end
  UNICODE_TO_ASCII.each do |unicode, ascii|
    result = result.gsub(unicode, ascii)
  end
  result
end

#warn(message) ⇒ Object

Log a warning message, converting Unicode to ASCII.

Parameters:

  • message (String)

    The message to log



63
64
65
# File 'lib/judges/ascii_loog.rb', line 63

def warn(message)
  @loog.warn(to_ascii(message))
end