Sha256: 44dd4b337557e095cb38b79bc2dd273c6b5f4481695df6316c6a139f53379245

Contents?: true

Size: 1.76 KB

Versions: 3

Compression:

Stored size: 1.76 KB

Contents

require_relative 'extension_constants'
require WEBCONSOLE_FILE

module WebConsole

  class Logger
    MESSAGE_PREFIX = 'MESSAGE '
    ERROR_PREFIX = 'ERROR '
    LOG_PLUGIN_NAME = 'Log'

    # Toggle

    SHOW_LOG_SCRIPT = File.join(APPLESCRIPT_DIRECTORY, "show_log.scpt")
    def show
      WebConsole::run_applescript(SHOW_LOG_SCRIPT, [window_id])
    end
    
    HIDE_LOG_SCRIPT = File.join(APPLESCRIPT_DIRECTORY, "hide_log.scpt")
    def hide
      WebConsole::run_applescript(HIDE_LOG_SCRIPT, [window_id])
    end
    
    TOGGLE_LOG_SCRIPT = File.join(APPLESCRIPT_DIRECTORY, "toggle_log.scpt")
    def toggle
      WebConsole::run_applescript(TOGGLE_LOG_SCRIPT, [window_id])
    end

    # Messages

    def info(message)
      message = message.dup
      message.gsub!(%r{^}, MESSAGE_PREFIX) # Prefix all lines
      # Strip trailing white space
      # Add a line break
      log_message(message)
    end

    def error(message)
      message = message.dup
      message.gsub!(%r{^}, ERROR_PREFIX)
      log_message(message)
    end

    # Properties

    def window_id
      @window_id ||= ENV.has_key?(WINDOW_ID_KEY) ? ENV[WINDOW_ID_KEY] : WebConsole::create_window
    end
  
    def view_id
      @view_id ||= WebConsole::split_id_in_window(window_id, LOG_PLUGIN_NAME)
      return @view_id unless @view_id.nil?

      @view_id = WebConsole::split_id_in_window_last(window_id)
      WebConsole::run_plugin_in_split(LOG_PLUGIN_NAME, window_id, @view_id)
      @view_id  
    end

    private

    READ_FROM_STANDARD_INPUT_SCRIPT = File.join(APPLESCRIPT_DIRECTORY, "read_from_standard_input.scpt")  
    def log_message(message)
      message.rstrip!
      message += "\n"
      WebConsole::run_applescript(READ_FROM_STANDARD_INPUT_SCRIPT, [message, window_id, view_id])
    end
  
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
webconsole-0.2.1 lib/webconsole/logger.rb
webconsole-0.2.0 lib/webconsole/logger.rb
webconsole-0.1.19 lib/webconsole/logger.rb