* Renamed "LeveledDebug" to "Alligator", cuz its funny name

* Changed debug writing to trace writing inside "Alligator"
* Added "HostedTelegramBotInfo" representing hosted version of ITelegramBotInfo. Provides access to services and configuration
* Filters now can acces services and configuration using new "HostedTelegramBotInfo" through context.BotInfo property
This commit is contained in:
2025-08-01 13:50:21 +04:00
parent 16e599bed8
commit 49310439e0
10 changed files with 84 additions and 45 deletions
@@ -3,14 +3,14 @@
namespace Telegrator
{
/// <summary>
/// Telegrator's Debug logger helper
/// Telegrator's FUNNY debug logger helper
/// </summary>
public static class LeveledDebug
public static class Alligator
{
/// <summary>
/// Gets or sets flags of what debug messages to write
/// </summary>
public static DebugLevel IndentFlags { get; set; } = DebugLevel.None;
public static DebugLevel Allowed { get; set; } = DebugLevel.None;
/// <summary>
/// Writes debug message if Indent level has Router flag
@@ -18,8 +18,8 @@ namespace Telegrator
/// <param name="message"></param>
public static void RouterWriteLine(string message)
{
if (IndentFlags.HasFlag(DebugLevel.Router))
Debug.WriteLine(message);
if (Allowed.HasFlag(DebugLevel.Router))
Trace.WriteLine(message);
}
/// <summary>
@@ -29,8 +29,8 @@ namespace Telegrator
/// <param name="args"></param>
public static void RouterWriteLine(string message, params object[] args)
{
if (IndentFlags.HasFlag(DebugLevel.Router))
Debug.WriteLine(message, args);
if (Allowed.HasFlag(DebugLevel.Router))
Trace.WriteLine(string.Format(message, args));
}
/// <summary>
@@ -39,8 +39,8 @@ namespace Telegrator
/// <param name="message"></param>
public static void ProviderWriteLine(string message)
{
if (IndentFlags.HasFlag(DebugLevel.Providers))
Debug.WriteLine(message);
if (Allowed.HasFlag(DebugLevel.Providers))
Trace.WriteLine(message);
}
/// <summary>
@@ -50,8 +50,8 @@ namespace Telegrator
/// <param name="args"></param>
public static void ProviderWriteLine(string message, params object[] args)
{
if (IndentFlags.HasFlag(DebugLevel.Providers))
Debug.WriteLine(message, args);
if (Allowed.HasFlag(DebugLevel.Providers))
Trace.WriteLine(string.Format(message, args));
}
/// <summary>
@@ -60,8 +60,8 @@ namespace Telegrator
/// <param name="message"></param>
public static void FilterWriteLine(string message)
{
if (IndentFlags.HasFlag(DebugLevel.Filters))
Debug.WriteLine(message);
if (Allowed.HasFlag(DebugLevel.Filters))
Trace.WriteLine(message);
}
/// <summary>
@@ -71,8 +71,8 @@ namespace Telegrator
/// <param name="args"></param>
public static void FilterWriteLine(string message, params object[] args)
{
if (IndentFlags.HasFlag(DebugLevel.Filters))
Debug.WriteLine(message, args);
if (Allowed.HasFlag(DebugLevel.Filters))
Trace.WriteLine(string.Format(message, args));
}
/// <summary>
@@ -81,8 +81,8 @@ namespace Telegrator
/// <param name="message"></param>
public static void PoolWriteLine(string message)
{
if (IndentFlags.HasFlag(DebugLevel.HandlersPool))
Debug.WriteLine(message);
if (Allowed.HasFlag(DebugLevel.HandlersPool))
Trace.WriteLine(message);
}
/// <summary>
@@ -92,8 +92,20 @@ namespace Telegrator
/// <param name="args"></param>
public static void PoolWriteLine(string message, params object[] args)
{
if (IndentFlags.HasFlag(DebugLevel.HandlersPool))
Debug.WriteLine(message, args);
if (Allowed.HasFlag(DebugLevel.HandlersPool))
Trace.WriteLine(string.Format(message, args));
}
public static void WriteLine(DebugLevel level, string message)
{
if (Allowed.HasFlag(level))
Trace.WriteLine(message);
}
public static void WriteLine(DebugLevel level, string message, params object[] args)
{
if (Allowed.HasFlag(level))
Trace.WriteLine(string.Format(message, args));
}
}
}
@@ -47,7 +47,7 @@ namespace Telegrator.Filters.Components
if (!filter.CanPass(context))
{
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter)
LeveledDebug.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
return false;
}
@@ -49,7 +49,7 @@ namespace Telegrator.Filters.Components
if (!filter.CanPass(context))
{
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter)
LeveledDebug.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
return false;
}
@@ -39,7 +39,7 @@
if (!filter.CanPass(context))
{
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter)
LeveledDebug.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
Alligator.FilterWriteLine("(E) {0} filter of {1} didnt pass!", filter.GetType().Name, context.Data["handler_name"]);
return false;
}
@@ -47,7 +47,7 @@ namespace Telegrator.MadiatorCore.Descriptors
{
if (!UpdateValidator.CanPass(filterContext))
{
LeveledDebug.FilterWriteLine("(E) UpdateValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
Alligator.FilterWriteLine("(E) UpdateValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
return false;
}
@@ -59,7 +59,7 @@ namespace Telegrator.MadiatorCore.Descriptors
{
if (!StateKeeperValidator.CanPass(filterContext))
{
LeveledDebug.FilterWriteLine("(E) StateKeeperValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
Alligator.FilterWriteLine("(E) StateKeeperValidator filter of {0} for Update ({1}) didnt pass!", filterContext.Data["handler_name"], filterContext.Update.Id);
return false;
}
@@ -74,7 +74,7 @@ namespace Telegrator.MadiatorCore.Descriptors
if (!filter.CanPass(filterContext))
{
if (filter is not AnonymousCompiledFilter && filter is not AnonymousTypeFilter)
LeveledDebug.FilterWriteLine("(E) {0} filter of {1} for Update ({2}) didnt pass!", filter.GetType().Name, filterContext.Data["handler_name"], filterContext.Update.Id);
Alligator.FilterWriteLine("(E) {0} filter of {1} for Update ({2}) didnt pass!", filter.GetType().Name, filterContext.Data["handler_name"], filterContext.Update.Id);
return false;
}
+15 -15
View File
@@ -84,7 +84,7 @@ namespace Telegrator.Polling
/// <returns>A task representing the asynchronous error handling operation.</returns>
public virtual Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, HandleErrorSource source, CancellationToken cancellationToken)
{
LeveledDebug.RouterWriteLine("Handling exception {0}", exception.GetType().Name);
Alligator.RouterWriteLine("Handling exception {0}", exception.GetType().Name);
ExceptionHandler?.HandleException(botClient, exception, source, cancellationToken);
return Task.CompletedTask;
}
@@ -99,7 +99,7 @@ namespace Telegrator.Polling
public virtual Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
// Logging
LeveledDebug.RouterWriteLine("Received Update ({0}) of type \"{1}\"", update.Id, update.Type);
Alligator.RouterWriteLine("Received Update ({0}) of type \"{1}\"", update.Id, update.Type);
LogUpdate(update);
try
@@ -114,24 +114,24 @@ namespace Telegrator.Polling
// Chicking if awaiting handlers has exclusive routing
if (Options.ExclusiveAwaitingHandlerRouting)
{
LeveledDebug.RouterWriteLine("Receiving Update ({0}) completed with only awaiting handlers", update.Id);
Alligator.RouterWriteLine("Receiving Update ({0}) completed with only awaiting handlers", update.Id);
return Task.CompletedTask;
}
}
// Queuing reagular handlers for execution
HandlersPool.Enqueue(GetHandlers(HandlersProvider, this, botClient, update, cancellationToken));
LeveledDebug.RouterWriteLine("Receiving Update ({0}) finished", update.Id);
Alligator.RouterWriteLine("Receiving Update ({0}) finished", update.Id);
return Task.CompletedTask;
}
catch (OperationCanceledException)
{
LeveledDebug.RouterWriteLine("Receiving Update ({0}) cancelled", update.Id);
Alligator.RouterWriteLine("Receiving Update ({0}) cancelled", update.Id);
return Task.CompletedTask;
}
catch (Exception ex)
{
LeveledDebug.RouterWriteLine("Receiving Update ({0}) finished with exception {1}", update.Id, ex.Message);
Alligator.RouterWriteLine("Receiving Update ({0}) finished with exception {1}", update.Id, ex.Message);
ExceptionHandler?.HandleException(botClient, ex, HandleErrorSource.PollingError, cancellationToken);
return Task.CompletedTask;
}
@@ -149,22 +149,22 @@ namespace Telegrator.Polling
/// <returns>A collection of described handler information for the update</returns>
protected virtual IEnumerable<DescribedHandlerInfo> GetHandlers(IHandlersProvider provider, IUpdateRouter updateRouter, ITelegramBotClient client, Update update, CancellationToken cancellationToken = default)
{
LeveledDebug.RouterWriteLine("Requested handlers for UpdateType.{0}", update.Type);
Alligator.RouterWriteLine("Requested handlers for UpdateType.{0}", update.Type);
if (!provider.TryGetDescriptorList(update.Type, out HandlerDescriptorList? descriptors))
{
LeveledDebug.RouterWriteLine("No registered, providing Any");
Alligator.RouterWriteLine("No registered, providing Any");
provider.TryGetDescriptorList(UpdateType.Unknown, out descriptors);
}
if (descriptors == null || descriptors.Count == 0)
{
LeveledDebug.RouterWriteLine("No handlers provided");
Alligator.RouterWriteLine("No handlers provided");
return [];
}
IEnumerable<DescribedHandlerInfo> described = DescribeDescriptors(provider, descriptors, updateRouter, client, update, cancellationToken);
LeveledDebug.RouterWriteLine("Described total of {0} handlers for Update ({1}) from {2} provider", described.Count(), update.Id, provider.GetType().Name);
LeveledDebug.RouterWriteLine("Described handlers : {0}", string.Join(", ", described));
Alligator.RouterWriteLine("Described total of {0} handlers for Update ({1}) from {2} provider", described.Count(), update.Id, provider.GetType().Name);
Alligator.RouterWriteLine("Described handlers : {0}", string.Join(", ", described));
return described;
}
@@ -183,7 +183,7 @@ namespace Telegrator.Polling
{
try
{
LeveledDebug.RouterWriteLine("Describing descriptors of descriptorsList.HandlingType.{0} for Update ({1})", descriptors.HandlingType, update.Id);
Alligator.RouterWriteLine("Describing descriptors of descriptorsList.HandlingType.{0} for Update ({1})", descriptors.HandlingType, update.Id);
foreach (HandlerDescriptor descriptor in descriptors.Reverse())
{
cancellationToken.ThrowIfCancellationRequested();
@@ -198,7 +198,7 @@ namespace Telegrator.Polling
}
finally
{
LeveledDebug.RouterWriteLine("Describing for Update ({0}) finished", update.Id);
Alligator.RouterWriteLine("Describing for Update ({0}) finished", update.Id);
}
}
@@ -252,7 +252,7 @@ namespace Telegrator.Polling
if (msg.Sticker != null)
sb.AppendFormat(" with sticker '{0}'", msg.Sticker.Emoji);
LeveledDebug.RouterWriteLine(sb.ToString());
Alligator.RouterWriteLine(sb.ToString());
break;
}
@@ -267,7 +267,7 @@ namespace Telegrator.Polling
if (cq.From != null)
sb.AppendFormat(" with data '{0}'", cq.Data);
LeveledDebug.RouterWriteLine(sb.ToString());
Alligator.RouterWriteLine(sb.ToString());
break;
}
}
+3 -3
View File
@@ -40,7 +40,7 @@ namespace Telegrator.Providers
AllowedTypes = handlers.AllowedTypes;
HandlersDictionary = handlers.Values.ForEach(list => list.Freeze()).ToReadOnlyDictionary(list => list.HandlingType);
Options = options ?? throw new ArgumentNullException(nameof(options));
LeveledDebug.ProviderWriteLine("{0} created!", GetType().Name);
Alligator.ProviderWriteLine("{0} created!", GetType().Name);
}
/// <summary>
@@ -54,7 +54,7 @@ namespace Telegrator.Providers
AllowedTypes = Update.AllTypes;
HandlersDictionary = handlers.ForEach(list => list.Freeze()).ToReadOnlyDictionary(list => list.HandlingType);
Options = options ?? throw new ArgumentNullException(nameof(options));
LeveledDebug.ProviderWriteLine("{0} created!", GetType().Name);
Alligator.ProviderWriteLine("{0} created!", GetType().Name);
}
/// <inheritdoc/>
@@ -76,7 +76,7 @@ namespace Telegrator.Providers
}
catch
{
LeveledDebug.ProviderWriteLine("Failed to create instance of {0}", descriptor.ToString());
Alligator.ProviderWriteLine("Failed to create instance of {0}", descriptor.ToString());
throw;
}
}