// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text.Json.Serialization;
namespace EpicGames.Core
{
///
/// Different types of log values (stored in the $type field of log properties)
///
public static class LogValueType
{
public static readonly Utf8String Asset = "Asset";
public static readonly Utf8String SourceFile = "SourceFile";
public static readonly Utf8String Object = "Object"; // Arbitrary structured object
public static readonly Utf8String Channel = "Channel";
public static readonly Utf8String Severity = "Severity";
public static readonly Utf8String Message = "Message";
public static readonly Utf8String LineNumber = "Line";
public static readonly Utf8String ColumnNumber = "Column";
public static readonly Utf8String Symbol = "Symbol";
public static readonly Utf8String ErrorCode = "ErrorCode";
public static readonly Utf8String ToolName = "ToolName";
public static readonly Utf8String ScreenshotTest = "ScreenshotTest";
public static readonly Utf8String DepotPath = "DepotPath";
}
///
/// Information for a structured value for use in log events
///
public sealed class LogValue
{
///
/// Type of the event
///
public Utf8String Type { get; set; }
///
/// Rendering of the value
///
public string Text { get; set; }
///
/// Properties associated with the value
///
public Dictionary? Properties { get; }
///
/// Constructor
///
/// Type of the value
/// Rendering of the value as text
/// Additional properties for this value
public LogValue(Utf8String type, string text, Dictionary? properties = null)
{
Type = type;
Text = text;
Properties = properties;
}
///
/// Creates a LogValue from an object, overriding the type and display text for it
///
/// The object to construct from
///
public static LogValue FromObject(object obj) => FromObject(LogValueType.Object, obj.ToString() ?? String.Empty, obj);
///
/// Creates a LogValue from an object, overriding the type and display text for it
///
/// Type of the object
/// Rendered representation of the object in the output string
/// The object to construct from
///
public static LogValue FromObject(Utf8String type, string text, object obj)
{
Type objType = obj.GetType();
Dictionary? properties = null;
foreach (PropertyInfo propertyInfo in objType.GetProperties())
{
if (propertyInfo.GetCustomAttribute() == null)
{
string name = propertyInfo.GetCustomAttribute()?.Name ?? propertyInfo.Name;
properties ??= new Dictionary();
properties[name] = propertyInfo.GetValue(obj)!;
}
}
return new LogValue(type, text, properties);
}
///
public override string ToString()
{
return Text;
}
}
}