Module: Bunnish::Core::Common

Defined in:
lib/bunnish/core/common.rb

Class Method Summary collapse

Class Method Details

.output_log(streams, log_level, message) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bunnish/core/common.rb', line 124

def self.output_log(streams, log_level, message)
  case log_level
  when "INFO"
    Bunnish.logger.info(message)
  when "EXCEPTION"
    Bunnish.logger.warn(message)
  end
  message =  Time.now.strftime("[%Y-%m-%d %H:%M:%S](#{log_level})#{message}")
  streams.each do |stream|
    if stream then
      stream.puts message
      stream.flush
    end
  end
end

.parse_opts(argv) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/bunnish/core/common.rb', line 3

def self.parse_opts(argv)
  host = 'localhost'
  port = 5672
  user = 'guest'
  password = 'guest'
  durable = false
  ack = true
  raise_exception_flag = false
  
  exchange_name = nil
  delimiter = nil
  
  consumer_tag = nil
  exclusive = false
  message_max = nil
  timeout = 1
  unit_size = nil
  min_size = nil
  current_all_flag = false
  
  warn_size = nil
  error_size = nil
  
  log_label = nil
  log_dir = nil
  log_path = nil
  
  verbose_flag = false
  
  next_argv = []
  
  while 0 < argv.size do
    val = argv.shift
    case val
    when '-h'
      host = argv.shift
    when '-p'
      port = argv.shift.to_i
    when '-u'
      user = argv.shift
    when '-P'
      password = argv.shift
    when '--ack'
      ack = (argv.shift == 't')
    when '--delimiter'
      delimiter = argv.shift
    when '--durable'
      durable = (argv.shift == 't')
    when '--exchange-name'
      exchange_name = argv.shift
    when '--unit-size'
      unit_size = argv.shift.to_i
    when '--log-label'
      log_label = argv.shift
      log_label = "[#{log_label}]"
    when '--log-dir'
      log_dir = argv.shift
    when '--log-file'
      log_path = argv.shift
    when '--raise-exception'
      raise_exception_flag = true
    when '--consumer-tag'
      consumer_tag = argv.shift
    when '--timeout'
      timeout = argv.shift.to_i
    when '--exclusive'
      exclusive = (argv.shift == 't')
    when '--message-max'
      message_max = argv.shift
    when '--current-all'
      current_all_flag = true
    when '--min-size'
      min_size = argv.shift.to_i
    when '--empty-list-max'
      empty_list_max = argv.shift.to_i
    when '--warn'
      warn_size = argv.shift.to_i
    when '--error'
      error_size = argv.shift.to_i
    when '--verbose'
      verbose_flag = true
    else 
      next_argv.push val
    end
  end
  argv.push(*next_argv)
  
  if verbose_flag then
    Bunnish.logger.level = Logger::INFO
  else
    Bunnish.logger.level = Logger::WARN
  end
  
  return {
    :host=>host,
    :port=>port,
    :user=>user,
    :password=>password,
    :durable=>durable,
    :ack=>ack,
    :exchange_name=>exchange_name,
    :unit_size=>unit_size,
    :raise_exception_flag=>raise_exception_flag,
    :delimiter=>delimiter,
    :log_label=>log_label,
    :log_dir=>log_dir,
    :log_path=>log_path,
    :consumer_tag=>consumer_tag,
    :timeout=>timeout,
    :exclusive=>exclusive,
    :message_max=>message_max,
    :current_all_flag=>current_all_flag,
    :min_size=>min_size,
    :empty_list_max=>empty_list_max,
    :warn_size=>warn_size,
    :error_size=>error_size,
    :verbose_flag=>verbose_flag
  }
  
end