From 28469a9119288035357177145afc3c4297afbf38 Mon Sep 17 00:00:00 2001 From: chrisgregan Date: Fri, 1 Apr 2016 16:49:27 +0100 Subject: [PATCH] Display line numbers in Lua error messages --- Assets/Fungus/Lua/Scripts/LuaEnvironment.cs | 29 ++++++++++++++++++--- Assets/Fungus/Lua/Scripts/LuaUtils.cs | 4 +-- Assets/Tests/Lua/LuaEnvironmentTests.unity | 6 ++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs b/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs index 1ef4f62e..2a12877c 100644 --- a/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs +++ b/Assets/Fungus/Lua/Scripts/LuaEnvironment.cs @@ -222,7 +222,7 @@ namespace Fungus } catch (InterpreterException ex) { - UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + luaString); + LogException(ex.DecoratedMessage, luaString); } if (res == null) @@ -249,7 +249,7 @@ namespace Fungus } catch (InterpreterException ex) { - UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + luaString); + LogException(ex.DecoratedMessage, luaString); } if (onComplete != null) @@ -289,7 +289,7 @@ namespace Fungus } catch (InterpreterException ex) { - UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + debugInfo); + LogException(ex.DecoratedMessage, debugInfo); } yield return null; @@ -347,6 +347,29 @@ namespace Fungus // pass the url to the user in some way. Process.Start(remoteDebuggerService.HttpUrlStringLocalHost); } + + /// + /// Writes a MoonSharp exception to the debug log in a helpful format. + /// + /// Decorated message from a MoonSharp exception + /// Debug info, usually the Lua script that was running. + public static void LogException(string decoratedMessage, string debugInfo) + { + string output = decoratedMessage + "\n"; + + char[] separators = { '\r', '\n' }; + string[] lines = debugInfo.Split(separators, StringSplitOptions.None); + + // Show line numbers for script listing + int count = 1; + foreach (string line in lines) + { + output += count.ToString() + ": " + line + "\n"; + count++; + } + + UnityEngine.Debug.LogError(output); + } } } \ No newline at end of file diff --git a/Assets/Fungus/Lua/Scripts/LuaUtils.cs b/Assets/Fungus/Lua/Scripts/LuaUtils.cs index 1465f391..ac37d308 100644 --- a/Assets/Fungus/Lua/Scripts/LuaUtils.cs +++ b/Assets/Fungus/Lua/Scripts/LuaUtils.cs @@ -180,11 +180,11 @@ namespace Fungus } catch (ScriptRuntimeException ex) { - UnityEngine.Debug.LogError("Lua runtime error: " + ex.DecoratedMessage); + LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); } catch (InterpreterException ex) { - UnityEngine.Debug.LogError(ex.DecoratedMessage); + LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text); } } } diff --git a/Assets/Tests/Lua/LuaEnvironmentTests.unity b/Assets/Tests/Lua/LuaEnvironmentTests.unity index 69e8d3b3..49e46f0e 100644 --- a/Assets/Tests/Lua/LuaEnvironmentTests.unity +++ b/Assets/Tests/Lua/LuaEnvironmentTests.unity @@ -478,7 +478,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!114 &637150168 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1132,7 +1132,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!114 &1532103953 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1333,8 +1333,6 @@ MonoBehaviour: luaFile: {fileID: 0} luaScript: '-- Test string table localisation system - print(stringtable) - fungus.assert(stringtable != nil)