TraceImpl.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // System.Diagnostics.TraceImpl.cs
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. //
  7. // (C) 2002 Jonathan Pryor
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Diagnostics;
  31. using System.Configuration;
  32. namespace System.Diagnostics {
  33. internal class TraceImpl {
  34. private static object lock_ = new object ();
  35. private static bool autoFlush;
  36. [ThreadStatic]
  37. private static int indentLevel = 0;
  38. [ThreadStatic]
  39. private static int indentSize;
  40. // Grab the .config file stuff.
  41. //
  42. // There are some ordering issues with the .config file.
  43. //
  44. // The DiagnosticsConfigurationHandler assumes that the TraceImpl.Listeners
  45. // collection exists (so it can initialize the DefaultTraceListener and
  46. // add/remove existing listeners).
  47. //
  48. // When is the .config file read? That's somewhat undefined. The .config
  49. // file will be read the first time someone calls
  50. // ConfigurationSettings.GetConfig(), but when that occurs is
  51. // indeterminate.
  52. //
  53. // Since it's probable that the Trace/Debug classes will be used by the
  54. // application, the .config file should be read in before they're used.
  55. //
  56. // Thus, place the initialization here. We can ensure that everything is
  57. // initialized before reading in the .config file, which should ensure
  58. // that everything is sane.
  59. static TraceImpl ()
  60. {
  61. // defaults
  62. autoFlush = false;
  63. indentLevel = 0;
  64. indentSize = 4;
  65. listeners = new TraceListenerCollection ();
  66. // Initialize the world
  67. System.Collections.IDictionary d = DiagnosticsConfiguration.Settings;
  68. // remove warning about d being unused
  69. d = d;
  70. }
  71. private TraceImpl ()
  72. {
  73. }
  74. public static bool AutoFlush {
  75. get {return autoFlush;}
  76. set {autoFlush = value;}
  77. }
  78. public static int IndentLevel {
  79. get {return indentLevel;}
  80. set {
  81. indentLevel = value;
  82. // Don't need to lock for threadsafety as
  83. // TraceListener.IndentLevel is [ThreadStatic]
  84. foreach (TraceListener t in Listeners) {
  85. t.IndentLevel = indentLevel;
  86. }
  87. }
  88. }
  89. public static int IndentSize {
  90. get {return indentSize;}
  91. set {
  92. indentSize = value;
  93. // Don't need to lock for threadsafety as
  94. // TraceListener.IndentSize is [ThreadStatic]
  95. foreach (TraceListener t in Listeners) {
  96. t.IndentSize = indentSize;
  97. }
  98. }
  99. }
  100. private static TraceListenerCollection listeners;
  101. public static TraceListenerCollection Listeners {
  102. get {return listeners;}
  103. }
  104. // FIXME: According to MSDN, this method should display a dialog box
  105. [MonoTODO]
  106. public static void Assert (bool condition)
  107. {
  108. if (!condition)
  109. Fail (new StackTrace().ToString());
  110. }
  111. // FIXME: According to MSDN, this method should display a dialog box
  112. [MonoTODO]
  113. public static void Assert (bool condition, string message)
  114. {
  115. if (!condition)
  116. Fail (message);
  117. }
  118. // FIXME: According to MSDN, this method should display a dialog box
  119. [MonoTODO]
  120. public static void Assert (bool condition, string message,
  121. string detailMessage)
  122. {
  123. if (!condition)
  124. Fail (message, detailMessage);
  125. }
  126. public static void Close ()
  127. {
  128. lock (lock_) {
  129. foreach (TraceListener listener in Listeners) {
  130. listener.Close ();
  131. }
  132. }
  133. }
  134. // FIXME: From testing .NET, this method should display a dialog
  135. [MonoTODO]
  136. public static void Fail (string message)
  137. {
  138. lock (lock_) {
  139. foreach (TraceListener listener in Listeners) {
  140. listener.Fail (message);
  141. }
  142. }
  143. }
  144. // FIXME: From testing .NET, this method should display a dialog
  145. [MonoTODO]
  146. public static void Fail (string message, string detailMessage)
  147. {
  148. lock (lock_) {
  149. foreach (TraceListener listener in Listeners) {
  150. listener.Fail (message, detailMessage);
  151. }
  152. }
  153. }
  154. public static void Flush ()
  155. {
  156. lock (lock_) {
  157. foreach (TraceListener listener in Listeners){
  158. listener.Flush ();
  159. }
  160. }
  161. }
  162. public static void Indent ()
  163. {
  164. lock (lock_) {
  165. foreach (TraceListener listener in Listeners) {
  166. listener.IndentLevel++;
  167. }
  168. }
  169. }
  170. public static void Unindent ()
  171. {
  172. lock (lock_) {
  173. foreach (TraceListener listener in Listeners) {
  174. listener.IndentLevel--;
  175. }
  176. }
  177. }
  178. public static void Write (object value)
  179. {
  180. lock (lock_) {
  181. foreach (TraceListener listener in Listeners) {
  182. listener.Write (value);
  183. if (AutoFlush)
  184. listener.Flush ();
  185. }
  186. }
  187. }
  188. public static void Write (string message)
  189. {
  190. lock (lock_) {
  191. foreach (TraceListener listener in Listeners) {
  192. listener.Write (message);
  193. if (AutoFlush)
  194. listener.Flush ();
  195. }
  196. }
  197. }
  198. public static void Write (object value, string category)
  199. {
  200. lock (lock_) {
  201. foreach (TraceListener listener in Listeners) {
  202. listener.Write (value, category);
  203. if (AutoFlush)
  204. listener.Flush ();
  205. }
  206. }
  207. }
  208. public static void Write (string message, string category)
  209. {
  210. lock (lock_) {
  211. foreach (TraceListener listener in Listeners) {
  212. listener.Write (message, category);
  213. if (AutoFlush)
  214. listener.Flush ();
  215. }
  216. }
  217. }
  218. public static void WriteIf (bool condition, object value)
  219. {
  220. if (condition)
  221. Write (value);
  222. }
  223. public static void WriteIf (bool condition, string message)
  224. {
  225. if (condition)
  226. Write (message);
  227. }
  228. public static void WriteIf (bool condition, object value,
  229. string category)
  230. {
  231. if (condition)
  232. Write (value, category);
  233. }
  234. public static void WriteIf (bool condition, string message,
  235. string category)
  236. {
  237. if (condition)
  238. Write (message, category);
  239. }
  240. public static void WriteLine (object value)
  241. {
  242. lock (lock_) {
  243. foreach (TraceListener listener in Listeners) {
  244. listener.WriteLine (value);
  245. if (AutoFlush)
  246. listener.Flush ();
  247. }
  248. }
  249. }
  250. public static void WriteLine (string message)
  251. {
  252. lock (lock_) {
  253. foreach (TraceListener listener in Listeners) {
  254. listener.WriteLine (message);
  255. if (AutoFlush)
  256. listener.Flush ();
  257. }
  258. }
  259. }
  260. public static void WriteLine (object value, string category)
  261. {
  262. lock (lock_) {
  263. foreach (TraceListener listener in Listeners) {
  264. listener.WriteLine (value, category);
  265. if (AutoFlush)
  266. listener.Flush ();
  267. }
  268. }
  269. }
  270. public static void WriteLine (string message, string category)
  271. {
  272. lock (lock_) {
  273. foreach (TraceListener listener in Listeners) {
  274. listener.WriteLine (message, category);
  275. if (AutoFlush)
  276. listener.Flush ();
  277. }
  278. }
  279. }
  280. public static void WriteLineIf (bool condition, object value)
  281. {
  282. if (condition)
  283. WriteLine (value);
  284. }
  285. public static void WriteLineIf (bool condition, string message)
  286. {
  287. if (condition)
  288. WriteLine (message);
  289. }
  290. public static void WriteLineIf (bool condition, object value,
  291. string category)
  292. {
  293. if (condition)
  294. WriteLine (value, category);
  295. }
  296. public static void WriteLineIf (bool condition, string message,
  297. string category)
  298. {
  299. if (condition)
  300. WriteLine (message, category);
  301. }
  302. }
  303. }