TraceImpl.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // System.Diagnostics.TraceImpl.cs
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. //
  7. // (C) 2002 Jonathan Pryor
  8. //
  9. using System;
  10. using System.Diagnostics;
  11. using System.Configuration;
  12. namespace System.Diagnostics {
  13. internal class TraceImpl {
  14. private static object lock_ = new object ();
  15. private static bool autoFlush;
  16. [ThreadStatic]
  17. private static int indentLevel = 0;
  18. [ThreadStatic]
  19. private static int indentSize;
  20. // Grab the .config file stuff.
  21. //
  22. // There are some ordering issues with the .config file.
  23. //
  24. // The DiagnosticsConfigurationHandler assumes that the TraceImpl.Listeners
  25. // collection exists (so it can initialize the DefaultTraceListener and
  26. // add/remove existing listeners).
  27. //
  28. // When is the .config file read? That's somewhat undefined. The .config
  29. // file will be read the first time someone calls
  30. // ConfigurationSettings.GetConfig(), but when that occurs is
  31. // indeterminate.
  32. //
  33. // Since it's probable that the Trace/Debug classes will be used by the
  34. // application, the .config file should be read in before they're used.
  35. //
  36. // Thus, place the initialization here. We can ensure that everything is
  37. // initialized before reading in the .config file, which should ensure
  38. // that everything is sane.
  39. static TraceImpl ()
  40. {
  41. // defaults
  42. autoFlush = false;
  43. indentLevel = 0;
  44. indentSize = 4;
  45. listeners = new TraceListenerCollection ();
  46. // Initialize the world
  47. System.Collections.IDictionary d = DiagnosticsConfiguration.Settings;
  48. // remove warning about d being unused
  49. d = d;
  50. }
  51. private TraceImpl ()
  52. {
  53. }
  54. public static bool AutoFlush {
  55. get {return autoFlush;}
  56. set {autoFlush = value;}
  57. }
  58. public static int IndentLevel {
  59. get {return indentLevel;}
  60. set {
  61. indentLevel = value;
  62. // Don't need to lock for threadsafety as
  63. // TraceListener.IndentLevel is [ThreadStatic]
  64. foreach (TraceListener t in Listeners) {
  65. t.IndentLevel = indentLevel;
  66. }
  67. }
  68. }
  69. public static int IndentSize {
  70. get {return indentSize;}
  71. set {
  72. indentSize = value;
  73. // Don't need to lock for threadsafety as
  74. // TraceListener.IndentSize is [ThreadStatic]
  75. foreach (TraceListener t in Listeners) {
  76. t.IndentSize = indentSize;
  77. }
  78. }
  79. }
  80. private static TraceListenerCollection listeners;
  81. public static TraceListenerCollection Listeners {
  82. get {return listeners;}
  83. }
  84. // FIXME: According to MSDN, this method should display a dialog box
  85. [MonoTODO]
  86. public static void Assert (bool condition)
  87. {
  88. if (!condition)
  89. Fail (new StackTrace().ToString());
  90. }
  91. // FIXME: According to MSDN, this method should display a dialog box
  92. [MonoTODO]
  93. public static void Assert (bool condition, string message)
  94. {
  95. if (!condition)
  96. Fail (message);
  97. }
  98. // FIXME: According to MSDN, this method should display a dialog box
  99. [MonoTODO]
  100. public static void Assert (bool condition, string message,
  101. string detailMessage)
  102. {
  103. if (!condition)
  104. Fail (message, detailMessage);
  105. }
  106. public static void Close ()
  107. {
  108. lock (lock_) {
  109. foreach (TraceListener listener in Listeners) {
  110. listener.Close ();
  111. }
  112. }
  113. }
  114. // FIXME: From testing .NET, this method should display a dialog
  115. [MonoTODO]
  116. public static void Fail (string message)
  117. {
  118. lock (lock_) {
  119. foreach (TraceListener listener in Listeners) {
  120. listener.Fail (message);
  121. }
  122. }
  123. }
  124. // FIXME: From testing .NET, this method should display a dialog
  125. [MonoTODO]
  126. public static void Fail (string message, string detailMessage)
  127. {
  128. lock (lock_) {
  129. foreach (TraceListener listener in Listeners) {
  130. listener.Fail (message, detailMessage);
  131. }
  132. }
  133. }
  134. public static void Flush ()
  135. {
  136. lock (lock_) {
  137. foreach (TraceListener listener in Listeners){
  138. listener.Flush ();
  139. }
  140. }
  141. }
  142. public static void Indent ()
  143. {
  144. lock (lock_) {
  145. foreach (TraceListener listener in Listeners) {
  146. listener.IndentLevel++;
  147. }
  148. }
  149. }
  150. public static void Unindent ()
  151. {
  152. lock (lock_) {
  153. foreach (TraceListener listener in Listeners) {
  154. listener.IndentLevel--;
  155. }
  156. }
  157. }
  158. public static void Write (object value)
  159. {
  160. lock (lock_) {
  161. foreach (TraceListener listener in Listeners) {
  162. listener.Write (value);
  163. if (AutoFlush)
  164. listener.Flush ();
  165. }
  166. }
  167. }
  168. public static void Write (string message)
  169. {
  170. lock (lock_) {
  171. foreach (TraceListener listener in Listeners) {
  172. listener.Write (message);
  173. if (AutoFlush)
  174. listener.Flush ();
  175. }
  176. }
  177. }
  178. public static void Write (object value, string category)
  179. {
  180. lock (lock_) {
  181. foreach (TraceListener listener in Listeners) {
  182. listener.Write (value, category);
  183. if (AutoFlush)
  184. listener.Flush ();
  185. }
  186. }
  187. }
  188. public static void Write (string message, string category)
  189. {
  190. lock (lock_) {
  191. foreach (TraceListener listener in Listeners) {
  192. listener.Write (message, category);
  193. if (AutoFlush)
  194. listener.Flush ();
  195. }
  196. }
  197. }
  198. public static void WriteIf (bool condition, object value)
  199. {
  200. if (condition)
  201. Write (value);
  202. }
  203. public static void WriteIf (bool condition, string message)
  204. {
  205. if (condition)
  206. Write (message);
  207. }
  208. public static void WriteIf (bool condition, object value,
  209. string category)
  210. {
  211. if (condition)
  212. Write (value, category);
  213. }
  214. public static void WriteIf (bool condition, string message,
  215. string category)
  216. {
  217. if (condition)
  218. Write (message, category);
  219. }
  220. public static void WriteLine (object value)
  221. {
  222. lock (lock_) {
  223. foreach (TraceListener listener in Listeners) {
  224. listener.WriteLine (value);
  225. if (AutoFlush)
  226. listener.Flush ();
  227. }
  228. }
  229. }
  230. public static void WriteLine (string message)
  231. {
  232. lock (lock_) {
  233. foreach (TraceListener listener in Listeners) {
  234. listener.WriteLine (message);
  235. if (AutoFlush)
  236. listener.Flush ();
  237. }
  238. }
  239. }
  240. public static void WriteLine (object value, string category)
  241. {
  242. lock (lock_) {
  243. foreach (TraceListener listener in Listeners) {
  244. listener.WriteLine (value, category);
  245. if (AutoFlush)
  246. listener.Flush ();
  247. }
  248. }
  249. }
  250. public static void WriteLine (string message, string category)
  251. {
  252. lock (lock_) {
  253. foreach (TraceListener listener in Listeners) {
  254. listener.WriteLine (message, category);
  255. if (AutoFlush)
  256. listener.Flush ();
  257. }
  258. }
  259. }
  260. public static void WriteLineIf (bool condition, object value)
  261. {
  262. if (condition)
  263. WriteLine (value);
  264. }
  265. public static void WriteLineIf (bool condition, string message)
  266. {
  267. if (condition)
  268. WriteLine (message);
  269. }
  270. public static void WriteLineIf (bool condition, object value,
  271. string category)
  272. {
  273. if (condition)
  274. WriteLine (value, category);
  275. }
  276. public static void WriteLineIf (bool condition, string message,
  277. string category)
  278. {
  279. if (condition)
  280. WriteLine (message, category);
  281. }
  282. }
  283. }