1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using System;
6 using System.Diagnostics;
7 using System.Globalization;
8 
9 namespace System.DirectoryServices.AccountManagement
10 {
11     internal enum DebugLevel
12     {
13         None = 0,
14         Info,
15         Warn,
16         Error
17     }
18 
19     internal static class GlobalDebug
20     {
GlobalDebug()21         static GlobalDebug()
22         {
23             GlobalDebug.s_debugLevel = GlobalConfig.DebugLevel;
24 //#if DEBUG
25 #if SUPPORTDEBUGLOGFILE // not defined
26             string debugLogFile = GlobalConfig.DebugLogFile;
27 
28             if (debugLogFile != null)
29             {
30                 foreach (TraceListener listener in Debug.Listeners)
31                 {
32                     if (listener is DefaultTraceListener)
33                         ((DefaultTraceListener)listener).LogFileName = debugLogFile;
34                 }
35 
36                 //
37                 Debug.WriteLine(
38                             String.Format(
39                                 System.Globalization.CultureInfo.InvariantCulture,
40                                 "Principal API Debug Log - AppDomain {0} with ID {1} - {2} (UTC)",
41                                 System.Threading.Thread.GetDomain().FriendlyName,
42                                 System.Threading.Thread.GetDomainID(),
43                                 DateTime.UtcNow));
44             }
45 #endif
46         }
47 
48         static public bool Error
49         {
50             get { return DebugLevel.Error >= GlobalDebug.s_debugLevel; }
51         }
52 
53         static public bool Warn
54         {
55             get { return DebugLevel.Warn >= GlobalDebug.s_debugLevel; }
56         }
57 
58         static public bool Info
59         {
60             get { return DebugLevel.Info >= GlobalDebug.s_debugLevel; }
61         }
62 
63         [ConditionalAttribute("DEBUG")]
WriteLineIf(bool f, string category, string message, params object[] args)64         static public void WriteLineIf(bool f, string category, string message, params object[] args)
65         {
66             message = "[" + SafeNativeMethods.GetCurrentThreadId().ToString("x", CultureInfo.InvariantCulture) + "] " + message;
67 
68             Debug.WriteLineIf(
69                             f,
70                             String.Format(
71                                 CultureInfo.InvariantCulture,
72                                 message,
73                                 args),
74                             category);
75         }
76 
77         [ConditionalAttribute("DEBUG")]
WriteLineIf(bool f, string category, string message)78         static public void WriteLineIf(bool f, string category, string message)
79         {
80             message = "[" + SafeNativeMethods.GetCurrentThreadId().ToString("x", CultureInfo.InvariantCulture) + "] " + message;
81 
82             Debug.WriteLineIf(
83                             f,
84                             message,
85                             category);
86         }
87 
88         private static DebugLevel s_debugLevel;
89     }
90 }
91