From f56680227d21de35b83510f64145563912869867 Mon Sep 17 00:00:00 2001 From: "Ryan P. McKinnon" <15917743+mrhoribu@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:54:02 -0400 Subject: [PATCH 1/2] Update log.lic * Rubocop code cleanup * Adjust filename to include hours, minutes, seconds * Save into subdirectories by year/month * Starts a new file when the day changes * Add log timestamp CLI option for non-DragonRealms --- log.lic | 105 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 73 insertions(+), 32 deletions(-) diff --git a/log.lic b/log.lic index 0e2e20b96f..7f93214163 100644 --- a/log.lic +++ b/log.lic @@ -3,71 +3,112 @@ logs the game to lich\logs\-\-.log starts a new file after 30,000 lines (somewhere around 1mb) - author: Tillmen (tillmen@lichproject.org) - game: any - version: 0.4 + SYNTAX: ;log + ;log --timestamp="%F %T %Z" + ;autostart add --global log --timestamp=\"%F %T %Z\" + log format based on https://apidock.com/ruby/DateTime/strftime + + author: elanthia-online + contributors: Tillmen, Tysong, Xanlin + game: gs + version: 0.7 + required: Lich > 5.0.1 changelog: - 0.5 (2023-09-09): - Removed $SAFE reference, updated for ruby 3.2 compatibility + 0.7 (2023-03-31): + * Rubocop code cleanup + * Adjust filename to include hours, minutes, seconds + * Save into subdirectories by year/month + * Starts a new file when the day changes + * Add log timestamp CLI option for non-DragonRealms + 0.6 (2023-01-23): + * Update for Ruby v3 compatibility + 0.5 (2022-11-24): + * Remove reference to $SAFE 0.4 (2022-01-10): - Making datetime stamps customizeable, and tuneable in a character's yaml. + * Making datetime stamps customizeable, and tuneable in a character's yaml. 0.3 (2022-01-04): - prepend datetime stamps to logged lines + * prepend datetime stamps to logged lines 0.2 (2015-01-13): - create log directory if needed + * create log directory if needed =end -unless defined?(script.want_script_output) - echo 'your version of Lich is too old for this script' +unless defined?(Script.current.want_script_output) + echo 'Your version of Lich is too old for this script.' exit end -hide_me +unless (Gem::Version.new(RUBY_VERSION) > Gem::Version.new('2.0.0')) || $SAFE == 0 + echo "This script must be trusted to be allowed to write to log files." + echo "You can trust it with the following command: #{$lich_char}trust #{script.name}" + exit +end -settings = get_settings -stamp_enable = settings.log_timestamp -stamp_format = settings.log_timestamp_format +hide_me +if defined?(get_settings) + settings = get_settings + stamp_enable = settings.log_timestamp + stamp_format = settings.log_timestamp_format +end +Script.current.want_script_output = true +Script.current.want_upstream = true -script.want_script_output = true -script.want_upstream = true +if Script.current.vars[0] =~ /\-\-timestamp="(.*)"/ + stamp_format = Regexp.last_match[1] + stamp_enable = true +end Thread.new { begin loop { - script.downstream_buffer.push ">#{upstream_get.sub(/^/, '')}" + Script.current.downstream_buffer.push ">#{upstream_get.sub(/^/, '')}" } rescue echo $! end } -Dir.mkdir("#{$lich_dir}logs") unless File.exist?("#{$lich_dir}logs") -dir = "#{$lich_dir}logs/#{XMLData.game}-#{XMLData.name}" -Dir.mkdir(dir) unless File.exist?(dir) - +started = false loop { - num = 1 - filename = "#{dir}/#{Time.now.strftime("%Y-%m-%d")}-#{num}.log" - filename = "#{dir}/#{Time.now.strftime("%Y-%m-%d")}-#{num += 1}.log" while File.exist?(filename) + dir = File.join(LICH_DIR, 'logs', "#{XMLData.game}-#{XMLData.name}", Time.now.strftime("%Y"), Time.now.strftime("%m")) + FileUtils.mkdir_p(dir) unless File.exist?(dir) + thisdate = Time.now.strftime("%Y-%m-%d") + filename = File.join(dir, "#{Time.now.strftime("%Y-%m-%d_%H-%M-%S")}.log") file = File.open(filename, 'a') file.sync = true - file.puts "#{Time.now.strftime("%Y-%m-%d %I:%M%P").sub(/0([0-9]+\:)/) { "#{$1}" }}\n" - file.puts(reget) if (Time.now - $login_time) < 30 + file.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L %:z")}\n" + unless started + if (Time.now - $login_time) < 30 + file.puts(reget) + file.puts "\n" + end + + echo "Non-XML Logging started, currently logging to #{filename}" + started = true + end begin 30000.times { line = get - unless line =~ /^<(?:push|pop|clear)Stream/ - if stamp_enable - file.puts "#{Time.now.strftime("#{stamp_format}")}: #{line}" - else + unless line =~ /^<(?:push|pop)Stream/ + unless defined?(stamp_enable) file.puts line + else + if stamp_enable + file.puts "#{Time.now.strftime("#{stamp_format}")}: #{line}" + else + file.puts line + end end end + break if Time.now.strftime("%Y-%m-%d") != thisdate } - file.puts "#{Time.now.strftime("%Y-%m-%d %I:%M%P").sub(/0([0-9]+\:)/) { "#{$1}" }}\n" + file.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L %:z")}\n" ensure - file.close rescue() + begin + file.close + rescue + Lich.log "Can't close file!" + end end } From bd64c91677a548feb9790758676b2552230305b9 Mon Sep 17 00:00:00 2001 From: "Ryan P. McKinnon" <15917743+mrhoribu@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:54:33 -0400 Subject: [PATCH 2/2] Create logxml.lic Pull in the EO\scripts version of logxml.lic --- logxml.lic | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 logxml.lic diff --git a/logxml.lic b/logxml.lic new file mode 100644 index 0000000000..90dcfc039a --- /dev/null +++ b/logxml.lic @@ -0,0 +1,123 @@ +=begin + + Logs raw XML output to lich\logs\-\-.xml + starts a new file after 30,000 lines (somewhere around 1mb) + + SYNTAX: ;logxml + ;logxml --timestamp="%F %T %Z" + ;autostart add --global logxml --timestamp=\"%F %T %Z\" + log format based on https://apidock.com/ruby/DateTime/strftime + + Shamelessly stolen and slightly altered (to use XML) from Tillmen's log script. + + Messages from the client will be wrapped in ... tags. + + author: elanthia-online + contributors: LostRanger, Xanlin, Tysong + game: gs + version: 0.5.0 (2023-03-31) + + changelog: + 0.5.0 (2023-03-31): + * Rubocop code cleanup + * Adjust filename to include hours, minutes, seconds + * Save into subdirectories by year/month + * Starts a new file when the day changes + * Add log timestamp CLI option for non-DragonRealms + 0.4.1 (2019-12-28): + * Implement 0.4, correctly. + 0.4 (2019-12-28): + * ';logxml streams' will now include pushStream and popStream messages. Note that this may be very, very noisy + due to room window updates and such. + 0.3 (2017-04-24): + * fix output including both XML and plaintext from downstream. + 0.2 (2017-04-23): + * Indicate where full logging started in the log file (as opposed to contents from regetall). Client data before + this point can't be fully logged due to Lich not loading the script in time, so yell about it. + * Use a more configuration-safe mechanism of complaining about not being trusted. Also, explain why trust is + needed. +=end + +unless defined?(Script.current.want_script_output) + echo 'Your version of Lich is too old for this script.' + exit +end + +unless (Gem::Version.new(RUBY_VERSION) > Gem::Version.new('2.0.0')) || $SAFE == 0 + echo "This script must be trusted to be allowed to write to log files." + echo "You can trust it with the following command: #{$lich_char}trust #{script.name}" + exit +end + +hide_me +if defined?(get_settings) + settings = get_settings + stamp_enable = settings.log_timestamp + stamp_format = settings.log_timestamp_format +end +Script.current.want_script_output = false +Script.current.want_upstream = true +Script.current.want_downstream = false +Script.current.want_downstream_xml = true + +if Script.current.vars[0] =~ /\-\-timestamp="(.*)"/ + stamp_format = Regexp.last_match[1] + stamp_enable = true +end + +log_streams = Script.current.vars.include?("streams") + +Thread.new { + begin + loop { + Script.current.downstream_buffer.push "#{upstream_get}" + } + rescue + echo $! + end +} + +started = false +loop { + dir = File.join(LICH_DIR, 'logs', "#{XMLData.game}-#{XMLData.name}", Time.now.strftime("%Y"), Time.now.strftime("%m")) + FileUtils.mkdir_p(dir) unless File.exist?(dir) + thisdate = Time.now.strftime("%Y-%m-%d") + filename = File.join(dir, "#{Time.now.strftime("%Y-%m-%d_%H-%M-%S")}.xml") + file = File.open(filename, 'a') + file.sync = true + file.puts "#{Time.now.strftime("%Y-%m-%d %I:%M%P").sub(/0([0-9]+\:)/) { "#{$1}" }}\n" + unless started + if (Time.now - $login_time) < 30 + file.puts(reget) + file.puts "\n" + end + + echo "XML Logging started, currently logging to #{filename}" + started = true + end + + begin + 30000.times { + line = get + if log_streams or line !~ /^<(?:push|pop)Stream/ + unless defined?(stamp_enable) + file.puts line + else + if stamp_enable + file.puts "#{Time.now.strftime("#{stamp_format}")}: #{line}" + else + file.puts line + end + end + end + break if Time.now.strftime("%Y-%m-%d") != thisdate + } + file.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%L %:z")}\n" + ensure + begin + file.close + rescue + Lich.log "Can't close file!" + end + end +}