Class: FtgLogger
- Inherits:
-
Object
- Object
- FtgLogger
- Defined in:
- lib/ftg/ftg_logger.rb
Instance Method Summary collapse
- #add_log(command, task) ⇒ Object
- #get_logs ⇒ Object
- #get_unclosed_logs ⇒ Object
-
#initialize(ftg_dir) ⇒ FtgLogger
constructor
A new instance of FtgLogger.
- #on_pause? ⇒ Boolean
- #remove_all_logs ⇒ Object
- #remove_logs(name) ⇒ Object
- #update_current ⇒ Object
Constructor Details
#initialize(ftg_dir) ⇒ FtgLogger
Returns a new instance of FtgLogger.
3 4 5 6 |
# File 'lib/ftg/ftg_logger.rb', line 3 def initialize(ftg_dir) @ftg_dir = ftg_dir @log_file = "#{ftg_dir}/log/ftg.log" end |
Instance Method Details
#add_log(command, task) ⇒ Object
8 9 10 11 |
# File 'lib/ftg/ftg_logger.rb', line 8 def add_log(command, task) lines = [command, task, Time.now.getutc.to_i] `echo "#{lines.join('\t')}" >> #{@log_file}` end |
#get_logs ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/ftg/ftg_logger.rb', line 33 def get_logs File.open(@log_file, File::RDONLY|File::CREAT) do |file| file.read.split("\n").map do |e| parts = e.split("\t") { command: parts[0], task_name: parts[1], timestamp: parts[2] } end end end |
#get_unclosed_logs ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/ftg/ftg_logger.rb', line 47 def get_unclosed_logs unclosed_logs = [] closed = {} get_logs.reverse.each do |log| if log[:command] == 'ftg_stop' closed[log[:task_name]] = true end if log[:command] == 'ftg_start' && !closed[log[:task_name]] unclosed_logs << log end end unclosed_logs end |
#on_pause? ⇒ Boolean
42 43 44 45 |
# File 'lib/ftg/ftg_logger.rb', line 42 def on_pause? unclosed_logs = get_unclosed_logs unclosed_logs[0] && unclosed_logs[0][:task_name] == 'pause' end |
#remove_all_logs ⇒ Object
13 14 15 |
# File 'lib/ftg/ftg_logger.rb', line 13 def remove_all_logs `echo "" > #{@log_file}` end |
#remove_logs(name) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ftg/ftg_logger.rb', line 17 def remove_logs(name) count = 0 logs = get_logs logs.keep_if do |log| cond = log[:task_name] != name || log[:timestamp].to_i <= Time.now.to_i - 24*3600 count += 1 unless cond cond end File.open(@log_file, 'w') do |f| f.write(logs.map{|l| l.values.join("\t")}.join("\n") + "\n") end puts "Removed #{count} entries" end |
#update_current ⇒ Object
61 62 63 64 65 66 |
# File 'lib/ftg/ftg_logger.rb', line 61 def update_current current = '' current_logs = get_unclosed_logs current = current_logs[0][:task_name] unless current_logs.empty? `echo "#{current}" > #{@ftg_dir}/current.txt` end |