Skip to content

Commit

Permalink
Possibility to set custom logger in options
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Oct 16, 2024
1 parent 083a121 commit a09ea52
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# pragma warning disable
// auto-generated by {{version}}

using global::Yapoml.Playwright;

namespace {{root_namespace}}
{
[global::System.Runtime.CompilerServices.CompilerGenerated]
Expand Down Expand Up @@ -57,28 +59,51 @@ namespace {{root_namespace}}
{
var spaceOptions = new global::Yapoml.Framework.Options.SpaceOptions();

var eventSource = new global::Yapoml.Playwright.Events.EventSource();
spaceOptions.Services.Register<global::Yapoml.Playwright.Events.IEventSource>(eventSource);

spaceOptionsCallback?.Invoke(spaceOptions);

// default logger
var logger = new global::Yapoml.Framework.Logging.Logger();
spaceOptions.Services.Register<global::Yapoml.Framework.Logging.ILogger>(logger);
global::Yapoml.Framework.Logging.ILogger logger;
if (!spaceOptions.Services.TryGet<global::Yapoml.Framework.Logging.ILogger>(out logger))
{
logger = new global::Yapoml.Framework.Logging.Logger();
spaceOptions.Services.Register<global::Yapoml.Framework.Logging.ILogger>(logger);
}
var consoleLoggerSink = new global::Yapoml.Framework.Logging.Sinks.ConsoleLoggerSink(logger);

// default factories
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.ISpaceFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultSpaceFactory());
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IPageFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultPageFactory());
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IComponentFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultComponentFactory());
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IElementsListHandlerFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultElementsListHandlerFactory());

spaceOptions.Services.Register<global::Yapoml.Playwright.Options.TimeoutOptions>(new global::Yapoml.Playwright.Options.TimeoutOptions(global::System.TimeSpan.FromSeconds(30), global::System.TimeSpan.FromMilliseconds(200)));
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Services.Factory.ISpaceFactory>(out _))
{
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.ISpaceFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultSpaceFactory());
}
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Services.Factory.IPageFactory>(out _))
{
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IPageFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultPageFactory());
}
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Services.Factory.IComponentFactory>(out _))
{
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IComponentFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultComponentFactory());
}
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Services.Factory.IElementsListHandlerFactory>(out _))
{
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Factory.IElementsListHandlerFactory>(new global::Yapoml.Playwright.Services.Factory.DefaultElementsListHandlerFactory());
}

spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Locator.IElementLocator>(new global::Yapoml.Playwright.Services.Locator.DefaultElementLocator());
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Options.TimeoutOptions>(out _))
{
spaceOptions.WithTimeout();
}

spaceOptions.Services.Register<global::Yapoml.Playwright.Events.IEventSource>(new global::Yapoml.Playwright.Events.EventSource());

var logProducer = new global::Yapoml.Playwright.Events.LogEventsProducer(spaceOptions);
if (!spaceOptions.Services.TryGet<global::Yapoml.Playwright.Services.Locator.IElementLocator>(out _))
{
spaceOptions.Services.Register<global::Yapoml.Playwright.Services.Locator.IElementLocator>(new global::Yapoml.Playwright.Services.Locator.DefaultElementLocator());
}

var logProducer = new global::Yapoml.Playwright.Events.LogEventsProducer(logger, eventSource);
logProducer.Init();

spaceOptionsCallback?.Invoke(spaceOptions);

var yaSpace = new YaSpace(null, driver, spaceOptions);
spaceOptions.Services.Register(yaSpace);

Expand Down
7 changes: 3 additions & 4 deletions src/Yapoml.Playwright/Events/LogEventsProducer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Yapoml.Framework.Logging;
using Yapoml.Framework.Options;
using Yapoml.Playwright.Events.Args.Element;
using Yapoml.Playwright.Events.Args.Page;

Expand All @@ -11,10 +10,10 @@ public class LogEventsProducer

private readonly IEventSource _source;

public LogEventsProducer(ISpaceOptions spaceOptions)
public LogEventsProducer(ILogger logger, IEventSource eventSource)
{
_logger = spaceOptions.Services.Get<ILogger>();
_source = spaceOptions.Services.Get<IEventSource>();
_logger = logger;
_source = eventSource;
}

public void Init()
Expand Down
2 changes: 1 addition & 1 deletion src/Yapoml.Playwright/Extensions/ServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class ServiceExtensions
{
public static ISpaceOptions WithService<T>(this ISpaceOptions spaceOptions, T service)
{
spaceOptions.Services.Register<T>(service);
spaceOptions.Services.Register(service);

return spaceOptions;
}
Expand Down
12 changes: 2 additions & 10 deletions src/Yapoml.Playwright/Extensions/TimeoutExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,9 @@ public static class TimeoutExtensions
{
public static ISpaceOptions WithTimeout(this ISpaceOptions spaceOptions, TimeSpan? timeout = null, TimeSpan? pollingInterval = null)
{
var timeoutOptions = spaceOptions.Services.Get<TimeoutOptions>();
var timeoutOptions = new TimeoutOptions(timeout, pollingInterval);

if (timeout.HasValue)
{
timeoutOptions.Timeout = timeout.Value;
}

if (pollingInterval.HasValue)
{
timeoutOptions.PollingInterval = pollingInterval.Value;
}
spaceOptions.WithService(timeoutOptions);

return spaceOptions;
}
Expand Down
11 changes: 6 additions & 5 deletions src/Yapoml.Playwright/Options/TimeoutOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ namespace Yapoml.Playwright.Options
{
public class TimeoutOptions
{
public TimeoutOptions(TimeSpan timeout, TimeSpan pollingInterval)
public TimeoutOptions(TimeSpan? timeout, TimeSpan? pollingInterval)
{
Timeout = timeout;
PollingInterval = pollingInterval;
Timeout = timeout ?? TimeSpan.FromSeconds(30);
PollingInterval = pollingInterval ?? TimeSpan.FromMilliseconds(200);
}

public TimeSpan Timeout { get; set; }
public TimeSpan PollingInterval { get; set; }
public TimeSpan Timeout { get; private set; }

public TimeSpan PollingInterval { get; private set; }
}
}

0 comments on commit a09ea52

Please sign in to comment.