Class: Cornucopia::Util::LogCapture
- Inherits:
-
Object
- Object
- Cornucopia::Util::LogCapture
- Defined in:
- lib/cornucopia/util/log_capture.rb
Constant Summary collapse
- TAIL_BUF_LENGTH =
1 << 16
Class Method Summary collapse
- .backup_log_files(backup_folder) ⇒ Object
-
.capture_logs(report_table) ⇒ Object
This function will capture the logs and output them to the report.
- .copy_log_file(dest_folder, source_file) ⇒ Object
- .highlight_log_output(log_text) ⇒ Object
-
.output_log_file(report_table, log_file_name, options = {}) ⇒ Object
A cheap and sleazy tail function, but it should work...
Class Method Details
.backup_log_files(backup_folder) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cornucopia/util/log_capture.rb', line 10 def backup_log_files(backup_folder) 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" copy_log_file backup_folder, File.join(log_folder, default_log_file) else log_folder = FileUtils.pwd end Cornucopia::Util::Configuration.user_log_files.each do |relative_log_file, | copy_log_file backup_folder, File.join(log_folder, relative_log_file) end end |
.capture_logs(report_table) ⇒ Object
This function will capture the logs and output them to the report
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 |
# File 'lib/cornucopia/util/log_capture.rb', line 47 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, | output_log_file(report_table, File.join(log_folder, relative_log_file), ) 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 |
.copy_log_file(dest_folder, source_file) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cornucopia/util/log_capture.rb', line 29 def copy_log_file(dest_folder, source_file) extension = File.extname(source_file) file_name = File.basename(source_file, extension) dest_name = File.join(dest_folder, "#{file_name}#{extension}") index = 0 while File.exists?(dest_name) index += 1 dest_name = File.join(dest_folder, "#{file_name}_#{index}#{extension}") end if File.exists?(source_file) FileUtils.mkdir_p File.dirname(dest_name) FileUtils.cp source_file, dest_name end end |
.highlight_log_output(log_text) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/cornucopia/util/log_capture.rb', line 74 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...
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 123 124 125 126 127 128 129 |
# File 'lib/cornucopia/util/log_capture.rb', line 83 def output_log_file(report_table, log_file_name, = {}) if File.exists?(log_file_name) output_file = false .reverse_merge!({ num_lines: Cornucopia::Util::Configuration.num_lines }) num_lines = [: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 |