opensurgsim
LogMessageBase.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_LOGMESSAGEBASE_H
17 #define SURGSIM_FRAMEWORK_LOGMESSAGEBASE_H
18 
19 #include <string>
20 #include <sstream>
21 #include <iomanip>
22 #include <ctime>
23 
24 #include "SurgSim/Framework/Logger.h"
25 
26 namespace SurgSim
27 {
28 namespace Framework
29 {
30 
33 
34 
41 {
42 public:
43 
47  LogMessageBase(Logger* logger, int level);
48 
51  {
52  }
53 
56  template <typename T>
58  {
59  m_stream << input;
60  return *this;
61  }
62 
63  // A specialization for output manipulators (functions that apply to the stream).
64  // Otherwise overloaded manipulators like std::endl and std::endl don't work, since the compiler can't know
65  // what overloaded variant to apply.
66  LogMessageBase& operator <<(std::ios_base& (*manipulator)(std::ios_base&))
67  {
68  m_stream << *manipulator;
69  return *this;
70  }
71 
72  // A specialization for output manipulators (functions that apply to the stream).
73  // Otherwise overloaded manipulators like std::hex and std::endl don't work, since the compiler can't know
74  // what overloaded variant to apply.
75  LogMessageBase& operator <<(std::ostream& (*manipulator)(std::ostream&))
76  {
77  m_stream << *manipulator;
78  return *this;
79  }
80 
81 protected:
83  std::string getMessage()
84  {
85  return m_stream.str();
86  }
87 
89  void flush()
90  {
91  m_logger->writeMessage(m_stream.str());
92  }
93 
94 private:
95  std::ostringstream m_stream;
96  Logger* m_logger;
97 };
98 
99 
101 
102 }; // namespace Framework
103 }; // namespace SurgSim
104 
105 #endif // SURGSIM_FRAMEWORK_LOGMESSAGEBASE_H
LogMessageBase(Logger *logger, int level)
Construct a LogMessage.
Definition: LogMessageBase.cpp:25
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
~LogMessageBase()
Destructor.
Definition: LogMessageBase.h:50
void flush()
write the current message to the logger
Definition: LogMessageBase.h:89
LogMessageBase & operator<<(T &&input)
Add the given input to the current log message.
Definition: LogMessageBase.h:57
An object that can be used to control logging parameters, such as verbosity and log output destinatio...
Definition: Logger.h:51
bool writeMessage(const std::string &message)
Uses the contained instance of LogOutput to write the log message.
Definition: Logger.h:65
std::string getMessage()
Definition: LogMessageBase.h:83
LogMessageBase is a base class to be used to customize messages for logging textual information can b...
Definition: LogMessageBase.h:40