Add basic support for log contexts.

This commit is contained in:
Menotdan
2024-03-26 21:09:18 -04:00
parent 4cd788cedc
commit ac36297e27
7 changed files with 180 additions and 14 deletions

View File

@@ -126,10 +126,11 @@ namespace FlaxEngine
/// </summary>
/// <param name="id">Unique ID of the object.</param>
/// <typeparam name="T">Type of the object.</typeparam>
/// <param name="skipLog">Whether or not to log warnings when objects aren't found.</param>
/// <returns>Found object or null if missing.</returns>
public static T Find<T>(ref Guid id) where T : Object
public static T Find<T>(ref Guid id, bool skipLog = false) where T : Object
{
return Internal_FindObject(ref id, typeof(T)) as T;
return Internal_FindObject(ref id, typeof(T), skipLog) as T;
}
/// <summary>
@@ -137,10 +138,11 @@ namespace FlaxEngine
/// </summary>
/// <param name="id">Unique ID of the object.</param>
/// <param name="type">Type of the object.</param>
/// <param name="skipLog">Whether or not to log warnings when objects aren't found.</param>
/// <returns>Found object or null if missing.</returns>
public static Object Find(ref Guid id, Type type)
public static Object Find(ref Guid id, Type type, bool skipLog = false)
{
return Internal_FindObject(ref id, type);
return Internal_FindObject(ref id, type, skipLog);
}
/// <summary>
@@ -335,7 +337,7 @@ namespace FlaxEngine
internal static partial string Internal_GetTypeName(IntPtr obj);
[LibraryImport("FlaxEngine", EntryPoint = "ObjectInternal_FindObject", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(Interop.StringMarshaller))]
internal static partial Object Internal_FindObject(ref Guid id, [MarshalUsing(typeof(Interop.SystemTypeMarshaller))] Type type);
internal static partial Object Internal_FindObject(ref Guid id, [MarshalUsing(typeof(Interop.SystemTypeMarshaller))] Type type, [MarshalAs(UnmanagedType.U1)] bool skipLog = false);
[LibraryImport("FlaxEngine", EntryPoint = "ObjectInternal_TryFindObject", StringMarshalling = StringMarshalling.Custom, StringMarshallingCustomType = typeof(Interop.StringMarshaller))]
internal static partial Object Internal_TryFindObject(ref Guid id, [MarshalUsing(typeof(Interop.SystemTypeMarshaller))] Type type);

View File

@@ -2,6 +2,7 @@
#include "Script.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Types/LogContexts.h"
#if USE_EDITOR
#include "Internal/StdTypesContainer.h"
#include "ManagedCLR/MClass.h"
@@ -330,6 +331,9 @@ void Script::Serialize(SerializeStream& stream, const void* otherObj)
void Script::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier)
{
// Store Logging Context
LogContexts::Set(GetID());
// Base
SceneObject::Deserialize(stream, modifier);
@@ -364,4 +368,6 @@ void Script::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier
}
}
}
LogContexts::Clear();
}

View File

@@ -24,6 +24,7 @@
#include "Engine/Core/ObjectsRemovalService.h"
#include "Engine/Core/Types/TimeSpan.h"
#include "Engine/Core/Types/Stopwatch.h"
#include "Engine/Core/Types/LogContexts.h"
#include "Engine/Content/Asset.h"
#include "Engine/Content/Content.h"
#include "Engine/Engine/EngineService.h"
@@ -891,7 +892,7 @@ ScriptingObject* Scripting::FindObject(Guid id, MClass* type)
// Check type
if (!type || result->Is(type))
return result;
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}.", id, String(result->GetType().Fullname), String(type->GetFullName()));
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}. {3}", id, String(result->GetType().Fullname), String(type->GetFullName()), LogContextFormatter::Format());
return nullptr;
}
@@ -910,7 +911,7 @@ ScriptingObject* Scripting::FindObject(Guid id, MClass* type)
return asset;
}
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}.", id, String(type->GetFullName()));
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}. {2}", id, String(type->GetFullName()), LogContextFormatter::Format());
return nullptr;
}

View File

@@ -7,6 +7,7 @@
#include "Engine/Level/Actor.h"
#include "Engine/Core/Log.h"
#include "Engine/Core/Types/Pair.h"
#include "Engine/Core/Types/LogContexts.h"
#include "Engine/Utilities/StringConverter.h"
#include "Engine/Content/Asset.h"
#include "Engine/Content/Content.h"
@@ -708,7 +709,7 @@ DEFINE_INTERNAL_CALL(MString*) ObjectInternal_GetTypeName(ScriptingObject* obj)
return MUtils::ToString(obj->GetType().Fullname);
}
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject* type)
DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject* type, bool skipLog = false)
{
if (!id->IsValid())
return nullptr;
@@ -725,15 +726,20 @@ DEFINE_INTERNAL_CALL(MObject*) ObjectInternal_FindObject(Guid* id, MTypeObject*
{
if (klass && !obj->Is(klass))
{
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}.", *id, String(obj->GetType().Fullname), String(klass->GetFullName()));
if (!skipLog)
LOG(Warning, "Found scripting object with ID={0} of type {1} that doesn't match type {2}. {3}", *id, String(obj->GetType().Fullname), String(klass->GetFullName()), LogContextFormatter::Format());
return nullptr;
}
return obj->GetOrCreateManagedInstance();
}
if (klass)
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}.", *id, String(klass->GetFullName()));
else
LOG(Warning, "Unable to find scripting object with ID={0}", *id);
if (!skipLog)
{
if (klass)
LOG(Warning, "Unable to find scripting object with ID={0}. Required type {1}. {2}", *id, String(klass->GetFullName()), LogContextFormatter::Format());
else
LOG(Warning, "Unable to find scripting object with ID={0}", *id);
}
return nullptr;
}