@@ -1,105 +0,0 @@ | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Logging | |||||
{ | |||||
internal class LogManager | |||||
{ | |||||
public LogSeverity Level { get; } | |||||
private Logger ClientLogger { get; } | |||||
public event Func<LogMessage, Task> Message { add { _messageEvent.Add(value); } remove { _messageEvent.Remove(value); } } | |||||
private readonly AsyncEvent<Func<LogMessage, Task>> _messageEvent = new AsyncEvent<Func<LogMessage, Task>>(); | |||||
public LogManager(LogSeverity minSeverity) | |||||
{ | |||||
Level = minSeverity; | |||||
ClientLogger = new Logger(this, "Discord"); | |||||
} | |||||
public async Task LogAsync(LogSeverity severity, string source, Exception ex) | |||||
{ | |||||
try | |||||
{ | |||||
if (severity <= Level) | |||||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, null, ex)).ConfigureAwait(false); | |||||
} | |||||
catch | |||||
{ | |||||
// ignored | |||||
} | |||||
} | |||||
public async Task LogAsync(LogSeverity severity, string source, string message, Exception ex = null) | |||||
{ | |||||
try | |||||
{ | |||||
if (severity <= Level) | |||||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, message, ex)).ConfigureAwait(false); | |||||
} | |||||
catch | |||||
{ | |||||
// ignored | |||||
} | |||||
} | |||||
public async Task LogAsync(LogSeverity severity, string source, FormattableString message, Exception ex = null) | |||||
{ | |||||
try | |||||
{ | |||||
if (severity <= Level) | |||||
await _messageEvent.InvokeAsync(new LogMessage(severity, source, message.ToString(), ex)).ConfigureAwait(false); | |||||
} | |||||
catch { } | |||||
} | |||||
public Task ErrorAsync(string source, Exception ex) | |||||
=> LogAsync(LogSeverity.Error, source, ex); | |||||
public Task ErrorAsync(string source, string message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Error, source, message, ex); | |||||
public Task ErrorAsync(string source, FormattableString message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Error, source, message, ex); | |||||
public Task WarningAsync(string source, Exception ex) | |||||
=> LogAsync(LogSeverity.Warning, source, ex); | |||||
public Task WarningAsync(string source, string message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Warning, source, message, ex); | |||||
public Task WarningAsync(string source, FormattableString message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Warning, source, message, ex); | |||||
public Task InfoAsync(string source, Exception ex) | |||||
=> LogAsync(LogSeverity.Info, source, ex); | |||||
public Task InfoAsync(string source, string message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Info, source, message, ex); | |||||
public Task InfoAsync(string source, FormattableString message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Info, source, message, ex); | |||||
public Task VerboseAsync(string source, Exception ex) | |||||
=> LogAsync(LogSeverity.Verbose, source, ex); | |||||
public Task VerboseAsync(string source, string message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | |||||
public Task VerboseAsync(string source, FormattableString message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Verbose, source, message, ex); | |||||
public Task DebugAsync(string source, Exception ex) | |||||
=> LogAsync(LogSeverity.Debug, source, ex); | |||||
public Task DebugAsync(string source, string message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Debug, source, message, ex); | |||||
public Task DebugAsync(string source, FormattableString message, Exception ex = null) | |||||
=> LogAsync(LogSeverity.Debug, source, message, ex); | |||||
public Logger CreateLogger(string name) => new Logger(this, name); | |||||
public async Task WriteInitialLog() | |||||
{ | |||||
await ClientLogger.InfoAsync($"Discord.Net v{DiscordConfig.Version} (API v{DiscordConfig.APIVersion})").ConfigureAwait(false); | |||||
} | |||||
} | |||||
} |
@@ -1,136 +0,0 @@ | |||||
using System; | |||||
using System.Text; | |||||
namespace Discord | |||||
{ | |||||
/// <summary> | |||||
/// Provides a message object used for logging purposes. | |||||
/// </summary> | |||||
public struct LogMessage | |||||
{ | |||||
/// <summary> | |||||
/// Gets the severity of the log entry. | |||||
/// </summary> | |||||
/// <returns> | |||||
/// A <see cref="LogSeverity"/> enum to indicate the severeness of the incident or event. | |||||
/// </returns> | |||||
public LogSeverity Severity { get; } | |||||
/// <summary> | |||||
/// Gets the source of the log entry. | |||||
/// </summary> | |||||
/// <returns> | |||||
/// A string representing the source of the log entry. | |||||
/// </returns> | |||||
public string Source { get; } | |||||
/// <summary> | |||||
/// Gets the message of this log entry. | |||||
/// </summary> | |||||
/// <returns> | |||||
/// A string containing the message of this log entry. | |||||
/// </returns> | |||||
public string Message { get; } | |||||
/// <summary> | |||||
/// Gets the exception of this log entry. | |||||
/// </summary> | |||||
/// <returns> | |||||
/// An <see cref="Discord.LogMessage.Exception" /> object associated with an incident; otherwise <c>null</c>. | |||||
/// </returns> | |||||
public Exception Exception { get; } | |||||
/// <summary> | |||||
/// Initializes a new <see cref="LogMessage"/> struct with the severity, source, message of the event, and | |||||
/// optionally, an exception. | |||||
/// </summary> | |||||
/// <param name="severity">The severity of the event.</param> | |||||
/// <param name="source">The source of the event.</param> | |||||
/// <param name="message">The message of the event.</param> | |||||
/// <param name="exception">The exception of the event.</param> | |||||
public LogMessage(LogSeverity severity, string source, string message, Exception exception = null) | |||||
{ | |||||
Severity = severity; | |||||
Source = source; | |||||
Message = message; | |||||
Exception = exception; | |||||
} | |||||
public override string ToString() => ToString(); | |||||
public string ToString(StringBuilder builder = null, bool fullException = true, bool prependTimestamp = true, DateTimeKind timestampKind = DateTimeKind.Local, int? padSource = 11) | |||||
{ | |||||
string sourceName = Source; | |||||
string message = Message; | |||||
string exMessage = fullException ? Exception?.ToString() : Exception?.Message; | |||||
int maxLength = 1 + | |||||
(prependTimestamp ? 8 : 0) + 1 + | |||||
(padSource.HasValue ? padSource.Value : sourceName?.Length ?? 0) + 1 + | |||||
(message?.Length ?? 0) + | |||||
(exMessage?.Length ?? 0) + 3; | |||||
if (builder == null) | |||||
builder = new StringBuilder(maxLength); | |||||
else | |||||
{ | |||||
builder.Clear(); | |||||
builder.EnsureCapacity(maxLength); | |||||
} | |||||
if (prependTimestamp) | |||||
{ | |||||
DateTime now; | |||||
if (timestampKind == DateTimeKind.Utc) | |||||
now = DateTime.UtcNow; | |||||
else | |||||
now = DateTime.Now; | |||||
if (now.Hour < 10) | |||||
builder.Append('0'); | |||||
builder.Append(now.Hour); | |||||
builder.Append(':'); | |||||
if (now.Minute < 10) | |||||
builder.Append('0'); | |||||
builder.Append(now.Minute); | |||||
builder.Append(':'); | |||||
if (now.Second < 10) | |||||
builder.Append('0'); | |||||
builder.Append(now.Second); | |||||
builder.Append(' '); | |||||
} | |||||
if (sourceName != null) | |||||
{ | |||||
if (padSource.HasValue) | |||||
{ | |||||
if (sourceName.Length < padSource.Value) | |||||
{ | |||||
builder.Append(sourceName); | |||||
builder.Append(' ', padSource.Value - sourceName.Length); | |||||
} | |||||
else if (sourceName.Length > padSource.Value) | |||||
builder.Append(sourceName.Substring(0, padSource.Value)); | |||||
else | |||||
builder.Append(sourceName); | |||||
} | |||||
builder.Append(' '); | |||||
} | |||||
if (!string.IsNullOrEmpty(Message)) | |||||
{ | |||||
for (int i = 0; i < message.Length; i++) | |||||
{ | |||||
//Strip control chars | |||||
char c = message[i]; | |||||
if (!char.IsControl(c)) | |||||
builder.Append(c); | |||||
} | |||||
} | |||||
if (exMessage != null) | |||||
{ | |||||
if (!string.IsNullOrEmpty(Message)) | |||||
{ | |||||
builder.Append(':'); | |||||
builder.AppendLine(); | |||||
} | |||||
builder.Append(exMessage); | |||||
} | |||||
return builder.ToString(); | |||||
} | |||||
} | |||||
} |
@@ -1,34 +0,0 @@ | |||||
namespace Discord | |||||
{ | |||||
/// <summary> | |||||
/// Specifies the severity of the log message. | |||||
/// </summary> | |||||
public enum LogSeverity | |||||
{ | |||||
/// <summary> | |||||
/// Logs that contain the most severe level of error. This type of error indicate that immediate attention | |||||
/// may be required. | |||||
/// </summary> | |||||
Critical = 0, | |||||
/// <summary> | |||||
/// Logs that highlight when the flow of execution is stopped due to a failure. | |||||
/// </summary> | |||||
Error = 1, | |||||
/// <summary> | |||||
/// Logs that highlight an abnormal activity in the flow of execution. | |||||
/// </summary> | |||||
Warning = 2, | |||||
/// <summary> | |||||
/// Logs that track the general flow of the application. | |||||
/// </summary> | |||||
Info = 3, | |||||
/// <summary> | |||||
/// Logs that are used for interactive investigation during development. | |||||
/// </summary> | |||||
Verbose = 4, | |||||
/// <summary> | |||||
/// Logs that contain the most detailed messages. | |||||
/// </summary> | |||||
Debug = 5 | |||||
} | |||||
} |
@@ -1,72 +0,0 @@ | |||||
using System; | |||||
using System.Threading.Tasks; | |||||
namespace Discord.Logging | |||||
{ | |||||
internal class Logger | |||||
{ | |||||
private readonly LogManager _manager; | |||||
public string Name { get; } | |||||
public LogSeverity Level => _manager.Level; | |||||
public Logger(LogManager manager, string name) | |||||
{ | |||||
_manager = manager; | |||||
Name = name; | |||||
} | |||||
public Task LogAsync(LogSeverity severity, Exception exception = null) | |||||
=> _manager.LogAsync(severity, Name, exception); | |||||
public Task LogAsync(LogSeverity severity, string message, Exception exception = null) | |||||
=> _manager.LogAsync(severity, Name, message, exception); | |||||
public Task LogAsync(LogSeverity severity, FormattableString message, Exception exception = null) | |||||
=> _manager.LogAsync(severity, Name, message, exception); | |||||
public Task ErrorAsync(Exception exception) | |||||
=> _manager.ErrorAsync(Name, exception); | |||||
public Task ErrorAsync(string message, Exception exception = null) | |||||
=> _manager.ErrorAsync(Name, message, exception); | |||||
public Task ErrorAsync(FormattableString message, Exception exception = null) | |||||
=> _manager.ErrorAsync(Name, message, exception); | |||||
public Task WarningAsync(Exception exception) | |||||
=> _manager.WarningAsync(Name, exception); | |||||
public Task WarningAsync(string message, Exception exception = null) | |||||
=> _manager.WarningAsync(Name, message, exception); | |||||
public Task WarningAsync(FormattableString message, Exception exception = null) | |||||
=> _manager.WarningAsync(Name, message, exception); | |||||
public Task InfoAsync(Exception exception) | |||||
=> _manager.InfoAsync(Name, exception); | |||||
public Task InfoAsync(string message, Exception exception = null) | |||||
=> _manager.InfoAsync(Name, message, exception); | |||||
public Task InfoAsync(FormattableString message, Exception exception = null) | |||||
=> _manager.InfoAsync(Name, message, exception); | |||||
public Task VerboseAsync(Exception exception) | |||||
=> _manager.VerboseAsync(Name, exception); | |||||
public Task VerboseAsync(string message, Exception exception = null) | |||||
=> _manager.VerboseAsync(Name, message, exception); | |||||
public Task VerboseAsync(FormattableString message, Exception exception = null) | |||||
=> _manager.VerboseAsync(Name, message, exception); | |||||
public Task DebugAsync(Exception exception) | |||||
=> _manager.DebugAsync(Name, exception); | |||||
public Task DebugAsync(string message, Exception exception = null) | |||||
=> _manager.DebugAsync(Name, message, exception); | |||||
public Task DebugAsync(FormattableString message, Exception exception = null) | |||||
=> _manager.DebugAsync(Name, message, exception); | |||||
} | |||||
} |