PageRenderTime 54ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/TextCapture.cs

#
C# | 95 lines | 46 code | 12 blank | 37 comment | 13 complexity | 60c7f0974ab3f37903a1b97fd6df8aac MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, 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. using System.IO;
  7. namespace NUnit.Core
  8. {
  9. /// <summary>
  10. /// Abstract base for classes that capture text output
  11. /// and redirect it to a TextWriter.
  12. /// </summary>
  13. public abstract class TextCapture
  14. {
  15. #region Private Fields
  16. /// <summary>
  17. /// True if capture is enabled
  18. /// </summary>
  19. private bool enabled;
  20. /// <summary>
  21. /// The TextWriter to which text is redirected
  22. /// </summary>
  23. private TextWriter writer;
  24. #endregion
  25. #region Properties
  26. /// <summary>
  27. /// The TextWriter to which text is redirected
  28. /// </summary>
  29. public TextWriter Writer
  30. {
  31. get { return writer; }
  32. set
  33. {
  34. writer = value;
  35. if (writer != null && enabled)
  36. StartCapture();
  37. }
  38. }
  39. /// <summary>
  40. /// Controls whether text is captured or not
  41. /// </summary>
  42. public bool Enabled
  43. {
  44. get { return enabled; }
  45. set
  46. {
  47. if (enabled != value)
  48. {
  49. if (writer != null && enabled)
  50. StopCapture();
  51. enabled = value;
  52. if (writer != null && enabled && DefaultThreshold != "Off")
  53. StartCapture();
  54. }
  55. }
  56. }
  57. /// <summary>
  58. /// Returns the default threshold value, which represents
  59. /// the degree of verbosity of the output text stream.
  60. /// Returns "None" in the base class. Derived classes that
  61. /// support verbosity levels should override it.
  62. /// </summary>
  63. public virtual string DefaultThreshold
  64. {
  65. get { return "None"; }
  66. }
  67. #endregion
  68. #region Abstract Members
  69. /// <summary>
  70. /// Override this to perform whatever actions are needed
  71. /// to start capturing text and sending it to the Writer.
  72. /// </summary>
  73. protected abstract void StartCapture();
  74. /// <summary>
  75. /// Override this to perform whatever actions are needed
  76. /// to flush remaining output and stop capturing text.
  77. /// The Writer should not be changed, allowing capture
  78. /// to be restarted at a future point.
  79. /// </summary>
  80. protected abstract void StopCapture();
  81. #endregion
  82. }
  83. }