⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _tracer.cs

📁 Aditional source code for sti5518 firmware uploader.
💻 CS
字号:
#region Copyright ArtfulBits Inc. 2005 - 2008
//
//  Copyright ArtfulBits Inc. 2005 - 2008. All rights reserved.
//
//  Use of this code is subject to the terms of our license.
//  A copy of the current license can be obtained at any time by e-mailing
//  info@artfulbits.com. Re-distribution in any form is strictly
//  prohibited. Any infringement will be prosecuted under applicable laws. 
//
#endregion

#region file using
using System;
using System.Diagnostics;
using System.Reflection;
#endregion

namespace Artfulbits.Utilities.Diagnostics
{
  /// <summary>
  /// Class provide functionality for tracing
  /// </summary>
  public class _tracer
  {
    #region Class constants
    /// <summary></summary>
    private const string DEF_NAME = "OpenBox Updater";
    #endregion

    #region Class static members
    /// <summary></summary>
    private static TraceSwitch _switch = new TraceSwitch( DEF_NAME, string.Empty );
    /// <summary></summary>
    private static readonly object m_sync = new object();
    #endregion

    #region Class Initialize/Finalize methods
    /// <summary></summary>
    private _tracer()
    {
      throw new NotImplementedException( "Class not designed for instance creation" );
    }
    #endregion

    #region Trace methods
    /// <summary></summary>
    /// <param name="ex"/>
    public static void TraceException( Exception ex )
    {
      TraceException( ex, string.Empty );
      
      if( _switch.TraceVerbose )
      {
        TraceException( ex.InnerException, "Inner" );
      }
    }
    /// <summary></summary>
    /// <param name="ex"/>
    /// <param name="category"/>
    public static void TraceException( Exception ex, string category )
    {
      if( ex != null )
      {
        Trace( ex.Message, category + " Exception" );
        
        if( _switch.TraceVerbose )
        {
          Trace( Environment.NewLine + ex.StackTrace, category + " Stack Trace" );
        }
      }
    }
    /// <summary>
    /// Trace only value
    /// </summary>
    /// <param name="value"></param>
    public static void Trace( object value )
    {
      Trace( value, string.Empty );
    }
    /// <summary>
    /// Trace infromation with specific layer information like date/time
    /// and Layer full name
    /// </summary>
    /// <param name="value">value to trace</param>
    /// <param name="category">category for tracing</param>
    public static void Trace( object value, string category )
    {
      try
      {
        lock( m_sync )
        {
          System.Diagnostics.Trace.WriteLine( value, BuildCategoryName( category ) );
        }
      }
      catch
      {
        // NOTE: This code required for proper disposing. 
        // Our Trace Listiners can be disposed before us and this will  
        // break this very simple code.
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="category"></param>
    /// <returns></returns>
    private static string BuildCategoryName( string category )
    {
      string date = DateTime.Now.ToString( "s" );
      return string.Format( "{0} : {1} : {2}", DEF_NAME, date, category ?? string.Empty );
    }
    #endregion

    #region Class utility methods
    /// <summary></summary>
    /// <param name="values"/>
    /// <param name="message"/>
    /// <param name="category"/>
    private static void TraceMethodParams( object[] values, string message, string category )
    {
      string[] strValues = null;

      if( values != null )
      {
        strValues = new string[ values.Length ];

        for( int i = 0, len = values.Length ; i < len ; i++ )
        {
          strValues[ i ] = ( values[ i ] == null ) ? "null" : values[ i ].ToString();
        }
      }

      try
      {
        lock( m_sync )
        {
          System.Diagnostics.Trace.Write( message, BuildCategoryName( category ) );

          if( values != null )
          {
            System.Diagnostics.Trace.Write( " {parameters:" );
            System.Diagnostics.Trace.Write( string.Join( ", ", strValues ) );
            System.Diagnostics.Trace.WriteLine( "}" );
          }
        }
      }
      catch
      {
        // NOTE: This code required for proper disposing. 
        // Our Trace Listiners can be disposed before us and this break 
        // this simple code.
      }
    }
    #endregion

    #region Verbose level of tracing
    /// <summary></summary>
    /// <param name="message"/>
    public static void Verbose( string message )
    {
      if( _switch.TraceVerbose )
      {
        Trace( message );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    public static void Verbose( string message, string category )
    {
      if( _switch.TraceVerbose )
      {
        Trace( message, category );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    /// <param name="values"/>
    public static void Verbose( string message, string category, params object[] values )
    {
      if( _switch.TraceVerbose )
      {
        TraceMethodParams( values, message, category );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    public static void Verbose( Exception ex )
    {
      if( _switch.TraceVerbose )
      {
        TraceException( ex );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    public static void Verbose( Exception ex, string category )
    {
      if( _switch.TraceVerbose )
      {
        TraceException( ex, category );
      }
    }
    /// <summary></summary>
    /// <param name="bTrace"></param>
    /// <param name="message"></param>
    public static void VerboseIf( bool bTrace, string message )
    {
      if( bTrace )
      {
        Verbose( message );
      }
    }
    #endregion

    #region Error level of tracing
    /// <summary></summary>
    /// <param name="message"/>
    public static void Error( string message )
    {
      if( _switch.TraceError )
      {
        Trace( message );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    public static void Error( string message, string category )
    {
      if( _switch.TraceError )
      {
        Trace( message, category );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    /// <param name="values"/>
    public static void Error( string message, string category, params object[] values )
    {
      if( _switch.TraceError )
      {
        TraceMethodParams( values, message, category );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    public static void Error( Exception ex )
    {
      if( _switch.TraceError )
      {
        TraceException( ex );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    public static void Error( Exception ex, string category )
    {
      if( _switch.TraceError )
      {
        TraceException( ex, category );
      }
    }
    /// <summary></summary>
    /// <param name="bTrace"></param>
    /// <param name="message"></param>
    public static void ErrorIf( bool bTrace, string message )
    {
      if( bTrace )
      {
        Error( message );
      }
    }
    #endregion

    #region Warning level of tracing
    /// <summary></summary>
    /// <param name="message"/>
    public static void Warning( string message )
    {
      if( _switch.TraceWarning )
      {
        Trace( message );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    public static void Warning( string message, string category )
    {
      if( _switch.TraceWarning )
      {
        Trace( message, category );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    /// <param name="values"/>
    public static void Warning( string message, string category, params object[] values )
    {
      if( _switch.TraceWarning )
      {
        TraceMethodParams( values, message, category );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    public static void Warning( Exception ex )
    {
      if( _switch.TraceWarning )
      {
        TraceException( ex );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    public static void Warning( Exception ex, string category )
    {
      if( _switch.TraceWarning )
      {
        TraceException( ex, category );
      }
    }
    /// <summary></summary>
    /// <param name="bTrace"></param>
    /// <param name="message"></param>
    public static void WarningIf( bool bTrace, string message )
    {
      if( bTrace )
      {
        Warning( message );
      }
    }
    #endregion

    #region Info level of tracing
    /// <summary></summary>
    /// <param name="message"/>
    public static void Info( string message )
    {
      if( _switch.TraceInfo )
      {
        Trace( message );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    public static void Info( string message, string category )
    {
      if( _switch.TraceInfo )
      {
        Trace( message, category );
      }
    }
    /// <summary></summary>
    /// <param name="message"/>
    /// <param name="category"/>
    /// <param name="values"/>
    public static void Info( string message, string category, params object[] values )
    {
      if( _switch.TraceInfo )
      {
        TraceMethodParams( values, message, category );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    public static void Info( Exception ex )
    {
      if( _switch.TraceInfo )
      {
        TraceException( ex );
      }
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ex"></param>
    /// <param name="category"></param>
    public static void Info( Exception ex, string category )
    {
      if( _switch.TraceInfo )
      {
        TraceException( ex, category );
      }
    }
    /// <summary></summary>
    /// <param name="bTrace"></param>
    /// <param name="message"></param>
    public static void InfoIf( bool bTrace, string message )
    {
      if( bTrace )
      {
        Info( message );
      }
    }
    #endregion

    #region Method calls
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public static MethodScope Method()
    {
      StackTrace stack = new StackTrace();
      MethodBase methodBase = stack.GetFrame( 1 ).GetMethod();
      string method = methodBase.Name;

      return new MethodScope( null, method, null );
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="arguments"></param>
    /// <returns></returns>
    public static MethodScope Method( params object[] arguments )
    {
      StackTrace stack = new StackTrace();
      MethodBase methodBase = stack.GetFrame( 1 ).GetMethod();
      string method = methodBase.Name;

      return new MethodScope( null, method, arguments );
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="message"></param>
    /// <param name="arguments"></param>
    /// <returns></returns>
    public static MethodScope Method( string message, params object[] arguments )
    {
      StackTrace stack = new StackTrace();
      MethodBase methodBase = stack.GetFrame( 1 ).GetMethod();
      string method = methodBase.Name;

      return new MethodScope( message, method, arguments );
    }
    #endregion

    #region Internal declarations
    /// <summary>
    /// 
    /// </summary>
    public struct MethodScope : IDisposable
    {
      #region Class public members
      /// <summary>
      /// 
      /// </summary>
      public readonly string MethodName;
      /// <summary>
      /// 
      /// </summary>
      public readonly string Message;
      /// <summary>
      /// 
      /// </summary>
      public readonly object[] Arguments;
      #endregion

      #region Class initialize/finilize methods
      /// <summary>
      /// 
      /// </summary>
      /// <param name="message"></param>
      /// <param name="method"></param>
      /// <param name="args"></param>
      public MethodScope( string message, string method, object[] args )
      {
        MethodName = method;
        Arguments = args;
        Message = message;

        _tracer.Verbose( MethodName, "Call", Arguments );

        System.Diagnostics.Trace.Indent();

        _tracer.VerboseIf( !string.IsNullOrEmpty( Message ), Message );
      }
      /// <summary>
      /// 
      /// </summary>
      public void Dispose()
      {
        System.Diagnostics.Trace.Unindent();

        _tracer.Verbose( MethodName, "Call End" );
      }
      #endregion
    }
    #endregion
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -