/* ======================================================================== * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. * * OPC Foundation MIT License 1.00 * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * The complete license agreement can be found here: * http://opcfoundation.org/License/MIT/1.00/ * ======================================================================*/ using System; using System.Diagnostics; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Logging; using ExternalLogLevel = SGF.Diagnostics.LogLevel; using IExternalLogger = SGF.Diagnostics.ILogger; using SourceProductionContext = SGF.SgfSourceProductionContext; namespace Opc.Ua.SourceGeneration { /// /// Source generation telemetry context /// internal sealed class SourceGeneratorTelemetry : TelemetryContextBase, IDisposable { /// /// Private constructor /// private SourceGeneratorTelemetry(ILoggerFactory factory) : base(factory) { m_ownedFactory = factory; } /// /// Dispose the owned logger factory. /// public void Dispose() { m_ownedFactory?.Dispose(); } /// /// Create telemetry context /// /// public static SourceGeneratorTelemetry Create( IExternalLogger logger, SourceProductionContext context) { ILoggerFactory factory = null; try { factory = new LoggerFactoryAdapter(logger, context); var result = new SourceGeneratorTelemetry(factory); factory = null; return result; } finally { factory?.Dispose(); } } /// /// Logger provider and adapter for logger factory /// private sealed class LoggerFactoryAdapter : ILoggerFactory, ILoggerProvider { /// /// Create adapter /// /// /// public LoggerFactoryAdapter( IExternalLogger logger, SourceProductionContext context) { m_logger = logger; m_context = context; } /// public void AddProvider(ILoggerProvider provider) { } /// public ILogger CreateLogger(string categoryName) { return new LoggerAdapter(categoryName, m_context, m_logger); } /// public void Dispose() { } private readonly IExternalLogger m_logger; private readonly SourceProductionContext m_context; } /// /// Logger adapter /// private sealed class LoggerAdapter : ILogger, IDisposable { /// /// Create logger adapter /// public LoggerAdapter( string categoryName, SourceProductionContext context, IExternalLogger logger) { m_categoryName = categoryName; m_context = context; m_logger = logger; } /// public void Dispose() { } /// public bool IsEnabled(LogLevel logLevel) { return logLevel > LogLevel.Information; #if UNUSED return m_logger.IsEnabled(GetLogLevel(logLevel)); #endif } /// public void Log( LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { if (!IsEnabled(logLevel)) { return; } string message = formatter(state, exception); if (SourceGenerator.TryGetDiagnostic( logLevel, eventId, out DiagnosticDescriptor descriptor)) { m_context.ReportDiagnostic( Diagnostic.Create(descriptor, Location.None, message)); return; } message = $"[{m_categoryName}] {message}"; m_logger.Log( GetLogLevel(logLevel), exception, message); Debug.WriteLine(message); } /// public IDisposable BeginScope(TState state) where TState : notnull { return this; } private static ExternalLogLevel GetLogLevel(LogLevel logLevel) { return logLevel switch { LogLevel.Trace or LogLevel.Debug => ExternalLogLevel.Debug, LogLevel.Information => ExternalLogLevel.Information, LogLevel.Warning => ExternalLogLevel.Warning, LogLevel.Error or LogLevel.Critical => ExternalLogLevel.Error, _ => ExternalLogLevel.Information }; } private readonly string m_categoryName; private readonly IExternalLogger m_logger; private readonly SourceProductionContext m_context; } private readonly ILoggerFactory m_ownedFactory; } }