=begin エラーログ出力(XP用) ver 1.0.0.1         by 半生 http://www.tktkgame.com/ RPGツクールXP用です ■ 概要 ・エラー終了時にデバッグに役立ちそうなログをいろいろ出力します。 ・ログはゲームフォルダに「error_log(日付).txt」として出力されます。 ■ 使い方 ・スクリプトエディタを開いて[Main]のすぐ上にコピー ・公開時等ログ出力が不要になった場合は項目ごと削除 ■ 注意 このスクリプトより下のスクリプトは実行されません。 ■ 更新履歴 ver 1.0.0.1 (2011/08/14)  バグ修正 ver 1.0.0.0 (2011/08/10)  公開。 =end module TKG;end module TKG::ErrorLog # スイッチ一覧を出力するかどうか OUTPUT_SWITCHES = false # 変数一覧を出力するかどうか OUTPUT_VARIABLES = false end module TKG::ErrorLog REGEX_BT = /^Section(\d+)\:(\d+)/ @last_common_id = 0 @error_script = "" @indent = " " module_function def set_last_common_id(common_id) @last_common_id = common_id end def set_error_script(code) @error_script = code end # セクションネームをスクリプト名に置き換え def replace_section_name(message) message.sub(REGEX_BT) {|item| section = $RGSS_SCRIPTS[$1.to_i] if section.nil? item else "#{section[1]}(#{$2})" end } end def error_script_info return "" if @error_script == "" info = "\n■ スクリプトコード\n" script_lines = @error_script.gsub("\r","").split("\n") script_lines.each_with_index do |code, index| info += @indent + sprintf("% 2d: %s\n", index + 1,code) end info += "\n" return info end def switches_log return "" unless OUTPUT_SWITCHES log = "■ スイッチ一覧\n" $data_system.switches.each_with_index do |name,index| next if index == 0 sw = $game_switches[index] log += @indent + sprintf("\[%04d:%s\] :\t%s\n", index, name, sw.to_s) end log += "\n" return log end def variables_log return "" unless OUTPUT_VARIABLES log = "■ 変数一覧\n" $data_system.variables.each_with_index do |name,index| next if index == 0 var = $game_variables[index] log += @indent + sprintf("\[var%04d:%s\] :\t%s\n", index, name, var.to_s) end log += "\n" return log end # バトラーの詳細情報 def battler_info(battler, depth=0) tab = @indent * depth msg = "" msg += tab + sprintf("HP: %d / %d\n",battler.hp, battler.maxhp) msg += tab + sprintf("SP: %d / %d\n",battler.sp, battler.maxsp) msg += tab + "States: [#{battler.instance_variable_get(:@states).join(",")}]\n" return msg end # アクターの詳細情報 def actor_info(actor, index=0, depth=0) tab = @indent * depth if actor.dead? info = " :Dead" else info = "" end msg = tab + sprintf("%03d: [%03d: %s]%s\n",index,actor.id, actor.name, info) msg += battler_info(actor, depth + 1) return msg end # 敵の詳細情報 def enemy_info(enemy, index=0, depth=0) tab = @indent * depth if enemy.hidden info = " :Hidden" elsif enemy.dead? info = " :Dead" else info = "" end msg = tab + sprintf("%02d: [%03d:%s]%s\n",index,enemy.enemy_id, enemy.name, info) msg += battler_info(enemy, depth + 1) return msg end # 戦闘関連の情報 def battle_info return "" unless $scene.is_a?(Scene_Battle) active_battler = $scene.instance_variable_get(:@active_battler) troop = $data_troops[$game_temp.battle_troop_id] return "" if troop.nil? msg = "■ Battle:\n" msg += @indent + "Troop:#{sprintf("[%03d: %s]", troop.id, troop.name)}\n" msg += @indent + "Turn: #{$game_temp.battle_turn}\n" if active_battler.is_a?(Game_Battler) msg += @indent + "ActiveBattler: #{active_battler.name}\n" unless active_battler.current_action.nil? case active_battler.current_action.kind when 0 # 基本 action = ["攻撃","防御","逃走","待機"][active_battler.current_action.basic] msg += (@indent * 2) + "Action: #{action}\n" when 1 # スキル skill = $data_skills[active_battler.current_action.skill_id] msg += (@indent * 2) + sprintf("Action: スキル: [%03d:%s]\n", skill.id, skill.name) when 2 # アイテム item = $data_items[active_battler.current_action.item_id] msg += (@indent * 2) + sprintf("Action: アイテム: [%03d:%s]\n", item.id, item.name) end msg += (@indent * 2) + "TargetIndex: #{active_battler.current_action.target_index.to_s}\n" msg += (@indent * 2) + "Forced?: #{active_battler.current_action.forcing}\n" end end msg += @indent + "Enemies:------\n" $game_troop.enemies.each_with_index do |enemy,index| msg += enemy_info(enemy, index+1, 2) end msg += @indent + "------:Enemies\n" msg += "\n" return msg end # マップ関連の情報 def map_info msg = "■ Map:\n" msg += @indent + "MAP_ID: #{$game_map.map_id.to_s}\n" msg += @indent + "Player: [#{$game_player.x}, #{$game_player.y}]\n" # 自動実行イベントのチェック if $game_system.map_interpreter.running? event = $game_system.map_interpreter.get_character(0) if !event.nil? if !(rpg_event = event.instance_variable_get(:@event)).nil? # マップイベント msg += @indent + "RunningEvent: MapEvent\n" msg += (@indent * 2) + "ID : #{event.id}\n" msg += (@indent * 2) + "Name: #{rpg_event.name}\n" msg += (@indent * 2) + "X : #{rpg_event.x}\n" msg += (@indent * 2) + "Y : #{rpg_event.y}\n" end else msg += @indent + "RunningEvent: CommonEvent\n" end end return msg end # エラーログをファイルに保存 def save(filename=nil, exception=$!) if filename.nil? filename = "error_log" + Time.now.strftime("%Y%m%d") + ".txt" end msg = "◆ #{Time.now.strftime('%Y-%m-%dT%H:%M:%S')}\n" msg += "■ エラーの種類 :\n" msg += @indent + "#{exception.class.to_s}\n" msg += "\n" if exception.message msg += "■ メッセージ :\n" msg += @indent + "#{self.replace_section_name(exception.message)}\n" msg += "\n" end msg += self.error_script_info() if exception.backtrace.size > 0 msg += "■ バックトレース :\n" exception.backtrace.map do |bt| msg += @indent + "#{self.replace_section_name(bt)}\n" end msg += "\n" end msg += "\n" if !(common_event = $data_common_events[@last_common_id.to_i]).nil? msg += "■ 最後に呼ばれたコモンイベント\n" msg += @indent + "ID : #{common_event.id}\n" msg += @indent + "Name: #{common_event.name}\n" msg += "\n" end msg += "■ Party\n" msg += @indent + "Members:------\n" $game_party.actors.each_with_index do |actor, index| msg += actor_info(actor,index+1, 1) end msg += @indent + "------:Members\n" msg += "\n" if $game_temp.in_battle msg += self.battle_info msg += "\n" elsif $game_map msg += self.map_info msg += "\n" end sw_log = self.switches_log var_log = self.variables_log open(filename,"w") do |log| log.print msg log.print sw_log log.print var_log end end end module TKG::ErrorLog::Extend_Interpreter def eval(*args) begin super rescue TKG::ErrorLog.set_error_script(args[0]) raise end end end # コマンド:スクリプトのエラーを補足 class Interpreter include TKG::ErrorLog::Extend_Interpreter end # 最後に呼び出されたコモンイベントを記憶 class RPG::CommonEvent def list _list = @list if trigger != 2 command = RPG::EventCommand.new(355,0,["TKG::ErrorLog.set_last_common_id(#{@id})"]) _list.unshift(command) end return _list end end begin # トランジション準備 Graphics.freeze # シーンオブジェクト (タイトル画面) を作成 $scene = Scene_Title.new # $scene が有効な限り main メソッドを呼び出す while $scene != nil $scene.main end # フェードアウト Graphics.transition(20) rescue Errno::ENOENT # 例外 Errno::ENOENT を補足 # ファイルがオープンできなかった場合、メッセージを表示して終了する filename = $!.message.sub("No such file or directory - ", "") print("ファイル #{filename} が見つかりません。") rescue error = $! TKG::ErrorLog.save() raise end exit