Class: Command::Log

Inherits:
CommandBase show all
Defined in:
lib/command/log.rb,
lib/command/log/tail.rb

Defined Under Namespace

Classes: Tail

Constant Summary collapse

NUM =
20

Instance Attribute Summary

Attributes inherited from CommandBase

#stream_io

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

#disable_logging, #display_help!, execute!, #execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeLog



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
# File 'lib/command/log.rb', line 17

def initialize
  super("[options] [<path>]")
  @opt.separator "\n    \u30FB\#{Narou.log_dir} \u306B\u4FDD\u5B58\u3055\u308C\u305F\u30ED\u30B0\u3092\u8868\u793A\u3057\u307E\u3059\u3002\n    \u30FBlogging \u8A2D\u5B9A\u304C\u6709\u52B9\u306A\u5834\u5408\u306E\u307F\u3001\u30ED\u30B0\u306F\u4FDD\u5B58\u3055\u308C\u307E\u3059\u3002\n    \u30FB<path> \u3092\u6307\u5B9A\u3057\u306A\u3044\u5834\u5408\u3001\u6700\u65B0\u306E\u30ED\u30B0\u304C\u8868\u793A\u3055\u308C\u307E\u3059\u3002\n    \u30FB\u30C7\u30D5\u30A9\u30EB\u30C8\u3067\u306F\u672B\u5C3E\#{NUM}\u884C\u306E\u30ED\u30B0\u3092\u8868\u793A\u3057\u307E\u3059\u3002\n\n      Examples:\n        narou s logging=true # \u30ED\u30B0\u306E\u4FDD\u5B58\u3092\u6709\u52B9\u306B\u3059\u308B\n\n        narou log\n        narou log -n 100     # 100\u884C\u5206\u306E\u30ED\u30B0\u3092\u8868\u793A\n        narou log -t         # tail -f \u30AA\u30D7\u30B7\u30E7\u30F3\u306E\u3088\u3046\u306B\u30ED\u30B0\u3092\u6D41\u3057\u7D9A\u3051\u308B\n\n        narou log log/narou.txt # \u76F4\u63A5\u30D5\u30A1\u30A4\u30EB\u3092\u6307\u5B9A\u53EF\u80FD\n\n        # concurrency \u8A2D\u5B9A\uFF08\u66F4\u65B0\u30FB\u5909\u63DB\u540C\u6642\u5B9F\u884C\uFF09\u304C\u6709\u52B9\u6642\u306F\u5909\u63DB\u30ED\u30B0\u304C\u5225\u30D5\u30A1\u30A4\u30EB\u306B\n        # \u5206\u304B\u308C\u308B\u306E\u3067\u3001\u5909\u63DB\u30ED\u30B0\u3092\u8868\u793A\u3057\u305F\u3044\u5834\u5408\u306F -c \u30AA\u30D7\u30B7\u30E7\u30F3\u3092\u4ED8\u3051\u308B\n        # \uFF08\u6307\u5B9A\u3057\u306A\u304B\u3063\u305F\u5834\u5408\u306F\u901A\u5E38\u30ED\u30B0\u306E\u65B9\u3092\u8868\u793A\uFF09\n        narou log -c         # \u5909\u63DB\u30ED\u30B0\u3092\u8868\u793A\n\n      Options:\n  HELP\n  @opt.on(\"-n\", \"--num NUM\", Integer, \"\u8868\u793A\u3059\u308B\u884C\u6570\u3092\u6307\u5B9A\u3059\u308B\") do |num|\n    @options[\"num\"] = num\n  end\n  @opt.on(\"-t\", \"--tail\", \"\u30ED\u30B0\u3092\u6D41\u3057\u7D9A\u3051\u308B\") do\n    @options[\"tail\"] = true\n  end\n  @opt.on(\"-c\", \"--source-convert\", \"\u5909\u63DB\u30ED\u30B0\u3092\u8868\u793A\u3059\u308B\u3002<path> \u3092\u76F4\u63A5\u6307\u5B9A\u3057\u305F\u5834\u5408\u306F\u7121\u8996\") do\n    @options[\"source-convert\"] = true\n  end\nend\n"

Class Method Details

.oneline_helpObject



13
14
15
# File 'lib/command/log.rb', line 13

def self.oneline_help
  "保存したログを表示します"
end

Instance Method Details

#execute(argv) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/command/log.rb', line 53

def execute(argv)
  disable_logging
  super
  path = resolve_path(argv.first) || latest_log_path
  unless path
    error "表示できるログファイルが一つも見つかりませんでした"
    exit Narou::EXIT_ERROR_CODE
  end
  stream_io.puts "<cyan>#{path}</cyan>".termcolor
  tail = Tail.new(path, @options["num"] || NUM)
  tail.stream_io = stream_io
  if @options["tail"]
    tail.stream
  else
    tail.display
  end
rescue Interrupt
  stream_io.puts
end

#latest_log_pathObject



73
74
75
76
77
# File 'lib/command/log.rb', line 73

def latest_log_path
  Narou.log_dir.glob("*").sort_by(&:mtime).reverse.find do |path|
    path_matches_source?(path)
  end
end

#path_matches_source?(path) ⇒ Boolean



79
80
81
82
83
84
85
86
87
88
# File 'lib/command/log.rb', line 79

def path_matches_source?(path)
  ext = path.extname
  base = path.basename(ext)
  ended_with_convert = base.to_s.end_with?("_convert")
  if @options["source-convert"]
    ended_with_convert
  else
    !ended_with_convert
  end
end

#resolve_path(path) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/command/log.rb', line 90

def resolve_path(path)
  return nil if path.blank?
  fullpath = Pathname(path).expand_path
  unless fullpath.exist?
    error "#{path} が存在しません"
    exit Narou::EXIT_ERROR_CODE
  end
  fullpath
end