// Copyright Epic Games, Inc. All Rights Reserved.
using System;
using EpicGames.Core;
using Microsoft.Extensions.Logging;
namespace UnrealBuildTool
{
///
/// Class to display an incrementing progress percentage. Handles progress markup and direct console output.
///
public class ProgressWriter : IDisposable
{
///
/// Global setting controlling whether to output markup
///
public static bool bWriteMarkup = false;
///
/// Logger for output
///
ILogger Logger;
///
/// The name to include with the status message
///
string Message;
///
/// The inner scope object
///
LogStatusScope? Status;
///
/// The current progress message
///
string? CurrentProgressString;
///
/// Constructor
///
/// The message to display before the progress percentage
/// Whether to write messages to the console
/// Logger for output
public ProgressWriter(string InMessage, bool bInUpdateStatus, ILogger InLogger)
{
Message = InMessage;
Logger = InLogger;
if (bInUpdateStatus)
{
Status = new LogStatusScope(InMessage);
}
Write(0, 100);
}
///
/// Write the terminating newline
///
public void Dispose()
{
if (Status != null)
{
Status.Dispose();
Status = null;
}
}
///
/// Writes the current progress
///
/// Numerator for the progress fraction
/// Denominator for the progress fraction
public void Write(int Numerator, int Denominator)
{
float ProgressValue = Denominator > 0 ? ((float)Numerator / (float)Denominator) : 1.0f;
string ProgressString = String.Format("{0}%", Math.Round(ProgressValue * 100.0f));
if (ProgressString != CurrentProgressString)
{
CurrentProgressString = ProgressString;
if (bWriteMarkup)
{
Logger.LogInformation("@progress '{Message}' {ProgressString}", Message, ProgressString);
}
Status?.SetProgress(ProgressString);
}
}
}
}