iMSTK
Interactive Medical Simulation Toolkit
imstkLoggerSynchronous.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkLoggerSynchronous.h"
8 
9 #include <ctime>
10 #include <iomanip>
11 
12 #if defined(_WIN32)
13 #include <windows.h>
14 #else // not defined(_WIN32)
15 #include <stdlib.h>
16 #endif // not defined(_WIN32)
17 #include <unordered_map>
18 
19 namespace imstk
20 {
21 StreamOutput::StreamOutput(std::ostream& ostream) : m_stream(ostream)
22 {
23 }
24 
25 bool
26 StreamOutput::writeMessage(const std::string& message)
27 {
28  bool result = false;
29  if (!m_stream.fail())
30  {
31  m_mutex.lock();
32  m_stream << message << std::endl;
33  result = true;
34  m_mutex.unlock();
35  }
36  else
37  {
38  throw("Default logging not implemented yet");
39  }
40  return result;
41 }
42 
43 CacheOutput::CacheOutput()
44 {
45  m_outFile.open("imstk.log");
46 }
47 
48 bool
49 CacheOutput::writeMessage(const std::string& message)
50 {
51  m_mutex.lock();
52  m_outFile << message << std::endl;
53  m_messages.push_back(message);
54  m_mutex.unlock();
55  return true;
56 }
57 
58 bool
59 CacheOutput::hasMessages() const
60 {
61  bool result;
62  m_mutex.lock();
63  result = !m_messages.empty();
64  m_mutex.unlock();
65  return result;
66 }
67 
68 std::string
69 CacheOutput::popLastMessage()
70 {
71  std::string result;
72  m_mutex.lock();
73  if (!m_messages.empty())
74  {
75  result = m_messages.front();
76  m_messages.pop_front();
77  }
78  m_mutex.unlock();
79  return result;
80 }
81 
83  : m_stream(), m_logger(logger)
84 {
85  CHECK(logger) << "logger should not be a null pointer";
86  static std::unordered_map<int, std::string> levelNames = {
87  { DEBUG.value, "DEBUG " },
88  { INFO.value, "INFO " },
89  { WARNING.value, "WARNING " },
90  { FATAL.value, "FATAL " }
91  };
92  std::time_t timeStamp;
93  std::time(&timeStamp);
94  ::tm tm;
95 #ifdef _MSC_VER
96  localtime_s(&tm, &timeStamp);
97 #else
98  localtime_r(&timeStamp, &tm);
99 #endif
100  std::string levelName("NONE ");
101  if (level >= 0 && level <= FATAL.value)
102  {
103  levelName = levelNames[level];
104  }
105  char fillChar = m_stream.fill();
106  m_stream << std::setfill('0') <<
107  std::setw(2) << 1 + tm.tm_mon << "." <<
108  std::setw(2) << tm.tm_mday << ' ' <<
109  std::setw(2) << tm.tm_hour << ':' <<
110  std::setw(2) << tm.tm_min << ':' <<
111  std::setw(2) << tm.tm_sec << ' ' <<
112  std::setfill(fillChar) <<
113  levelName << " " <<
114  m_logger->getName() << " ";
115 }
116 
117 void
119 {
120  m_killMeNow = callback;
121 }
122 
125 {
126  return m_killMeNow;
127 }
128 
129 void
130 AssertMessage::throwException(const std::string& errorMessage)
131 {
132  //throw AssertionFailure(errorMessage);
133  throw std::runtime_error(errorMessage);
134 }
135 
136 void
137 AssertMessage::killApplication(const std::string&)
138 {
139 #if defined(_WIN32)
140  DebugBreak();
141 #else // not defined(_WIN32)
142  abort();
143 #endif // not defined(_WIN32)
144 }
145 
146 AssertMessage::DeathCallback AssertMessage::m_killMeNow = AssertMessage::throwException;
147 } // namespace imstk
void(* DeathCallback)(const std::string &message)
The type used for the callback function that is triggered after an assertion has failed.
void unlock()
End a thread-safe region.
Definition: imstkSpinLock.h:53
LogMessageBase(LoggerSynchronous *logger, int level)
Compound Geometry.
void lock()
Start a thread-safe region, where only one thread can execute at a time until a call to the unlock fu...
Definition: imstkSpinLock.h:45
bool writeMessage(const std::string &message) override
static void setFailureCallback(DeathCallback callback)
static DeathCallback getFailureCallback()
bool writeMessage(const std::string &message) override
StreamOutput(std::ostream &ostream)