forked from elanthia-online/dr-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.lic
114 lines (102 loc) · 3.16 KB
/
log.lic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
=begin
logs the game to lich\logs\<game code>-<char name>\<date>-<number>.log
starts a new file after 30,000 lines (somewhere around 1mb)
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.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.
0.3 (2022-01-04):
* prepend datetime stamps to logged lines
0.2 (2015-01-13):
* create log directory if 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 = true
Script.current.want_upstream = true
if Script.current.vars[0] =~ /\-\-timestamp="(.*)"/
stamp_format = Regexp.last_match[1]
stamp_enable = true
end
Thread.new {
begin
loop {
Script.current.downstream_buffer.push ">#{upstream_get.sub(/^<c>/, '')}"
}
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")}.log")
file = File.open(filename, 'a')
file.sync = true
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)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
}