TraceImpl.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. namespace System.Diagnostics {
  12. internal class TraceImpl {
  13. private static object lock_ = new object ();
  14. private static bool autoFlush = false;
  15. public static bool AutoFlush {
  16. get {return autoFlush;}
  17. set {autoFlush = value;}
  18. }
  19. // FIXME: From MSDN: "This property is stored on
  20. // per-thread/pre-reqeust basis"
  21. //
  22. // What exactly does this mean? Sure, we can mark it
  23. // [ThreadStatic], which make a per-thread value, but what
  24. // does this mean for each of the writers? Do *they* need to
  25. // store this as a thread-static value?
  26. [MonoTODO, ThreadStatic]
  27. private static int indentLevel = 0;
  28. public static int IndentLevel {
  29. get {return indentLevel;}
  30. set {indentLevel = value;}
  31. }
  32. // FIXME: From MSDN: "This property is stored on
  33. // per-thread/pre-reqeust basis"
  34. //
  35. // What exactly does this mean? Sure, we can mark it
  36. // [ThreadStatic], which makes a per-thread value, but what
  37. // does this mean for each of the writers? Do *they* need to
  38. // store this as a thread-static value?
  39. [MonoTODO, ThreadStatic]
  40. private static int indentSize = 4;
  41. public static int IndentSize {
  42. get {return indentSize;}
  43. set {indentSize = value;}
  44. }
  45. private static TraceListenerCollection listeners =
  46. new TraceListenerCollection ();
  47. public static TraceListenerCollection Listeners {
  48. get {return listeners;}
  49. }
  50. // FIXME: According to MSDN, this method should display a dialog box
  51. [MonoTODO]
  52. public static void Assert (bool condition)
  53. {
  54. if (!condition)
  55. Fail (new StackTrace().ToString());
  56. }
  57. // FIXME: According to MSDN, this method should display a dialog box
  58. [MonoTODO]
  59. public static void Assert (bool condition, string message)
  60. {
  61. if (!condition)
  62. Fail (message);
  63. }
  64. // FIXME: According to MSDN, this method should display a dialog box
  65. [MonoTODO]
  66. public static void Assert (bool condition, string message,
  67. string detailMessage)
  68. {
  69. if (!condition)
  70. Fail (message, detailMessage);
  71. }
  72. public static void Close ()
  73. {
  74. lock (lock_) {
  75. foreach (TraceListener listener in Listeners) {
  76. listener.Close ();
  77. }
  78. }
  79. }
  80. // FIXME: From testing .NET, this method should display a dialog
  81. [MonoTODO]
  82. public static void Fail (string message)
  83. {
  84. lock (lock_) {
  85. foreach (TraceListener listener in Listeners) {
  86. listener.Fail (message);
  87. }
  88. }
  89. }
  90. // FIXME: From testing .NET, this method should display a dialog
  91. [MonoTODO]
  92. public static void Fail (string message, string detailMessage)
  93. {
  94. lock (lock_) {
  95. foreach (TraceListener listener in Listeners) {
  96. listener.Fail (message, detailMessage);
  97. }
  98. }
  99. }
  100. public static void Flush ()
  101. {
  102. lock (lock_) {
  103. foreach (TraceListener listener in Listeners){
  104. listener.Flush ();
  105. }
  106. }
  107. }
  108. public static void Indent ()
  109. {
  110. lock (lock_) {
  111. foreach (TraceListener listener in Listeners) {
  112. listener.IndentLevel++;
  113. }
  114. }
  115. }
  116. public static void Unindent ()
  117. {
  118. lock (lock_) {
  119. foreach (TraceListener listener in Listeners) {
  120. listener.IndentLevel--;
  121. }
  122. }
  123. }
  124. public static void Write (object value)
  125. {
  126. lock (lock_) {
  127. foreach (TraceListener listener in Listeners) {
  128. listener.Write (value);
  129. if (AutoFlush)
  130. listener.Flush ();
  131. }
  132. }
  133. }
  134. public static void Write (string message)
  135. {
  136. lock (lock_) {
  137. foreach (TraceListener listener in Listeners) {
  138. listener.Write (message);
  139. if (AutoFlush)
  140. listener.Flush ();
  141. }
  142. }
  143. }
  144. public static void Write (object value, string category)
  145. {
  146. lock (lock_) {
  147. foreach (TraceListener listener in Listeners) {
  148. listener.Write (value, category);
  149. if (AutoFlush)
  150. listener.Flush ();
  151. }
  152. }
  153. }
  154. public static void Write (string message, string category)
  155. {
  156. lock (lock_) {
  157. foreach (TraceListener listener in Listeners) {
  158. listener.Write (message, category);
  159. if (AutoFlush)
  160. listener.Flush ();
  161. }
  162. }
  163. }
  164. public static void WriteIf (bool condition, object value)
  165. {
  166. if (condition)
  167. Write (value);
  168. }
  169. public static void WriteIf (bool condition, string message)
  170. {
  171. if (condition)
  172. Write (message);
  173. }
  174. public static void WriteIf (bool condition, object value,
  175. string category)
  176. {
  177. if (condition)
  178. Write (value, category);
  179. }
  180. public static void WriteIf (bool condition, string message,
  181. string category)
  182. {
  183. if (condition)
  184. Write (message, category);
  185. }
  186. public static void WriteLine (object value)
  187. {
  188. lock (lock_) {
  189. foreach (TraceListener listener in Listeners) {
  190. listener.WriteLine (value);
  191. if (AutoFlush)
  192. listener.Flush ();
  193. }
  194. }
  195. }
  196. public static void WriteLine (string message)
  197. {
  198. lock (lock_) {
  199. foreach (TraceListener listener in Listeners) {
  200. listener.WriteLine (message);
  201. if (AutoFlush)
  202. listener.Flush ();
  203. }
  204. }
  205. }
  206. public static void WriteLine (object value, string category)
  207. {
  208. lock (lock_) {
  209. foreach (TraceListener listener in Listeners) {
  210. listener.WriteLine (value, category);
  211. if (AutoFlush)
  212. listener.Flush ();
  213. }
  214. }
  215. }
  216. public static void WriteLine (string message, string category)
  217. {
  218. lock (lock_) {
  219. foreach (TraceListener listener in Listeners) {
  220. listener.WriteLine (message, category);
  221. if (AutoFlush)
  222. listener.Flush ();
  223. }
  224. }
  225. }
  226. public static void WriteLineIf (bool condition, object value)
  227. {
  228. if (condition)
  229. WriteLine (value);
  230. }
  231. public static void WriteLineIf (bool condition, string message)
  232. {
  233. if (condition)
  234. WriteLine (message);
  235. }
  236. public static void WriteLineIf (bool condition, object value,
  237. string category)
  238. {
  239. if (condition)
  240. WriteLine (value, category);
  241. }
  242. public static void WriteLineIf (bool condition, string message,
  243. string category)
  244. {
  245. if (condition)
  246. WriteLine (message, category);
  247. }
  248. }
  249. }