Skip to content

Commit

Permalink
2024.52
Browse files Browse the repository at this point in the history
- LogLevel to Debug Messages
- Added LevelHandler and ErrorHandler
- Update IObserver.cs
  • Loading branch information
simon-techkid committed Jul 8, 2024
1 parent f42f3f4 commit 0fd2a4c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
27 changes: 23 additions & 4 deletions SpotifyGPX/Broadcasting/Broadcaster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,31 @@ protected Broadcaster()
/// <param name="level">The <see cref="LogLevel"/> of the message.</param>
public void Broadcast(T message, LogLevel level = DefaultForMessages)
{
T newMessage = BroadcastHandler(message);
T broadcastMessage = BroadcastHandler(message, level);

foreach (var observer in Observers.Where(observer => observer.MessageMatcher(level)))
{
observer.OnNext(newMessage);
T levelSpecificMessage = LevelHandler(broadcastMessage, observer.Level);

observer.OnNext(levelSpecificMessage);
}
}

/// <summary>
/// Allow special handling by the <see cref="Broadcaster{T}"/> class of the message of type <typeparamref name="T"/> before it is sent.
/// Allow special handling by the <see cref="Broadcaster{T}"/> class of the message of type <typeparamref name="T"/> before it is sent based on the <see cref="LogLevel"/> of the message.
/// </summary>
/// <param name="message">A message of type <typeparamref name="T"/> to modify based on broadcaster-specific parameters.</param>
/// <param name="l">The log level of this message.</param>
/// <returns>The message to be broadcast of type <typeparamref name="T"/>.</returns>
protected virtual T BroadcastHandler(T message) => message;
protected virtual T BroadcastHandler(T message, LogLevel l) => message;

/// <summary>
/// Allow special handling by the <see cref="Broadcaster{T}"/> class of the message of type <typeparamref name="T"/> before it is sent based on the <see cref="LogLevel"/> of the observer the message is being sent to.
/// </summary>
/// <param name="message">A message of type <typeparamref name="T"/> to modify based on broadcaster-specific parameters.</param>
/// <param name="l">The log level of the observer the message is being sent to.</param>
/// <returns>The message to be broadcast of type <typeparamref name="T"/>.</returns>
protected virtual T LevelHandler(T message, LogLevel l) => message;

/// <summary>
/// Broadcast an error (of type <see cref="Exception"/>) to all observers of type <typeparamref name="T"/>.
Expand All @@ -60,6 +71,14 @@ public void BroadcastError(Exception error)
}
}

/// <summary>
/// Allow special handling by the <see cref="Broadcaster{T}"/> class of the error (of type <see cref="Exception"/>) before it is sent.
/// </summary>
/// <param name="error">An <see cref="Exception"/> to modify based on broadcaster-specific parameters.</param>
/// <param name="l">The log level of this message.</param>
/// <returns>The message to be broadcast of type <see cref="Exception"/>.</returns>
protected virtual Exception ErrorHandler(Exception error, LogLevel l) => error;

/// <summary>
/// The default <see cref="LogLevel"/> to use for error messages is <see cref="LogLevel.Error"/>.
/// </summary>
Expand Down
17 changes: 13 additions & 4 deletions SpotifyGPX/Broadcasting/StringBroadcaster.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
// SpotifyGPX by Simon Field

using SpotifyGPX.Observation;

namespace SpotifyGPX.Broadcasting;

public class StringBroadcaster : Broadcaster<string>
{
public string Type { get; set; } = "INFO";

protected override string BroadcastHandler(string message)
protected override string BroadcastHandler(string message, LogLevel l)
{
return $"[{Type}] {message}";
//return $"[{HashCode}] [{Type}] {message}";
// optionally, show the hash code of the broadcaster
// When observers handle LogLevel, allow Debug observer to see hash codes
}

protected override string LevelHandler(string message, LogLevel l)
{
if (l == LogLevel.Debug)
{
return $"[OBSERVER-{HashCode}] [{l.ToString().ToUpper()}] {message}";
}

return message;
}

protected override void AdditionalSubscriptionInstructions()
Expand Down
1 change: 1 addition & 0 deletions SpotifyGPX/Observation/IObserver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace SpotifyGPX.Observation;

public interface IObserver<in T>
{
bool Silent { get; set; }
void OnCompleted();
void OnError(Exception error);
void OnNext(T value);
Expand Down
2 changes: 1 addition & 1 deletion SpotifyGPX/Observation/Observer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void OnNext(T value)
/// <summary>
/// Handles a new message produced by the observable sequence.
/// </summary>
/// <param name="message"></param>
/// <param name="message">The message of type <typeparamref name="T"/> to take in.</param>
protected abstract void HandleMessage(T message);

/// <summary>
Expand Down

0 comments on commit 0fd2a4c

Please sign in to comment.