Class: Cornucopia::Util::LogCapture

Inherits:
Object
  • Object
show all
Defined in:
lib/cornucopia/util/log_capture.rb

Constant Summary collapse

TAIL_BUF_LENGTH =
1 << 16

Class Method Summary collapse

Class Method Details

.capture_logs(report_table) ⇒ Object

This function will capture the logs and output them to the report



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
# File 'lib/cornucopia/util/log_capture.rb', line 11

def capture_logs(report_table)
  if report_table
    if Object.const_defined?("Rails")
      log_folder = Rails.root.to_s
      if (log_folder =~ /\/features\/?$/ || log_folder =~ /\/spec\/?$/)
        log_folder = File.join(log_folder, "../")
      end

      default_log_file = "log/#{Rails.env.to_s}.log"

      output_log_file(report_table, File.join(log_folder, default_log_file))
    else
      log_folder = FileUtils.pwd
    end

    Cornucopia::Util::Configuration.user_log_files.each do |relative_log_file, options|
      output_log_file(report_table, File.join(log_folder, relative_log_file), options)
    end
  else
    Cornucopia::Util::ReportBuilder.current_report.within_section("Log Dump:") do |report|
      report.within_table do |new_report_table|
        Cornucopia::Util::LogCapture.capture_logs new_report_table
      end
    end
  end
end

.highlight_log_output(log_text) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cornucopia/util/log_capture.rb', line 38

def highlight_log_output(log_text)
  output_text = Cornucopia::Util::ReportBuilder.format_code_refs(log_text)
  output_text = output_text.gsub(/^(Completed [^23].*)$/, "<span class=\"completed-error\">\\1<\/span>")
  output_text = output_text.gsub(/^(Completed [23].*)$/, "<span class=\"completed-other\">\\1<\/span>")

  output_text.html_safe
end

.output_log_file(report_table, log_file_name, options = {}) ⇒ Object

A cheap and sleazy tail function, but it should work...



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
# File 'lib/cornucopia/util/log_capture.rb', line 47

def output_log_file(report_table, log_file_name, options = {})
  if File.exists?(log_file_name)
    output_file = false

    options.reverse_merge!({ num_lines: Cornucopia::Util::Configuration.num_lines })

    num_lines  = options[:num_lines] || Cornucopia::Util::Configuration.num_lines
    num_lines  = Cornucopia::Util::Configuration.num_lines if num_lines <= 0
    log_buffer = ""
    file_size  = File.size(log_file_name)

    File.open(log_file_name) do |log_file|
      seek_len = [file_size, TAIL_BUF_LENGTH].min
      log_file.seek -seek_len, IO::SEEK_END

      while (log_buffer.count("\n") <= num_lines)
        log_buffer = log_file.read(seek_len) + log_buffer

        file_size -= seek_len
        seek_len  = [file_size, TAIL_BUF_LENGTH].min

        break if seek_len <= 0

        log_file.seek -seek_len - TAIL_BUF_LENGTH, IO::SEEK_CUR
      end
    end

    if log_buffer
      log_buffer = log_buffer.split("\n")
      if (log_buffer.length > num_lines)
        log_buffer = log_buffer[-num_lines..-1]
      end

      report_table.write_stats File.basename(log_file_name),
                               Cornucopia::Util::PrettyFormatter.format_string(
                                   Cornucopia::Util::LogCapture.highlight_log_output(
                                       "log_file - #{log_file_name}:#{file_size}\n#{log_buffer.join("\n")}"
                                   )
                               ),
                               do_not_pretty_print: true

      output_file = true
    end

    output_file
  end
end