Skip to content

Commit

Permalink
Merge pull request #6699 from mrhoribu/log-update
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtraDR authored Sep 19, 2023
2 parents e37f772 + bd64c91 commit 010e09c
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 32 deletions.
105 changes: 73 additions & 32 deletions log.lic
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,112 @@
logs the game to lich\logs\<game code>-<char name>\<date>-<number>.log
starts a new file after 30,000 lines (somewhere around 1mb)
author: Tillmen ([email protected])
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(/^<c>/, '')}"
Script.current.downstream_buffer.push ">#{upstream_get.sub(/^<c>/, '')}"
}
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 "<!-- Above contents from reget; full logging now active -->\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
}
123 changes: 123 additions & 0 deletions logxml.lic
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
=begin
Logs raw XML output to lich\logs\<game code>-<char name>\<date>-<number>.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 <!-- CLIENT -->...<!-- ENDCLIENT --> 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 "<!-- CLIENT -->#{upstream_get}<!-- ENDCLIENT -->"
}
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 "<!-- Above contents from reget; full logging now active -->\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
}

0 comments on commit 010e09c

Please sign in to comment.