-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLogMessage.cs
111 lines (102 loc) · 4 KB
/
LogMessage.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Collections;
namespace VolumeControl.Log
{
/// <summary>
/// Represents a message to be written to the log.
/// </summary>
public sealed class LogMessage : IEnumerable<object?>, IEnumerable
{
#region Constructor
/// <summary>
/// Creates a new <see cref="LogMessage"/> instance with the specified <paramref name="eventType"/>.
/// </summary>
/// <param name="eventType">The <see cref="Log.EventType"/> of this message.</param>
/// <param name="lines">The contents of the message, where each element represents one "line".</param>
public LogMessage(EventType eventType, params object?[] lines)
{
EventType = eventType;
Lines = lines.ToList();
}
/// <summary>
/// Creates a new empty <see cref="LogMessage"/> instance with the specified <paramref name="eventType"/>.
/// </summary>
/// <param name="eventType">The <see cref="Log.EventType"/> of this message.</param>
public LogMessage(EventType eventType)
{
EventType = eventType;
Lines = new();
}
/// <summary>
/// Creates a new <see cref="LogMessage"/> instance with the specified <paramref name="lines"/> and an undefined EventType.
/// </summary>
/// <param name="lines">The contents of the message, where each element represents one "line".</param>
public LogMessage(params object?[] lines)
{
EventType = EventType.NONE;
Lines = lines.ToList();
}
#endregion Constructor
#region Properties
/// <summary>
/// Gets or sets the event type of this message.
/// </summary>
public EventType EventType { get; set; }
/// <summary>
/// Gets whether this <see cref="LogMessage"/> instance has an EventType or not.
/// </summary>
public bool HasEventType => EventType != EventType.NONE;
/// <summary>
/// Gets or sets the lines in this message.
/// </summary>
public List<object?> Lines { get; set; }
/// <summary>
/// Gets whether there are any lines in this message.
/// </summary>
public bool IsEmpty => Lines.Count == 0;
#endregion Properties
#region Operators
/// <summary>
/// Converts a <see cref="string"/> to a new <see cref="LogMessage"/> instance.
/// </summary>
/// <param name="s">The string to convert.</param>
public static implicit operator LogMessage(string s) => new(s);
#endregion Operators
#region Methods
#region ToString
/// <summary>
/// Gets the contents of the log message as a single string.
/// </summary>
public override string ToString() => string.Join(Environment.NewLine, Lines);
#endregion ToString
#region Add
/// <summary>
/// Appends the specified <paramref name="line"/> to the message.
/// </summary>
/// <param name="line">The content of the line.</param>
/// <returns>This instance.</returns>
public LogMessage Add(object? line)
{
Lines.Add(line);
return this;
}
#endregion Add
#region SetEventType
/// <summary>
/// Pipeline method that sets the event type of this log message.
/// </summary>
/// <param name="eventType">The EventType to use for this log message.</param>
/// <returns>This instance.</returns>
public LogMessage SetEventType(EventType eventType)
{
EventType = eventType;
return this;
}
#endregion SetEventType
#endregion Methods
#region IEnumerable
/// <inheritdoc/>
public IEnumerator<object?> GetEnumerator() => ((IEnumerable<object?>)this.Lines).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this.Lines).GetEnumerator();
#endregion IEnumerable
}
}