Skip to content

Commit 9292631

Browse files
authored
Merge pull request #467 from OSVR/update-spdlog
Merging this pull request for now, which addresses the crashing issue and @godbyk will provide additional improvements in a new pull request.
2 parents 3a449d7 + d9358fc commit 9292631

File tree

10 files changed

+178
-424
lines changed

10 files changed

+178
-424
lines changed

inc/osvr/Util/LineLogger.h

Lines changed: 0 additions & 180 deletions
This file was deleted.

inc/osvr/Util/Logger.h

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
// Internal Includes
3232
#include <osvr/Util/Export.h>
33-
#include <osvr/Util/LineLogger.h>
3433
#include <osvr/Util/Log.h> // for LoggerPtr
3534
#include <osvr/Util/LogLevel.h>
3635

@@ -39,8 +38,9 @@
3938

4039
// Standard includes
4140
#include <initializer_list>
42-
#include <memory> // for std::shared_ptr
43-
#include <string> // for std::string
41+
#include <memory> // for std::shared_ptr
42+
#include <sstream> // for std::ostringstream
43+
#include <string> // for std::string
4444

4545
// Forward declarations
4646

@@ -120,51 +120,104 @@ namespace util {
120120
/// Set the log level at which this logger will trigger a flush.
121121
OSVR_UTIL_EXPORT void flushOn(LogLevel level);
122122

123+
/// An object returned the logging functions (including operator<<),
124+
/// serves to accumulate streamed output in a single ostringstream
125+
/// then write it to the logger at the end of the expression's
126+
/// lifetime.
127+
class StreamProxy {
128+
public:
129+
StreamProxy(Logger &logger, LogLevel level)
130+
: logger_(logger), level_(level),
131+
os_(new std::ostringstream) {}
132+
133+
StreamProxy(Logger &logger, LogLevel level, const char *msg)
134+
: logger_(logger), level_(level),
135+
os_(new std::ostringstream) {
136+
os_->operator<<(msg);
137+
}
138+
139+
/// destructor appends the finished stringstream at the end
140+
/// of the expression.
141+
~StreamProxy() {
142+
if (active_ && os_) {
143+
logger_.write(level_, os_->str().c_str());
144+
}
145+
}
146+
147+
/// move construction
148+
StreamProxy(StreamProxy &&other)
149+
: logger_(other.logger_), level_(other.level_),
150+
os_(std::move(other.os_)), active_(other.active_) {
151+
other.active_ = false;
152+
}
153+
154+
StreamProxy(StreamProxy const &) = delete;
155+
StreamProxy &operator=(StreamProxy const &) = delete;
156+
157+
operator std::ostream &() { return (*os_); }
158+
159+
template <typename T> std::ostream &operator<<(T &&what) {
160+
(*os_) << std::forward<T>(what);
161+
return (*os_);
162+
}
163+
164+
private:
165+
Logger &logger_;
166+
LogLevel level_;
167+
std::unique_ptr<std::ostringstream> os_;
168+
bool active_ = true;
169+
};
170+
123171
/// @name logger->info(msg) (with optional << "more message") call
124172
/// style
125173
/// @{
126-
OSVR_UTIL_EXPORT detail::LineLogger trace(const char *msg);
127-
OSVR_UTIL_EXPORT detail::LineLogger debug(const char *msg);
128-
OSVR_UTIL_EXPORT detail::LineLogger info(const char *msg);
129-
OSVR_UTIL_EXPORT detail::LineLogger notice(const char *msg);
130-
OSVR_UTIL_EXPORT detail::LineLogger warn(const char *msg);
131-
OSVR_UTIL_EXPORT detail::LineLogger error(const char *msg);
132-
OSVR_UTIL_EXPORT detail::LineLogger critical(const char *msg);
174+
OSVR_UTIL_EXPORT StreamProxy trace(const char *msg);
175+
OSVR_UTIL_EXPORT StreamProxy debug(const char *msg);
176+
OSVR_UTIL_EXPORT StreamProxy info(const char *msg);
177+
OSVR_UTIL_EXPORT StreamProxy notice(const char *msg);
178+
OSVR_UTIL_EXPORT StreamProxy warn(const char *msg);
179+
OSVR_UTIL_EXPORT StreamProxy error(const char *msg);
180+
OSVR_UTIL_EXPORT StreamProxy critical(const char *msg);
133181
/// @}
134182

135183
/// @name logger->info() << "msg" call style
136184
/// @{
137-
OSVR_UTIL_EXPORT detail::LineLogger trace();
138-
OSVR_UTIL_EXPORT detail::LineLogger debug();
139-
OSVR_UTIL_EXPORT detail::LineLogger info();
140-
OSVR_UTIL_EXPORT detail::LineLogger notice();
141-
OSVR_UTIL_EXPORT detail::LineLogger warn();
142-
OSVR_UTIL_EXPORT detail::LineLogger error();
143-
OSVR_UTIL_EXPORT detail::LineLogger critical();
185+
OSVR_UTIL_EXPORT StreamProxy trace();
186+
OSVR_UTIL_EXPORT StreamProxy debug();
187+
OSVR_UTIL_EXPORT StreamProxy info();
188+
OSVR_UTIL_EXPORT StreamProxy notice();
189+
OSVR_UTIL_EXPORT StreamProxy warn();
190+
OSVR_UTIL_EXPORT StreamProxy error();
191+
OSVR_UTIL_EXPORT StreamProxy critical();
144192
/// @}
145193

146194
/// logger.log(log_level, msg) (with optional << "more message")
147195
/// call style
148-
OSVR_UTIL_EXPORT detail::LineLogger log(LogLevel level,
149-
const char *msg);
196+
OSVR_UTIL_EXPORT StreamProxy log(LogLevel level, const char *msg);
150197

151198
/// logger.log(log_level) << "msg" call style
152-
OSVR_UTIL_EXPORT detail::LineLogger log(LogLevel level);
199+
OSVR_UTIL_EXPORT StreamProxy log(LogLevel level);
153200

154201
/// Make sure this logger has written out its data.
155202
OSVR_UTIL_EXPORT void flush();
156203

157204
/// Get the logger name
158-
std::string const &getName() const { return name_; }
205+
OSVR_UTIL_EXPORT std::string const &getName() const {
206+
return name_;
207+
}
159208

160209
private:
161210
static LoggerPtr
162211
makeLogger(std::string const &name,
163212
std::shared_ptr<spdlog::logger> const &logger);
213+
164214
/// In case a spdlog logger is not available, this will create a
165215
/// fallback logger instance using just ostream.
166216
static LoggerPtr makeFallback(std::string const &name);
167217

218+
/// Pass the constructed message along to the underlying logger.
219+
OSVR_UTIL_EXPORT void write(LogLevel level, const char* msg);
220+
168221
const std::string name_;
169222
std::shared_ptr<spdlog::logger> logger_;
170223
};

src/osvr/Util/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ set(API
7979
"${HEADER_LOCATION}/ImagingReportTypesC.h"
8080
"${HEADER_LOCATION}/IndentingStream.h"
8181
"${HEADER_LOCATION}/KeyedOwnershipContainer.h"
82-
"${HEADER_LOCATION}/LineLogger.h"
8382
"${HEADER_LOCATION}/Logger.h"
8483
"${HEADER_LOCATION}/Log.h"
8584
"${HEADER_LOCATION}/LogLevel.h"
@@ -146,7 +145,6 @@ set(SOURCE
146145
GetEnvironmentVariable.cpp
147146
GuardInterface.cpp
148147
TimeValueC.cpp
149-
LineLogger.cpp
150148
LogConfig.h.in
151149
LogDefaults.h
152150
Log.cpp
@@ -196,6 +194,12 @@ target_link_libraries(${LIBNAME_FULL}
196194
boost_filesystem
197195
osvrTypePack)
198196

197+
if(ANDROID)
198+
target_link_libraries(${LIBNAME_FULL}
199+
PRIVATE
200+
log)
201+
endif()
202+
199203
if(NOT OSVR_HAVE_STDALIGN)
200204
target_link_libraries(${LIBNAME_FULL}
201205
PRIVATE

0 commit comments

Comments
 (0)