Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script filename to script runner log message filename #289

Merged
merged 1 commit into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions openc3-cosmos-script-runner-api/app/models/running_script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,16 @@ class RunningScript
@@cancel_limits = false

def self.message_log(id = @@id)
unless @@message_log
if @@instance
@@message_log = OpenC3::MessageLog.new("sr", File.join(RAILS_ROOT, 'log'), scope: @@instance.scope)
else
@@message_log = OpenC3::MessageLog.new("sr", File.join(RAILS_ROOT, 'log'), scope: $openc3_scope)
end
return @@message_log if @@message_log

if @@instance
scope = @@instance.scope
tags = [File.basename(@@instance.filename, '.rb').gsub(/(\s|\W)/, '_')]
else
scope = $openc3_scope
tags = []
end
return @@message_log
@@message_log = OpenC3::MessageLog.new("sr", File.join(RAILS_ROOT, 'log'), tags: tags, scope: scope)
end

def message_log
Expand Down
11 changes: 6 additions & 5 deletions openc3/lib/openc3/utilities/message_log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'openc3/config/config_parser'
Expand All @@ -36,9 +36,10 @@ class MessageLog
# @param 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.
# @param log_dir [String] The filesystem path to store the message log file.
def initialize(tool_name, log_dir, scope:)
# @param tags [Array<String>] Array of strings to put into the filename
def initialize(tool_name, log_dir, tags: ['messages'], scope:)
@remote_log_directory = "#{scope}/tool_logs/#{tool_name}/"
@tool_name = tool_name
@tags = tags.unshift(tool_name)
@log_dir = log_dir
@filename = ''
@file = nil
Expand Down Expand Up @@ -82,9 +83,9 @@ def stop(take_mutex = true, metadata: {})
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([@tool_name, 'messages'])))
sleep(0.1) until !File.exist?(File.join(@log_dir, File.build_timestamped_filename(@tags)))
stop(false)
timed_filename = File.build_timestamped_filename([@tool_name, 'messages'])
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')
Expand Down
14 changes: 13 additions & 1 deletion openc3/spec/utilities/message_log_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# All changes Copyright 2022, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.

require 'spec_helper'
Expand Down Expand Up @@ -50,6 +50,18 @@ module OpenC3
log.start
log.stop
expect(File.exist?(log.filename)).to be true
# By default the list of tags is just ['messages']
expect(File.basename(log.filename)).to match(/TEST_messages.txt/)
expect(log.filename).to match(File.expand_path(File.dirname(__FILE__)))
File.delete log.filename
end

it "accepts a list of tags" do
log = MessageLog.new('TEST', File.expand_path(File.dirname(__FILE__)), tags: ['more', 'stuff'], scope: 'DEFAULT')
log.start
log.stop
expect(File.exist?(log.filename)).to be true
expect(File.basename(log.filename)).to match(/TEST_more_stuff.txt/)
expect(log.filename).to match(File.expand_path(File.dirname(__FILE__)))
File.delete log.filename
end
Expand Down