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 /*=========================================================================
19  *
20  *  Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  *  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  *  For complete copyright, license and disclaimer of warranty information
25  *  please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 # include "itkOutputWindow.h"
29 # include "itkObjectFactory.h"
30 
31 namespace itk
32 {
33 OutputWindow::Pointer OutputWindow:: m_Instance = nullptr;
34 
35 /**
36  * Prompting off by default
37  */
38 OutputWindow
OutputWindow()39 ::OutputWindow()
40 {
41   m_PromptUser = false;
42 }
43 
44 OutputWindow
45 ::~OutputWindow() = default;
46 
47 void
OutputWindowDisplayText(const char * message)48 OutputWindowDisplayText(const char *message)
49 {
50   OutputWindow::GetInstance()->DisplayText(message);
51 }
52 
53 void
OutputWindowDisplayErrorText(const char * message)54 OutputWindowDisplayErrorText(const char *message)
55 {
56   OutputWindow::GetInstance()->DisplayErrorText(message);
57 }
58 
59 void
OutputWindowDisplayWarningText(const char * message)60 OutputWindowDisplayWarningText(const char *message)
61 {
62   OutputWindow::GetInstance()->DisplayWarningText(message);
63 }
64 
65 void
OutputWindowDisplayGenericOutputText(const char * message)66 OutputWindowDisplayGenericOutputText(const char *message)
67 {
68   OutputWindow::GetInstance()->DisplayGenericOutputText(message);
69 }
70 
71 void
OutputWindowDisplayDebugText(const char * message)72 OutputWindowDisplayDebugText(const char *message)
73 {
74   OutputWindow::GetInstance()->DisplayDebugText(message);
75 }
76 
77 void
78 OutputWindow
PrintSelf(std::ostream & os,Indent indent) const79 ::PrintSelf(std::ostream & os, Indent indent) const
80 {
81   Superclass::PrintSelf(os, indent);
82 
83   os << indent << "OutputWindow (single instance): "
84      << (void *)OutputWindow::m_Instance << std::endl;
85 
86   os << indent << "Prompt User: " << ( m_PromptUser ? "On\n" : "Off\n" );
87 }
88 
89 /**
90  * default implementation outputs to cerr only
91  */
92 void
93 OutputWindow
DisplayText(const char * txt)94 ::DisplayText(const char *txt)
95 {
96   std::cerr << txt;
97   if ( m_PromptUser )
98     {
99     char c = 'n';
100     std::cerr << "\nDo you want to suppress any further messages (y,n)?."
101               << std::endl;
102     std::cin >> c;
103     if ( c == 'y' || c == 'Y' )
104       {
105       Object::GlobalWarningDisplayOff();
106       }
107     }
108 }
109 
110 /**
111  * Return the single instance of the OutputWindow
112  */
113 OutputWindow::Pointer
114 OutputWindow
GetInstance()115 ::GetInstance()
116 {
117   if ( !OutputWindow::m_Instance )
118     {
119     // Try the factory first
120     OutputWindow::m_Instance  = ObjectFactory< Self >::Create();
121     // if the factory did not provide one, then create it here
122     if ( !OutputWindow::m_Instance )
123       {
124       OutputWindow::m_Instance = new OutputWindow;
125       // Remove extra reference from construction.
126       OutputWindow::m_Instance->UnRegister();
127       }
128     }
129   /**
130    * return the instance
131    */
132   return OutputWindow::m_Instance;
133 }
134 
135 void
136 OutputWindow
SetInstance(OutputWindow * instance)137 ::SetInstance(OutputWindow *instance)
138 {
139   if ( OutputWindow::m_Instance == instance )
140     {
141     return;
142     }
143   OutputWindow::m_Instance = instance;
144 }
145 
146 /**
147  * This just calls GetInstance
148  */
149 OutputWindow::Pointer
150 OutputWindow
New()151 ::New()
152 {
153   return GetInstance();
154 }
155 } // end namespace itk
156