Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Error log

Error log
Version: 1.5

Introduction

There is already a script about logging error by Me(tm), 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(tm), 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.
    The following data is logged:
    • The time when an error occurs
    • Error type
    • Scene class in which an error occurs
    • Error message
    • Error details (list of all methods involved when an error occurs). v1.5 now provides script (section) names instead of section numbers.
  • Gives player a more user-friendly error message.

Screenshots

No screenshots provided. It does nothing to the appearance of your game.

Demo

None for now.

Script

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:
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=email@gmail.com]email@gmail.com[/email]",

    "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(tm) 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.

Terms and Conditions

You can use this script for free and/or commercial use, as long as you credit Me(tm). My credit is optional as I only edit his script.
 
Perhaps change the p in warn. I don't know if that works in rmxp, but warn shows the warning message, instead of raise showing the error message ;)
 
This is pretty cool, i was interested in finding something like this, but is it possible to send other information into a txt file? Like for example, player steps, how many times a player buys a potion from a store?
 
@Me(tm):
Yes, I am thinking of changing the p part. p or print in RGSS are interpreted into a Windows normal message box (without the exclamation mark icon) instead of standard console output as originally in Ruby. Although tested and works, I think it's still a little bit ugly, so I am thinking of creating a custom scene when an error occurs. That way, the error message will appear in game windowskin instead. Maybe I'll add an option to return to title, so testers can replay the game and see it they can recreate the error. I was thinking to autosave when error occurs, but later it would be useless (haven't tried it, but if you loaded the game just exactly before the game error, wouldn't you get the same error again without ever play the game?).

I never had a "p" message in fullscreen (I always test and play in normal window), is it annoying?

@germen:
If you want to change the error log into an txt file, just change ErrorLog.rxdata into ErrorLog.txt, although the first can be opened easily by Notepad.

More information can always be appended. If you can script, just add some more fh.puts("some data here"). I will add more information for the next version, as map name, events (don't know it will work or not).

I wonder how player steps could help debugging :D
 
I am glad that now instead of section number, script name will appear in error log. So, instead of:

Section001:15:in 'main'

it will be

My Script, line 15, in 'main'

Thanks to Dervvulfman for pointing me out.

After a new error scene is produced, I'll update the script.
 
Hey i've got this all setup, it will show the new error message window type, but it will not type the text into my ErrorLog.txt text document. I was just asking please could you help me solve this error. It will help when I have my playtesters test out my game for bugs. I would be grateful if somone could help.

Edit: Error fixed, it was a temporary error and it wrote them all later -.-.

I checked and it writes the errors perfectly (made a map script error purposfully and fixed it) and instantly. This is a great code. Thank you.
 
Updated to 1.5. Now instead of annoying section numbers, you have script names in your error log. Thanks for DerVVulfman for pointing me this.
 
Whoa, Exshan!! you're actually made a script? Why you didn't tell me before? Well i'm gonna try this script. After i could open my RMXP again.  :tongue:
 
Well, I usually don't share my scripts since they are custom-made for my game (or scripts made by request), but I realize this one is not custom-made. That's why I share it ;)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top