Class: OpenC3::MessageLog

Inherits:
Object show all
Defined in:
lib/openc3/utilities/message_log.rb

Overview

Handles writing message logs to a file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_name, log_dir, tags: ['messages'], scope:) ⇒ MessageLog

Returns a new instance of MessageLog.

Parameters:

  • tool_name (String)

    The name of the tool creating the message log. This will be inserted into the message log filename to help identify it.

  • log_dir (String)

    The filesystem path to store the message log file.

  • tags (Array<String>) (defaults to: ['messages'])

    Array of strings to put into the filename



40
41
42
43
44
45
46
47
48
# File 'lib/openc3/utilities/message_log.rb', line 40

def initialize(tool_name, log_dir, tags: ['messages'], scope:)
  @remote_log_directory = "#{scope}/tool_logs/#{tool_name}/"
  @tags = tags.unshift(tool_name)
  @log_dir = log_dir
  @filename = ''
  @file = nil
  @start_day = nil
  @mutex = Mutex.new
end

Instance Attribute Details

#filenameString (readonly)

Returns The name of the message log file. Empty string until the write or start methods are called at which point it is set to the filename. Retains the last filename even after stop is called.

Returns:

  • (String)

    The name of the message log file. Empty string until the write or start methods are called at which point it is set to the filename. Retains the last filename even after stop is called.



34
35
36
# File 'lib/openc3/utilities/message_log.rb', line 34

def filename
  @filename
end

Instance Method Details

#start(take_mutex = true) ⇒ Object

Creates a new message log and sets the filename



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openc3/utilities/message_log.rb', line 83

def start(take_mutex = true)
  @mutex.lock if take_mutex
  # Prevent starting files too fast
  sleep(0.1) until !File.exist?(File.join(@log_dir, File.build_timestamped_filename(@tags)))
  stop(false)
  timed_filename = File.build_timestamped_filename(@tags)
  @start_day = timed_filename[0..9].gsub("_", "") # YYYYMMDD
  @filename = File.join(@log_dir, timed_filename)
  @file = File.open(@filename, 'a')
  @mutex.unlock if take_mutex
end

#stop(take_mutex = true, metadata: {}) ⇒ Object

Closes the message log and marks it read only



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openc3/utilities/message_log.rb', line 66

def stop(take_mutex = true, metadata: {})
  @mutex.lock if take_mutex
  if @file and not @file.closed?
    @file.close
    File.chmod(0444, @filename)
    bucket_key = File.join(@remote_log_directory, @start_day, File.basename(@filename))
    begin
      thread = BucketUtilities.move_log_file_to_bucket(@filename, bucket_key, metadata: )
      thread.join
    rescue StandardError => e
      Logger.error e.formatted
    end
  end
  @mutex.unlock if take_mutex
end

#write(message, flush = false) ⇒ Object

Ensures the log file is opened and ready to write. It then writes the message to the log and flushes it to force the write.

Parameters:

  • message (String)

    Message to write to the log



54
55
56
57
58
59
60
61
62
63
# File 'lib/openc3/utilities/message_log.rb', line 54

def write(message, flush = false)
  @mutex.synchronize do
    if @file.nil? or @file.closed? or (not File.exist?(@filename))
      start(false)
    end

    @file.write(message)
    @file.flush if flush
  end
end