// // (C) Copyright 2003-2023 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Diagnostics; using System.IO; using Autodesk.Revit.DB; namespace Revit.SDK.Samples.CancelSave.CS { /// /// One log file will be created by this class for tracking events raise. /// public static class LogManager { // a trace listener for the output log of CancelSave sample private static TraceListener TxtListener; // the directory where this assembly in. private static string AssemblyLocation = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); /// /// Retrieval if doing regression test now. /// If the ExpectedOutPut.log exists in the assembly folder returns true, else returns false. /// public static bool RegressionTestNow { get { string expectedLogFile = Path.Combine(AssemblyLocation, "ExpectedOutPut.log"); if (File.Exists(expectedLogFile)) { return true; } return false; } } /// /// Static constructor which creates a log file. /// static LogManager() { // Create CancelSave.log . string actullyLogFile = Path.Combine(AssemblyLocation, "CancelSave.log"); // if already existed, delete it. if (File.Exists(actullyLogFile)) { File.Delete(actullyLogFile); } TxtListener = new TextWriterTraceListener(actullyLogFile); Trace.Listeners.Add(TxtListener); Trace.AutoFlush = true; } /// /// Finalize and close the output log. /// public static void LogFinalize() { Trace.Flush(); TxtListener.Close(); Trace.Close(); Trace.Listeners.Remove(TxtListener); } /// /// Write log to file: which event occurred in which document. /// /// Event arguments that contains the event data. /// document in which the event is occur. public static void WriteLog(EventArgs args, Document doc) { Trace.WriteLine(""); Trace.WriteLine("[Event] " + GetEventName(args.GetType()) + ": " + TitleNoExt(doc.Title)); } /// /// Write specified message into log file. /// /// the message which will be written into the log file. public static void WriteLog(string message) { Trace.WriteLine(message); } /// /// Get event name from its EventArgs, without namespace prefix /// /// Generic event arguments type. /// the event name private static string GetEventName(Type type) { String argName = type.ToString(); String tail = "EventArgs"; String head = "Autodesk.Revit.DB.Events."; int firstIndex = head.Length; int length = argName.Length - head.Length - tail.Length; String eventName = argName.Substring(firstIndex, length); return eventName; } /// /// This method will remove the extension name of file name(if have). /// /// Document.Title will return title of project depends on OS setting: /// If we choose show extension name by IE:Tools\Folder Options, then the title will end with accordingly extension name. /// If we don't show extension, the Document.Title will only return file name without extension. /// /// Origin file name to be revised. /// New file name without extension name. private static string TitleNoExt(String orgTitle) { // return null directly if it's null if (String.IsNullOrEmpty(orgTitle)) { return ""; } // Remove the extension int pos = orgTitle.LastIndexOf('.'); if (-1 != pos) { return orgTitle.Remove(pos); } else { return orgTitle; } } } }