Class: Pagoda::Command::Log

Inherits:
Base show all
Defined in:
lib/pagoda/cli/helpers/log.rb

Constant Summary collapse

COLORS =
[
'light_green',
'light_red',
'light_yellow',
'light_blue',
'light_magenta',
'light_cyan',
'white',
'red',
'yellow',
'blue',
'magenta',
'cyan',
'green'
]

Constants included from Helpers

Helpers::INDENT

Instance Attribute Summary

Attributes inherited from Base

#args, #client, #globals, #options

Instance Method Summary collapse

Methods inherited from Base

#app, ask_for_credentials, #branch, #commit, #extract_app_from_git_config, #extract_app_from_remote, #extract_git_clone_url, #find_branch, #find_commit, #git_remotes, #home_dir, #initialize, #locate_app_root, #loop_transaction, #password, #remote, #shell, #user

Methods included from Helpers

#build_indent, #confirm, #create_git_remote, #display, #display_name, #error, #format_date, #git, #has_git?, #home_directory, #remove_app, #remove_git_remote, #running_on_a_mac?, #running_on_windows?

Constructor Details

This class inherits a constructor from Pagoda::Command::Base

Instance Method Details

#colorize(message, name, newline = true) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/pagoda/cli/helpers/log.rb', line 94

def colorize message, name, newline = true
  @hash ||= {}
  if color = @hash[name]
    newline ? (puts message.send(color)) : (print message.send(color))
  else
    newline ? (puts message.send(@hash[name] = next_color)) : (print message.send(@hash[name] = next_color))
    # retry  
  end
end

#next_colorObject



104
105
106
# File 'lib/pagoda/cli/helpers/log.rb', line 104

def next_color
  COLORS[@hash.length % COLORS.length]
end

#output_errorObject



108
109
110
111
112
113
114
# File 'lib/pagoda/cli/helpers/log.rb', line 108

def output_error
  errors = []
  errors << "Input unrecoginized"
  errors << "try 'pagoda -a [appname] log [component]'"
  errors << "ie. 'pagoda -a app log db1'"
  error errors
end

#runObject



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
# File 'lib/pagoda/cli/helpers/log.rb', line 26

def run
  
  app_id = client.app_info(app)[:id]
  if options[:live]
    STDOUT.sync = true
    socket = WebSocket::Client::Simple.connect("wss://live.pagodabox.com/stormpack/websocket/pagodabox?protocol=1&client=ruby&version=1.0.0&batch=false&adapter=json&flash=false&authorization=Basic%20#{Base64.encode64("#{user}:#{password}").chop.gsub(/\=/, '%3D')}")

    socket.on :open do
      socket.send({"event"=>"stormpack:subscribe","data"=>{"channel"=>"app-log-#{app_id}"}}.to_json, {:type => :binary})
      puts "Now tailing your live stream"
      puts "press 'ctrl+c to exit"
      puts "----------------------------"
    end        

    socket.on :message do |msg|
      data = JSON.parse(msg.to_s)
      if (data['keypath'] rescue false)
        @cla ||= Pagoda::Command::Log.new(nil, nil, nil)
        @cla.colorize "[#{Time.at(data['data']['time'].to_i / 1000000) rescue Time.now}] #{(data['data']['log'].split(':').first rescue '')} : ", (data['data']['log'].split(':').first rescue 'str'), false
        puts data['data']['log'].to_s.split(':')[1..-1].join(":").strip
      end
    end

    socket.on :close do |e|
      p e
      exit 1
    end

    [:INT, :TERM].each do |sig|
      Signal.trap(sig) do
        STDOUT.sync = false
        # socket.close 1000, 'Going away'
        puts "Log closing."
        puts "----------------------------"
        puts
        exit 1
      end
    end
    sleep
  else
    count = (options[:count] || 100).to_i

    log = []
    last_time_stamp = Time.now.to_i * 100_000_000
    while log.length < count
      # puts "****"
      # puts "#{app}?start=#{last_time_stamp + 1}&stop=#{Time.now.to_i * 1000000}"
      # puts "****"

      r = JSON.parse(RestClient.get("https://#{user}:#{password}@logvac.pagodabox.com/logs/apps/#{app_id}?limit=#{count - log.length}&start=#{last_time_stamp -  1}&stop=#{0}"))
      if r == []
        break
      else
        last_time_stamp = r.last['time']
        log = log + r
      end
      print " loading... (#{log.length} of #{count})                 \r"
    end
    puts
    log.reverse.each do |line|
      colorize("[#{Time.at(line['time'].to_i / 1000000)}] #{line['log'].split(':').first} : ", line['log'].split(':').first, false)
      puts line['log'].split(':')[1..-1].join(":").strip
    end
  end


end