Browse Source

Display line numbers in Lua error messages

master
chrisgregan 9 years ago
parent
commit
28469a9119
  1. 29
      Assets/Fungus/Lua/Scripts/LuaEnvironment.cs
  2. 4
      Assets/Fungus/Lua/Scripts/LuaUtils.cs
  3. 6
      Assets/Tests/Lua/LuaEnvironmentTests.unity

29
Assets/Fungus/Lua/Scripts/LuaEnvironment.cs

@ -222,7 +222,7 @@ namespace Fungus
} }
catch (InterpreterException ex) catch (InterpreterException ex)
{ {
UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + luaString); LogException(ex.DecoratedMessage, luaString);
} }
if (res == null) if (res == null)
@ -249,7 +249,7 @@ namespace Fungus
} }
catch (InterpreterException ex) catch (InterpreterException ex)
{ {
UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + luaString); LogException(ex.DecoratedMessage, luaString);
} }
if (onComplete != null) if (onComplete != null)
@ -289,7 +289,7 @@ namespace Fungus
} }
catch (InterpreterException ex) catch (InterpreterException ex)
{ {
UnityEngine.Debug.LogError(ex.DecoratedMessage + "\n" + debugInfo); LogException(ex.DecoratedMessage, debugInfo);
} }
yield return null; yield return null;
@ -347,6 +347,29 @@ namespace Fungus
// pass the url to the user in some way. // pass the url to the user in some way.
Process.Start(remoteDebuggerService.HttpUrlStringLocalHost); Process.Start(remoteDebuggerService.HttpUrlStringLocalHost);
} }
/// <summary>
/// Writes a MoonSharp exception to the debug log in a helpful format.
/// </summary>
/// <param name="decoratedMessage">Decorated message from a MoonSharp exception</param>
/// <param name="debugInfo">Debug info, usually the Lua script that was running.</param>
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);
}
} }
} }

4
Assets/Fungus/Lua/Scripts/LuaUtils.cs

@ -180,11 +180,11 @@ namespace Fungus
} }
catch (ScriptRuntimeException ex) catch (ScriptRuntimeException ex)
{ {
UnityEngine.Debug.LogError("Lua runtime error: " + ex.DecoratedMessage); LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
} }
catch (InterpreterException ex) catch (InterpreterException ex)
{ {
UnityEngine.Debug.LogError(ex.DecoratedMessage); LuaEnvironment.LogException(ex.DecoratedMessage, stringTable.text);
} }
} }
} }

6
Assets/Tests/Lua/LuaEnvironmentTests.unity

@ -478,7 +478,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1 m_IsActive: 0
--- !u!114 &637150168 --- !u!114 &637150168
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1132,7 +1132,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 0 m_IsActive: 1
--- !u!114 &1532103953 --- !u!114 &1532103953
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1333,8 +1333,6 @@ MonoBehaviour:
luaFile: {fileID: 0} luaFile: {fileID: 0}
luaScript: '-- Test string table localisation system luaScript: '-- Test string table localisation system
print(stringtable)
fungus.assert(stringtable != nil) fungus.assert(stringtable != nil)

Loading…
Cancel
Save