1 /*=========================================================================
2  *
3  *  Copyright Insight Software Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef itkFileOutputWindow_h
19 #define itkFileOutputWindow_h
20 
21 #include "itkOutputWindow.h"
22 #include "itkObjectFactory.h"
23 #include <fstream>
24 
25 namespace itk
26 {
27 /** \class FileOutputWindow
28  * \brief Messages sent from the system are sent to a file.
29  *
30  * Text messages that the system should display to the user are sent to
31  * this object (or subclasses of this object) and are logged to a file.
32  *
33  * \ingroup OSSystemObjects
34  *
35  * \ingroup ITKCommon
36  */
37 
38 class ITKCommon_EXPORT FileOutputWindow:public OutputWindow
39 {
40 public:
41   ITK_DISALLOW_COPY_AND_ASSIGN(FileOutputWindow);
42 
43   /** Standard class type aliases. */
44   using Self = FileOutputWindow;
45   using Superclass = OutputWindow;
46   using Pointer = SmartPointer< Self >;
47   using ConstPointer = SmartPointer< const Self >;
48 
49   /** Method for creation through the object factory. */
50   itkNewMacro(Self);
51 
52   /** Run-time type information (and related methods). */
53   itkTypeMacro(FileOutputWindow, OutputWindow);
54 
55   /** Send a string to display. */
56   void DisplayText(const char *) override;
57 
58   /** Set the filename for the log file */
59   itkSetStringMacro(FileName);
60 
61   /** Get the filename for the log file */
62   itkGetStringMacro(FileName);
63 
64   /** Set/Get the buffer flushing mode */
65   itkSetMacro(Flush, bool);
66   itkGetConstMacro(Flush, bool);
67   itkBooleanMacro(Flush);
68 
69   /** Setting append will cause the log file to be
70    * opened in append mode.  Otherwise, if the log file exists,
71    * it will be overwritten each time the FileOutputWindow
72    * is created. */
73   itkSetMacro(Append, bool);
74   itkGetConstMacro(Append, bool);
75   itkBooleanMacro(Append);
76 
77 protected:
78   FileOutputWindow();
79   ~FileOutputWindow() override;
80   void PrintSelf(std::ostream & os, Indent indent) const override;
81 
82   void Initialize();
83 
84   std::ofstream *m_Stream;
85 
86   std::string m_FileName;
87 
88   bool m_Flush;
89   bool m_Append;
90 };
91 } // end namespace itk
92 
93 #endif
94