using System.Collections;
using System.Text;
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
namespace FileFlows.BasicNodes.Logging;
///
/// Flow that logs all variables
///
public class LogVariables: Node
{
///
public override int Inputs => 1;
///
public override int Outputs => 1;
///
public override FlowElementType Type => FlowElementType.Logic;
///
public override string Icon => "fas fa-at";
///
public override string HelpUrl => "https://fileflows.com/docs/plugins/basic-nodes/log-variables";
///
public override bool FailureNode => true;
///
public override string Group => "Logging";
///
/// Gets or sets if the logging will be recursive
///
[Boolean(1)]
public bool Recursive { get; set; }
///
public override int Execute(NodeParameters args)
{
var log = GetVariablesString(args.Variables, Recursive);
args.Logger?.ILog("Variables: \n" + log);
return 1;
}
///
/// Generates a string representation of variables to be logged.
///
/// The variables to log.
/// Indicates whether to log variables recursively.
/// A formatted string of variables.
internal static string GetVariablesString(Dictionary variables, bool recursive)
{
StringBuilder log = new();
foreach (var variable in variables)
{
// Only log the variable's key and value for simple types (non-recursive)
string valueRepresentation = recursive
? AppendComplexObject(variable.Key, variable.Value)
: FormatValue(variable.Value);
if (recursive)
{
log.AppendLine(valueRepresentation);
}
else
{
// For non-recursive, log only the key and value
log.AppendLine($"{variable.Key}: {valueRepresentation}");
}
}
return log.ToString();
}
///
/// Formats a value as a string.
///
/// The value to format.
/// A formatted string representation of the value.
private static string FormatValue(object value)
{
return value switch
{
null => "null",
IEnumerable enumerable when value is not string => $"[{string.Join(", ", enumerable.Cast