Skip to content

Commit

Permalink
Merge pull request #10 from veeman/feature/log4net-optional
Browse files Browse the repository at this point in the history
Make log4net library optional
  • Loading branch information
frederikja163 authored Aug 28, 2020
2 parents 7389b59 + 64ea2b9 commit 65b1338
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 56 deletions.
11 changes: 9 additions & 2 deletions Examples/ExampleBrowserEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ public static class ExampleBrowserEntry
[STAThread]
public static void Main()
{
// initialize log4net via app.config
XmlConfigurator.Configure();
// initialize log4net via app.config if available
if (ObjectTK.Logging.LogFactory.IsAvailable)
ConfigureLogging();

// show example browser
using (var browser = new ExampleBrowser())
{
Application.Run(browser);
}
}

public static void ConfigureLogging()
{
XmlConfigurator.Configure();
}
}
}
21 changes: 10 additions & 11 deletions ObjectTK.Tools/DerpWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//

using System;
using log4net;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
Expand All @@ -22,7 +21,7 @@ namespace ObjectTK.Tools
public abstract class DerpWindow
: GameWindow
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(DerpWindow));
private static readonly Logging.IObjectTKLogger Logger = Logging.LogFactory.GetLogger(typeof(DerpWindow));

protected readonly FrameTimer FrameTimer;

Expand All @@ -33,16 +32,16 @@ protected DerpWindow(int width, int height, GraphicsMode mode, string title)
: base(width, height, mode, title)
{
// log some OpenGL information
Logger.Info("OpenGL context information:");
Logger.InfoFormat("{0}: {1}", StringName.Vendor, GL.GetString(StringName.Vendor));
Logger.InfoFormat("{0}: {1}", StringName.Renderer, GL.GetString(StringName.Renderer));
Logger.InfoFormat("{0}: {1}", StringName.Version, GL.GetString(StringName.Version));
Logger.InfoFormat("{0}: {1}", StringName.ShadingLanguageVersion, GL.GetString(StringName.ShadingLanguageVersion));
Logger?.Info("OpenGL context information:");
Logger?.InfoFormat("{0}: {1}", StringName.Vendor, GL.GetString(StringName.Vendor));
Logger?.InfoFormat("{0}: {1}", StringName.Renderer, GL.GetString(StringName.Renderer));
Logger?.InfoFormat("{0}: {1}", StringName.Version, GL.GetString(StringName.Version));
Logger?.InfoFormat("{0}: {1}", StringName.ShadingLanguageVersion, GL.GetString(StringName.ShadingLanguageVersion));
int numExtensions;
GL.GetInteger(GetPName.NumExtensions, out numExtensions);
Logger.DebugFormat("Number available extensions: {0}", numExtensions);
for (var i = 0; i < numExtensions; i++) Logger.DebugFormat("{0}: {1}", i, GL.GetString(StringNameIndexed.Extensions, i));
Logger.InfoFormat("Initializing game window: {0}", title);
Logger?.DebugFormat("Number available extensions: {0}", numExtensions);
for (var i = 0; i < numExtensions; i++) Logger?.DebugFormat("{0}: {1}", i, GL.GetString(StringNameIndexed.Extensions, i));
Logger?.InfoFormat("Initializing game window: {0}", title);
// set up GameWindow events
Resize += OnResize;
UpdateFrame += OnUpdateFrame;
Expand All @@ -52,7 +51,7 @@ protected DerpWindow(int width, int height, GraphicsMode mode, string title)

private void OnResize(object sender, EventArgs eventArgs)
{
Logger.InfoFormat("Window resized to: {0}x{1}", Width, Height);
Logger?.InfoFormat("Window resized to: {0}x{1}", Width, Height);
}

private void OnUpdateFrame(object sender, FrameEventArgs e)
Expand Down
4 changes: 0 additions & 4 deletions ObjectTK.Tools/ObjectTK.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
<Compile Include="Shapes\TexturedShape.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
</Reference>
<Reference Include="OpenTK, Version=1.1.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\OpenTK.1.1.1589.5942\lib\NET40\OpenTK.dll</HintPath>
Expand Down
1 change: 0 additions & 1 deletion ObjectTK.Tools/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net40" />
<package id="OpenTK" version="1.1.1589.5942" targetFramework="net40" />
</packages>
5 changes: 2 additions & 3 deletions ObjectTK/GLResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

using System;
using System.Reflection;
using log4net;
using ObjectTK.Exceptions;

namespace ObjectTK
Expand All @@ -22,7 +21,7 @@ namespace ObjectTK
public abstract class GLResource
: IDisposable
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(GLResource));
private static readonly Logging.IObjectTKLogger Logger = Logging.LogFactory.GetLogger(typeof(GLResource));

/// <summary>
/// Gets a values specifying if this resource has already been disposed.
Expand All @@ -42,7 +41,7 @@ protected GLResource()
/// </summary>
~GLResource()
{
Logger.WarnFormat("GLResource leaked: {0}", this);
Logger?.WarnFormat("GLResource leaked: {0}", this);
Dispose(false);
#if DEBUG
throw new ObjectTKException(string.Format("GLResource leaked: {0}", this));
Expand Down
135 changes: 135 additions & 0 deletions ObjectTK/Logging/DefaultLogImpl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// DefaultLogImpl.cs
//
// Copyright (C) 2018 OpenTK
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
//

using System;
using log4net;
using log4net.Core;

namespace ObjectTK.Logging
{
public class DefaultLogImpl : IObjectTKLogger
{
protected virtual Type ThisDeclaringType => typeof(DefaultLogImpl);
protected ILogger _logger => _log.Logger;

protected readonly ILog _log;

public DefaultLogImpl(ILog logger)
{
_log = logger;
}

public bool IsFatalEnabled => _log.IsFatalEnabled;

public bool IsWarnEnabled => _log.IsWarnEnabled;

public bool IsInfoEnabled => _log.IsInfoEnabled;

public bool IsDebugEnabled => _log.IsDebugEnabled;

public bool IsErrorEnabled => _log.IsErrorEnabled;

private void Log(Level level, object message, Exception exception = null)
{
if (!_logger.IsEnabledFor(level)) return;
_logger.Log(ThisDeclaringType, level, message, exception);
}

private void LogFormat(Level level, IFormatProvider provider, string format, params object[] args)
{
if (!_logger.IsEnabledFor(level)) return;

var message = (provider == null) ?
string.Format(format, args) :
string.Format(provider, format, args);

_logger.Log(ThisDeclaringType, level, message, null);
}

private void LogFormat(Level level, string format, params object[] args)
{
LogFormat(null, format, args);
}

public void Debug(object message, Exception exception = null)
{
Log(Level.Debug, message);
}

public void DebugFormat(IFormatProvider provider, string format, params object[] args)
{
LogFormat(Level.Debug, provider, format, args);
}

public void DebugFormat(string format, params object[] args)
{
LogFormat(Level.Debug, null, format, args);
}

public void Error(object message, Exception exception = null)
{
Log(Level.Error, message);
}

public void ErrorFormat(IFormatProvider provider, string format, params object[] args)
{
LogFormat(Level.Error, provider, format, args);
}

public void ErrorFormat(string format, params object[] args)
{
LogFormat(Level.Error, null, format, args);
}

public void Fatal(object message, Exception exception = null)
{
Log(Level.Fatal, message);
}

public void FatalFormat(IFormatProvider provider, string format, params object[] args)
{
LogFormat(Level.Fatal, provider, format, args);
}

public void FatalFormat(string format, params object[] args)
{
LogFormat(Level.Fatal, null, format, args);
}

public void Info(object message, Exception exception = null)
{
Log(Level.Info, message);
}

public void InfoFormat(IFormatProvider provider, string format, params object[] args)
{
LogFormat(Level.Info, provider, format, args);
}

public void InfoFormat(string format, params object[] args)
{
LogFormat(Level.Info, null, format, args);
}

public void Warn(object message, Exception exception = null)
{
Log(Level.Warn, message);
}

public void WarnFormat(IFormatProvider provider, string format, params object[] args)
{
LogFormat(Level.Warn, provider, format, args);
}

public void WarnFormat(string format, params object[] args)
{
LogFormat(Level.Warn, null, format, args);
}
}
}
45 changes: 45 additions & 0 deletions ObjectTK/Logging/IObjectTKLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// IObjectTKLogger.cs
//
// Copyright (C) 2018 OpenTK
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ObjectTK.Logging
{
public interface IObjectTKLogger
{
bool IsFatalEnabled { get; }
bool IsWarnEnabled { get; }
bool IsInfoEnabled { get; }
bool IsDebugEnabled { get; }
bool IsErrorEnabled { get; }

void Debug(object message, Exception exception = null);
void DebugFormat(IFormatProvider provider, string format, params object[] args);
void DebugFormat(string format, params object[] args);

void Error(object message, Exception exception = null);
void ErrorFormat(IFormatProvider provider, string format, params object[] args);
void ErrorFormat(string format, params object[] args);

void Fatal(object message, Exception exception = null);
void FatalFormat(IFormatProvider provider, string format, params object[] args);
void FatalFormat(string format, params object[] args);

void Info(object message, Exception exception = null);
void InfoFormat(IFormatProvider provider, string format, params object[] args);
void InfoFormat(string format, params object[] args);

void Warn(object message, Exception exception = null);
void WarnFormat(IFormatProvider provider, string format, params object[] args);
void WarnFormat(string format, params object[] args);
}
}
31 changes: 31 additions & 0 deletions ObjectTK/Logging/LogFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// LogFactory.cs
//
// Copyright (C) 2018 OpenTK
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
//

using System;
using System.IO;
using log4net;

namespace ObjectTK.Logging
{
public static class LogFactory
{
public static readonly bool IsAvailable = File.Exists(AppDomain.CurrentDomain.BaseDirectory + "log4net.dll");

static IObjectTKLogger CreateLogger(Type type)
{
var logger = LogManager.GetLogger(type);
return logger != null ? new DefaultLogImpl(logger) : null;
}

public static IObjectTKLogger GetLogger(Type type)
{
return IsAvailable ? CreateLogger(type) : null;
}
}
}
5 changes: 4 additions & 1 deletion ObjectTK/ObjectTK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
<Compile Include="Extensions.cs" />
<Compile Include="GLObject.cs" />
<Compile Include="GLResource.cs" />
<Compile Include="Logging\DefaultLogImpl.cs" />
<Compile Include="Logging\IObjectTKLogger.cs" />
<Compile Include="Logging\LogFactory.cs" />
<Compile Include="MathF.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Queries\QueryIndexer.cs" />
Expand Down Expand Up @@ -122,4 +125,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
5 changes: 2 additions & 3 deletions ObjectTK/Shaders/Effect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using log4net;

namespace ObjectTK.Shaders
{
Expand All @@ -20,7 +19,7 @@ namespace ObjectTK.Shaders
/// </summary>
public class Effect
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(Effect));
private static readonly Logging.IObjectTKLogger Logger = Logging.LogFactory.GetLogger(typeof(Effect));

private static readonly Dictionary<string, Effect> Cache = new Dictionary<string, Effect>();

Expand Down Expand Up @@ -149,7 +148,7 @@ public static Effect LoadEffect(string path)
}
catch (FileNotFoundException ex)
{
Logger.Error("Effect source file not found.", ex);
Logger?.Error("Effect source file not found.", ex);
throw;
}
}
Expand Down
Loading

0 comments on commit 65b1338

Please sign in to comment.