PageRenderTime 9ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/src/NUnit/core/TextCapture.cs

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