// Copyright (c) 2020-2021 Nicholas Corgan
// SPDX-License-Identifier: BSL-1.0
using System;
namespace Pothosware.SoapySDR
{
///
/// SoapySDR's logging interface.
///
/// Through this, messages can be logged to console or file by registering C# delegates.
/// The logging API is designed to match System.String.Format().
///
public class Logger
{
///
/// The signature of a function to use for logging.
///
/// The message's level of importance.
/// The message text.
public delegate void LoggerDelegate(LogLevel logLevel, string message);
private static LoggerDelegate Delegate = null;
private class CSharpLogHandler : LogHandlerBase
{
public CSharpLogHandler() : base()
{
}
public override void Handle(LogLevel logLevel, string message) => Delegate?.Invoke(logLevel, message);
}
///
/// The log level threshold. Messages with lower priorities are dropped.
///
public static LogLevel LogLevel
{
get => LogHandlerBase.GetLogLevel();
set => LogHandlerBase.SetLogLevel(value);
}
private static CSharpLogHandler LogHandler = null;
///
/// Register a custom logging function to be used for all SoapySDR logging calls.
///
/// A logging function, or null for the default logger (prints to stderr).
public static void RegisterLogHandler(LoggerDelegate del)
{
if(del != null)
{
LogHandler = new CSharpLogHandler();
Delegate = del;
}
else
{
LogHandler = null;
Delegate = null;
}
}
///
/// Log a message with a given level and string.
///
/// The message's priority
/// The message string
public static void Log(LogLevel logLevel, string message) => LogHandlerBase.Log(logLevel, message);
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, string format, object arg) =>
Log(logLevel, string.Format(format, arg));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, string format, object[] args) =>
Log(logLevel, string.Format(format, args));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, IFormatProvider formatProvider, string format, object arg) =>
Log(logLevel, string.Format(formatProvider, format, arg));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, IFormatProvider formatProvider, string format, object[] args) =>
Log(logLevel, string.Format(formatProvider, format, args));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, string format, object arg0, object arg1) =>
Log(logLevel, string.Format(format, arg0, arg1));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, IFormatProvider formatProvider, string format, object arg0, object arg1) =>
Log(logLevel, string.Format(formatProvider, format, arg0, arg1));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, string format, object arg0, object arg1, object arg2) =>
Log(logLevel, string.Format(format, arg0, arg1, arg2));
///
/// Log a message with a given level and string, formatted with System.String.Format().
///
/// The message's priority
/// The message format
public static void LogF(LogLevel logLevel, IFormatProvider formatProvider, string format, object arg0, object arg1, object arg2) =>
Log(logLevel, string.Format(formatProvider, format, arg0, arg1, arg2));
}
}