| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- //
- // System.Diagnostics.TraceImpl.cs
- //
- // Authors:
- // Jonathan Pryor ([email protected])
- //
- // (C) 2002 Jonathan Pryor
- //
- using System;
- using System.Diagnostics;
- using System.Configuration;
- namespace System.Diagnostics {
- internal class TraceImpl {
- private static object lock_ = new object ();
- private static bool autoFlush;
- [ThreadStatic]
- private static int indentLevel = 0;
- [ThreadStatic]
- private static int indentSize;
- // Grab the .config file stuff.
- //
- // There are some ordering issues with the .config file.
- //
- // The DiagnosticsConfigurationHandler assumes that the TraceImpl.Listeners
- // collection exists (so it can initialize the DefaultTraceListener and
- // add/remove existing listeners).
- //
- // When is the .config file read? That's somewhat undefined. The .config
- // file will be read the first time someone calls
- // ConfigurationSettings.GetConfig(), but when that occurs is
- // indeterminate.
- //
- // Since it's probable that the Trace/Debug classes will be used by the
- // application, the .config file should be read in before they're used.
- //
- // Thus, place the initialization here. We can ensure that everything is
- // initialized before reading in the .config file, which should ensure
- // that everything is sane.
- static TraceImpl ()
- {
- // defaults
- autoFlush = false;
- indentLevel = 0;
- indentSize = 4;
- listeners = new TraceListenerCollection ();
- // Initialize the world
- System.Collections.IDictionary d = DiagnosticsConfiguration.Settings;
- // remove warning about d being unused
- d = d;
- }
- private TraceImpl ()
- {
- }
- public static bool AutoFlush {
- get {return autoFlush;}
- set {autoFlush = value;}
- }
- public static int IndentLevel {
- get {return indentLevel;}
- set {
- indentLevel = value;
- // Don't need to lock for threadsafety as
- // TraceListener.IndentLevel is [ThreadStatic]
- foreach (TraceListener t in Listeners) {
- t.IndentLevel = indentLevel;
- }
- }
- }
- public static int IndentSize {
- get {return indentSize;}
- set {
- indentSize = value;
- // Don't need to lock for threadsafety as
- // TraceListener.IndentSize is [ThreadStatic]
- foreach (TraceListener t in Listeners) {
- t.IndentSize = indentSize;
- }
- }
- }
- private static TraceListenerCollection listeners;
- public static TraceListenerCollection Listeners {
- get {return listeners;}
- }
- // FIXME: According to MSDN, this method should display a dialog box
- [MonoTODO]
- public static void Assert (bool condition)
- {
- if (!condition)
- Fail (new StackTrace().ToString());
- }
- // FIXME: According to MSDN, this method should display a dialog box
- [MonoTODO]
- public static void Assert (bool condition, string message)
- {
- if (!condition)
- Fail (message);
- }
- // FIXME: According to MSDN, this method should display a dialog box
- [MonoTODO]
- public static void Assert (bool condition, string message,
- string detailMessage)
- {
- if (!condition)
- Fail (message, detailMessage);
- }
- public static void Close ()
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Close ();
- }
- }
- }
- // FIXME: From testing .NET, this method should display a dialog
- [MonoTODO]
- public static void Fail (string message)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Fail (message);
- }
- }
- }
- // FIXME: From testing .NET, this method should display a dialog
- [MonoTODO]
- public static void Fail (string message, string detailMessage)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Fail (message, detailMessage);
- }
- }
- }
- public static void Flush ()
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners){
- listener.Flush ();
- }
- }
- }
- public static void Indent ()
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.IndentLevel++;
- }
- }
- }
- public static void Unindent ()
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.IndentLevel--;
- }
- }
- }
- public static void Write (object value)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Write (value);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void Write (string message)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Write (message);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void Write (object value, string category)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Write (value, category);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void Write (string message, string category)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.Write (message, category);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void WriteIf (bool condition, object value)
- {
- if (condition)
- Write (value);
- }
- public static void WriteIf (bool condition, string message)
- {
- if (condition)
- Write (message);
- }
- public static void WriteIf (bool condition, object value,
- string category)
- {
- if (condition)
- Write (value, category);
- }
- public static void WriteIf (bool condition, string message,
- string category)
- {
- if (condition)
- Write (message, category);
- }
- public static void WriteLine (object value)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.WriteLine (value);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void WriteLine (string message)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.WriteLine (message);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void WriteLine (object value, string category)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.WriteLine (value, category);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void WriteLine (string message, string category)
- {
- lock (lock_) {
- foreach (TraceListener listener in Listeners) {
- listener.WriteLine (message, category);
- if (AutoFlush)
- listener.Flush ();
- }
- }
- }
- public static void WriteLineIf (bool condition, object value)
- {
- if (condition)
- WriteLine (value);
- }
- public static void WriteLineIf (bool condition, string message)
- {
- if (condition)
- WriteLine (message);
- }
- public static void WriteLineIf (bool condition, object value,
- string category)
- {
- if (condition)
- WriteLine (value, category);
- }
- public static void WriteLineIf (bool condition, string message,
- string category)
- {
- if (condition)
- WriteLine (message, category);
- }
- }
- }
|