PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/EventListenerTextWriter.cs

#
C# | 114 lines | 93 code | 13 blank | 8 comment | 2 complexity | d950716a04cc6d1d53e460847f253c0d MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2007, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. namespace NUnit.Core
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Text;
  11. public class EventListenerTextWriter : TextWriter
  12. {
  13. private EventListener eventListener;
  14. private TestOutputType type;
  15. public EventListenerTextWriter( EventListener eventListener, TestOutputType type )
  16. {
  17. this.eventListener = eventListener;
  18. this.type = type;
  19. }
  20. override public void Write(char aChar)
  21. {
  22. this.eventListener.TestOutput( new TestOutput( aChar.ToString(), this.type ) );
  23. }
  24. override public void Write(string aString)
  25. {
  26. this.eventListener.TestOutput( new TestOutput( aString, this.type ) );
  27. }
  28. override public void WriteLine(string aString)
  29. {
  30. this.eventListener.TestOutput( new TestOutput( aString + this.NewLine, this.type ) );
  31. }
  32. override public System.Text.Encoding Encoding
  33. {
  34. get { return Encoding.Default; }
  35. }
  36. }
  37. /// <summary>
  38. /// This wrapper adds buffering to improve cross-domain performance.
  39. /// </summary>
  40. public class BufferedEventListenerTextWriter : TextWriter
  41. {
  42. private EventListener eventListener;
  43. private TestOutputType type;
  44. private const int MAX_BUFFER = 1024;
  45. private StringBuilder sb = new StringBuilder( MAX_BUFFER );
  46. public BufferedEventListenerTextWriter( EventListener eventListener, TestOutputType type )
  47. {
  48. this.eventListener = eventListener;
  49. this.type = type;
  50. }
  51. public override Encoding Encoding
  52. {
  53. get
  54. {
  55. return Encoding.Default;
  56. }
  57. }
  58. override public void Write(char ch)
  59. {
  60. lock( sb )
  61. {
  62. sb.Append( ch );
  63. this.CheckBuffer();
  64. }
  65. }
  66. override public void Write(string str)
  67. {
  68. lock( sb )
  69. {
  70. sb.Append( str );
  71. this.CheckBuffer();
  72. }
  73. }
  74. override public void WriteLine(string str)
  75. {
  76. lock( sb )
  77. {
  78. sb.Append( str );
  79. sb.Append( base.NewLine );
  80. this.CheckBuffer();
  81. }
  82. }
  83. override public void Flush()
  84. {
  85. if ( sb.Length > 0 )
  86. {
  87. lock( sb )
  88. {
  89. TestOutput output = new TestOutput(sb.ToString(), this.type);
  90. this.eventListener.TestOutput( output );
  91. sb.Length = 0;
  92. }
  93. }
  94. }
  95. private void CheckBuffer()
  96. {
  97. if ( sb.Length >= MAX_BUFFER )
  98. this.Flush();
  99. }
  100. }
  101. }