Add support for capturing stack trace of called DebugLog from C++

This commit is contained in:
Wojtek Figat
2021-02-26 13:58:42 +01:00
parent a45b71617d
commit ff70c16051
5 changed files with 49 additions and 20 deletions

View File

@@ -216,7 +216,7 @@ namespace FlaxEditor.Windows
/// </summary>
public void Copy()
{
Clipboard.Text = Info.Replace("\n", Environment.NewLine);
Clipboard.Text = Info.Replace("\r\n", "\n").Replace("\n", Environment.NewLine);
}
public override bool OnMouseDoubleClick(Vector2 location, MouseButton button)
@@ -275,7 +275,7 @@ namespace FlaxEditor.Windows
private readonly VerticalPanel _entriesPanel;
private LogEntry _selected;
private readonly int[] _logCountPerGroup = new int[(int)LogGroup.Max];
private readonly Regex _logRegex = new Regex("at(.*) in (.*):(\\d*)");
private readonly Regex _logRegex = new Regex("at (.*) in (.*):(line (\\d*)|(\\d*))");
private InterfaceOptions.TimestampsFormats _timestampsFormats;
private readonly object _locker = new object();
@@ -508,20 +508,27 @@ namespace FlaxEditor.Windows
for (int i = 0; i < matches.Count; i++)
{
var match = matches[i];
if (foundStart)
var matchLocation = match.Groups[1].Value.Trim();
if (matchLocation.StartsWith("FlaxEngine.Debug.", StringComparison.Ordinal))
{
// C# start
foundStart = true;
}
else if (matchLocation.StartsWith("DebugLog::", StringComparison.Ordinal))
{
// C++ start
foundStart = true;
}
else if (foundStart)
{
if (noLocation)
{
desc.LocationFile = match.Groups[2].Value;
int.TryParse(match.Groups[3].Value, out desc.LocationLine);
int.TryParse(match.Groups[5].Value, out desc.LocationLine);
noLocation = false;
}
fineStackTrace.AppendLine(match.Groups[0].Value);
}
else if (match.Groups[1].Value.Trim().StartsWith("FlaxEngine.Debug.", StringComparison.Ordinal))
{
foundStart = true;
}
}
desc.Description = fineStackTrace.ToString();
}

View File

@@ -43,7 +43,7 @@ bool CacheMethods()
if (!debugLogHandlerClass)
return false;
Internal_SendLog = debugLogHandlerClass->GetMethod("Internal_SendLog", 2);
Internal_SendLog = debugLogHandlerClass->GetMethod("Internal_SendLog", 3);
if (!Internal_SendLog)
return false;
@@ -60,7 +60,7 @@ bool CacheMethods()
return false;
}
void DebugLog::Log(LogType type, const String& message)
void DebugLog::Log(LogType type, const StringView& message)
{
if (CacheMethods())
return;
@@ -69,15 +69,18 @@ void DebugLog::Log(LogType type, const String& message)
MainThreadManagedInvokeAction::ParamsBuilder params;
params.AddParam(type);
params.AddParam(message, scriptsDomain->GetNative());
#if BUILD_RELEASE
params.AddParam(StringView::Empty, scriptsDomain->GetNative());
#else
const String stackTrace = Platform::GetStackTrace(1);
params.AddParam(stackTrace, scriptsDomain->GetNative());
#endif
MainThreadManagedInvokeAction::Invoke(Internal_SendLog, params);
}
void DebugLog::LogException(MonoObject* exceptionObject)
{
if (exceptionObject == nullptr)
return;
if (CacheMethods())
if (exceptionObject == nullptr || CacheMethods())
return;
MainThreadManagedInvokeAction::ParamsBuilder params;

View File

@@ -17,13 +17,13 @@ public:
/// </summary>
/// <param name="type">The message type.</param>
/// <param name="message">The message.</param>
static void Log(LogType type, const String& message);
static void Log(LogType type, const StringView& message);
/// <summary>
/// A variant of Debug.Log that logs a warning message to the console.
/// </summary>
/// <param name="message">The text message to display.</param>
FORCE_INLINE static void Log(const String& message)
FORCE_INLINE static void Log(const StringView& message)
{
Log(LogType::Info, message);
}
@@ -32,7 +32,7 @@ public:
/// A variant of Debug.Log that logs a warning message to the console.
/// </summary>
/// <param name="message">The text message to display.</param>
FORCE_INLINE static void LogWarning(const String& message)
FORCE_INLINE static void LogWarning(const StringView& message)
{
Log(LogType::Warning, message);
}
@@ -41,7 +41,7 @@ public:
/// A variant of Debug.Log that logs a error message to the console.
/// </summary>
/// <param name="message">The text message to display.</param>
FORCE_INLINE static void LogError(const String& message)
FORCE_INLINE static void LogError(const StringView& message)
{
Log(LogType::Error, message);
}

View File

@@ -47,9 +47,16 @@ namespace FlaxEngine
SendLog?.Invoke(logType, message, context, stackTrace);
}
internal static void Internal_SendLog(LogType type, string message)
internal static void Internal_SendLog(LogType logType, string message, string stackTrace)
{
Debug.Logger.Log(type, message);
if (message == null)
return;
var logger = Debug.Logger;
if (logger.IsLogTypeAllowed(logType))
{
Internal_Log(logType, message, IntPtr.Zero, stackTrace);
((DebugLogHandler)logger.LogHandler).SendLog?.Invoke(logType, message, null, stackTrace);
}
}
internal static void Internal_SendLogException(Exception exception)

View File

@@ -77,12 +77,24 @@ public:
AddParam(val);
}
FORCE_INLINE void AddParam(const StringView& value)
{
MonoString* val = MUtils::ToString(value);
AddParam(val);
}
FORCE_INLINE void AddParam(const String& value, MonoDomain* domain)
{
MonoString* val = MUtils::ToString(value, domain);
AddParam(val);
}
FORCE_INLINE void AddParam(const StringView& value, MonoDomain* domain)
{
MonoString* val = MUtils::ToString(value, domain);
AddParam(val);
}
void GetParams(void* params[8])
{
for (int32 i = 0; i < Count; i++)