03-07-2008, 06:53 PM (This post was last modified: 09-02-2024, 09:35 PM by DerVVulfman.)
Error log
Version: 1.5
Same version can be found in rmxp.org.
Introduction
There is already a script about error logging by Me, but somehow I find it doesn't work all times and gives less information about the error. Since my beta testers sometimes find an error by accident, they fail to note in which line of the script that causes error (and sometimes they can't recreate it). This script, based on Me, logs an error by the time it happens, hides the normal error message (Script XXX line YYY error message), and replaces it with more user-friendly error message.
Features
Logs and appends any error into a file in a human-readable format.
Gives player a more user-friendly error message.
The following data is logged:
The time when an error occurs
Error type
The class in which an error occurs
Error message
Error details (list of all methods involved when an error occurs).
Error log example:
Quote:Time: Sun 03 Feb 2008, 22:50:43
Error type: NoMethodError
Class: Scene_Battle
Message: undefined method `ipskilled' for nil:NilClass
at
IP skill, line 203, at `set_ip_weapon'
NEW! Scene_BattleEquip, line 113, at `update_item'
NEW! Scene_BattleEquip, line 110, at `each'
NEW! Scene_BattleEquip, line 110, at `update_item'
NEW! Scene_BattleEquip, line 37, at `update_phase_equip'
Scene_Battle 1, line 461, at `raz_update'
Enemy HP Bar, line 61, at `update'
Scene_Battle 1, line 111, at `trick_bars_main'
Scene_Battle 1, line 105, at `loop'
Scene_Battle 1, line 116, at `trick_bars_main'
Extra Modifications, line 19, at `raz_enemy_hp_main'
Enemy HP Bar, line 53, at `bt_main'
Customized Battle Test, line 884, at `main'
Main, baris 14
--------------------
Screenshots
No screenshots provided. It does nothing to the appearance of your game.
Demo
None for now.
Script
Error log
Code:
rescue StandardError => stderr
# Bring message to user that an error has occured
p "Your error message here. See Instructions for more details."
# Optional, when debugging, you will still receive the error message, but in less details.
if $DEBUG
p stderr.message
end
# When?
time = Time.now
time = time.strftime("%a %d %b %Y, %X")
# Write to file
File.open("ErrorLog.rxdata","a+") do |fh|
# Time
fh.puts("Time: #{time}")
# Error type
fh.puts("Error type: #{stderr.class}")
# In which class?
fh.puts("Class: #{$scene.class}")
# Message
fh.puts("Message: #{stderr.message}")
# Where?
fh.puts("at")
for location in stderr.backtrace
# Get the section number
section = location[/(?#Section)(\d)*(:)/]
# Strip off anything but numbers
section_err = section[0, section.length - 1]
script_name = $RGSS_SCRIPTS[section_err.to_i][1]
# Get line number
line_num = location[/(:)(\d)*(\Z|(:))/]
# Strip off anything but line number
#line_num_err = line_num[section_err.length + 1, line_num.length - section_err.length]
# Strip off ":" if any
line_num_err = line_num[1, line_num.length - 1]
line_num_err = line_num_err[/(\d)*/]
# If any, get method name
method = location[/\s\W(\w)*\W/]
# Strip the space
method = method[1, method.length - 1] unless method == nil
# Now construct location string
loc_err = script_name + ", line " + line_num_err +
(method == nil ? "" : ", at " + method)
# Write to file
fh.puts(" #{loc_err}")
end
# Delimiter
fh.puts("--------------------")
end
Instructions
Installation
Please backup your game first before attempting to install this script.
1. Go to your Script Editor and scroll through the end of your script. You will find Main.
Yes, this time we will edit the main processing itself (that's why I suggest you to backup first, in case there is something wrong).
2. Before the last end, paste the code above.
A sample Main will be like this:
Sample Main after installation
Code:
#==============================================================================
# ** Main
#------------------------------------------------------------------------------
# After defining each class, actual processing begins here.
#==============================================================================
begin
# Prepare for transition
Graphics.freeze
# Make scene object (title screen)
$scene = Scene_Title.new
# Call main method as long as $scene is effective
while $scene != nil
$scene.main
end
# Fade out
Graphics.transition(20)
rescue Errno::ENOENT
# Supplement Errno::ENOENT exception
# If unable to open file, display message and end
filename = $!.message.sub("No such file or directory - ", "")
print("Tidak dapat menemukan berkas #{filename}.")
[b]rescue StandardError => stderr
# Bring message to user that an error has occured
p "Maaf, terjadi kesalahan! Silakan kirimkan berkas ErrorLog.rxdata",
"pada map instalasi RPG OJ ke email@gmail.com",
"dengan subjek 'Kesalahan RPG OJ', atau lampirkan berkas tersebut",
"pada forum RPGFWID, bagian Enter Our Journey! > Bug Squash.",
"Versi: 0.3"
if $DEBUG
p stderr.message
end
# When?
time = Time.now
time = time.strftime("%a %d %b %Y, %X")
# Write to file
File.open("ErrorLog.rxdata","a+") do |fh|
# Time
fh.puts("Waktu: #{time}")
# Error type
fh.puts("Kesalahan: #{stderr.class}")
# In which class?
fh.puts("Kelas: #{$scene.class}")
# Message
fh.puts("Pesan: #{stderr.message}")
# Where?
fh.puts("pada")
for location in stderr.backtrace
# Get the section number
section = location[/(?#Section)(\d)*(:)/]
# Strip off anything but numbers
section_err = section[0, section.length - 1]
script_name = $RGSS_SCRIPTS[section_err.to_i][1]
# Get line number
line_num = location[/(:)(\d)*(\Z|(:))/]
# Strip off anything but line number
#line_num_err = line_num[section_err.length + 1, line_num.length - section_err.length]
# Strip off ":" if any
line_num_err = line_num[1, line_num.length - 1]
line_num_err = line_num_err[/(\d)*/]
# If any, get method name
method = location[/\s\W(\w)*\W/]
# Strip the space
method = method[1, method.length - 1] unless method == nil
# Now construct location string
loc_err = script_name + ", baris " + line_num_err +
(method == nil ? "" : ", pada " + method)
# Write to file
fh.puts(" #{loc_err}")
end
# Delimiter
fh.puts("--------------------")
end[/b]
end
Sorry for being written in Indonesian, too lazy to translate ^_^;
Custom error message
To replace the custom error message, go to line 25, which says:
Code:
p "Your error message here. See Instructions for more details."
and edit it as you like. To insert a new line, provide \n. Or, you can use it like this:
Code:
p "First line here", "Second line here", "and so on, as you wish"
Error information are saved in stderr object, which is an Exception (in this case, it's specific to StandardError) class. Refer to RGSS Help on Exception class to display error messages. Basically, you can use stderr.message for error message.
FAQ
None so far. Awaiting questions...
Compatibility
Should work with any script, since it doesn't modify any class but the main processing.
Credits and Thanks
Thank you for Me for the base of this script.
Thank you for DerVVulfman, I accidentally found how to get section name from his script.
Author's Notes
I decided not to create a special scene when a error occured, since it wouldn't help so much. It is nicer for your game to have less (and even no) errors, right? So I don't see why I should make a special scene for errors.
This script can only handle StandardError exception, but if you want to handle all exception, replace
Code:
rescue StandardError => stderr
with
Code:
rescue [b]Exception [/b]=> stderr
If you still need the old error message window, I'm sorry, for now it's gone, but you can use p stderr.backtrace to gain similar effect.
The errors are written into ErrorLog.rxdata file, found in your game folder. It is actually a plain text file, so you can open it with Notepad. If you want to use a TXT file instead, change this line:
Code:
File.open("[b]ErrorLog.rxdata[/b]","a+") do |fh|
into anything you wish (e.g. ErrorLog.txt, Error.dat, error.mex, etc.).
Terms and Conditions
You can use this script for free and/or commercial use, as long as you credit Me. My credit is optional as I only edit his script.