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 <<~HELP

#{Narou.log_dir} に保存されたログを表示します。
    ・logging 設定が有効な場合のみ、ログは保存されます。
    ・<path> を指定しない場合、最新のログが表示されます。
    ・デフォルトでは末尾#{NUM}行のログを表示します。

      Examples:
        narou s logging=true # ログの保存を有効にする

        narou log
        narou log -n 100     # 100行分のログを表示
        narou log -t         # tail -f オプションのようにログを流し続ける

        narou log log/narou.txt # 直接ファイルを指定可能

        # concurrency 設定(更新・変換同時実行)が有効時は変換ログが別ファイルに
        # 分かれるので、変換ログを表示したい場合は -c オプションを付ける
        # (指定しなかった場合は通常ログの方を表示)
        narou log -c         # 変換ログを表示

      Options:
  HELP
  @opt.on("-n", "--num NUM", Integer, "表示する行数を指定する") do |num|
    @options["num"] = num
  end
  @opt.on("-t", "--tail", "ログを流し続ける") do
    @options["tail"] = true
  end
  @opt.on("-c", "--source-convert", "変換ログを表示する。<path> を直接指定した場合は無視") do
    @options["source-convert"] = true
  end
end

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