// Copyright (C) 2018, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.IO; using Duplicati.Library.Logging; using Duplicati.Library.Utility; namespace Duplicati.CommandLine { /// /// Log output handler for the console /// public class ConsoleLogTarget : Library.Logging.ILogDestination { /// /// The stdout stream /// private readonly TextWriter m_stdout; /// /// The stderr stream /// private readonly TextWriter m_stderr; /// /// The default log level /// private readonly LogMessageType m_level; /// /// The filter, if any /// private readonly IFilter m_filter; /// /// Initializes a new instance of the class. /// /// The stdout stream. /// The stderr stream. /// The minimum log level to consider. /// The log filter, if any. public ConsoleLogTarget(TextWriter stdout, TextWriter stderr, LogMessageType level, string logfilter) { m_stdout = stdout; m_stderr = stderr; m_level = level; m_filter = Library.Main.Options.ParseLogFilter(logfilter); } /// /// Writes the message to stdout or stderr. /// /// The entry to write. public void WriteMessage(LogEntry entry) { var found = m_filter.Matches(entry.Tag, out var result, out var match); // If there is a filter match, use that if (found) { if (!result) return; if (entry.Level == LogMessageType.Error) m_stderr.WriteLine(entry.ToString()); else m_stdout.WriteLine(entry.ToString()); } else { // Otherwise, filter by log-level if (entry.Level < m_level) return; } if (entry.Level == LogMessageType.Error) m_stderr.WriteLine(entry.FormattedMessage); else m_stdout.WriteLine(entry.FormattedMessage); } } }