// MonoGame - Copyright (C) The MonoGame Team // This file is subject to the terms and conditions defined in // file 'LICENSE.txt', which is part of this source code package. using System; using System.Collections.Generic; using System.IO; namespace Microsoft.Xna.Framework.Content.Pipeline { /// /// Provides methods for reporting informational messages or warnings from content importers and processors. /// Do not use this class to report errors. Instead, report errors by throwing a PipelineException or InvalidContentException. /// public abstract class ContentBuildLogger { Stack filenames = new Stack(); private int indentCount = 0; protected string IndentString { get { return String.Empty.PadLeft(Math.Max(0, indentCount), '\t'); } } /// /// Gets or sets the base reference path used when reporting errors during the content build process. /// public string LoggerRootDirectory { get; set; } /// /// Initializes a new instance of ContentBuildLogger. /// protected ContentBuildLogger () { } /// /// Returns the relative path to the filename from the root directory. /// /// The target filename. /// The root directory. If not specified, the current directory is used. /// The relative path. string GetRelativePath(string filename, string rootDirectory) { rootDirectory = Path.GetFullPath(string.IsNullOrEmpty(rootDirectory) ? "." : rootDirectory); filename = Path.GetFullPath(filename); if (filename.StartsWith(rootDirectory)) return filename.Substring(rootDirectory.Length); return filename; } /// /// Gets the filename currently being processed, for use in warning and error messages. /// /// Identity of a content item. If specified, GetCurrentFilename uses this value to refine the search. If no value is specified, the current PushFile state is used. /// Name of the file being processed. protected string GetCurrentFilename( ContentIdentity contentIdentity ) { if ((contentIdentity != null) && !string.IsNullOrEmpty(contentIdentity.SourceFilename)) return GetRelativePath(contentIdentity.SourceFilename, LoggerRootDirectory); if (filenames.Count > 0) return GetRelativePath(filenames.Peek(), LoggerRootDirectory); return null; } /// /// Outputs a high-priority status message from a content importer or processor. /// /// Message being reported. /// Arguments for the reported message. public abstract void LogImportantMessage( string message, params Object[] messageArgs ); /// /// Outputs a low priority status message from a content importer or processor. /// /// Message being reported. /// Arguments for the reported message. public abstract void LogMessage( string message, params Object[] messageArgs ); /// /// Outputs a warning message from a content importer or processor. /// /// Link to an existing online help topic containing related information. /// Identity of the content item that generated the message. /// Message being reported. /// Arguments for the reported message. public abstract void LogWarning( string helpLink, ContentIdentity contentIdentity, string message, params Object[] messageArgs ); /// /// Outputs a message indicating that a content asset has completed processing. /// public void PopFile() { filenames.Pop(); } /// /// Outputs a message indicating that a content asset has begun processing. /// All logger warnings or error exceptions from this time forward to the next PopFile call refer to this file. /// /// Name of the file containing future messages. public void PushFile(string filename) { filenames.Push(filename); } public void Indent() { indentCount++; } public void Unindent() { indentCount--; } } }