Class: FileSeries
- Inherits:
-
Object
- Object
- FileSeries
- Defined in:
- lib/file_series.rb,
lib/file_series/version.rb
Overview
FileSeries rubocop loves comments
Constant Summary collapse
- DEFAULT_DIR =
'.'- DEFAULT_PREFIX =
'log'- DEFAULT_FREQ =
60- DEFAULT_SEPARATOR =
"\n"- VERSION =
'0.7.0'
Instance Attribute Summary collapse
-
#current_ts ⇒ Object
Returns the value of attribute current_ts.
-
#dir ⇒ Object
Returns the value of attribute dir.
-
#file ⇒ Object
Returns the value of attribute file.
-
#separator ⇒ Object
Returns the value of attribute separator.
Class Method Summary collapse
-
.parse_filename(filename) ⇒ Object
extract the parts of a filename.
Instance Method Summary collapse
-
#complete_files ⇒ Object
get all files which match our pattern which are not current.
-
#each ⇒ Object
enumerate over all the writes in a series, across all files.
-
#filename(ts = nil) ⇒ Object
return a string filename for the logfile for the supplied timestamp.
-
#initialize(options = {}) ⇒ FileSeries
constructor
A new instance of FileSeries.
-
#log_file ⇒ Object
return a File object for the current log file.
- #parse_filename(filename) ⇒ Object
- #path ⇒ Object
-
#rotate(ts = nil) ⇒ Object
close current file handle and open a new one for a new logging period.
-
#this_period ⇒ Object
compute the current time period.
-
#write(message) ⇒ Object
write something to the current log file.
Constructor Details
#initialize(options = {}) ⇒ FileSeries
Returns a new instance of FileSeries.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/file_series.rb', line 33 def initialize( = {}) @dir = [:dir] || DEFAULT_DIR @file = nil @current_ts = nil @filename_prefix = [:prefix] || DEFAULT_PREFIX @rotate_freq = [:rotate_every] || DEFAULT_FREQ #seconds @binary_mode = [:binary] @separator = [:separator] || DEFAULT_SEPARATOR @sync = [:sync] || false end |
Instance Attribute Details
#current_ts ⇒ Object
Returns the value of attribute current_ts.
31 32 33 |
# File 'lib/file_series.rb', line 31 def current_ts @current_ts end |
#dir ⇒ Object
Returns the value of attribute dir.
29 30 31 |
# File 'lib/file_series.rb', line 29 def dir @dir end |
#file ⇒ Object
Returns the value of attribute file.
30 31 32 |
# File 'lib/file_series.rb', line 30 def file @file end |
#separator ⇒ Object
Returns the value of attribute separator.
28 29 30 |
# File 'lib/file_series.rb', line 28 def separator @separator end |
Class Method Details
.parse_filename(filename) ⇒ Object
extract the parts of a filename
87 88 89 90 91 92 93 94 95 |
# File 'lib/file_series.rb', line 87 def self.parse_filename(filename) base = File.basename(filename, '.log') prefix, date, time, duration = base.split('-') { prefix: prefix, start_time: Time.parse("#{date} #{time}").utc, duration: duration.to_i } end |
Instance Method Details
#complete_files ⇒ Object
get all files which match our pattern which are not current. (safe for consumption. no longer being written to.)
107 108 109 110 111 112 113 114 115 |
# File 'lib/file_series.rb', line 107 def complete_files current_file = filename Dir.glob( File.join(@dir, "#{@filename_prefix}-*-#{@rotate_freq}.log") ).select do |name| name != current_file end end |
#each ⇒ Object
enumerate over all the writes in a series, across all files.
118 119 120 121 122 123 124 |
# File 'lib/file_series.rb', line 118 def each complete_files.sort.each do |file| File.open(file, "r#{'b' if @binary_mode}").each_line(@separator) do |raw| yield raw end end end |
#filename(ts = nil) ⇒ Object
return a string filename for the logfile for the supplied timestamp. defaults to current time period.
changes to filename structure must be matched by changes to parse_filename
81 82 83 84 |
# File 'lib/file_series.rb', line 81 def filename(ts = nil) ts ||= this_period File.join(@dir, "#{@filename_prefix}-#{Time.at(ts).utc.strftime('%Y%m%d-%H%M%SZ')}-#{@rotate_freq}.log") end |
#log_file ⇒ Object
return a File object for the current log file.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/file_series.rb', line 50 def log_file ts = this_period # if we're in a new time period, start writing to new file. if (! file) || (ts != current_ts) rotate(ts) end file end |
#parse_filename(filename) ⇒ Object
97 98 99 |
# File 'lib/file_series.rb', line 97 def parse_filename(filename) self.class.parse_filename(filename) end |
#path ⇒ Object
101 102 103 |
# File 'lib/file_series.rb', line 101 def path filename end |
#rotate(ts = nil) ⇒ Object
close current file handle and open a new one for a new logging period. ts defaults to the current time period.
69 70 71 72 73 74 75 |
# File 'lib/file_series.rb', line 69 def rotate(ts = nil) ts ||= this_period @file.close if @file @file = File.open(filename(ts), "a#{'b' if @binary_mode}") @file.sync = @sync @current_ts = ts end |
#this_period ⇒ Object
compute the current time period.
62 63 64 65 |
# File 'lib/file_series.rb', line 62 def this_period t = Time.now.to_i t - (t % @rotate_freq) end |
#write(message) ⇒ Object
write something to the current log file.
45 46 47 |
# File 'lib/file_series.rb', line 45 def write() log_file.write(.to_s + @separator) end |