| using System; |
| using System; |
| using System.Collections.Generic; |
| using System.Collections.Generic; |
| using System.Diagnostics; |
| using System.Diagnostics; |
| using System.Linq; |
| using System.Linq; |
. | |
| using System.Reflection; |
| |
| using System.Runtime.CompilerServices; |
| using System.Text; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Threading.Tasks; |
| |
| |
| namespace Playnite.SDK |
| namespace Playnite.SDK |
| { |
| { |
| /// <summary> |
| /// <summary> |
| /// Describes logger not logging anywhere. |
| /// Describes logger not logging anywhere. |
| /// </summary> |
| /// </summary> |
| public class NullLoggger : ILogger |
| public class NullLoggger : ILogger |
| { |
| { |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Debug(string message) |
| public void Debug(string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Debug(Exception exception, string message) |
| public void Debug(Exception exception, string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Error(string message) |
| public void Error(string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Error(Exception exception, string message) |
| public void Error(Exception exception, string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Info(string message) |
| public void Info(string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Info(Exception exception, string message) |
| public void Info(Exception exception, string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Warn(string message) |
| public void Warn(string message) |
| { |
| { |
| } |
| } |
| |
| |
| /// <inheritdoc /> |
| /// <inheritdoc /> |
| public void Warn(Exception exception, string message) |
| public void Warn(Exception exception, string message) |
| { |
| { |
| } |
| } |
. | |
| |
| |
| /// <inheritdoc /> |
| |
| public void Trace(string message) |
| |
| { |
| |
| } |
| |
| |
| |
| /// <inheritdoc /> |
| |
| public void Trace(Exception exception, string message) |
| |
| { |
| |
| } |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Describes log provider. |
| /// Describes log provider. |
| /// </summary> |
| /// </summary> |
| public interface ILogProvider |
| public interface ILogProvider |
| { |
| { |
| /// <summary> |
| /// <summary> |
| /// Gets new logger. |
| /// Gets new logger. |
| /// </summary> |
| /// </summary> |
| /// <param name="loggerName">Logger name.</param> |
| /// <param name="loggerName">Logger name.</param> |
| /// <returns>Logger.</returns> |
| /// <returns>Logger.</returns> |
| ILogger GetLogger(string loggerName); |
| ILogger GetLogger(string loggerName); |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Represents log manager. |
| /// Represents log manager. |
| /// </summary> |
| /// </summary> |
| public static class LogManager |
| public static class LogManager |
| { |
| { |
| private static ILogProvider logManager; |
| private static ILogProvider logManager; |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Initializes log manager using specific log provider. |
| /// Initializes log manager using specific log provider. |
| /// </summary> |
| /// </summary> |
| /// <param name="manager"></param> |
| /// <param name="manager"></param> |
| public static void Init(ILogProvider manager) |
| public static void Init(ILogProvider manager) |
| { |
| { |
| logManager = manager; |
| logManager = manager; |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Gets logger with name of calling class. |
| /// Gets logger with name of calling class. |
| /// </summary> |
| /// </summary> |
| /// <returns>Logger.</returns> |
| /// <returns>Logger.</returns> |
. | |
| [MethodImpl(MethodImplOptions.NoInlining)] |
| public static ILogger GetLogger() |
| public static ILogger GetLogger() |
| { |
| { |
. | var className = (new StackFrame(1)).GetMethod().DeclaringType.Name; |
| |
| if (logManager != null) |
| if (logManager != null) |
| { |
| { |
. | return logManager.GetLogger(className); |
| var asmName = Assembly.GetCallingAssembly().GetName().Name; |
| |
| var isCore = asmName == "Playnite.DesktopApp" || asmName == "Playnite.FullscreenApp" || asmName == "Playnite"; |
| |
| var className = (new StackFrame(1)).GetMethod().DeclaringType.Name; |
| |
| if (isCore) |
| |
| { |
| |
| return logManager.GetLogger(className); |
| |
| } |
| |
| else |
| |
| { |
| |
| return logManager.GetLogger($"{asmName}#{className}"); |
| |
| } |
| } |
| } |
| else |
| else |
| { |
| { |
| return new NullLoggger(); |
| return new NullLoggger(); |
| } |
| } |
| } |
| } |
| |
| |
| /// <summary> |
| /// <summary> |
| /// Gets logger with specific name. |
| /// Gets logger with specific name. |
| /// </summary> |
| /// </summary> |
| /// <param name="loggerName">Logger name.</param> |
| /// <param name="loggerName">Logger name.</param> |
| /// <returns>Logger.</returns> |
| /// <returns>Logger.</returns> |
. | |
| [MethodImpl(MethodImplOptions.NoInlining)] |
| public static ILogger GetLogger(string loggerName) |
| public static ILogger GetLogger(string loggerName) |
| { |
| { |
. | return logManager.GetLogger(loggerName); |
| if (string.IsNullOrEmpty(loggerName)) |
| |
| { |
| |
| throw new ArgumentNullException(nameof(loggerName)); |
| |
| } |
| |
| |
| |
| var asmName = Assembly.GetCallingAssembly().GetName().Name; |
| |
| var isCore = asmName == "Playnite.DesktopApp" || asmName == "Playnite.FullscreenApp" || asmName == "Playnite"; |
| |
| if (isCore || loggerName.Contains("#")) |
| |
| { |
| |
| return logManager.GetLogger(loggerName); |
| |
| } |
| |
| else |
| |
| { |
| |
| return logManager.GetLogger($"{asmName}#{loggerName}"); |
| |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| |