From 487f0f8afcb3431420e2106819cb7fd0afe4e118 Mon Sep 17 00:00:00 2001 From: Bob Arnson Date: Fri, 17 Jan 2020 21:51:39 -0500 Subject: Let listeners adjust message levels; general messaging cleanup. --- src/WixToolset.BuildTasks/DoIt.cs | 2 + .../ConsoleMessageListener.cs | 72 +++++++++++++++++----- 2 files changed, 58 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/WixToolset.BuildTasks/DoIt.cs b/src/WixToolset.BuildTasks/DoIt.cs index 7089ff12..6af2a50a 100644 --- a/src/WixToolset.BuildTasks/DoIt.cs +++ b/src/WixToolset.BuildTasks/DoIt.cs @@ -278,6 +278,8 @@ namespace WixToolset.BuildTasks { this.Logger.LogMessage(MessageImportance.Low, message); } + + public MessageLevel CalculateMessageLevel(IMessaging messaging, Message message, MessageLevel defaultMessageLevel) => defaultMessageLevel; } } } diff --git a/src/WixToolset.Tools.Core/ConsoleMessageListener.cs b/src/WixToolset.Tools.Core/ConsoleMessageListener.cs index 5b1fb988..709fb576 100644 --- a/src/WixToolset.Tools.Core/ConsoleMessageListener.cs +++ b/src/WixToolset.Tools.Core/ConsoleMessageListener.cs @@ -1,39 +1,54 @@ -using System; -using System.Globalization; -using System.Text; -using System.Threading; -using WixToolset.Data; -using WixToolset.Extensibility; +// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Tools.Core { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Globalization; + using System.Text; + using System.Threading; + using WixToolset.Data; + using WixToolset.Extensibility; + using WixToolset.Extensibility.Services; + public sealed class ConsoleMessageListener : IMessageListener { - public ConsoleMessageListener(string shortName, string longName) + public ConsoleMessageListener(string prefix, string appName) { - this.ShortAppName = shortName; - this.LongAppName = longName; + this.Prefix = prefix; + this.AppName = appName; PrepareConsoleForLocalization(); } - public string LongAppName { get; } + public string AppName { get; } - public string ShortAppName { get; } + public string Prefix { get; } public void Write(Message message) { - var filename = message.SourceLineNumbers?.FileName ?? this.LongAppName; - var line = message.SourceLineNumbers?.LineNumber ?? -1; + var filename = message.SourceLineNumbers?.FileName ?? this.AppName; var type = message.Level.ToString().ToLowerInvariant(); var output = message.Level >= MessageLevel.Warning ? Console.Out : Console.Error; - if (line > 0) + if (message.SourceLineNumbers?.LineNumber.HasValue == true) { - filename = String.Concat(filename, "(", line, ")"); + filename = String.Concat(filename, "(", message.SourceLineNumbers?.LineNumber.Value, ")"); } - output.WriteLine("{0} : {1} {2}{3:0000}: {4}", filename, type, this.ShortAppName, message.Id, message.ToString()); + output.WriteLine("{0} : {1} {2}{3:0000}: {4}", filename, type, this.Prefix, message.Id, message.ToString()); + + var fileNames = GetFileNames(message.SourceLineNumbers); + if (fileNames.Any()) + { + output.WriteLine("Source trace:"); + + foreach (var fileName in fileNames) + { + output.WriteLine("Source trace: {0}", fileName); + } + } } public void Write(string message) @@ -41,6 +56,31 @@ namespace WixToolset.Tools.Core Console.Out.WriteLine(message); } + public MessageLevel CalculateMessageLevel(IMessaging messaging, Message message, MessageLevel defaultMessageLevel) => defaultMessageLevel; + + private static IList GetFileNames(SourceLineNumber sourceLineNumbers) + { + var fileNames = new List(); + + for (var sln = sourceLineNumbers; null != sln; sln = sln.Parent) + { + if (String.IsNullOrEmpty(sln.FileName)) + { + continue; + } + else if (sln.LineNumber.HasValue) + { + fileNames.Add(String.Format(CultureInfo.CurrentUICulture, "{0}: line {1}", sln.FileName, sln.LineNumber)); + } + else + { + fileNames.Add(sln.FileName); + } + } + + return fileNames; + } + private static void PrepareConsoleForLocalization() { Thread.CurrentThread.CurrentUICulture = CultureInfo.CurrentUICulture.GetConsoleFallbackUICulture(); -- cgit v1.2.3-55-g6feb