1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOutputWindow.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkOutputWindow.h"
#if defined(_WIN32) && !defined(VTK_USE_X)
#include "vtkWin32OutputWindow.h"
#endif
#if defined(__ANDROID__) || defined(ANDROID)
#include "vtkAndroidOutputWindow.h"
#endif
#include "vtkCommand.h"
#include "vtkLogger.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include <mutex>
#include <sstream>
#include <thread>
namespace
{
// helps in set and restore value when an instance goes in
// and out of scope respectively.
template <class T>
class vtkScopedSet
{
std::atomic<T>* Ptr;
T OldVal;
public:
vtkScopedSet(std::atomic<T>* ptr, const T& newval)
: Ptr(ptr)
, OldVal(*ptr)
{
*this->Ptr = newval;
}
~vtkScopedSet() { *this->Ptr = this->OldVal; }
};
}
VTK_ABI_NAMESPACE_BEGIN
//------------------------------------------------------------------------------
std::mutex InstanceLock; // XXX(c++17): use a `shared_mutex`
vtkSmartPointer<vtkOutputWindow> vtkOutputWindowGlobalInstance;
// helps accessing private members in vtkOutputWindow.
class vtkOutputWindowPrivateAccessor
{
vtkOutputWindow* Instance;
public:
vtkOutputWindowPrivateAccessor(vtkOutputWindow* self)
: Instance(self)
{
++self->InStandardMacros;
}
~vtkOutputWindowPrivateAccessor() { --(this->Instance->InStandardMacros); }
};
void vtkOutputWindowDisplayText(const char* message)
{
vtkOutputWindow::GetInstance()->DisplayText(message);
}
void vtkOutputWindowDisplayErrorText(const char* message)
{
vtkLogF(ERROR, "%s", message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayErrorText(message);
}
}
void vtkOutputWindowDisplayWarningText(const char* message)
{
vtkLogF(WARNING, "%s", message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayWarningText(message);
}
}
void vtkOutputWindowDisplayGenericWarningText(const char* message)
{
vtkLogF(WARNING, "%s", message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayGenericWarningText(message);
}
}
void vtkOutputWindowDisplayDebugText(const char* message)
{
vtkLogF(INFO, "%s", message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayDebugText(message);
}
}
void vtkOutputWindowDisplayErrorText(
const char* fname, int lineno, const char* message, vtkObject* sourceObj)
{
std::ostringstream vtkmsg;
vtkmsg << "ERROR: In " << fname << ", line " << lineno << "\n" << message << "\n\n";
if (sourceObj && sourceObj->HasObserver(vtkCommand::ErrorEvent))
{
sourceObj->InvokeEvent(vtkCommand::ErrorEvent, const_cast<char*>(vtkmsg.str().c_str()));
}
else if (auto win = vtkOutputWindow::GetInstance())
{
vtkLogger::Log(vtkLogger::VERBOSITY_ERROR, fname, lineno, message);
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayErrorText(vtkmsg.str().c_str());
}
}
void vtkOutputWindowDisplayWarningText(
const char* fname, int lineno, const char* message, vtkObject* sourceObj)
{
std::ostringstream vtkmsg;
vtkmsg << "Warning: In " << fname << ", line " << lineno << "\n" << message << "\n\n";
if (sourceObj && sourceObj->HasObserver(vtkCommand::WarningEvent))
{
sourceObj->InvokeEvent(vtkCommand::WarningEvent, const_cast<char*>(vtkmsg.str().c_str()));
}
else if (auto win = vtkOutputWindow::GetInstance())
{
vtkLogger::Log(vtkLogger::VERBOSITY_WARNING, fname, lineno, message);
vtkOutputWindowPrivateAccessor helper_raii(win);
win->DisplayWarningText(vtkmsg.str().c_str());
}
}
void vtkOutputWindowDisplayGenericWarningText(const char* fname, int lineno, const char* message)
{
vtkLogger::Log(vtkLogger::VERBOSITY_WARNING, fname, lineno, message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
std::ostringstream vtkmsg;
vtkmsg << "Generic Warning: In " << fname << ", line " << lineno << "\n" << message << "\n\n";
win->DisplayGenericWarningText(vtkmsg.str().c_str());
}
}
void vtkOutputWindowDisplayDebugText(
const char* fname, int lineno, const char* message, vtkObject* vtkNotUsed(sourceObj))
{
vtkLogger::Log(vtkLogger::VERBOSITY_INFO, fname, lineno, message);
if (auto win = vtkOutputWindow::GetInstance())
{
vtkOutputWindowPrivateAccessor helper_raii(win);
std::ostringstream vtkmsg;
vtkmsg << "Debug: In " << fname << ", line " << lineno << "\n" << message << "\n\n";
win->DisplayDebugText(vtkmsg.str().c_str());
}
}
vtkObjectFactoryNewMacro(vtkOutputWindow);
vtkOutputWindow::vtkOutputWindow()
{
this->PromptUser = false;
this->CurrentMessageType = MESSAGE_TYPE_TEXT;
this->DisplayMode = vtkOutputWindow::DEFAULT;
this->InStandardMacros = 0;
}
vtkOutputWindow::~vtkOutputWindow() = default;
void vtkOutputWindow::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "vtkOutputWindow Single instance = " << (void*)vtkOutputWindowGlobalInstance
<< endl;
os << indent << "Prompt User: " << (this->PromptUser ? "On\n" : "Off\n");
os << indent << "DisplayMode: ";
switch (this->DisplayMode)
{
case DEFAULT:
os << "Default\n";
break;
case NEVER:
os << "Never\n";
break;
case ALWAYS:
os << "Always\n";
break;
case ALWAYS_STDERR:
os << "AlwaysStderr\n";
break;
}
}
vtkOutputWindow::StreamType vtkOutputWindow::GetDisplayStream(MessageTypes msgType) const
{
switch (this->DisplayMode)
{
case DEFAULT:
if (this->InStandardMacros && vtkLogger::IsEnabled())
{
return StreamType::Null;
}
VTK_FALLTHROUGH;
case ALWAYS:
switch (msgType)
{
case MESSAGE_TYPE_TEXT:
return StreamType::StdOutput;
default:
return StreamType::StdError;
}
case ALWAYS_STDERR:
return StreamType::StdError;
case NEVER:
default:
return StreamType::Null;
}
}
// default implementation outputs to cerr only
void vtkOutputWindow::DisplayText(const char* txt)
{
// pick correct output channel to dump text on.
const auto stream_type = this->GetDisplayStream(this->CurrentMessageType);
switch (stream_type)
{
case StreamType::StdOutput:
cout << txt;
break;
case StreamType::StdError:
cerr << txt;
break;
case StreamType::Null:
break;
}
if (this->PromptUser && this->CurrentMessageType != MESSAGE_TYPE_TEXT &&
stream_type != StreamType::Null)
{
char c = 'n';
cerr << "\nDo you want to suppress any further messages (y,n,q)?." << endl;
cin >> c;
if (c == 'y')
{
vtkObject::GlobalWarningDisplayOff();
}
if (c == 'q')
{
this->PromptUser = false;
}
}
this->InvokeEvent(vtkCommand::MessageEvent, const_cast<char*>(txt));
if (this->CurrentMessageType == MESSAGE_TYPE_TEXT)
{
this->InvokeEvent(vtkCommand::TextEvent, const_cast<char*>(txt));
}
}
void vtkOutputWindow::DisplayErrorText(const char* txt)
{
vtkScopedSet<MessageTypes> setter(&this->CurrentMessageType, MESSAGE_TYPE_ERROR);
this->DisplayText(txt);
this->InvokeEvent(vtkCommand::ErrorEvent, const_cast<char*>(txt));
}
void vtkOutputWindow::DisplayWarningText(const char* txt)
{
vtkScopedSet<MessageTypes> setter(&this->CurrentMessageType, MESSAGE_TYPE_WARNING);
this->DisplayText(txt);
this->InvokeEvent(vtkCommand::WarningEvent, const_cast<char*>(txt));
}
void vtkOutputWindow::DisplayGenericWarningText(const char* txt)
{
vtkScopedSet<MessageTypes> setter(&this->CurrentMessageType, MESSAGE_TYPE_GENERIC_WARNING);
this->DisplayText(txt);
this->InvokeEvent(vtkCommand::WarningEvent, const_cast<char*>(txt));
}
void vtkOutputWindow::DisplayDebugText(const char* txt)
{
vtkScopedSet<MessageTypes> setter(&this->CurrentMessageType, MESSAGE_TYPE_DEBUG);
this->DisplayText(txt);
}
// Return the single instance of the vtkOutputWindow
vtkOutputWindow* vtkOutputWindow::GetInstance()
{
// Check if we have an instance already.
{
std::unique_lock<std::mutex> lock(InstanceLock);
// std::shared_lock lock(InstanceLock); // XXX(c++17)
(void)lock;
if (vtkOutputWindowGlobalInstance)
{
return vtkOutputWindowGlobalInstance;
}
}
{
std::unique_lock<std::mutex> lock(InstanceLock);
(void)lock;
// Another thread may have raced us here; if it already exists, use it.
if (vtkOutputWindowGlobalInstance)
{
return vtkOutputWindowGlobalInstance;
}
// Try the factory first
vtkOutputWindowGlobalInstance.TakeReference(
(vtkOutputWindow*)vtkObjectFactory::CreateInstance("vtkOutputWindow"));
// if the factory did not provide one, then create it here
if (!vtkOutputWindowGlobalInstance)
{
#if defined(_WIN32) && !defined(VTK_USE_X)
vtkOutputWindowGlobalInstance.TakeReference(vtkWin32OutputWindow::New());
#elif defined(ANDROID)
vtkOutputWindowGlobalInstance.TakeReference(vtkAndroidOutputWindow::New());
#else
vtkOutputWindowGlobalInstance.TakeReference(vtkOutputWindow::New());
#endif
}
}
// return the instance
return vtkOutputWindowGlobalInstance;
}
void vtkOutputWindow::SetInstance(vtkOutputWindow* instance)
{
std::unique_lock<std::mutex> lock(InstanceLock);
(void)lock;
if (vtkOutputWindowGlobalInstance == instance)
{
return;
}
vtkOutputWindowGlobalInstance = vtk::MakeSmartPointer(instance);
}
VTK_ABI_NAMESPACE_END
|