Files
UnrealEngineUWP/Engine/Source/Programs/DotNETCommon/DotNETUtilities/LogStatusScope.cs
Ben Marsh adef91feea UAT: Add support for setting a status message through the log class. Allows writing transient messages (eg. progress messages) which will be cleared out before writing other messages. Best used through the LogStatusScope class, which can set a status message for the duration of a using() block.
As part of this change, the console no longer has to be added as a dedicated trace listener. Since we already special-case this listener when formatting log output, it's easier to just keep the implementation separate to the other trace listeners.

#rb none

[CL 4111304 by Ben Marsh in Dev-Core branch]
2018-06-04 17:23:16 -04:00

97 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tools.DotNETCommon
{
/// <summary>
/// Writes a status message to the log, which can be updated with a progress indicator as a slow task is being performed.
/// </summary>
public class LogStatusScope : IDisposable
{
/// <summary>
/// The base status message
/// </summary>
string Message;
/// <summary>
/// Constructor
/// </summary>
/// <param name="Message">The status message</param>
public LogStatusScope(string Message)
{
this.Message = Message;
Log.PushStatus(Message);
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="Format">The format specifier for the message</param>
/// <param name="Args">Arguments for the status message</param>
public LogStatusScope(string Format, params object[] Args)
: this(String.Format(Format, Args))
{
}
/// <summary>
/// Updates the base status message passed into the constructor.
/// </summary>
/// <param name="Message">The status message</param>
public void SetMessage(string Message)
{
this.Message = Message;
Log.UpdateStatus(Message);
}
/// <summary>
/// Updates the base status message passed into the constructor.
/// </summary>
/// <param name="Format">The format specifier for the message</param>
/// <param name="Args">Arguments for the status message</param>
public void SetMessage(string Format, params object[] Args)
{
SetMessage(String.Format(Format, Args));
}
/// <summary>
/// Appends a progress string to the status message. Overwrites any previous progress message.
/// </summary>
/// <param name="Progress">The progress message</param>
public void SetProgress(string Progress)
{
StringBuilder FullMessage = new StringBuilder(Message);
FullMessage.Append(' ');
FullMessage.Append(Progress);
Log.UpdateStatus(FullMessage.ToString());
}
/// <summary>
/// Appends a progress string to the status message. Overwrites any previous progress message.
/// </summary>
/// <param name="Format">The format specifier for the message</param>
/// <param name="Args">Arguments for the status message</param>
public void SetProgress(string Format, params object[] Args)
{
StringBuilder FullMessage = new StringBuilder(Message);
FullMessage.Append(' ');
FullMessage.AppendFormat(Format, Args);
Log.UpdateStatus(FullMessage.ToString());
}
/// <summary>
/// Pops the status message from the log.
/// </summary>
public void Dispose()
{
if(Message != null)
{
Log.PopStatus();
Message = null;
}
}
}
}