From 45132c9573868b37479362d854f91b07d3c4b409 Mon Sep 17 00:00:00 2001 From: christiankerl Date: Sat, 27 Jun 2015 21:08:38 +0200 Subject: [PATCH 01/10] Initial log api definition fixed WithLogImpl::setLog; removed global ConsoleLog instance; updated Freenect2 to manage lifetime of Log instance renamed Log to Logger added LIBFREENECT2_API macro to logging classes added environment variable LIBFREENECT2_LOGGER_LEVEL to change default logger level, possible values 'debug','info','warning','error' made logger level immutable --- examples/protonect/CMakeLists.txt | 1 + examples/protonect/Protonect.cpp | 3 + .../include/libfreenect2/libfreenect2.hpp | 6 +- .../protonect/include/libfreenect2/logging.h | 104 +++++++++++ examples/protonect/src/libfreenect2.cpp | 131 +++++++++----- examples/protonect/src/logging.cpp | 164 ++++++++++++++++++ 6 files changed, 364 insertions(+), 45 deletions(-) create mode 100644 examples/protonect/include/libfreenect2/logging.h create mode 100644 examples/protonect/src/logging.cpp diff --git a/examples/protonect/CMakeLists.txt b/examples/protonect/CMakeLists.txt index 7ef31c831..d1a6d69d1 100644 --- a/examples/protonect/CMakeLists.txt +++ b/examples/protonect/CMakeLists.txt @@ -102,6 +102,7 @@ SET(SOURCES src/command_transaction.cpp src/registration.cpp src/timer.cpp + src/logging.cpp src/libfreenect2.cpp ${LIBFREENECT2_THREADING_SOURCE} diff --git a/examples/protonect/Protonect.cpp b/examples/protonect/Protonect.cpp index a3b3df992..1f4627b25 100644 --- a/examples/protonect/Protonect.cpp +++ b/examples/protonect/Protonect.cpp @@ -58,6 +58,9 @@ int main(int argc, char *argv[]) } libfreenect2::Freenect2 freenect2; + // create a console logger with debug level (default is console logger with info level) + freenect2.setLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug)); + libfreenect2::Freenect2Device *dev = 0; libfreenect2::PacketPipeline *pipeline = 0; diff --git a/examples/protonect/include/libfreenect2/libfreenect2.hpp b/examples/protonect/include/libfreenect2/libfreenect2.hpp index 38f36728a..d5b876b01 100644 --- a/examples/protonect/include/libfreenect2/libfreenect2.hpp +++ b/examples/protonect/include/libfreenect2/libfreenect2.hpp @@ -29,6 +29,7 @@ #include #include +#include namespace libfreenect2 { @@ -95,12 +96,15 @@ class LIBFREENECT2_API Freenect2Device class Freenect2Impl; -class LIBFREENECT2_API Freenect2 +class LIBFREENECT2_API Freenect2 : public WithLogger { public: Freenect2(void *usb_context = 0); virtual ~Freenect2(); + virtual void setLogger(Logger *logger); + virtual Logger *logger(); + int enumerateDevices(); std::string getDeviceSerialNumber(int idx); diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h new file mode 100644 index 000000000..50b3c5a16 --- /dev/null +++ b/examples/protonect/include/libfreenect2/logging.h @@ -0,0 +1,104 @@ +/* + * This file is part of the OpenKinect Project. http://www.openkinect.org + * + * Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file + * for details. + * + * This code is licensed to you under the terms of the Apache License, version + * 2.0, or, at your option, the terms of the GNU General Public License, + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, + * or the following URLs: + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/gpl-2.0.txt + * + * If you redistribute this file in source form, modified or unmodified, you + * may: + * 1) Leave this header intact and distribute it under the same terms, + * accompanying it with the APACHE20 and GPL20 files, or + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file + * In all cases you must keep the copyright notice intact and include a copy + * of the CONTRIB file. + * + * Binary distributions must follow the binary distribution requirements of + * either License. + */ + +#ifndef LOGGING_H_ +#define LOGGING_H_ + +#include +#include + +#include + +namespace libfreenect2 +{ + +class LIBFREENECT2_API Logger +{ +public: + enum Level + { + Debug = 1, + Info = 2, + Warning = 3, + Error = 4, + }; + static Level getDefaultLevel(); + + virtual ~Logger(); + + virtual Level level() const; + + virtual void log(Level level, const std::string &message) = 0; +protected: + Level level_; +}; + +LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level); +LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel(); +LIBFREENECT2_API Logger *createNoopLogger(); + +class LIBFREENECT2_API LogMessage +{ +private: + Logger *logger_; + Logger::Level level_; + std::ostringstream stream_; +public: + LogMessage(Logger *logger, Logger::Level level); + ~LogMessage(); + + std::ostream &stream(); +}; + +class LIBFREENECT2_API WithLogger +{ +public: + virtual ~WithLogger(); + virtual void setLogger(Logger *logger) = 0; + virtual Logger *logger() = 0; +}; + +class LIBFREENECT2_API WithLoggerImpl : public WithLogger +{ +protected: + Logger *logger_; + + virtual void onLoggerChanged(Logger *logger); +public: + WithLoggerImpl(); + virtual ~WithLoggerImpl(); + virtual void setLogger(Logger *logger); + virtual Logger *logger(); +}; + +} /* namespace libfreenect2 */ + +#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream()) +#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream()) +#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream()) +#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream()) + +#endif /* LOGGING_H_ */ diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index e34da1b1b..920b88ffc 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -45,7 +45,7 @@ using namespace libfreenect2; using namespace libfreenect2::usb; using namespace libfreenect2::protocol; -class Freenect2DeviceImpl : public Freenect2Device +class Freenect2DeviceImpl : public Freenect2Device, public WithLoggerImpl { private: enum State @@ -110,7 +110,7 @@ std::ostream &operator<<(std::ostream &out, const PrintBusAndDevice& dev) return out; } -class Freenect2Impl +class Freenect2Impl : public WithLoggerImpl { private: bool managed_usb_context_; @@ -134,20 +134,22 @@ class Freenect2Impl usb_context_(reinterpret_cast(usb_context)), has_device_enumeration_(false) { + logger_ = createConsoleLoggerWithDefaultLevel(); + if(managed_usb_context_) { int r = libusb_init(&usb_context_); // TODO: error handling if(r != 0) { - std::cout << "[Freenect2Impl] failed to create usb context!" << std::endl; + LOG_ERROR << "[Freenect2Impl] failed to create usb context!"; } } usb_event_loop_.start(usb_context_); } - ~Freenect2Impl() + virtual ~Freenect2Impl() { clearDevices(); clearDeviceEnumeration(); @@ -159,10 +161,33 @@ class Freenect2Impl libusb_exit(usb_context_); usb_context_ = 0; } + + setLoggerInternal(0); + } + + void setLoggerInternal(Logger *logger) + { + Logger *old_logger = logger_; + WithLoggerImpl::setLogger(logger); + delete old_logger; + } + + virtual void setLogger(Logger *logger) + { + setLoggerInternal(logger != 0 ? logger : createNoopLogger()); + } + + virtual void onLoggerChanged(Logger *logger) + { + for(DeviceVector::iterator it = devices_.begin(); it != devices_.end(); ++it) + { + (*it)->setLogger(logger); + } } void addDevice(Freenect2DeviceImpl *device) { + device->setLogger(logger_); devices_.push_back(device); } @@ -173,10 +198,11 @@ class Freenect2Impl if(it != devices_.end()) { devices_.erase(it); + device->setLogger(0); } else { - std::cout << "[Freenect2Impl] tried to remove device, which is not in the internal device list!" << std::endl; + LOG_WARNING << "[Freenect2Impl] tried to remove device, which is not in the internal device list!"; } } @@ -205,7 +231,7 @@ class Freenect2Impl if(!devices_.empty()) { - std::cout << "[Freenect2Impl] after deleting all devices the internal device list should be empty!" << std::endl; + LOG_WARNING << "[Freenect2Impl] after deleting all devices the internal device list should be empty!"; } } @@ -223,11 +249,11 @@ class Freenect2Impl void enumerateDevices() { - std::cout << "[Freenect2Impl] enumerating devices..." << std::endl; + LOG_INFO << "[Freenect2Impl] enumerating devices..."; libusb_device **device_list; int num_devices = libusb_get_device_list(usb_context_, &device_list); - std::cout << "[Freenect2Impl] " << num_devices << " usb devices connected" << std::endl; + LOG_INFO << "[Freenect2Impl] " << num_devices << " usb devices connected"; if(num_devices > 0) { @@ -268,21 +294,21 @@ class Freenect2Impl dev_with_serial.dev = dev; dev_with_serial.serial = std::string(reinterpret_cast(buffer), size_t(r)); - std::cout << "[Freenect2Impl] found valid Kinect v2 " << PrintBusAndDevice(dev) << " with serial " << dev_with_serial.serial << std::endl; + LOG_INFO << "[Freenect2Impl] found valid Kinect v2 " << PrintBusAndDevice(dev) << " with serial " << dev_with_serial.serial; // valid Kinect v2 enumerated_devices_.push_back(dev_with_serial); continue; } else { - std::cout << "[Freenect2Impl] failed to get serial number of Kinect v2 " << PrintBusAndDevice(dev) << "!" << std::endl; + LOG_ERROR << "[Freenect2Impl] failed to get serial number of Kinect v2 " << PrintBusAndDevice(dev) << "!"; } libusb_close(dev_handle); } else { - std::cout << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev) << "!" << std::endl; + LOG_ERROR << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev) << "!"; } } } @@ -293,7 +319,7 @@ class Freenect2Impl libusb_free_device_list(device_list, 0); has_device_enumeration_ = true; - std::cout << "[Freenect2Impl] found " << enumerated_devices_.size() << " devices" << std::endl; + LOG_INFO << "[Freenect2Impl] found " << enumerated_devices_.size() << " devices"; } int getNumDevices() @@ -397,7 +423,7 @@ void Freenect2DeviceImpl::setIrAndDepthFrameListener(libfreenect2::FrameListener bool Freenect2DeviceImpl::open() { - std::cout << "[Freenect2DeviceImpl] opening..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] opening..."; if(state_ != Created) return false; @@ -417,7 +443,7 @@ bool Freenect2DeviceImpl::open() if(max_iso_packet_size < 0x8400) { - std::cout << "[Freenect2DeviceImpl] max iso packet size for endpoint 0x84 too small! (expected: " << 0x8400 << " got: " << max_iso_packet_size << ")" << std::endl; + LOG_ERROR << "[Freenect2DeviceImpl] max iso packet size for endpoint 0x84 too small! (expected: " << 0x8400 << " got: " << max_iso_packet_size << ")"; return false; } @@ -426,14 +452,14 @@ bool Freenect2DeviceImpl::open() state_ = Open; - std::cout << "[Freenect2DeviceImpl] opened" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] opened"; return true; } void Freenect2DeviceImpl::start() { - std::cout << "[Freenect2DeviceImpl] starting..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] starting..."; if(state_ != Open) return; CommandTransaction::Result serial_result, firmware_result, result; @@ -444,15 +470,16 @@ void Freenect2DeviceImpl::start() firmware_ = FirmwareVersionResponse(firmware_result.data, firmware_result.length).toString(); command_tx_.execute(ReadData0x14Command(nextCommandSeq()), result); - std::cout << "[Freenect2DeviceImpl] ReadData0x14 response" << std::endl; - std::cout << GenericResponse(result.data, result.length).toString() << std::endl; + LOG_DEBUG + << "[Freenect2DeviceImpl] ReadData0x14 response" << std::endl + << GenericResponse(result.data, result.length).toString(); command_tx_.execute(ReadSerialNumberCommand(nextCommandSeq()), serial_result); std::string new_serial = SerialNumberResponse(serial_result.data, serial_result.length).toString(); if(serial_ != new_serial) { - std::cout << "[Freenect2DeviceImpl] serial number reported by libusb " << serial_ << " differs from serial number " << new_serial << " in device protocol! " << std::endl; + LOG_WARNING << "[Freenect2DeviceImpl] serial number reported by libusb " << serial_ << " differs from serial number " << new_serial << " in device protocol! "; } command_tx_.execute(ReadDepthCameraParametersCommand(nextCommandSeq()), result); @@ -506,16 +533,18 @@ void Freenect2DeviceImpl::start() rgb_camera_params_.my_x0y0 = rgb_p->my_x0y0; // 1 command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); - std::cout << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl; - std::cout << GenericResponse(result.data, result.length).toString() << std::endl; + LOG_DEBUG + << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl + << GenericResponse(result.data, result.length).toString(); command_tx_.execute(InitStreamsCommand(nextCommandSeq()), result); usb_control_.setIrInterfaceState(UsbControl::Enabled); command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); - std::cout << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl; - std::cout << GenericResponse(result.data, result.length).toString() << std::endl; + LOG_DEBUG + << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl + << GenericResponse(result.data, result.length).toString(); command_tx_.execute(SetStreamEnabledCommand(nextCommandSeq()), result); @@ -536,33 +565,33 @@ void Freenect2DeviceImpl::start() command_tx_.execute(ReadData0x26Command(nextCommandSeq()), result); command_tx_.execute(ReadData0x26Command(nextCommandSeq()), result); */ - std::cout << "[Freenect2DeviceImpl] enabling usb transfer submission..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] enabling usb transfer submission..."; rgb_transfer_pool_.enableSubmission(); ir_transfer_pool_.enableSubmission(); - std::cout << "[Freenect2DeviceImpl] submitting usb transfers..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] submitting usb transfers..."; rgb_transfer_pool_.submit(20); ir_transfer_pool_.submit(60); state_ = Streaming; - std::cout << "[Freenect2DeviceImpl] started" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] started"; } void Freenect2DeviceImpl::stop() { - std::cout << "[Freenect2DeviceImpl] stopping..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] stopping..."; if(state_ != Streaming) { - std::cout << "[Freenect2DeviceImpl] already stopped, doing nothing" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] already stopped, doing nothing"; return; } - std::cout << "[Freenect2DeviceImpl] disabling usb transfer submission..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] disabling usb transfer submission..."; rgb_transfer_pool_.disableSubmission(); ir_transfer_pool_.disableSubmission(); - std::cout << "[Freenect2DeviceImpl] canceling usb transfers..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] canceling usb transfers..."; rgb_transfer_pool_.cancel(); ir_transfer_pool_.cancel(); @@ -575,16 +604,16 @@ void Freenect2DeviceImpl::stop() usb_control_.setVideoTransferFunctionState(UsbControl::Disabled); state_ = Open; - std::cout << "[Freenect2DeviceImpl] stopped" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] stopped"; } void Freenect2DeviceImpl::close() { - std::cout << "[Freenect2DeviceImpl] closing..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] closing..."; if(state_ == Closed) { - std::cout << "[Freenect2DeviceImpl] already closed, doing nothing" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] already closed, doing nothing"; return; } @@ -601,24 +630,24 @@ void Freenect2DeviceImpl::close() if(has_usb_interfaces_) { - std::cout << "[Freenect2DeviceImpl] releasing usb interfaces..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] releasing usb interfaces..."; usb_control_.releaseInterfaces(); has_usb_interfaces_ = false; } - std::cout << "[Freenect2DeviceImpl] deallocating usb transfer pools..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] deallocating usb transfer pools..."; rgb_transfer_pool_.deallocate(); ir_transfer_pool_.deallocate(); - std::cout << "[Freenect2DeviceImpl] closing usb device..." << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] closing usb device..."; libusb_close(usb_device_handle_); usb_device_handle_ = 0; usb_device_ = 0; state_ = Closed; - std::cout << "[Freenect2DeviceImpl] closed" << std::endl; + LOG_INFO << "[Freenect2DeviceImpl] closed"; } PacketPipeline *createDefaultPacketPipeline() @@ -644,6 +673,16 @@ Freenect2::~Freenect2() delete impl_; } +void Freenect2::setLogger(Logger *logger) +{ + impl_->setLogger(logger); +} + +Logger *Freenect2::logger() +{ + return impl_->logger(); +} + int Freenect2::enumerateDevices() { impl_->clearDeviceEnumeration(); @@ -677,8 +716,9 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(idx >= num_devices) { - std::cout << "[Freenect2Impl] requested device " << idx << " is not connected!" << std::endl; + LOG_ERROR << "[Freenect2Impl] requested device " << idx << " is not connected!"; delete pipeline; + return device; } @@ -687,9 +727,10 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(impl_->tryGetDevice(dev.dev, &device)) { - std::cout << "[Freenect2Impl] failed to get device " << PrintBusAndDevice(dev.dev) - << " (the device may already be open)" << std::endl; + LOG_WARNING << "[Freenect2Impl] device " << PrintBusAndDevice(dev.dev) + << " is already be open!"; delete pipeline; + return device; } @@ -697,8 +738,9 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(r != LIBUSB_SUCCESS) { - std::cout << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!" << std::endl; + LOG_ERROR << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; delete pipeline; + return device; } @@ -726,7 +768,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(1000)); // reenumerate devices - std::cout << "[Freenect2Impl] re-enumerating devices after reset" << std::endl; + LOG_INFO << "[Freenect2Impl] re-enumerating devices after reset"; impl_->clearDeviceEnumeration(); impl_->enumerateDevices(); @@ -735,8 +777,9 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, } else if(r != LIBUSB_SUCCESS) { - std::cout << "[Freenect2Impl] failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!" << std::endl; + LOG_ERROR << "[Freenect2Impl] failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; delete pipeline; + return device; } } @@ -749,7 +792,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, delete device; device = 0; - std::cout << "[Freenect2DeviceImpl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!" << std::endl; + LOG_ERROR << "[Freenect2DeviceImpl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; } return device; diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp new file mode 100644 index 000000000..ef1ad986e --- /dev/null +++ b/examples/protonect/src/logging.cpp @@ -0,0 +1,164 @@ +/* + * This file is part of the OpenKinect Project. http://www.openkinect.org + * + * Copyright (c) 2015 individual OpenKinect contributors. See the CONTRIB file + * for details. + * + * This code is licensed to you under the terms of the Apache License, version + * 2.0, or, at your option, the terms of the GNU General Public License, + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, + * or the following URLs: + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/gpl-2.0.txt + * + * If you redistribute this file in source form, modified or unmodified, you + * may: + * 1) Leave this header intact and distribute it under the same terms, + * accompanying it with the APACHE20 and GPL20 files, or + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file + * In all cases you must keep the copyright notice intact and include a copy + * of the CONTRIB file. + * + * Binary distributions must follow the binary distribution requirements of + * either License. + */ + +#include +#include +#include +#include +#include + +namespace libfreenect2 +{ +Logger::~Logger() {} + + +Logger::Level Logger::getDefaultLevel() +{ + Logger::Level l = Logger::Info; + + char *env_logger_level_c_str = getenv("LIBFREENECT2_LOGGER_LEVEL"); + + if(env_logger_level_c_str != 0) + { + std::string env_logger_level_str(env_logger_level_c_str); + std::transform(env_logger_level_str.begin(), env_logger_level_str.end(), env_logger_level_str.begin(), ::tolower); + + if(env_logger_level_str == "debug") + l = Logger::Debug; + else if(env_logger_level_str == "info") + l = Logger::Info; + else if(env_logger_level_str == "warning") + l = Logger::Warning; + else if(env_logger_level_str == "error") + l = Logger::Error; + } + + return l; +} + +Logger::Level Logger::level() const +{ + return level_; +} + +std::string level2str(const Logger::Level &l) +{ + switch(l) + { + case Logger::Debug: + return "Debug"; + case Logger::Info: + return "Info"; + case Logger::Warning: + return "Warning"; + case Logger::Error: + return "Error"; + default: + return ""; + } +} + +class ConsoleLogger : public Logger +{ +public: + ConsoleLogger(Level level) + { + level_ = level; + } + virtual ~ConsoleLogger() {} + virtual void log(Level level, const std::string &message) + { + if(level < level_) return; + + (level >= Warning ? std::cerr : std::cout) << "[" << level2str(level) << "]" << message << std::endl; + } +}; + +class NoopLogger : public Logger +{ +public: + NoopLogger() + { + level_ = Debug; + } + virtual ~NoopLogger() {} + virtual void log(Level level, const std::string &message) {} +}; + +Logger *createConsoleLogger(Logger::Level level) +{ + return new ConsoleLogger(level); +} + +Logger *createConsoleLoggerWithDefaultLevel() +{ + return new ConsoleLogger(Logger::getDefaultLevel()); +} + +Logger *createNoopLogger() +{ + return new NoopLogger(); +} + +LogMessage::LogMessage(Logger *logger, Logger::Level level) : logger_(logger), level_(level) +{ + +} + +LogMessage::~LogMessage() +{ + if(logger_ != 0) + { + logger_->log(level_, stream_.str()); + } +} + +std::ostream &LogMessage::stream() +{ + return stream_; +} + +WithLogger::~WithLogger() {} + +WithLoggerImpl::WithLoggerImpl() : logger_(0) +{ +} + +WithLoggerImpl::~WithLoggerImpl() {} +void WithLoggerImpl::onLoggerChanged(Logger *logger) {} + +void WithLoggerImpl::setLogger(Logger *logger) +{ + logger_ = logger; + onLoggerChanged(logger_); +} + +Logger *WithLoggerImpl::logger() +{ + return logger_; +} + +} /* namespace libfreenect2 */ From a50e58d2501a6821e50482aae38b5c57e1f58174 Mon Sep 17 00:00:00 2001 From: christiankerl Date: Fri, 10 Jul 2015 22:15:20 +0200 Subject: [PATCH 02/10] Changed LOG_* macros to prepend function signature --- .../protonect/include/libfreenect2/logging.h | 16 +++- examples/protonect/src/libfreenect2.cpp | 74 +++++++++---------- 2 files changed, 49 insertions(+), 41 deletions(-) diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h index 50b3c5a16..2dcd3d0f1 100644 --- a/examples/protonect/include/libfreenect2/logging.h +++ b/examples/protonect/include/libfreenect2/logging.h @@ -96,9 +96,17 @@ class LIBFREENECT2_API WithLoggerImpl : public WithLogger } /* namespace libfreenect2 */ -#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream()) -#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream()) -#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream()) -#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream()) +#if defined(__GNUC__) or defined(__clang__) +#define LIBFREENECT2_LOG_SOURCE __PRETTY_FUNCTION__ +#elif defined(_MSC_VER) +#define LIBFREENECT2_LOG_SOURCE __FUNCSIG__ +#else +#define LIBFREENECT2_LOG_SOURCE "" +#endif + +#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") #endif /* LOGGING_H_ */ diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index 920b88ffc..c66659e95 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -142,7 +142,7 @@ class Freenect2Impl : public WithLoggerImpl // TODO: error handling if(r != 0) { - LOG_ERROR << "[Freenect2Impl] failed to create usb context!"; + LOG_ERROR << "failed to create usb context!"; } } @@ -202,7 +202,7 @@ class Freenect2Impl : public WithLoggerImpl } else { - LOG_WARNING << "[Freenect2Impl] tried to remove device, which is not in the internal device list!"; + LOG_WARNING << "tried to remove device, which is not in the internal device list!"; } } @@ -231,7 +231,7 @@ class Freenect2Impl : public WithLoggerImpl if(!devices_.empty()) { - LOG_WARNING << "[Freenect2Impl] after deleting all devices the internal device list should be empty!"; + LOG_WARNING << "after deleting all devices the internal device list should be empty!"; } } @@ -249,11 +249,11 @@ class Freenect2Impl : public WithLoggerImpl void enumerateDevices() { - LOG_INFO << "[Freenect2Impl] enumerating devices..."; + LOG_INFO << "enumerating devices..."; libusb_device **device_list; int num_devices = libusb_get_device_list(usb_context_, &device_list); - LOG_INFO << "[Freenect2Impl] " << num_devices << " usb devices connected"; + LOG_INFO << num_devices << " usb devices connected"; if(num_devices > 0) { @@ -294,21 +294,21 @@ class Freenect2Impl : public WithLoggerImpl dev_with_serial.dev = dev; dev_with_serial.serial = std::string(reinterpret_cast(buffer), size_t(r)); - LOG_INFO << "[Freenect2Impl] found valid Kinect v2 " << PrintBusAndDevice(dev) << " with serial " << dev_with_serial.serial; + LOG_INFO << "found valid Kinect v2 " << PrintBusAndDevice(dev) << " with serial " << dev_with_serial.serial; // valid Kinect v2 enumerated_devices_.push_back(dev_with_serial); continue; } else { - LOG_ERROR << "[Freenect2Impl] failed to get serial number of Kinect v2 " << PrintBusAndDevice(dev) << "!"; + LOG_ERROR << "failed to get serial number of Kinect v2 " << PrintBusAndDevice(dev) << "!"; } libusb_close(dev_handle); } else { - LOG_ERROR << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev) << "!"; + LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev) << "!"; } } } @@ -319,7 +319,7 @@ class Freenect2Impl : public WithLoggerImpl libusb_free_device_list(device_list, 0); has_device_enumeration_ = true; - LOG_INFO << "[Freenect2Impl] found " << enumerated_devices_.size() << " devices"; + LOG_INFO << "found " << enumerated_devices_.size() << " devices"; } int getNumDevices() @@ -423,7 +423,7 @@ void Freenect2DeviceImpl::setIrAndDepthFrameListener(libfreenect2::FrameListener bool Freenect2DeviceImpl::open() { - LOG_INFO << "[Freenect2DeviceImpl] opening..."; + LOG_INFO << "opening..."; if(state_ != Created) return false; @@ -443,7 +443,7 @@ bool Freenect2DeviceImpl::open() if(max_iso_packet_size < 0x8400) { - LOG_ERROR << "[Freenect2DeviceImpl] max iso packet size for endpoint 0x84 too small! (expected: " << 0x8400 << " got: " << max_iso_packet_size << ")"; + LOG_ERROR << "max iso packet size for endpoint 0x84 too small! (expected: " << 0x8400 << " got: " << max_iso_packet_size << ")"; return false; } @@ -452,14 +452,14 @@ bool Freenect2DeviceImpl::open() state_ = Open; - LOG_INFO << "[Freenect2DeviceImpl] opened"; + LOG_INFO << "opened"; return true; } void Freenect2DeviceImpl::start() { - LOG_INFO << "[Freenect2DeviceImpl] starting..."; + LOG_INFO << "starting..."; if(state_ != Open) return; CommandTransaction::Result serial_result, firmware_result, result; @@ -471,7 +471,7 @@ void Freenect2DeviceImpl::start() command_tx_.execute(ReadData0x14Command(nextCommandSeq()), result); LOG_DEBUG - << "[Freenect2DeviceImpl] ReadData0x14 response" << std::endl + << "ReadData0x14 response" << std::endl << GenericResponse(result.data, result.length).toString(); command_tx_.execute(ReadSerialNumberCommand(nextCommandSeq()), serial_result); @@ -479,7 +479,7 @@ void Freenect2DeviceImpl::start() if(serial_ != new_serial) { - LOG_WARNING << "[Freenect2DeviceImpl] serial number reported by libusb " << serial_ << " differs from serial number " << new_serial << " in device protocol! "; + LOG_WARNING << "serial number reported by libusb " << serial_ << " differs from serial number " << new_serial << " in device protocol! "; } command_tx_.execute(ReadDepthCameraParametersCommand(nextCommandSeq()), result); @@ -534,7 +534,7 @@ void Freenect2DeviceImpl::start() command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); LOG_DEBUG - << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl + << "ReadStatus0x090000 response" << std::endl << GenericResponse(result.data, result.length).toString(); command_tx_.execute(InitStreamsCommand(nextCommandSeq()), result); @@ -543,7 +543,7 @@ void Freenect2DeviceImpl::start() command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); LOG_DEBUG - << "[Freenect2DeviceImpl] ReadStatus0x090000 response" << std::endl + << "ReadStatus0x090000 response" << std::endl << GenericResponse(result.data, result.length).toString(); command_tx_.execute(SetStreamEnabledCommand(nextCommandSeq()), result); @@ -565,33 +565,33 @@ void Freenect2DeviceImpl::start() command_tx_.execute(ReadData0x26Command(nextCommandSeq()), result); command_tx_.execute(ReadData0x26Command(nextCommandSeq()), result); */ - LOG_INFO << "[Freenect2DeviceImpl] enabling usb transfer submission..."; + LOG_INFO << "enabling usb transfer submission..."; rgb_transfer_pool_.enableSubmission(); ir_transfer_pool_.enableSubmission(); - LOG_INFO << "[Freenect2DeviceImpl] submitting usb transfers..."; + LOG_INFO << "submitting usb transfers..."; rgb_transfer_pool_.submit(20); ir_transfer_pool_.submit(60); state_ = Streaming; - LOG_INFO << "[Freenect2DeviceImpl] started"; + LOG_INFO << "started"; } void Freenect2DeviceImpl::stop() { - LOG_INFO << "[Freenect2DeviceImpl] stopping..."; + LOG_INFO << "stopping..."; if(state_ != Streaming) { - LOG_INFO << "[Freenect2DeviceImpl] already stopped, doing nothing"; + LOG_INFO << "already stopped, doing nothing"; return; } - LOG_INFO << "[Freenect2DeviceImpl] disabling usb transfer submission..."; + LOG_INFO << "disabling usb transfer submission..."; rgb_transfer_pool_.disableSubmission(); ir_transfer_pool_.disableSubmission(); - LOG_INFO << "[Freenect2DeviceImpl] canceling usb transfers..."; + LOG_INFO << "canceling usb transfers..."; rgb_transfer_pool_.cancel(); ir_transfer_pool_.cancel(); @@ -604,16 +604,16 @@ void Freenect2DeviceImpl::stop() usb_control_.setVideoTransferFunctionState(UsbControl::Disabled); state_ = Open; - LOG_INFO << "[Freenect2DeviceImpl] stopped"; + LOG_INFO << "stopped"; } void Freenect2DeviceImpl::close() { - LOG_INFO << "[Freenect2DeviceImpl] closing..."; + LOG_INFO << "closing..."; if(state_ == Closed) { - LOG_INFO << "[Freenect2DeviceImpl] already closed, doing nothing"; + LOG_INFO << "already closed, doing nothing"; return; } @@ -630,24 +630,24 @@ void Freenect2DeviceImpl::close() if(has_usb_interfaces_) { - LOG_INFO << "[Freenect2DeviceImpl] releasing usb interfaces..."; + LOG_INFO << "releasing usb interfaces..."; usb_control_.releaseInterfaces(); has_usb_interfaces_ = false; } - LOG_INFO << "[Freenect2DeviceImpl] deallocating usb transfer pools..."; + LOG_INFO << "deallocating usb transfer pools..."; rgb_transfer_pool_.deallocate(); ir_transfer_pool_.deallocate(); - LOG_INFO << "[Freenect2DeviceImpl] closing usb device..."; + LOG_INFO << "closing usb device..."; libusb_close(usb_device_handle_); usb_device_handle_ = 0; usb_device_ = 0; state_ = Closed; - LOG_INFO << "[Freenect2DeviceImpl] closed"; + LOG_INFO << "closed"; } PacketPipeline *createDefaultPacketPipeline() @@ -716,7 +716,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(idx >= num_devices) { - LOG_ERROR << "[Freenect2Impl] requested device " << idx << " is not connected!"; + LOG_ERROR << "requested device " << idx << " is not connected!"; delete pipeline; return device; @@ -727,7 +727,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(impl_->tryGetDevice(dev.dev, &device)) { - LOG_WARNING << "[Freenect2Impl] device " << PrintBusAndDevice(dev.dev) + LOG_WARNING << "device " << PrintBusAndDevice(dev.dev) << " is already be open!"; delete pipeline; @@ -738,7 +738,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, if(r != LIBUSB_SUCCESS) { - LOG_ERROR << "[Freenect2Impl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; + LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; delete pipeline; return device; @@ -768,7 +768,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(1000)); // reenumerate devices - LOG_INFO << "[Freenect2Impl] re-enumerating devices after reset"; + LOG_INFO << "re-enumerating devices after reset"; impl_->clearDeviceEnumeration(); impl_->enumerateDevices(); @@ -777,7 +777,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, } else if(r != LIBUSB_SUCCESS) { - LOG_ERROR << "[Freenect2Impl] failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; + LOG_ERROR << "failed to reset Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; delete pipeline; return device; @@ -792,7 +792,7 @@ Freenect2Device *Freenect2::openDevice(int idx, const PacketPipeline *pipeline, delete device; device = 0; - LOG_ERROR << "[Freenect2DeviceImpl] failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; + LOG_ERROR << "failed to open Kinect v2 " << PrintBusAndDevice(dev.dev) << "!"; } return device; From 81e115178ce36e40093c06660748941837f46724 Mon Sep 17 00:00:00 2001 From: christiankerl Date: Fri, 10 Jul 2015 23:51:12 +0200 Subject: [PATCH 03/10] Use LOG_* macros in all classes except packet processors --- .../libfreenect2/depth_packet_stream_parser.h | 3 +- .../protonect/include/libfreenect2/logging.h | 10 ++++ .../include/libfreenect2/packet_pipeline.h | 5 +- .../protocol/command_transaction.h | 5 +- .../libfreenect2/protocol/usb_control.h | 3 +- .../libfreenect2/rgb_packet_stream_parser.h | 3 +- .../include/libfreenect2/usb/transfer_pool.h | 3 +- .../protonect/src/command_transaction.cpp | 13 +++-- .../src/depth_packet_stream_parser.cpp | 11 ++-- examples/protonect/src/libfreenect2.cpp | 13 +++++ examples/protonect/src/packet_pipeline.cpp | 8 +++ .../src/rgb_packet_stream_parser.cpp | 11 ++-- examples/protonect/src/transfer_pool.cpp | 13 +++-- examples/protonect/src/usb_control.cpp | 50 +++++++++---------- 14 files changed, 93 insertions(+), 58 deletions(-) diff --git a/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h b/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h index 7773cadc4..8086ae903 100644 --- a/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h +++ b/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -51,7 +52,7 @@ LIBFREENECT2_PACK(struct LIBFREENECT2_API DepthSubPacketFooter uint32_t fields[32]; }); -class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback +class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback, public WithLoggerImpl { public: DepthPacketStreamParser(); diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h index 2dcd3d0f1..f71a386af 100644 --- a/examples/protonect/include/libfreenect2/logging.h +++ b/examples/protonect/include/libfreenect2/logging.h @@ -81,6 +81,16 @@ class LIBFREENECT2_API WithLogger virtual Logger *logger() = 0; }; +template +void trySetLogger(T *obj, Logger *logger) +{ + WithLogger *obj_with_logger = dynamic_cast(obj); + if(obj_with_logger != 0) + { + obj_with_logger->setLogger(logger); + } +} + class LIBFREENECT2_API WithLoggerImpl : public WithLogger { protected: diff --git a/examples/protonect/include/libfreenect2/packet_pipeline.h b/examples/protonect/include/libfreenect2/packet_pipeline.h index b1bf467c2..2d6003138 100644 --- a/examples/protonect/include/libfreenect2/packet_pipeline.h +++ b/examples/protonect/include/libfreenect2/packet_pipeline.h @@ -28,6 +28,7 @@ #define PACKET_PIPELINE_H_ #include +#include #include #include #include @@ -50,7 +51,7 @@ class LIBFREENECT2_API PacketPipeline virtual DepthPacketProcessor *getDepthPacketProcessor() const = 0; }; -class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline +class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline, public WithLoggerImpl { protected: RgbPacketStreamParser *rgb_parser_; @@ -63,6 +64,8 @@ class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline virtual void initialize(); virtual DepthPacketProcessor *createDepthPacketProcessor() = 0; + + virtual void onLoggerChanged(Logger *logger); public: virtual ~BasePacketPipeline(); diff --git a/examples/protonect/include/libfreenect2/protocol/command_transaction.h b/examples/protonect/include/libfreenect2/protocol/command_transaction.h index 6994c9104..56bd0b38a 100644 --- a/examples/protonect/include/libfreenect2/protocol/command_transaction.h +++ b/examples/protonect/include/libfreenect2/protocol/command_transaction.h @@ -28,6 +28,7 @@ #define COMMAND_TRANSACTION_H_ #include +#include #include namespace libfreenect2 @@ -35,7 +36,7 @@ namespace libfreenect2 namespace protocol { -class CommandTransaction +class CommandTransaction : public WithLoggerImpl { public: static const int ResponseCompleteLength = 16; @@ -63,7 +64,7 @@ class CommandTransaction }; CommandTransaction(libusb_device_handle *handle, int inbound_endpoint, int outbound_endpoint); - ~CommandTransaction(); + virtual ~CommandTransaction(); void execute(const CommandBase& command, Result& result); private: diff --git a/examples/protonect/include/libfreenect2/protocol/usb_control.h b/examples/protonect/include/libfreenect2/protocol/usb_control.h index f42cafed0..d4ffeb2eb 100644 --- a/examples/protonect/include/libfreenect2/protocol/usb_control.h +++ b/examples/protonect/include/libfreenect2/protocol/usb_control.h @@ -28,6 +28,7 @@ #define USB_CONTROL_H_ #include +#include namespace libfreenect2 { @@ -61,7 +62,7 @@ namespace protocol * * The 2. interface can be enabled/disabled by changing its alternate setting to 1/0 */ -class UsbControl +class UsbControl : public WithLoggerImpl { public: UsbControl(libusb_device_handle *handle); diff --git a/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h b/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h index ac1cd0caf..090d7cc1e 100644 --- a/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h +++ b/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -38,7 +39,7 @@ namespace libfreenect2 { -class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback +class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback, public WithLoggerImpl { public: RgbPacketStreamParser(); diff --git a/examples/protonect/include/libfreenect2/usb/transfer_pool.h b/examples/protonect/include/libfreenect2/usb/transfer_pool.h index e4091a97a..8d54fbde4 100644 --- a/examples/protonect/include/libfreenect2/usb/transfer_pool.h +++ b/examples/protonect/include/libfreenect2/usb/transfer_pool.h @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -39,7 +40,7 @@ namespace libfreenect2 namespace usb { -class TransferPool +class TransferPool : public WithLoggerImpl { public: TransferPool(libusb_device_handle *device_handle, unsigned char device_endpoint); diff --git a/examples/protonect/src/command_transaction.cpp b/examples/protonect/src/command_transaction.cpp index b1b6efe84..90e4ac481 100644 --- a/examples/protonect/src/command_transaction.cpp +++ b/examples/protonect/src/command_transaction.cpp @@ -27,7 +27,6 @@ #include #include -#include namespace libfreenect2 { @@ -106,7 +105,7 @@ void CommandTransaction::execute(const CommandBase& command, Result& result) if(complete) { - std::cerr << "[CommandTransaction::execute] received premature response complete!" << std::endl; + LOG_ERROR << "received premature response complete!"; result.code = Error; } @@ -119,7 +118,7 @@ void CommandTransaction::execute(const CommandBase& command, Result& result) if(!complete) { - std::cerr << "[CommandTransaction::execute] missing response complete!" << std::endl; + LOG_ERROR << "missing response complete!"; result.code = Error; } @@ -135,13 +134,13 @@ CommandTransaction::ResultCode CommandTransaction::send(const CommandBase& comma if(r != LIBUSB_SUCCESS) { - std::cerr << "[CommandTransaction::send] bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r) << std::endl; + LOG_ERROR << "bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r); code = Error; } if(transferred_bytes != command.size()) { - std::cerr << "[CommandTransaction::send] sent number of bytes differs from expected number! expected: " << command.size() << " got: " << transferred_bytes << std::endl; + LOG_ERROR << "sent number of bytes differs from expected number! expected: " << command.size() << " got: " << transferred_bytes; code = Error; } @@ -157,7 +156,7 @@ void CommandTransaction::receive(CommandTransaction::Result& result) if(r != LIBUSB_SUCCESS) { - std::cerr << "[CommandTransaction::receive] bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r) << std::endl; + LOG_ERROR << "bulk transfer failed! libusb error " << r << ": " << libusb_error_name(r); result.code = Error; } } @@ -176,7 +175,7 @@ bool CommandTransaction::isResponseCompleteResult(CommandTransaction::Result& re if(data[1] != sequence) { - std::cerr << "[CommandTransaction::isResponseCompleteResult] response complete with wrong sequence number! expected: " << sequence << " got: " << data[1]<< std::endl; + LOG_ERROR << "response complete with wrong sequence number! expected: " << sequence << " got: " << data[1]; } } } diff --git a/examples/protonect/src/depth_packet_stream_parser.cpp b/examples/protonect/src/depth_packet_stream_parser.cpp index 3a03c44ea..29c299f94 100644 --- a/examples/protonect/src/depth_packet_stream_parser.cpp +++ b/examples/protonect/src/depth_packet_stream_parser.cpp @@ -25,7 +25,6 @@ */ #include -#include #include namespace libfreenect2 @@ -79,7 +78,7 @@ void DepthPacketStreamParser::onDataReceived(unsigned char* buffer, size_t in_le if(wb.length + in_length > wb.capacity) { - std::cerr << "[DepthPacketStreamParser::onDataReceived] subpacket too large" << std::endl; + LOG_ERROR << "subpacket too large"; wb.length = 0; return; } @@ -91,7 +90,7 @@ void DepthPacketStreamParser::onDataReceived(unsigned char* buffer, size_t in_le { if(footer->length != wb.length) { - std::cerr << "[DepthPacketStreamParser::onDataReceived] image data too short!" << std::endl; + LOG_ERROR << "image data too short!"; } else { @@ -113,12 +112,12 @@ void DepthPacketStreamParser::onDataReceived(unsigned char* buffer, size_t in_le } else { - std::cerr << "[DepthPacketStreamParser::onDataReceived] skipping depth packet" << std::endl; + LOG_WARNING << "skipping depth packet"; } } else { - std::cerr << "[DepthPacketStreamParser::onDataReceived] not all subsequences received " << current_subsequence_ << std::endl; + LOG_ERROR << "not all subsequences received " << current_subsequence_; } current_sequence_ = footer->sequence; @@ -132,7 +131,7 @@ void DepthPacketStreamParser::onDataReceived(unsigned char* buffer, size_t in_le if(footer->subsequence * footer->length > fb.length) { - std::cerr << "[DepthPacketStreamParser::onDataReceived] front buffer too short! subsequence number is " << footer->subsequence << std::endl; + LOG_ERROR << "front buffer too short! subsequence number is " << footer->subsequence; } else { diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index c66659e95..383c43a39 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -74,6 +74,8 @@ class Freenect2DeviceImpl : public Freenect2Device, public WithLoggerImpl std::string serial_, firmware_; Freenect2Device::IrCameraParams ir_camera_params_; Freenect2Device::ColorCameraParams rgb_camera_params_; +protected: + virtual void onLoggerChanged(Logger *logger); public: Freenect2DeviceImpl(Freenect2Impl *context, const PacketPipeline *pipeline, libusb_device *usb_device, libusb_device_handle *usb_device_handle, const std::string &serial); virtual ~Freenect2DeviceImpl(); @@ -364,6 +366,17 @@ Freenect2DeviceImpl::~Freenect2DeviceImpl() delete pipeline_; } +void Freenect2DeviceImpl::onLoggerChanged(Logger *logger) +{ + rgb_transfer_pool_.setLogger(logger); + ir_transfer_pool_.setLogger(logger); + usb_control_.setLogger(logger); + command_tx_.setLogger(logger); + + // TODO: is this ok? + trySetLogger(const_cast(pipeline_), logger); +} + int Freenect2DeviceImpl::nextCommandSeq() { return command_seq_++; diff --git a/examples/protonect/src/packet_pipeline.cpp b/examples/protonect/src/packet_pipeline.cpp index 1c66f66b6..9642291f9 100644 --- a/examples/protonect/src/packet_pipeline.cpp +++ b/examples/protonect/src/packet_pipeline.cpp @@ -49,6 +49,14 @@ void BasePacketPipeline::initialize() depth_parser_->setPacketProcessor(async_depth_processor_); } +void BasePacketPipeline::onLoggerChanged(Logger *logger) +{ + rgb_parser_->setLogger(logger); + depth_parser_->setLogger(logger); + trySetLogger(rgb_processor_, logger); + trySetLogger(depth_processor_, logger); +} + BasePacketPipeline::~BasePacketPipeline() { delete async_rgb_processor_; diff --git a/examples/protonect/src/rgb_packet_stream_parser.cpp b/examples/protonect/src/rgb_packet_stream_parser.cpp index 4eaaf49c5..c9805a7c1 100644 --- a/examples/protonect/src/rgb_packet_stream_parser.cpp +++ b/examples/protonect/src/rgb_packet_stream_parser.cpp @@ -27,7 +27,6 @@ #include #include #include -#include namespace libfreenect2 { @@ -88,7 +87,7 @@ void RgbPacketStreamParser::onDataReceived(unsigned char* buffer, size_t length) } else { - std::cerr << "[RgbPacketStreamParser::onDataReceived] buffer overflow!" << std::endl; + LOG_ERROR << "buffer overflow!"; fb.length = 0; return; } @@ -105,14 +104,14 @@ void RgbPacketStreamParser::onDataReceived(unsigned char* buffer, size_t length) if (fb.length != footer->packet_size || raw_packet->sequence != footer->sequence) { - std::cerr << "[RgbPacketStreamParser::onDataReceived] packetsize or sequence doesn't match!" << std::endl; + LOG_ERROR << "packetsize or sequence doesn't match!"; fb.length = 0; return; } if (fb.length - sizeof(RawRgbPacket) - sizeof(RgbPacketFooter) < footer->filler_length) { - std::cerr << "[RgbPacketStreamParser::onDataReceived] not enough space for packet filler!" << std::endl; + LOG_ERROR << "not enough space for packet filler!"; fb.length = 0; return; } @@ -132,7 +131,7 @@ void RgbPacketStreamParser::onDataReceived(unsigned char* buffer, size_t length) if (jpeg_length == 0) { - std::cerr << "[RgbPacketStreamParser::onDataReceived] no JPEG detected!" << std::endl; + LOG_ERROR << "no JPEG detected!"; fb.length = 0; return; } @@ -155,7 +154,7 @@ void RgbPacketStreamParser::onDataReceived(unsigned char* buffer, size_t length) } else { - std::cerr << "[RgbPacketStreamParser::onDataReceived] skipping rgb packet!" << std::endl; + LOG_WARNING << "skipping rgb packet!"; } // reset front buffer diff --git a/examples/protonect/src/transfer_pool.cpp b/examples/protonect/src/transfer_pool.cpp index b324659a4..b45087db1 100644 --- a/examples/protonect/src/transfer_pool.cpp +++ b/examples/protonect/src/transfer_pool.cpp @@ -25,7 +25,6 @@ */ #include -#include namespace libfreenect2 { @@ -77,13 +76,13 @@ void TransferPool::submit(size_t num_parallel_transfers) { if(!enable_submit_) { - std::cerr << "[TransferPool::submit] transfer submission disabled!" << std::endl; + LOG_WARNING << "transfer submission disabled!"; return; } if(transfers_.size() < num_parallel_transfers) { - std::cerr << "[TransferPool::submit] too few idle transfers!" << std::endl; + LOG_ERROR << "too few idle transfers!"; } for(size_t i = 0; i < num_parallel_transfers; ++i) @@ -95,7 +94,7 @@ void TransferPool::submit(size_t num_parallel_transfers) if(r != LIBUSB_SUCCESS) { - std::cerr << "[TransferPool::submit] failed to submit transfer: " << libusb_error_name(r) << std::endl; + LOG_ERROR << "failed to submit transfer: " << libusb_error_name(r); transfers_[i].setStopped(true); } } @@ -109,7 +108,7 @@ void TransferPool::cancel() if(r != LIBUSB_SUCCESS && r != LIBUSB_ERROR_NOT_FOUND) { - std::cerr << "[TransferPool::cancel] failed to cancel transfer: " << libusb_error_name(r) << std::endl; + LOG_ERROR << "failed to cancel transfer: " << libusb_error_name(r); } } @@ -121,7 +120,7 @@ void TransferPool::cancel() stopped_transfers += it->getStopped(); if (stopped_transfers == transfers_.size()) break; - std::cerr << "[TransferPool::cancel] waiting for transfer cancellation" << std::endl; + LOG_INFO << "waiting for transfer cancellation"; libfreenect2::this_thread::sleep_for(libfreenect2::chrono::milliseconds(1000)); } } @@ -186,7 +185,7 @@ void TransferPool::onTransferComplete(TransferPool::Transfer* t) if(r != LIBUSB_SUCCESS) { - std::cerr << "[TransferPool::onTransferComplete] failed to submit transfer: " << libusb_error_name(r) << std::endl; + LOG_ERROR << "failed to submit transfer: " << libusb_error_name(r); t->setStopped(true); } } diff --git a/examples/protonect/src/usb_control.cpp b/examples/protonect/src/usb_control.cpp index 360b96b09..2497e3151 100644 --- a/examples/protonect/src/usb_control.cpp +++ b/examples/protonect/src/usb_control.cpp @@ -187,18 +187,8 @@ UsbControl::~UsbControl() { } -static UsbControl::ResultCode checkLibusbResult(const char* method, int r) -{ - if(r != LIBUSB_SUCCESS) - { - std::cerr << "[UsbControl::" << method << "] failed! libusb error " << r << ": " << libusb_error_name(r) << std::endl; - return UsbControl::Error; - } - else - { - return UsbControl::Success; - } -} +#define CHECK_LIBUSB_RESULT(__CODE, __RESULT) if((__CODE = (__RESULT == LIBUSB_SUCCESS ? Success : Error)) == Error) LOG_ERROR +#define WRITE_LIBUSB_ERROR(__RESULT) "libusb error " << __RESULT << ": " << libusb_error_name(__RESULT) UsbControl::ResultCode UsbControl::setConfiguration() { @@ -208,14 +198,14 @@ UsbControl::ResultCode UsbControl::setConfiguration() int r; r = libusb_get_configuration(handle_, ¤t_config_id); - code = checkLibusbResult("setConfiguration(initial get)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to get configuration! " << WRITE_LIBUSB_ERROR(r); if(code == Success) { if(current_config_id != desired_config_id) { r = libusb_set_configuration(handle_, desired_config_id); - code = checkLibusbResult("setConfiguration(set)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to set configuration! " << WRITE_LIBUSB_ERROR(r); } } @@ -228,12 +218,12 @@ UsbControl::ResultCode UsbControl::claimInterfaces() int r; r = libusb_claim_interface(handle_, ControlAndRgbInterfaceId); - code = checkLibusbResult("claimInterfaces(ControlAndRgbInterfaceId)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to claim interface with ControlAndRgbInterfaceId(="<< ControlAndRgbInterfaceId << ")! " << WRITE_LIBUSB_ERROR(r); if(code == Success) { r = libusb_claim_interface(handle_, IrInterfaceId); - code = checkLibusbResult("claimInterfaces(IrInterfaceId)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to claim interface with IrInterfaceId(="<< IrInterfaceId << ")! " << WRITE_LIBUSB_ERROR(r); } return code; @@ -245,12 +235,12 @@ UsbControl::ResultCode UsbControl::releaseInterfaces() int r; r = libusb_release_interface(handle_, ControlAndRgbInterfaceId); - code = checkLibusbResult("releaseInterfaces(ControlAndRgbInterfaceId)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to release interface with ControlAndRgbInterfaceId(="<< ControlAndRgbInterfaceId << ")! " << WRITE_LIBUSB_ERROR(r); if(code == Success) { r = libusb_release_interface(handle_, IrInterfaceId); - code = checkLibusbResult("releaseInterfaces(IrInterfaceId)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to release interface with IrInterfaceId(="<< IrInterfaceId << ")! " << WRITE_LIBUSB_ERROR(r); } return code; @@ -260,14 +250,18 @@ UsbControl::ResultCode UsbControl::setIsochronousDelay() { int r = libusb_ext::set_isochronous_delay(handle_, timeout_); - return checkLibusbResult("setIsochronousDelay", r); + UsbControl::ResultCode code; + CHECK_LIBUSB_RESULT(code, r) << "failed to set isochronous delay! " << WRITE_LIBUSB_ERROR(r); + return code; } UsbControl::ResultCode UsbControl::setPowerStateLatencies() { int r = libusb_ext::set_sel(handle_, timeout_, 0x55, 0, 0x55, 0); - return checkLibusbResult("setPowerStateLatencies", r); + UsbControl::ResultCode code; + CHECK_LIBUSB_RESULT(code, r) << "failed to set power state latencies! " << WRITE_LIBUSB_ERROR(r); + return code; } UsbControl::ResultCode UsbControl::enablePowerStates() @@ -276,12 +270,12 @@ UsbControl::ResultCode UsbControl::enablePowerStates() int r; r = libusb_ext::set_feature(handle_, timeout_, libusb_ext::U1_ENABLE); - code = checkLibusbResult("enablePowerStates(U1)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to enable power states U1! " << WRITE_LIBUSB_ERROR(r); if(code == Success) { r = libusb_ext::set_feature(handle_, timeout_, libusb_ext::U2_ENABLE); - code = checkLibusbResult("enablePowerStates(U2)", r); + CHECK_LIBUSB_RESULT(code, r) << "failed to enable power states U2! " << WRITE_LIBUSB_ERROR(r); } return code; @@ -292,7 +286,9 @@ UsbControl::ResultCode UsbControl::setVideoTransferFunctionState(UsbControl::Sta bool suspend = state == Enabled ? false : true; int r = libusb_ext::set_feature_function_suspend(handle_, timeout_, suspend, suspend); - return checkLibusbResult("setVideoTransferFunctionState", r); + UsbControl::ResultCode code; + CHECK_LIBUSB_RESULT(code, r) << "failed to set video transfer function state! " << WRITE_LIBUSB_ERROR(r); + return code; } UsbControl::ResultCode UsbControl::setIrInterfaceState(UsbControl::State state) @@ -300,7 +296,9 @@ UsbControl::ResultCode UsbControl::setIrInterfaceState(UsbControl::State state) int alternate_setting = state == Enabled ? 1 : 0; int r = libusb_set_interface_alt_setting(handle_, IrInterfaceId, alternate_setting); - return checkLibusbResult("setIrInterfaceState", r); + UsbControl::ResultCode code; + CHECK_LIBUSB_RESULT(code, r) << "failed to set ir interface state! " << WRITE_LIBUSB_ERROR(r); + return code; } @@ -316,7 +314,9 @@ UsbControl::ResultCode UsbControl::getIrMaxIsoPacketSize(int &size) r = LIBUSB_SUCCESS; } - return checkLibusbResult("getIrMaxIsoPacketSize", r); + UsbControl::ResultCode code; + CHECK_LIBUSB_RESULT(code, r) << "failed to get max iso packet size! " << WRITE_LIBUSB_ERROR(r); + return code; } } /* namespace protocol */ From 40248978873b3796c23a2a968156e33e2adeaf07 Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Thu, 20 Aug 2015 01:53:11 -0400 Subject: [PATCH 04/10] Convert to a global static logger Before this commit, logger pointers get passed around through inheritance and manually constructed dependency assignment lists. The manual management is hard to scale with logging calls which can appear anywhere in the code. This commit implements a single global static logger for all Freenect2 contexts. It still can be replaced by different loggers, but only one at a time. Now it is the responsibility of each logging point to include libfreenect2/logging.h, which is not automatically included. --- examples/protonect/Protonect.cpp | 3 +- .../libfreenect2/depth_packet_stream_parser.h | 3 +- .../include/libfreenect2/libfreenect2.hpp | 6 +- .../protonect/include/libfreenect2/logging.h | 48 ++++------------ .../include/libfreenect2/packet_pipeline.h | 5 +- .../protocol/command_transaction.h | 5 +- .../libfreenect2/protocol/usb_control.h | 3 +- .../libfreenect2/rgb_packet_stream_parser.h | 3 +- .../include/libfreenect2/usb/transfer_pool.h | 3 +- .../protonect/src/command_transaction.cpp | 1 + .../src/depth_packet_stream_parser.cpp | 1 + examples/protonect/src/libfreenect2.cpp | 55 ++----------------- examples/protonect/src/logging.cpp | 39 ++++++++----- examples/protonect/src/packet_pipeline.cpp | 8 --- .../src/rgb_packet_stream_parser.cpp | 1 + examples/protonect/src/transfer_pool.cpp | 1 + examples/protonect/src/usb_control.cpp | 1 + 17 files changed, 57 insertions(+), 129 deletions(-) diff --git a/examples/protonect/Protonect.cpp b/examples/protonect/Protonect.cpp index 1f4627b25..d093069ef 100644 --- a/examples/protonect/Protonect.cpp +++ b/examples/protonect/Protonect.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT #include "viewer.h" #endif @@ -59,7 +60,7 @@ int main(int argc, char *argv[]) libfreenect2::Freenect2 freenect2; // create a console logger with debug level (default is console logger with info level) - freenect2.setLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug)); + libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug)); libfreenect2::Freenect2Device *dev = 0; libfreenect2::PacketPipeline *pipeline = 0; diff --git a/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h b/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h index 8086ae903..7773cadc4 100644 --- a/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h +++ b/examples/protonect/include/libfreenect2/depth_packet_stream_parser.h @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -52,7 +51,7 @@ LIBFREENECT2_PACK(struct LIBFREENECT2_API DepthSubPacketFooter uint32_t fields[32]; }); -class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback, public WithLoggerImpl +class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback { public: DepthPacketStreamParser(); diff --git a/examples/protonect/include/libfreenect2/libfreenect2.hpp b/examples/protonect/include/libfreenect2/libfreenect2.hpp index d5b876b01..38f36728a 100644 --- a/examples/protonect/include/libfreenect2/libfreenect2.hpp +++ b/examples/protonect/include/libfreenect2/libfreenect2.hpp @@ -29,7 +29,6 @@ #include #include -#include namespace libfreenect2 { @@ -96,15 +95,12 @@ class LIBFREENECT2_API Freenect2Device class Freenect2Impl; -class LIBFREENECT2_API Freenect2 : public WithLogger +class LIBFREENECT2_API Freenect2 { public: Freenect2(void *usb_context = 0); virtual ~Freenect2(); - virtual void setLogger(Logger *logger); - virtual Logger *logger(); - int enumerateDevices(); std::string getDeviceSerialNumber(int idx); diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h index f71a386af..5f7a27bfb 100644 --- a/examples/protonect/include/libfreenect2/logging.h +++ b/examples/protonect/include/libfreenect2/logging.h @@ -60,6 +60,10 @@ LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level); LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel(); LIBFREENECT2_API Logger *createNoopLogger(); +//libfreenect2 frees the memory of the logger passed in. +LIBFREENECT2_API Logger *getGlobalLogger(); +LIBFREENECT2_API void setGlobalLogger(Logger *logger); + class LIBFREENECT2_API LogMessage { private: @@ -73,50 +77,22 @@ class LIBFREENECT2_API LogMessage std::ostream &stream(); }; -class LIBFREENECT2_API WithLogger -{ -public: - virtual ~WithLogger(); - virtual void setLogger(Logger *logger) = 0; - virtual Logger *logger() = 0; -}; - -template -void trySetLogger(T *obj, Logger *logger) -{ - WithLogger *obj_with_logger = dynamic_cast(obj); - if(obj_with_logger != 0) - { - obj_with_logger->setLogger(logger); - } -} - -class LIBFREENECT2_API WithLoggerImpl : public WithLogger -{ -protected: - Logger *logger_; - - virtual void onLoggerChanged(Logger *logger); -public: - WithLoggerImpl(); - virtual ~WithLoggerImpl(); - virtual void setLogger(Logger *logger); - virtual Logger *logger(); -}; +std::string getShortName(const char *func); } /* namespace libfreenect2 */ #if defined(__GNUC__) or defined(__clang__) -#define LIBFREENECT2_LOG_SOURCE __PRETTY_FUNCTION__ +#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__PRETTY_FUNCTION__) #elif defined(_MSC_VER) -#define LIBFREENECT2_LOG_SOURCE __FUNCSIG__ +#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__FUNCSIG__) #else #define LIBFREENECT2_LOG_SOURCE "" #endif -#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") -#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") -#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") -#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG(LEVEL) (::libfreenect2::LogMessage(::libfreenect2::getGlobalLogger(), ::libfreenect2::Logger::LEVEL).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG_DEBUG LOG(Debug) +#define LOG_INFO LOG(Info) +#define LOG_WARNING LOG(Warning) +#define LOG_ERROR LOG(Error) #endif /* LOGGING_H_ */ diff --git a/examples/protonect/include/libfreenect2/packet_pipeline.h b/examples/protonect/include/libfreenect2/packet_pipeline.h index 2d6003138..b1bf467c2 100644 --- a/examples/protonect/include/libfreenect2/packet_pipeline.h +++ b/examples/protonect/include/libfreenect2/packet_pipeline.h @@ -28,7 +28,6 @@ #define PACKET_PIPELINE_H_ #include -#include #include #include #include @@ -51,7 +50,7 @@ class LIBFREENECT2_API PacketPipeline virtual DepthPacketProcessor *getDepthPacketProcessor() const = 0; }; -class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline, public WithLoggerImpl +class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline { protected: RgbPacketStreamParser *rgb_parser_; @@ -64,8 +63,6 @@ class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline, public WithLo virtual void initialize(); virtual DepthPacketProcessor *createDepthPacketProcessor() = 0; - - virtual void onLoggerChanged(Logger *logger); public: virtual ~BasePacketPipeline(); diff --git a/examples/protonect/include/libfreenect2/protocol/command_transaction.h b/examples/protonect/include/libfreenect2/protocol/command_transaction.h index 56bd0b38a..6994c9104 100644 --- a/examples/protonect/include/libfreenect2/protocol/command_transaction.h +++ b/examples/protonect/include/libfreenect2/protocol/command_transaction.h @@ -28,7 +28,6 @@ #define COMMAND_TRANSACTION_H_ #include -#include #include namespace libfreenect2 @@ -36,7 +35,7 @@ namespace libfreenect2 namespace protocol { -class CommandTransaction : public WithLoggerImpl +class CommandTransaction { public: static const int ResponseCompleteLength = 16; @@ -64,7 +63,7 @@ class CommandTransaction : public WithLoggerImpl }; CommandTransaction(libusb_device_handle *handle, int inbound_endpoint, int outbound_endpoint); - virtual ~CommandTransaction(); + ~CommandTransaction(); void execute(const CommandBase& command, Result& result); private: diff --git a/examples/protonect/include/libfreenect2/protocol/usb_control.h b/examples/protonect/include/libfreenect2/protocol/usb_control.h index d4ffeb2eb..f42cafed0 100644 --- a/examples/protonect/include/libfreenect2/protocol/usb_control.h +++ b/examples/protonect/include/libfreenect2/protocol/usb_control.h @@ -28,7 +28,6 @@ #define USB_CONTROL_H_ #include -#include namespace libfreenect2 { @@ -62,7 +61,7 @@ namespace protocol * * The 2. interface can be enabled/disabled by changing its alternate setting to 1/0 */ -class UsbControl : public WithLoggerImpl +class UsbControl { public: UsbControl(libusb_device_handle *handle); diff --git a/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h b/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h index 090d7cc1e..ac1cd0caf 100644 --- a/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h +++ b/examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h @@ -30,7 +30,6 @@ #include #include -#include #include #include @@ -39,7 +38,7 @@ namespace libfreenect2 { -class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback, public WithLoggerImpl +class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback { public: RgbPacketStreamParser(); diff --git a/examples/protonect/include/libfreenect2/usb/transfer_pool.h b/examples/protonect/include/libfreenect2/usb/transfer_pool.h index 8d54fbde4..e4091a97a 100644 --- a/examples/protonect/include/libfreenect2/usb/transfer_pool.h +++ b/examples/protonect/include/libfreenect2/usb/transfer_pool.h @@ -30,7 +30,6 @@ #include #include -#include #include #include @@ -40,7 +39,7 @@ namespace libfreenect2 namespace usb { -class TransferPool : public WithLoggerImpl +class TransferPool { public: TransferPool(libusb_device_handle *device_handle, unsigned char device_endpoint); diff --git a/examples/protonect/src/command_transaction.cpp b/examples/protonect/src/command_transaction.cpp index 90e4ac481..716dd32e2 100644 --- a/examples/protonect/src/command_transaction.cpp +++ b/examples/protonect/src/command_transaction.cpp @@ -25,6 +25,7 @@ */ #include +#include #include diff --git a/examples/protonect/src/depth_packet_stream_parser.cpp b/examples/protonect/src/depth_packet_stream_parser.cpp index 29c299f94..a90f1ed70 100644 --- a/examples/protonect/src/depth_packet_stream_parser.cpp +++ b/examples/protonect/src/depth_packet_stream_parser.cpp @@ -25,6 +25,7 @@ */ #include +#include #include namespace libfreenect2 diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index 383c43a39..fb34a726d 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -38,6 +38,7 @@ #include #include #include +#include namespace libfreenect2 { @@ -45,7 +46,7 @@ using namespace libfreenect2; using namespace libfreenect2::usb; using namespace libfreenect2::protocol; -class Freenect2DeviceImpl : public Freenect2Device, public WithLoggerImpl +class Freenect2DeviceImpl : public Freenect2Device { private: enum State @@ -74,8 +75,6 @@ class Freenect2DeviceImpl : public Freenect2Device, public WithLoggerImpl std::string serial_, firmware_; Freenect2Device::IrCameraParams ir_camera_params_; Freenect2Device::ColorCameraParams rgb_camera_params_; -protected: - virtual void onLoggerChanged(Logger *logger); public: Freenect2DeviceImpl(Freenect2Impl *context, const PacketPipeline *pipeline, libusb_device *usb_device, libusb_device_handle *usb_device_handle, const std::string &serial); virtual ~Freenect2DeviceImpl(); @@ -112,7 +111,7 @@ std::ostream &operator<<(std::ostream &out, const PrintBusAndDevice& dev) return out; } -class Freenect2Impl : public WithLoggerImpl +class Freenect2Impl { private: bool managed_usb_context_; @@ -136,8 +135,6 @@ class Freenect2Impl : public WithLoggerImpl usb_context_(reinterpret_cast(usb_context)), has_device_enumeration_(false) { - logger_ = createConsoleLoggerWithDefaultLevel(); - if(managed_usb_context_) { int r = libusb_init(&usb_context_); @@ -151,7 +148,7 @@ class Freenect2Impl : public WithLoggerImpl usb_event_loop_.start(usb_context_); } - virtual ~Freenect2Impl() + ~Freenect2Impl() { clearDevices(); clearDeviceEnumeration(); @@ -164,32 +161,10 @@ class Freenect2Impl : public WithLoggerImpl usb_context_ = 0; } - setLoggerInternal(0); - } - - void setLoggerInternal(Logger *logger) - { - Logger *old_logger = logger_; - WithLoggerImpl::setLogger(logger); - delete old_logger; - } - - virtual void setLogger(Logger *logger) - { - setLoggerInternal(logger != 0 ? logger : createNoopLogger()); - } - - virtual void onLoggerChanged(Logger *logger) - { - for(DeviceVector::iterator it = devices_.begin(); it != devices_.end(); ++it) - { - (*it)->setLogger(logger); - } } void addDevice(Freenect2DeviceImpl *device) { - device->setLogger(logger_); devices_.push_back(device); } @@ -200,7 +175,6 @@ class Freenect2Impl : public WithLoggerImpl if(it != devices_.end()) { devices_.erase(it); - device->setLogger(0); } else { @@ -366,17 +340,6 @@ Freenect2DeviceImpl::~Freenect2DeviceImpl() delete pipeline_; } -void Freenect2DeviceImpl::onLoggerChanged(Logger *logger) -{ - rgb_transfer_pool_.setLogger(logger); - ir_transfer_pool_.setLogger(logger); - usb_control_.setLogger(logger); - command_tx_.setLogger(logger); - - // TODO: is this ok? - trySetLogger(const_cast(pipeline_), logger); -} - int Freenect2DeviceImpl::nextCommandSeq() { return command_seq_++; @@ -686,16 +649,6 @@ Freenect2::~Freenect2() delete impl_; } -void Freenect2::setLogger(Logger *logger) -{ - impl_->setLogger(logger); -} - -Logger *Freenect2::logger() -{ - return impl_->logger(); -} - int Freenect2::enumerateDevices() { impl_->clearDeviceEnumeration(); diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp index ef1ad986e..ffce10d0d 100644 --- a/examples/protonect/src/logging.cpp +++ b/examples/protonect/src/logging.cpp @@ -93,7 +93,7 @@ class ConsoleLogger : public Logger { if(level < level_) return; - (level >= Warning ? std::cerr : std::cout) << "[" << level2str(level) << "]" << message << std::endl; + (level >= Warning ? std::cerr : std::cout) << "[" << level2str(level) << "] " << message << std::endl; } }; @@ -130,9 +130,11 @@ LogMessage::LogMessage(Logger *logger, Logger::Level level) : logger_(logger), l LogMessage::~LogMessage() { - if(logger_ != 0) + if(logger_ != 0 && stream_.good()) { - logger_->log(level_, stream_.str()); + const std::string &message = stream_.str(); + if (message.size()) + logger_->log(level_, message); } } @@ -141,24 +143,35 @@ std::ostream &LogMessage::stream() return stream_; } -WithLogger::~WithLogger() {} +static ConsoleLogger defaultLogger_(Logger::getDefaultLevel()); +static Logger *userLogger_ = &defaultLogger_; -WithLoggerImpl::WithLoggerImpl() : logger_(0) +Logger *getGlobalLogger() { + return userLogger_; } -WithLoggerImpl::~WithLoggerImpl() {} -void WithLoggerImpl::onLoggerChanged(Logger *logger) {} - -void WithLoggerImpl::setLogger(Logger *logger) +void setGlobalLogger(Logger *logger) { - logger_ = logger; - onLoggerChanged(logger_); + if (userLogger_ != &defaultLogger_) + delete userLogger_; + userLogger_ = logger; } -Logger *WithLoggerImpl::logger() +std::string getShortName(const char *func) { - return logger_; + std::string src(func); + size_t end = src.rfind('('); + if (end == std::string::npos) + end = src.size(); + size_t begin = 1 + src.rfind(' ', end); + size_t first_ns = src.find("::", begin); + if (first_ns != std::string::npos) + begin = first_ns + 2; + size_t last_ns = src.rfind("::", end); + if (last_ns != std::string::npos) + end = last_ns; + return src.substr(begin, end - begin); } } /* namespace libfreenect2 */ diff --git a/examples/protonect/src/packet_pipeline.cpp b/examples/protonect/src/packet_pipeline.cpp index 9642291f9..1c66f66b6 100644 --- a/examples/protonect/src/packet_pipeline.cpp +++ b/examples/protonect/src/packet_pipeline.cpp @@ -49,14 +49,6 @@ void BasePacketPipeline::initialize() depth_parser_->setPacketProcessor(async_depth_processor_); } -void BasePacketPipeline::onLoggerChanged(Logger *logger) -{ - rgb_parser_->setLogger(logger); - depth_parser_->setLogger(logger); - trySetLogger(rgb_processor_, logger); - trySetLogger(depth_processor_, logger); -} - BasePacketPipeline::~BasePacketPipeline() { delete async_rgb_processor_; diff --git a/examples/protonect/src/rgb_packet_stream_parser.cpp b/examples/protonect/src/rgb_packet_stream_parser.cpp index c9805a7c1..199514f09 100644 --- a/examples/protonect/src/rgb_packet_stream_parser.cpp +++ b/examples/protonect/src/rgb_packet_stream_parser.cpp @@ -26,6 +26,7 @@ #include #include +#include #include namespace libfreenect2 diff --git a/examples/protonect/src/transfer_pool.cpp b/examples/protonect/src/transfer_pool.cpp index b45087db1..dfe70921e 100644 --- a/examples/protonect/src/transfer_pool.cpp +++ b/examples/protonect/src/transfer_pool.cpp @@ -25,6 +25,7 @@ */ #include +#include namespace libfreenect2 { diff --git a/examples/protonect/src/usb_control.cpp b/examples/protonect/src/usb_control.cpp index 2497e3151..f410fdbc8 100644 --- a/examples/protonect/src/usb_control.cpp +++ b/examples/protonect/src/usb_control.cpp @@ -25,6 +25,7 @@ */ #include +#include #include #include From e30d4a2685d03630f5ef7169f952aa3d5639a1e9 Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Tue, 25 Aug 2015 15:46:30 +0000 Subject: [PATCH 05/10] Separate internal logging.h and API logger.h Also add a "None" logging level Thus remove NoopLogger, and sort logging levels by verbosity. --- .../protonect/include/libfreenect2/logger.h | 68 +++++++++++++++++++ .../protonect/include/libfreenect2/logging.h | 40 ++--------- examples/protonect/src/logging.cpp | 20 +----- 3 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 examples/protonect/include/libfreenect2/logger.h diff --git a/examples/protonect/include/libfreenect2/logger.h b/examples/protonect/include/libfreenect2/logger.h new file mode 100644 index 000000000..6e9f00009 --- /dev/null +++ b/examples/protonect/include/libfreenect2/logger.h @@ -0,0 +1,68 @@ +/* + * This file is part of the OpenKinect Project. http://www.openkinect.org + * + * Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file + * for details. + * + * This code is licensed to you under the terms of the Apache License, version + * 2.0, or, at your option, the terms of the GNU General Public License, + * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, + * or the following URLs: + * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.gnu.org/licenses/gpl-2.0.txt + * + * If you redistribute this file in source form, modified or unmodified, you + * may: + * 1) Leave this header intact and distribute it under the same terms, + * accompanying it with the APACHE20 and GPL20 files, or + * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or + * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file + * In all cases you must keep the copyright notice intact and include a copy + * of the CONTRIB file. + * + * Binary distributions must follow the binary distribution requirements of + * either License. + */ + +#ifndef LIBFREENECT2_LOGGER_H_ +#define LIBFREENECT2_LOGGER_H_ + +#include +#include + +#include + +namespace libfreenect2 +{ + +class LIBFREENECT2_API Logger +{ +public: + enum Level + { + None = 0, + Error = 1, + Warning = 2, + Info = 3, + Debug = 4, + }; + static Level getDefaultLevel(); + + virtual ~Logger(); + + virtual Level level() const; + + virtual void log(Level level, const std::string &message) = 0; +protected: + Level level_; +}; + +LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level); +LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel(); + +//libfreenect2 frees the memory of the logger passed in. +LIBFREENECT2_API Logger *getGlobalLogger(); +LIBFREENECT2_API void setGlobalLogger(Logger *logger); + +} /* namespace libfreenect2 */ +#endif /* LIBFREENECT2_LOGGER_H_ */ diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h index 5f7a27bfb..04bbc89a9 100644 --- a/examples/protonect/include/libfreenect2/logging.h +++ b/examples/protonect/include/libfreenect2/logging.h @@ -31,40 +31,12 @@ #include #include +#include namespace libfreenect2 { -class LIBFREENECT2_API Logger -{ -public: - enum Level - { - Debug = 1, - Info = 2, - Warning = 3, - Error = 4, - }; - static Level getDefaultLevel(); - - virtual ~Logger(); - - virtual Level level() const; - - virtual void log(Level level, const std::string &message) = 0; -protected: - Level level_; -}; - -LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level); -LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel(); -LIBFREENECT2_API Logger *createNoopLogger(); - -//libfreenect2 frees the memory of the logger passed in. -LIBFREENECT2_API Logger *getGlobalLogger(); -LIBFREENECT2_API void setGlobalLogger(Logger *logger); - -class LIBFREENECT2_API LogMessage +class LogMessage { private: Logger *logger_; @@ -82,14 +54,14 @@ std::string getShortName(const char *func); } /* namespace libfreenect2 */ #if defined(__GNUC__) or defined(__clang__) -#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__PRETTY_FUNCTION__) +#define LOG_SOURCE ::libfreenect2::getShortName(__PRETTY_FUNCTION__) #elif defined(_MSC_VER) -#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__FUNCSIG__) +#define LOG_SOURCE ::libfreenect2::getShortName(__FUNCSIG__) #else -#define LIBFREENECT2_LOG_SOURCE "" +#define LOG_SOURCE "" #endif -#define LOG(LEVEL) (::libfreenect2::LogMessage(::libfreenect2::getGlobalLogger(), ::libfreenect2::Logger::LEVEL).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ") +#define LOG(LEVEL) (::libfreenect2::LogMessage(::libfreenect2::getGlobalLogger(), ::libfreenect2::Logger::LEVEL).stream() << "[" << LOG_SOURCE << "] ") #define LOG_DEBUG LOG(Debug) #define LOG_INFO LOG(Info) #define LOG_WARNING LOG(Warning) diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp index ffce10d0d..8c2357f82 100644 --- a/examples/protonect/src/logging.cpp +++ b/examples/protonect/src/logging.cpp @@ -54,6 +54,8 @@ Logger::Level Logger::getDefaultLevel() l = Logger::Warning; else if(env_logger_level_str == "error") l = Logger::Error; + else if(env_logger_level_str == "none") + l = Logger::None; } return l; @@ -91,23 +93,12 @@ class ConsoleLogger : public Logger virtual ~ConsoleLogger() {} virtual void log(Level level, const std::string &message) { - if(level < level_) return; + if(level > level_) return; (level >= Warning ? std::cerr : std::cout) << "[" << level2str(level) << "] " << message << std::endl; } }; -class NoopLogger : public Logger -{ -public: - NoopLogger() - { - level_ = Debug; - } - virtual ~NoopLogger() {} - virtual void log(Level level, const std::string &message) {} -}; - Logger *createConsoleLogger(Logger::Level level) { return new ConsoleLogger(level); @@ -118,11 +109,6 @@ Logger *createConsoleLoggerWithDefaultLevel() return new ConsoleLogger(Logger::getDefaultLevel()); } -Logger *createNoopLogger() -{ - return new NoopLogger(); -} - LogMessage::LogMessage(Logger *logger, Logger::Level level) : logger_(logger), level_(level) { From ef7fd4eeec4318bd1bf238257d6683e23b156ecb Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Thu, 20 Aug 2015 21:20:10 -0400 Subject: [PATCH 06/10] Use LOG_* macros in remaining classes --- .../src/cpu_depth_packet_processor.cpp | 18 +++---- examples/protonect/src/libfreenect2.cpp | 17 +++---- .../src/opencl_depth_packet_processor.cpp | 49 +++++++++---------- .../src/opengl_depth_packet_processor.cpp | 35 ++++++------- examples/protonect/src/resource.cpp | 6 +-- .../src/turbo_jpeg_rgb_packet_processor.cpp | 10 ++-- examples/protonect/src/usb_control.cpp | 1 - examples/protonect/viewer.h | 14 ++---- 8 files changed, 68 insertions(+), 82 deletions(-) diff --git a/examples/protonect/src/cpu_depth_packet_processor.cpp b/examples/protonect/src/cpu_depth_packet_processor.cpp index f0517f7bf..86d2e0303 100644 --- a/examples/protonect/src/cpu_depth_packet_processor.cpp +++ b/examples/protonect/src/cpu_depth_packet_processor.cpp @@ -27,8 +27,8 @@ #include #include #include +#include -#include #include #include @@ -253,7 +253,7 @@ class CpuDepthPacketProcessorImpl if(timing_acc_n >= 100.0) { double avg = (timing_acc / timing_acc_n); - std::cout << "[CpuDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz" << std::endl; + LOG_INFO << "[CpuDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; timing_acc = 0.0; timing_acc_n = 0.0; } @@ -774,7 +774,7 @@ void CpuDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char* buf if(buffer_length < sizeof(libfreenect2::protocol::P0TablesResponse)) { - std::cerr << "[CpuDepthPacketProcessor::loadP0TablesFromCommandResponse] P0Table response too short!" << std::endl; + LOG_ERROR << "P0Table response too short!"; return; } @@ -801,19 +801,19 @@ void CpuDepthPacketProcessor::loadP0TablesFromFiles(const char* p0_filename, con Mat p0_table0(424, 512); if(!loadBufferFromFile2(p0_filename, p0_table0.buffer(), p0_table0.sizeInBytes())) { - std::cerr << "[CpuDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 0 from '" << p0_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 0 from '" << p0_filename << "' failed!"; } Mat p0_table1(424, 512); if(!loadBufferFromFile2(p1_filename, p0_table1.buffer(), p0_table1.sizeInBytes())) { - std::cerr << "[CpuDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 1 from '" << p1_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 1 from '" << p1_filename << "' failed!"; } Mat p0_table2(424, 512); if(!loadBufferFromFile2(p2_filename, p0_table2.buffer(), p0_table2.sizeInBytes())) { - std::cerr << "[CpuDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 2 from '" << p2_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 2 from '" << p2_filename << "' failed!"; } if(impl_->flip_ptables) @@ -846,7 +846,7 @@ void CpuDepthPacketProcessor::loadXTableFromFile(const char* filename) } else { - std::cerr << "[CpuDepthPacketProcessor::loadXTableFromFile] Loading xtable from resource 'xTable.bin' failed!" << std::endl; + LOG_ERROR << "Loading xtable from resource 'xTable.bin' failed!"; } } @@ -863,7 +863,7 @@ void CpuDepthPacketProcessor::loadZTableFromFile(const char* filename) } else { - std::cerr << "[CpuDepthPacketProcessor::loadZTableFromFile] Loading ztable from resource 'zTable.bin' failed!" << std::endl; + LOG_ERROR << "Loading ztable from resource 'zTable.bin' failed!"; } } @@ -878,7 +878,7 @@ void CpuDepthPacketProcessor::load11To16LutFromFile(const char* filename) } else { - std::cerr << "[CpuDepthPacketProcessor::load11To16LutFromFile] Loading 11to16 lut from resource '11to16.bin' failed!" << std::endl; + LOG_ERROR << "Loading 11to16 lut from resource '11to16.bin' failed!"; } } diff --git a/examples/protonect/src/libfreenect2.cpp b/examples/protonect/src/libfreenect2.cpp index fb34a726d..88d52b225 100644 --- a/examples/protonect/src/libfreenect2.cpp +++ b/examples/protonect/src/libfreenect2.cpp @@ -24,7 +24,7 @@ * either License. */ -#include +#include #include #include #include @@ -446,9 +446,8 @@ void Freenect2DeviceImpl::start() firmware_ = FirmwareVersionResponse(firmware_result.data, firmware_result.length).toString(); command_tx_.execute(ReadData0x14Command(nextCommandSeq()), result); - LOG_DEBUG - << "ReadData0x14 response" << std::endl - << GenericResponse(result.data, result.length).toString(); + LOG_DEBUG << "ReadData0x14 response"; + LOG_DEBUG << GenericResponse(result.data, result.length).toString(); command_tx_.execute(ReadSerialNumberCommand(nextCommandSeq()), serial_result); std::string new_serial = SerialNumberResponse(serial_result.data, serial_result.length).toString(); @@ -509,18 +508,16 @@ void Freenect2DeviceImpl::start() rgb_camera_params_.my_x0y0 = rgb_p->my_x0y0; // 1 command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); - LOG_DEBUG - << "ReadStatus0x090000 response" << std::endl - << GenericResponse(result.data, result.length).toString(); + LOG_DEBUG << "ReadStatus0x090000 response"; + LOG_DEBUG << GenericResponse(result.data, result.length).toString(); command_tx_.execute(InitStreamsCommand(nextCommandSeq()), result); usb_control_.setIrInterfaceState(UsbControl::Enabled); command_tx_.execute(ReadStatus0x090000Command(nextCommandSeq()), result); - LOG_DEBUG - << "ReadStatus0x090000 response" << std::endl - << GenericResponse(result.data, result.length).toString(); + LOG_DEBUG << "ReadStatus0x090000 response"; + LOG_DEBUG << GenericResponse(result.data, result.length).toString(); command_tx_.execute(SetStreamEnabledCommand(nextCommandSeq()), result); diff --git a/examples/protonect/src/opencl_depth_packet_processor.cpp b/examples/protonect/src/opencl_depth_packet_processor.cpp index 822bfb06f..caf52dace 100644 --- a/examples/protonect/src/opencl_depth_packet_processor.cpp +++ b/examples/protonect/src/opencl_depth_packet_processor.cpp @@ -27,9 +27,8 @@ #include #include #include +#include -#include -#include #include #define _USE_MATH_DEFINES @@ -49,8 +48,6 @@ #define REG_OPENCL_FILE "" #endif -#define OUT_NAME(FUNCTION) "[OpenCLDepthPacketProcessor::" FUNCTION "] " - namespace libfreenect2 { @@ -61,7 +58,7 @@ std::string loadCLSource(const std::string &filename) if(!loadResource(filename, &data, &length)) { - std::cerr << OUT_NAME("loadCLSource") "failed to load cl source!" << std::endl; + LOG_ERROR << "failed to load cl source!"; return ""; } @@ -226,7 +223,7 @@ class OpenCLDepthPacketProcessorImpl void listDevice(std::vector &devices) { - std::cout << OUT_NAME("listDevice") " devices:" << std::endl; + LOG_INFO << " devices:"; for(size_t i = 0; i < devices.size(); ++i) { cl::Device &dev = devices[i]; @@ -254,7 +251,7 @@ class OpenCLDepthPacketProcessorImpl devType = "UNKNOWN"; } - std::cout << " " << i << ": " << devName << " (" << devType << ")[" << devVendor << ']' << std::endl; + LOG_INFO << " " << i << ": " << devName << " (" << devType << ")[" << devVendor << ']'; } } @@ -298,12 +295,12 @@ class OpenCLDepthPacketProcessorImpl std::vector platforms; if(cl::Platform::get(&platforms) != CL_SUCCESS) { - std::cerr << OUT_NAME("init") "error while getting opencl platforms." << std::endl; + LOG_ERROR << "error while getting opencl platforms."; return false; } if(platforms.empty()) { - std::cerr << OUT_NAME("init") "no opencl platforms found." << std::endl; + LOG_ERROR << "no opencl platforms found."; return false; } @@ -335,11 +332,11 @@ class OpenCLDepthPacketProcessorImpl default: devType = "UNKNOWN"; } - std::cout << OUT_NAME("init") " selected device: " << devName << " (" << devType << ")[" << devVendor << ']' << std::endl; + LOG_INFO << "selected device: " << devName << " (" << devType << ")[" << devVendor << ']'; } else { - std::cerr << OUT_NAME("init") "could not find any suitable device" << std::endl; + LOG_ERROR << "could not find any suitable device"; return false; } @@ -347,7 +344,7 @@ class OpenCLDepthPacketProcessorImpl } catch(const cl::Error &err) { - std::cerr << OUT_NAME("init") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl; + LOG_ERROR << err.what() << "(" << err.err() << ")"; throw; } @@ -451,7 +448,7 @@ class OpenCLDepthPacketProcessorImpl } catch(const cl::Error &err) { - std::cerr << OUT_NAME("init") "ERROR: " << err.what() << "(" << err.err() << ")" << std::endl; + LOG_ERROR << err.what() << "(" << err.err() << ")"; throw; } programInitialized = true; @@ -496,7 +493,7 @@ class OpenCLDepthPacketProcessorImpl } catch(const cl::Error &err) { - std::cerr << OUT_NAME("run") "ERROR: " << err.what() << " (" << err.err() << ")" << std::endl; + LOG_ERROR << err.what() << " (" << err.err() << ")"; throw; } } @@ -511,7 +508,7 @@ class OpenCLDepthPacketProcessorImpl { try { - std::cout<< OUT_NAME("buildProgram") "building OpenCL program..." <(device) << std::endl; - std::cout << OUT_NAME("buildProgram") "Build Options:\t" << program.getBuildInfo(device) << std::endl; - std::cout << OUT_NAME("buildProgram") "Build Log:\t " << program.getBuildInfo(device) << std::endl; + LOG_ERROR << "Build Status: " << program.getBuildInfo(device); + LOG_ERROR << "Build Options:\t" << program.getBuildInfo(device); + LOG_ERROR << "Build Log:\t " << program.getBuildInfo(device); } programBuilt = false; @@ -552,7 +549,7 @@ class OpenCLDepthPacketProcessorImpl if(timing_acc_n >= 100.0) { double avg = (timing_acc / timing_acc_n); - std::cout << "[OpenCLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0 / avg) << "Hz" << std::endl; + LOG_INFO << "[OpenCLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0 / avg) << "Hz"; timing_acc = 0.0; timing_acc_n = 0.0; } @@ -626,7 +623,7 @@ void OpenCLDepthPacketProcessor::loadP0TablesFromCommandResponse(unsigned char * if(buffer_length < sizeof(libfreenect2::protocol::P0TablesResponse)) { - std::cerr << OUT_NAME("loadP0TablesFromCommandResponse") "P0Table response too short!" << std::endl; + LOG_ERROR << "P0Table response too short!"; return; } @@ -637,7 +634,7 @@ void OpenCLDepthPacketProcessor::loadXTableFromFile(const char *filename) { if(!loadBufferFromResources(filename, (unsigned char *)impl_->x_table, impl_->image_size * sizeof(float))) { - std::cerr << OUT_NAME("loadXTableFromFile") "could not load x table from: " << filename << std::endl; + LOG_ERROR << "could not load x table from: " << filename; } } @@ -645,7 +642,7 @@ void OpenCLDepthPacketProcessor::loadZTableFromFile(const char *filename) { if(!loadBufferFromResources(filename, (unsigned char *)impl_->z_table, impl_->image_size * sizeof(float))) { - std::cerr << OUT_NAME("loadZTableFromFile") "could not load z table from: " << filename << std::endl; + LOG_ERROR << "could not load z table from: " << filename; } } @@ -653,7 +650,7 @@ void OpenCLDepthPacketProcessor::load11To16LutFromFile(const char *filename) { if(!loadBufferFromResources(filename, (unsigned char *)impl_->lut11to16, 2048 * sizeof(cl_ushort))) { - std::cerr << OUT_NAME("load11To16LutFromFile") "could not load lut table from: " << filename << std::endl; + LOG_ERROR << "could not load lut table from: " << filename; } } @@ -663,7 +660,7 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet) if(!impl_->programInitialized && !impl_->initProgram()) { - std::cerr << OUT_NAME("process") "could not initialize OpenCLDepthPacketProcessor" << std::endl; + LOG_ERROR << "could not initialize OpenCLDepthPacketProcessor"; return; } diff --git a/examples/protonect/src/opengl_depth_packet_processor.cpp b/examples/protonect/src/opengl_depth_packet_processor.cpp index e069ce8bf..446d4f0b3 100644 --- a/examples/protonect/src/opengl_depth_packet_processor.cpp +++ b/examples/protonect/src/opengl_depth_packet_processor.cpp @@ -27,10 +27,10 @@ #include #include #include +#include #include "flextGL.h" #include -#include #include @@ -55,7 +55,7 @@ ChangeCurrentOpenGLContext::ChangeCurrentOpenGLContext(GLFWwindow *new_context) ChangeCurrentOpenGLContext::~ChangeCurrentOpenGLContext() { - //std::cerr << "[ChangeCurrentOpenGLContext] restoring context!" << std::endl; + //LOG_INFO << "restoring context!"; if(last_ctx != 0) { glfwMakeContextCurrent(last_ctx); @@ -95,7 +95,7 @@ std::string loadShaderSource(const std::string& filename) if(!loadResource(filename, &data, &length)) { - std::cerr << "[loadShaderSource] failed to load shader source!" << std::endl; + LOG_ERROR << "failed to load shader source!"; return ""; } @@ -155,8 +155,7 @@ struct ShaderProgram : public WithOpenGLBindings { gl()->glGetShaderInfoLog(vertex_shader, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to compile vertex shader!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to compile vertex shader!" << std::endl << error_buffer; } gl()->glCompileShader(fragment_shader); @@ -166,8 +165,7 @@ struct ShaderProgram : public WithOpenGLBindings { gl()->glGetShaderInfoLog(fragment_shader, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to compile fragment shader!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to compile fragment shader!" << std::endl << error_buffer; } program = gl()->glCreateProgram(); @@ -181,8 +179,7 @@ struct ShaderProgram : public WithOpenGLBindings if(status != GL_TRUE) { gl()->glGetProgramInfoLog(program, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to link shader program!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to link shader program!" << std::endl << error_buffer; } } @@ -457,7 +454,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings if(timing_acc_n >= 100.0) { double avg = (timing_acc / timing_acc_n); - std::cout << "[OpenGLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz" << std::endl; + LOG_INFO << "[OpenGLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; timing_acc = 0.0; timing_acc_n = 0.0; } @@ -470,7 +467,7 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings OpenGLBindings *b = new OpenGLBindings(); if (flextInit(opengl_context_ptr, b) == 0) { - std::cerr << "[OpenGLDepthPacketProcessor] Failed to initialize flextGL."; + LOG_ERROR << "Failed to initialize flextGL."; exit(-1); } gl(b); @@ -773,7 +770,7 @@ OpenGLDepthPacketProcessor::OpenGLDepthPacketProcessor(void *parent_opengl_conte // init glfw - if already initialized nothing happens if (glfwInit() == GL_FALSE) { - std::cerr << "[OpenGLDepthPacketProcessor] Failed to initialize GLFW."; + LOG_ERROR << "Failed to initialize GLFW."; exit(-1); } @@ -791,7 +788,7 @@ OpenGLDepthPacketProcessor::OpenGLDepthPacketProcessor(void *parent_opengl_conte if (window == NULL) { - std::cerr << "[OpenGLDepthPacketProcessor] Failed to create opengl window."; + LOG_ERROR << "Failed to create opengl window."; exit(-1); } @@ -851,7 +848,7 @@ void OpenGLDepthPacketProcessor::loadP0TablesFromFiles(const char* p0_filename, } else { - std::cerr << "[OpenGLDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 0 from '" << p0_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 0 from '" << p0_filename << "' failed!"; } impl_->p0table[1].allocate(512, 424); @@ -861,7 +858,7 @@ void OpenGLDepthPacketProcessor::loadP0TablesFromFiles(const char* p0_filename, } else { - std::cerr << "[OpenGLDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 1 from '" << p1_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 1 from '" << p1_filename << "' failed!"; } impl_->p0table[2].allocate(512, 424); @@ -871,7 +868,7 @@ void OpenGLDepthPacketProcessor::loadP0TablesFromFiles(const char* p0_filename, } else { - std::cerr << "[OpenGLDepthPacketProcessor::loadP0TablesFromFiles] Loading p0table 2 from '" << p2_filename << "' failed!" << std::endl; + LOG_ERROR << "Loading p0table 2 from '" << p2_filename << "' failed!"; } } @@ -890,7 +887,7 @@ void OpenGLDepthPacketProcessor::loadXTableFromFile(const char* filename) } else { - std::cerr << "[OpenGLDepthPacketProcessor::loadXTableFromFile] Loading xtable from resource 'xTable.bin' failed!" << std::endl; + LOG_ERROR << "Loading xtable from resource 'xTable.bin' failed!"; } } @@ -910,7 +907,7 @@ void OpenGLDepthPacketProcessor::loadZTableFromFile(const char* filename) } else { - std::cerr << "[OpenGLDepthPacketProcessor::loadZTableFromFile] Loading ztable from resource 'zTable.bin' failed!" << std::endl; + LOG_ERROR << "Loading ztable from resource 'zTable.bin' failed!"; } } @@ -930,7 +927,7 @@ void OpenGLDepthPacketProcessor::load11To16LutFromFile(const char* filename) } else { - std::cerr << "[OpenGLDepthPacketProcessor::load11To16LutFromFile] Loading 11to16 lut from resource '11to16.bin' failed!" << std::endl; + LOG_ERROR << "Loading 11to16 lut from resource '11to16.bin' failed!"; } } diff --git a/examples/protonect/src/resource.cpp b/examples/protonect/src/resource.cpp index 1c3d24caf..b78d78d0d 100644 --- a/examples/protonect/src/resource.cpp +++ b/examples/protonect/src/resource.cpp @@ -25,9 +25,9 @@ */ #include +#include #include #include -#include namespace libfreenect2 { @@ -69,13 +69,13 @@ bool loadBufferFromResources(const std::string &filename, unsigned char *buffer, if(!loadResource(filename, &data, &length)) { - std::cerr << "loadBufferFromResources: failed to load resource: " << filename << std::endl; + LOG_ERROR << "failed to load resource: " << filename; return false; } if(length != n) { - std::cerr << "loadBufferFromResources: wrong size of resource: " << filename << std::endl; + LOG_ERROR << "wrong size of resource: " << filename; return false; } diff --git a/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp b/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp index b35037533..2e83e6f52 100644 --- a/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp +++ b/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp @@ -25,8 +25,8 @@ */ #include +#include #include -#include namespace libfreenect2 { @@ -49,7 +49,7 @@ class TurboJpegRgbPacketProcessorImpl decompressor = tjInitDecompress(); if(decompressor == 0) { - std::cerr << "[TurboJpegRgbPacketProcessorImpl] Failed to initialize TurboJPEG decompressor! TurboJPEG error: '" << tjGetErrorStr() << "'" << std::endl; + LOG_ERROR << "Failed to initialize TurboJPEG decompressor! TurboJPEG error: '" << tjGetErrorStr() << "'"; } newFrame(); @@ -64,7 +64,7 @@ class TurboJpegRgbPacketProcessorImpl { if(tjDestroy(decompressor) == -1) { - std::cerr << "[~TurboJpegRgbPacketProcessorImpl] Failed to destroy TurboJPEG decompressor! TurboJPEG error: '" << tjGetErrorStr() << "'" << std::endl; + LOG_ERROR << "Failed to destroy TurboJPEG decompressor! TurboJPEG error: '" << tjGetErrorStr() << "'"; } } } @@ -87,7 +87,7 @@ class TurboJpegRgbPacketProcessorImpl if(timing_acc_n >= 100.0) { double avg = (timing_acc / timing_acc_n); - std::cout << "[TurboJpegRgbPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz" << std::endl; + LOG_INFO << "[TurboJpegRgbPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; timing_acc = 0.0; timing_acc_n = 0.0; } @@ -124,7 +124,7 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet) } else { - std::cerr << "[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'" << std::endl; + LOG_ERROR << "[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'"; } impl_->stopTiming(); diff --git a/examples/protonect/src/usb_control.cpp b/examples/protonect/src/usb_control.cpp index f410fdbc8..00e3e5da6 100644 --- a/examples/protonect/src/usb_control.cpp +++ b/examples/protonect/src/usb_control.cpp @@ -28,7 +28,6 @@ #include #include -#include namespace libfreenect2 { diff --git a/examples/protonect/viewer.h b/examples/protonect/viewer.h index 306ee6a36..e85e4cc6f 100644 --- a/examples/protonect/viewer.h +++ b/examples/protonect/viewer.h @@ -1,10 +1,9 @@ #ifndef VIEWER_H #define VIEWER_H -#include - #include #include +#include #include #include @@ -190,8 +189,7 @@ struct ShaderProgram : public WithOpenGLBindings { gl()->glGetShaderInfoLog(vertex_shader, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to compile vertex shader!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to compile vertex shader!" << std::endl << error_buffer; } gl()->glCompileShader(fragment_shader); @@ -201,8 +199,7 @@ struct ShaderProgram : public WithOpenGLBindings { gl()->glGetShaderInfoLog(fragment_shader, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to compile fragment shader!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to compile fragment shader!" << std::endl << error_buffer; } program = gl()->glCreateProgram(); @@ -216,8 +213,7 @@ struct ShaderProgram : public WithOpenGLBindings if (status != GL_TRUE) { gl()->glGetProgramInfoLog(program, sizeof(error_buffer), NULL, error_buffer); - std::cerr << "[ShaderProgram::build] failed to link shader program!" << std::endl; - std::cerr << error_buffer << std::endl; + LOG_ERROR << "failed to link shader program!" << std::endl << error_buffer; } } @@ -285,4 +281,4 @@ class Viewer : WithOpenGLBindings { static void key_callbackstatic(GLFWwindow* window, int key, int scancode, int action, int mods); }; -#endif \ No newline at end of file +#endif From d7f6688ca1a9218dab0636c30a0f873a41ed21bc Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Thu, 20 Aug 2015 21:33:36 -0400 Subject: [PATCH 07/10] Move timing code into logging system Also implement a WithPerfLogging class based on timing code to remove duplicate timing code in several processors. --- examples/protonect/CMakeLists.txt | 1 - .../protonect/include/libfreenect2/logging.h | 13 +++ .../include/libfreenect2/packet_processor.h | 2 - .../protonect/include/libfreenect2/timer.h | 51 --------- .../src/cpu_depth_packet_processor.cpp | 31 +----- examples/protonect/src/logging.cpp | 100 ++++++++++++++++++ .../src/opencl_depth_packet_processor.cpp | 30 +----- .../src/opengl_depth_packet_processor.cpp | 30 +----- examples/protonect/src/timer.cpp | 93 ---------------- .../src/turbo_jpeg_rgb_packet_processor.cpp | 33 +----- 10 files changed, 122 insertions(+), 262 deletions(-) delete mode 100644 examples/protonect/include/libfreenect2/timer.h delete mode 100644 examples/protonect/src/timer.cpp diff --git a/examples/protonect/CMakeLists.txt b/examples/protonect/CMakeLists.txt index d1a6d69d1..11ab5b394 100644 --- a/examples/protonect/CMakeLists.txt +++ b/examples/protonect/CMakeLists.txt @@ -101,7 +101,6 @@ SET(SOURCES src/resource.cpp src/command_transaction.cpp src/registration.cpp - src/timer.cpp src/logging.cpp src/libfreenect2.cpp diff --git a/examples/protonect/include/libfreenect2/logging.h b/examples/protonect/include/libfreenect2/logging.h index 04bbc89a9..fe362b772 100644 --- a/examples/protonect/include/libfreenect2/logging.h +++ b/examples/protonect/include/libfreenect2/logging.h @@ -36,6 +36,19 @@ namespace libfreenect2 { +class WithPerfLoggingImpl; + +class WithPerfLogging +{ +public: + WithPerfLogging(); + virtual ~WithPerfLogging(); + void startTiming(); + std::ostream &stopTiming(std::ostream &stream); +private: + WithPerfLoggingImpl *impl_; +}; + class LogMessage { private: diff --git a/examples/protonect/include/libfreenect2/packet_processor.h b/examples/protonect/include/libfreenect2/packet_processor.h index 9d151cf2c..ea21b9d8a 100644 --- a/examples/protonect/include/libfreenect2/packet_processor.h +++ b/examples/protonect/include/libfreenect2/packet_processor.h @@ -27,8 +27,6 @@ #ifndef PACKET_PROCESSOR_H_ #define PACKET_PROCESSOR_H_ -#include - namespace libfreenect2 { diff --git a/examples/protonect/include/libfreenect2/timer.h b/examples/protonect/include/libfreenect2/timer.h deleted file mode 100644 index 00ce33d6f..000000000 --- a/examples/protonect/include/libfreenect2/timer.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * This file is part of the OpenKinect Project. http://www.openkinect.org - * - * Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file - * for details. - * - * This code is licensed to you under the terms of the Apache License, version - * 2.0, or, at your option, the terms of the GNU General Public License, - * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, - * or the following URLs: - * http://www.apache.org/licenses/LICENSE-2.0 - * http://www.gnu.org/licenses/gpl-2.0.txt - * - * If you redistribute this file in source form, modified or unmodified, you - * may: - * 1) Leave this header intact and distribute it under the same terms, - * accompanying it with the APACHE20 and GPL20 files, or - * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or - * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file - * In all cases you must keep the copyright notice intact and include a copy - * of the CONTRIB file. - * - * Binary distributions must follow the binary distribution requirements of - * either License. - */ - -#ifndef TIMER_H_ -#define TIMER_H_ - -#include - -namespace libfreenect2 -{ - -class TimerImpl; - -class Timer { - public: - Timer(); - virtual ~Timer(); - - void start(); - double stop(); - - private: - TimerImpl *impl_; -}; - -} /* namespace libfreenect2 */ - -#endif /* TIMER_H_ */ diff --git a/examples/protonect/src/cpu_depth_packet_processor.cpp b/examples/protonect/src/cpu_depth_packet_processor.cpp index 86d2e0303..56e55cc9a 100644 --- a/examples/protonect/src/cpu_depth_packet_processor.cpp +++ b/examples/protonect/src/cpu_depth_packet_processor.cpp @@ -202,7 +202,7 @@ inline int bfi(int width, int offset, int src2, int src3) return ((src2 << offset) & bitmask) | (src3 & ~bitmask); } -class CpuDepthPacketProcessorImpl +class CpuDepthPacketProcessorImpl: public WithPerfLogging { public: Mat p0_table0, p0_table1, p0_table2; @@ -214,11 +214,6 @@ class CpuDepthPacketProcessorImpl float trig_table1[512*424][6]; float trig_table2[512*424][6]; - double timing_acc; - double timing_acc_n; - - Timer timer; - bool enable_bilateral_filter, enable_edge_filter; DepthPacketProcessor::Parameters params; @@ -231,34 +226,12 @@ class CpuDepthPacketProcessorImpl newIrFrame(); newDepthFrame(); - timing_acc = 0.0; - timing_acc_n = 0.0; - enable_bilateral_filter = true; enable_edge_filter = true; flip_ptables = true; } - void startTiming() - { - timer.start(); - } - - void stopTiming() - { - timing_acc += timer.stop(); - timing_acc_n += 1.0; - - if(timing_acc_n >= 100.0) - { - double avg = (timing_acc / timing_acc_n); - LOG_INFO << "[CpuDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; - timing_acc = 0.0; - timing_acc_n = 0.0; - } - } - void newIrFrame() { ir_frame = new Frame(512, 424, 4); @@ -975,7 +948,7 @@ void CpuDepthPacketProcessor::process(const DepthPacket &packet) impl_->newDepthFrame(); } - impl_->stopTiming(); + impl_->stopTiming(LOG_INFO); } } /* namespace libfreenect2 */ diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp index 8c2357f82..6e5678d84 100644 --- a/examples/protonect/src/logging.cpp +++ b/examples/protonect/src/logging.cpp @@ -30,6 +30,14 @@ #include #include +#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT +#include +#endif + +#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT +#include +#endif + namespace libfreenect2 { Logger::~Logger() {} @@ -144,6 +152,98 @@ void setGlobalLogger(Logger *logger) userLogger_ = logger; } +class Timer +{ + public: + double duration; + size_t count; + + Timer() + { + reset(); + } + + void reset() + { + duration = 0; + count = 0; + } + +#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT + std::chrono::time_point time_start; + + void start() + { + time_start = std::chrono::high_resolution_clock::now(); + } + + void stop() + { + duration += std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - time_start).count(); + count++; + } +#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT) + double time_start; + + void start() + { + time_start = glfwGetTime(); + } + + void stop() + { + duration += glfwGetTime() - time_start; + count++; + } +#else + void start() + { + } + + void stop() + { + } +#endif +}; + +class WithPerfLoggingImpl: public Timer +{ +public: + std::ostream &stop(std::ostream &stream) + { + Timer::stop(); + if (count < 100) + { + stream.setstate(std::ios::eofbit); + return stream; + } + double avg = duration / count; + reset(); + stream << "avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; + return stream; + } +}; + +WithPerfLogging::WithPerfLogging() + :impl_(new WithPerfLoggingImpl) +{ +} + +WithPerfLogging::~WithPerfLogging() +{ + delete impl_; +} + +void WithPerfLogging::startTiming() +{ + impl_->start(); +} + +std::ostream &WithPerfLogging::stopTiming(std::ostream &stream) +{ + impl_->stop(stream); +} + std::string getShortName(const char *func) { std::string src(func); diff --git a/examples/protonect/src/opencl_depth_packet_processor.cpp b/examples/protonect/src/opencl_depth_packet_processor.cpp index caf52dace..cb2662517 100644 --- a/examples/protonect/src/opencl_depth_packet_processor.cpp +++ b/examples/protonect/src/opencl_depth_packet_processor.cpp @@ -65,7 +65,7 @@ std::string loadCLSource(const std::string &filename) return std::string(reinterpret_cast(data), length); } -class OpenCLDepthPacketProcessorImpl +class OpenCLDepthPacketProcessorImpl: public WithPerfLogging { public: cl_short lut11to16[2048]; @@ -75,11 +75,6 @@ class OpenCLDepthPacketProcessorImpl libfreenect2::DepthPacketProcessor::Config config; DepthPacketProcessor::Parameters params; - double timing_acc; - double timing_acc_n; - - Timer timer; - Frame *ir_frame, *depth_frame; cl::Context context; @@ -144,8 +139,6 @@ class OpenCLDepthPacketProcessorImpl newIrFrame(); newDepthFrame(); - timing_acc = 0.0; - timing_acc_n = 0.0; image_size = 512 * 424; deviceInitialized = initDevice(deviceId); @@ -536,25 +529,6 @@ class OpenCLDepthPacketProcessorImpl return true; } - void startTiming() - { - timer.start(); - } - - void stopTiming() - { - timing_acc += timer.stop(); - timing_acc_n += 1.0; - - if(timing_acc_n >= 100.0) - { - double avg = (timing_acc / timing_acc_n); - LOG_INFO << "[OpenCLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0 / avg) << "Hz"; - timing_acc = 0.0; - timing_acc_n = 0.0; - } - } - void newIrFrame() { ir_frame = new Frame(512, 424, 4); @@ -673,7 +647,7 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet) impl_->run(packet); - impl_->stopTiming(); + impl_->stopTiming(LOG_INFO); if(has_listener) { diff --git a/examples/protonect/src/opengl_depth_packet_processor.cpp b/examples/protonect/src/opengl_depth_packet_processor.cpp index 446d4f0b3..e42b0f73b 100644 --- a/examples/protonect/src/opengl_depth_packet_processor.cpp +++ b/examples/protonect/src/opengl_depth_packet_processor.cpp @@ -330,7 +330,7 @@ struct Texture : public WithOpenGLBindings } }; -struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings +struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings, public WithPerfLogging { GLFWwindow *opengl_context_ptr; std::string shader_folder; @@ -364,11 +364,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings DepthPacketProcessor::Parameters params; bool params_need_update; - double timing_acc; - double timing_acc_n; - - Timer timer; - bool do_debug; struct Vertex @@ -387,8 +382,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings stage2_framebuffer(0), filter2_framebuffer(0), params_need_update(true), - timing_acc(0), - timing_acc_n(0), do_debug(debug) { } @@ -441,25 +434,6 @@ struct OpenGLDepthPacketProcessorImpl : public WithOpenGLBindings debug.gl(b); } - void startTiming() - { - timer.start(); - } - - void stopTiming() - { - timing_acc += timer.stop(); - timing_acc_n += 1.0; - - if(timing_acc_n >= 100.0) - { - double avg = (timing_acc / timing_acc_n); - LOG_INFO << "[OpenGLDepthPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; - timing_acc = 0.0; - timing_acc_n = 0.0; - } - } - void initialize() { ChangeCurrentOpenGLContext ctx(opengl_context_ptr); @@ -946,7 +920,7 @@ void OpenGLDepthPacketProcessor::process(const DepthPacket &packet) if(impl_->do_debug) glfwSwapBuffers(impl_->opengl_context_ptr); - impl_->stopTiming(); + impl_->stopTiming(LOG_INFO); if(has_listener) { diff --git a/examples/protonect/src/timer.cpp b/examples/protonect/src/timer.cpp deleted file mode 100644 index f53901df9..000000000 --- a/examples/protonect/src/timer.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/* - * This file is part of the OpenKinect Project. http://www.openkinect.org - * - * Copyright (c) 2015 individual OpenKinect contributors. See the CONTRIB file - * for details. - * - * This code is licensed to you under the terms of the Apache License, version - * 2.0, or, at your option, the terms of the GNU General Public License, - * version 2.0. See the APACHE20 and GPL2 files for the text of the licenses, - * or the following URLs: - * http://www.apache.org/licenses/LICENSE-2.0 - * http://www.gnu.org/licenses/gpl-2.0.txt - * - * If you redistribute this file in source form, modified or unmodified, you - * may: - * 1) Leave this header intact and distribute it under the same terms, - * accompanying it with the APACHE20 and GPL20 files, or - * 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or - * 3) Delete the GPL v2 clause and accompany it with the APACHE20 file - * In all cases you must keep the copyright notice intact and include a copy - * of the CONTRIB file. - * - * Binary distributions must follow the binary distribution requirements of - * either License. - */ - -#include - -#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT -#include -#endif - -#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT -#include -#endif - -namespace libfreenect2 { - -#ifdef LIBFREENECT2_WITH_CXX11_SUPPORT -class TimerImpl { - public: - void start() { - time_start = std::chrono::high_resolution_clock::now(); - } - - double stop() { - return std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - time_start).count(); - } - - std::chrono::time_point time_start; -}; -#elif defined(LIBFREENECT2_WITH_OPENGL_SUPPORT) -class TimerImpl { - public: - void start() { - time_start = glfwGetTime(); - } - - double stop() { - return glfwGetTime() - time_start; - } - - double time_start; -}; -#else -class TimerImpl { - public: - void start() { - } - - double stop() { - return 0; - } -}; -#endif - -Timer::Timer() : - impl_(new TimerImpl()) { -} - -Timer::~Timer() { - delete impl_; -} - -void Timer::start() { - impl_->start(); -} - -double Timer::stop() { - return impl_->stop(); -} - -} /* namespace libfreenect2 */ \ No newline at end of file diff --git a/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp b/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp index 2e83e6f52..c5c0a77cb 100644 --- a/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp +++ b/examples/protonect/src/turbo_jpeg_rgb_packet_processor.cpp @@ -31,7 +31,7 @@ namespace libfreenect2 { -class TurboJpegRgbPacketProcessorImpl +class TurboJpegRgbPacketProcessorImpl: public WithPerfLogging { public: @@ -39,11 +39,6 @@ class TurboJpegRgbPacketProcessorImpl Frame *frame; - double timing_acc; - double timing_acc_n; - - Timer timer; - TurboJpegRgbPacketProcessorImpl() { decompressor = tjInitDecompress(); @@ -53,9 +48,6 @@ class TurboJpegRgbPacketProcessorImpl } newFrame(); - - timing_acc = 0.0; - timing_acc_n = 0.0; } ~TurboJpegRgbPacketProcessorImpl() @@ -73,25 +65,6 @@ class TurboJpegRgbPacketProcessorImpl { frame = new Frame(1920, 1080, tjPixelSize[TJPF_BGRX]); } - - void startTiming() - { - timer.start(); - } - - void stopTiming() - { - timing_acc += timer.stop(); - timing_acc_n += 1.0; - - if(timing_acc_n >= 100.0) - { - double avg = (timing_acc / timing_acc_n); - LOG_INFO << "[TurboJpegRgbPacketProcessor] avg. time: " << (avg * 1000) << "ms -> ~" << (1.0/avg) << "Hz"; - timing_acc = 0.0; - timing_acc_n = 0.0; - } - } }; TurboJpegRgbPacketProcessor::TurboJpegRgbPacketProcessor() : @@ -124,10 +97,10 @@ void TurboJpegRgbPacketProcessor::process(const RgbPacket &packet) } else { - LOG_ERROR << "[TurboJpegRgbPacketProcessor::doProcess] Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'"; + LOG_ERROR << "Failed to decompress rgb image! TurboJPEG error: '" << tjGetErrorStr() << "'"; } - impl_->stopTiming(); + impl_->stopTiming(LOG_INFO); } } From 3da9d4ae0bd7809a0f07d70a1c7bbfdaf74af5b0 Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Thu, 20 Aug 2015 21:57:04 -0400 Subject: [PATCH 08/10] Add an example on how to create custom logger Also export level2str() in Logger for external use. --- examples/protonect/Protonect.cpp | 28 ++++++++++++++++++- .../protonect/include/libfreenect2/logger.h | 1 + examples/protonect/src/logging.cpp | 2 +- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/examples/protonect/Protonect.cpp b/examples/protonect/Protonect.cpp index d093069ef..0bd82bc27 100644 --- a/examples/protonect/Protonect.cpp +++ b/examples/protonect/Protonect.cpp @@ -33,7 +33,7 @@ #include #include #include -#include +#include #ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT #include "viewer.h" #endif @@ -46,6 +46,29 @@ void sigint_handler(int s) protonect_shutdown = true; } +//The following demostrates how to create a custom logger +#include +#include +class MyFileLogger: public libfreenect2::Logger +{ +private: + std::ofstream logfile_; +public: + MyFileLogger(const char *filename) + : logfile_(filename) + { + level_ = Debug; + } + bool good() + { + return logfile_.good(); + } + virtual void log(Level level, const std::string &message) + { + logfile_ << "[" << libfreenect2::Logger::level2str(level) << "] " << message << std::endl; + } +}; + int main(int argc, char *argv[]) { std::string program_path(argv[0]); @@ -61,6 +84,9 @@ int main(int argc, char *argv[]) libfreenect2::Freenect2 freenect2; // create a console logger with debug level (default is console logger with info level) libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug)); + MyFileLogger *filelogger = new MyFileLogger(getenv("LOGFILE")); + if (filelogger->good()) + libfreenect2::setGlobalLogger(filelogger); libfreenect2::Freenect2Device *dev = 0; libfreenect2::PacketPipeline *pipeline = 0; diff --git a/examples/protonect/include/libfreenect2/logger.h b/examples/protonect/include/libfreenect2/logger.h index 6e9f00009..10af9c8d3 100644 --- a/examples/protonect/include/libfreenect2/logger.h +++ b/examples/protonect/include/libfreenect2/logger.h @@ -47,6 +47,7 @@ class LIBFREENECT2_API Logger Debug = 4, }; static Level getDefaultLevel(); + static std::string level2str(Level level); virtual ~Logger(); diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp index 6e5678d84..60e6dd80c 100644 --- a/examples/protonect/src/logging.cpp +++ b/examples/protonect/src/logging.cpp @@ -74,7 +74,7 @@ Logger::Level Logger::level() const return level_; } -std::string level2str(const Logger::Level &l) +std::string Logger::level2str(Level l) { switch(l) { From 0dbff0a83a4e45124e9def3cfb865a6e950be009 Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Fri, 21 Aug 2015 20:37:49 +0000 Subject: [PATCH 09/10] Allow Protonect to run without a viewer --- examples/protonect/Protonect.cpp | 25 +++++++++++++++++++++---- examples/protonect/viewer.cpp | 10 +++++----- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/examples/protonect/Protonect.cpp b/examples/protonect/Protonect.cpp index 0bd82bc27..e69c1b529 100644 --- a/examples/protonect/Protonect.cpp +++ b/examples/protonect/Protonect.cpp @@ -99,6 +99,8 @@ int main(int argc, char *argv[]) std::string serial = freenect2.getDefaultDeviceSerialNumber(); + bool viewer_enabled = true; + for(int argI = 1; argI < argc; ++argI) { const std::string arg(argv[argI]); @@ -130,6 +132,10 @@ int main(int argc, char *argv[]) { serial = arg; } + else if(arg == "-noviewer") + { + viewer_enabled = false; + } else { std::cout << "Unknown argument: " << arg << std::endl; @@ -167,9 +173,13 @@ int main(int argc, char *argv[]) libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams()); + size_t framecount = 0; #ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT Viewer viewer; - viewer.initialize(); + if (viewer_enabled) + viewer.initialize(); +#else + viewer_enabled = false; #endif while(!protonect_shutdown) @@ -181,15 +191,22 @@ int main(int argc, char *argv[]) registration->apply(rgb, depth, &undistorted, ®istered); + framecount++; + if (!viewer_enabled) + { + if (framecount % 100 == 0) + std::cout << "The viewer is turned off. Received " << framecount << " frames. Ctrl-C to stop." << std::endl; + listener.release(frames); + continue; + } + #ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT viewer.addFrame("RGB", rgb); viewer.addFrame("ir", ir); viewer.addFrame("depth", depth); viewer.addFrame("registered", ®istered); - protonect_shutdown = viewer.render(); -#else - protonect_shutdown = true; + protonect_shutdown = protonect_shutdown || viewer.render(); #endif listener.release(frames); diff --git a/examples/protonect/viewer.cpp b/examples/protonect/viewer.cpp index 5e41e668d..71801f702 100644 --- a/examples/protonect/viewer.cpp +++ b/examples/protonect/viewer.cpp @@ -5,6 +5,10 @@ Viewer::Viewer() : shader_folder("src/shader/") // init glfw - if already initialized nothing happens int init = glfwInit(); +} + +void Viewer::initialize() +{ // setup context glfwDefaultWindowHints(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); @@ -16,10 +20,6 @@ Viewer::Viewer() : shader_folder("src/shader/") //glfwWindowHint(GLFW_VISIBLE, debug ? GL_TRUE : GL_FALSE); window = glfwCreateWindow(1280, 800, "Viewer", 0, NULL); -} - -void Viewer::initialize() -{ glfwMakeContextCurrent(window); OpenGLBindings *b = new OpenGLBindings(); flextInit(window, b); @@ -190,4 +190,4 @@ bool Viewer::render() void Viewer::addFrame(std::string id, libfreenect2::Frame* frame) { frames[id] = frame; -} \ No newline at end of file +} From 5172def0454daa2267541286709f6700eceb65ef Mon Sep 17 00:00:00 2001 From: Lingzhu Xiang Date: Tue, 25 Aug 2015 17:55:46 +0000 Subject: [PATCH 10/10] Work around buggy OpenCL ICD loader ocl-icd under 2.2.3 calls dlopen() in its library constructor and accesses a thread local variable in the process. This causes all subsequent access to any other thread local variables to deadlock. The bug is fixed in ocl-icd 2.2.4, which is not in stable releases in Ubuntu or Debian. Thus this provides a workaround given buggy ocl-icd. To avoid access to thread local variable, errno, std::ostream with unitbuf, and exception handling in libstdc++ cannot be used. This commit checks ocl-icd version, and refactor the OpenCL processor to not use exceptions. Then disable unitbuf on std::cerr and disable all exceptions with -fno-exceptions (when available). This commit and the ocl-icd bug do not affect Mac OS X or Windows. --- examples/protonect/CMakeLists.txt | 7 + examples/protonect/src/logging.cpp | 6 + .../src/opencl_depth_packet_processor.cpp | 309 ++++++++++-------- .../protonect/src/tinythread/tinythread.cpp | 9 - 4 files changed, 185 insertions(+), 146 deletions(-) diff --git a/examples/protonect/CMakeLists.txt b/examples/protonect/CMakeLists.txt index 11ab5b394..4b69b17a7 100644 --- a/examples/protonect/CMakeLists.txt +++ b/examples/protonect/CMakeLists.txt @@ -166,6 +166,13 @@ IF(ENABLE_OPENCL) LIST(APPEND RESOURCES src/opencl_depth_packet_processor.cl ) + + # Major Linux distro stable releases have buggy OpenCL ICD loader. + # The workaround of disabling exceptions can only be set up during compile time. + # Diabling it for all should be harmless. The flag is the same for GCC/Clang/ICC. + IF(UNIX AND NOT APPLE) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") + ENDIF() ENDIF(OPENCL_FOUND) ENDIF(ENABLE_OPENCL) diff --git a/examples/protonect/src/logging.cpp b/examples/protonect/src/logging.cpp index 60e6dd80c..20dff7476 100644 --- a/examples/protonect/src/logging.cpp +++ b/examples/protonect/src/logging.cpp @@ -97,6 +97,12 @@ class ConsoleLogger : public Logger ConsoleLogger(Level level) { level_ = level; + //std::ios_base::unitbuf causes automatic flushing which access + //thread local variable via std::uncaught_exception(). + //This causes deadlock with ocl-icd until its recent update. + //Accessing TLS has a slight performance penalty. + //log() always flush the ostream so unitbuf is unnecessary anyway. + std::nounitbuf(std::cerr); } virtual ~ConsoleLogger() {} virtual void log(Level level, const std::string &message) diff --git a/examples/protonect/src/opencl_depth_packet_processor.cpp b/examples/protonect/src/opencl_depth_packet_processor.cpp index cb2662517..91a8e7ad5 100644 --- a/examples/protonect/src/opencl_depth_packet_processor.cpp +++ b/examples/protonect/src/opencl_depth_packet_processor.cpp @@ -34,7 +34,6 @@ #define _USE_MATH_DEFINES #include -#define __CL_ENABLE_EXCEPTIONS #ifdef __APPLE__ #include #else @@ -142,6 +141,19 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging image_size = 512 * 424; deviceInitialized = initDevice(deviceId); + + const int CL_ICDL_VERSION = 2; + typedef cl_int (*icdloader_func)(int, size_t, void*, size_t*); + icdloader_func clGetICDLoaderInfoOCLICD = (icdloader_func)clGetExtensionFunctionAddress("clGetICDLoaderInfoOCLICD"); + if (clGetICDLoaderInfoOCLICD != NULL) + { + char buf[16]; + if (clGetICDLoaderInfoOCLICD(CL_ICDL_VERSION, sizeof(buf), buf, NULL) == CL_SUCCESS) + { + if (strcmp(buf, "2.2.4") < 0) + LOG_WARNING << "Your ocl-icd has deadlock bugs. Update to 2.2.4+ is recommended."; + } + } } void generateOptions(std::string &options) const @@ -214,37 +226,41 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging } } + std::string deviceString(cl::Device &dev) + { + std::string devName, devVendor, devType; + cl_device_type devTypeID; + dev.getInfo(CL_DEVICE_NAME, &devName); + dev.getInfo(CL_DEVICE_VENDOR, &devVendor); + dev.getInfo(CL_DEVICE_TYPE, &devTypeID); + + switch(devTypeID) + { + case CL_DEVICE_TYPE_CPU: + devType = "CPU"; + break; + case CL_DEVICE_TYPE_GPU: + devType = "GPU"; + break; + case CL_DEVICE_TYPE_ACCELERATOR: + devType = "ACCELERATOR"; + break; + case CL_DEVICE_TYPE_CUSTOM: + devType = "CUSTOM"; + break; + default: + devType = "UNKNOWN"; + } + + return devName + " (" + devType + ")[" + devVendor + ']'; + } + void listDevice(std::vector &devices) { LOG_INFO << " devices:"; for(size_t i = 0; i < devices.size(); ++i) { - cl::Device &dev = devices[i]; - std::string devName, devVendor, devType; - cl_device_type devTypeID; - dev.getInfo(CL_DEVICE_NAME, &devName); - dev.getInfo(CL_DEVICE_VENDOR, &devVendor); - dev.getInfo(CL_DEVICE_TYPE, &devTypeID); - - switch(devTypeID) - { - case CL_DEVICE_TYPE_CPU: - devType = "CPU"; - break; - case CL_DEVICE_TYPE_GPU: - devType = "GPU"; - break; - case CL_DEVICE_TYPE_ACCELERATOR: - devType = "ACCELERATOR"; - break; - case CL_DEVICE_TYPE_CUSTOM: - devType = "CUSTOM"; - break; - default: - devType = "UNKNOWN"; - } - - LOG_INFO << " " << i << ": " << devName << " (" << devType << ")[" << devVendor << ']'; + LOG_INFO << " " << i << ": " << deviceString(devices[i]); } } @@ -262,7 +278,7 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging for(size_t i = 0; i < devices.size(); ++i) { cl::Device &dev = devices[i]; - cl_device_type devTypeID; + cl_device_type devTypeID = 0; dev.getInfo(CL_DEVICE_TYPE, &devTypeID); if(!selected || (selectedType != CL_DEVICE_TYPE_GPU && devTypeID == CL_DEVICE_TYPE_GPU)) @@ -275,6 +291,8 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging return selected; } +#define CHECK_CL_ERROR(err, str) do {if (err != CL_SUCCESS) {LOG_ERROR << str << " failed: " << err; return false; } } while(0) + bool initDevice(const int deviceId) { if(!readProgram(sourceCode)) @@ -283,14 +301,11 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging } cl_int err = CL_SUCCESS; - try { std::vector platforms; - if(cl::Platform::get(&platforms) != CL_SUCCESS) - { - LOG_ERROR << "error while getting opencl platforms."; - return false; - } + err = cl::Platform::get(&platforms); + CHECK_CL_ERROR(err, "cl::Platform::get"); + if(platforms.empty()) { LOG_ERROR << "no opencl platforms found."; @@ -300,45 +315,15 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging std::vector devices; getDevices(platforms, devices); listDevice(devices); - if(selectDevice(devices, deviceId)) - { - std::string devName, devVendor, devType; - cl_device_type devTypeID; - device.getInfo(CL_DEVICE_NAME, &devName); - device.getInfo(CL_DEVICE_VENDOR, &devVendor); - device.getInfo(CL_DEVICE_TYPE, &devTypeID); - - switch(devTypeID) - { - case CL_DEVICE_TYPE_CPU: - devType = "CPU"; - break; - case CL_DEVICE_TYPE_GPU: - devType = "GPU"; - break; - case CL_DEVICE_TYPE_ACCELERATOR: - devType = "ACCELERATOR"; - break; - case CL_DEVICE_TYPE_CUSTOM: - devType = "CUSTOM"; - break; - default: - devType = "UNKNOWN"; - } - LOG_INFO << "selected device: " << devName << " (" << devType << ")[" << devVendor << ']'; - } - else + if(!selectDevice(devices, deviceId)) { LOG_ERROR << "could not find any suitable device"; return false; } + LOG_INFO << "selected device: " << deviceString(device); - context = cl::Context(device); - } - catch(const cl::Error &err) - { - LOG_ERROR << err.what() << "(" << err.err() << ")"; - throw; + context = cl::Context(device, NULL, NULL, NULL, &err); + CHECK_CL_ERROR(err, "cl::Context"); } return buildProgram(sourceCode); @@ -356,9 +341,9 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging return false; cl_int err = CL_SUCCESS; - try { queue = cl::CommandQueue(context, device, 0, &err); + CHECK_CL_ERROR(err, "cl::CommandQueue"); //Read only buf_lut11to16_size = 2048 * sizeof(cl_short); @@ -368,10 +353,15 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging buf_packet_size = ((image_size * 11) / 16) * 10 * sizeof(cl_ushort); buf_lut11to16 = cl::Buffer(context, CL_READ_ONLY_CACHE, buf_lut11to16_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_p0_table = cl::Buffer(context, CL_READ_ONLY_CACHE, buf_p0_table_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_x_table = cl::Buffer(context, CL_READ_ONLY_CACHE, buf_x_table_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_z_table = cl::Buffer(context, CL_READ_ONLY_CACHE, buf_z_table_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_packet = cl::Buffer(context, CL_READ_ONLY_CACHE, buf_packet_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); //Read-Write buf_a_size = image_size * sizeof(cl_float3); @@ -386,109 +376,156 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging buf_filtered_size = image_size * sizeof(cl_float); buf_a = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_a_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_b = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_b_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_n = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_n_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_ir = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_ir_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_a_filtered = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_a_filtered_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_b_filtered = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_b_filtered_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_edge_test = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_edge_test_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_depth = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_depth_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_ir_sum = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_ir_sum_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); buf_filtered = cl::Buffer(context, CL_READ_WRITE_CACHE, buf_filtered_size, NULL, &err); + CHECK_CL_ERROR(err, "cl::Buffer"); kernel_processPixelStage1 = cl::Kernel(program, "processPixelStage1", &err); - kernel_processPixelStage1.setArg(0, buf_lut11to16); - kernel_processPixelStage1.setArg(1, buf_z_table); - kernel_processPixelStage1.setArg(2, buf_p0_table); - kernel_processPixelStage1.setArg(3, buf_packet); - kernel_processPixelStage1.setArg(4, buf_a); - kernel_processPixelStage1.setArg(5, buf_b); - kernel_processPixelStage1.setArg(6, buf_n); - kernel_processPixelStage1.setArg(7, buf_ir); + CHECK_CL_ERROR(err, "cl::Kernel"); + err = kernel_processPixelStage1.setArg(0, buf_lut11to16); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(1, buf_z_table); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(2, buf_p0_table); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(3, buf_packet); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(4, buf_a); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(5, buf_b); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(6, buf_n); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage1.setArg(7, buf_ir); + CHECK_CL_ERROR(err, "setArg"); kernel_filterPixelStage1 = cl::Kernel(program, "filterPixelStage1", &err); - kernel_filterPixelStage1.setArg(0, buf_a); - kernel_filterPixelStage1.setArg(1, buf_b); - kernel_filterPixelStage1.setArg(2, buf_n); - kernel_filterPixelStage1.setArg(3, buf_a_filtered); - kernel_filterPixelStage1.setArg(4, buf_b_filtered); - kernel_filterPixelStage1.setArg(5, buf_edge_test); + CHECK_CL_ERROR(err, "cl::Kernel"); + err = kernel_filterPixelStage1.setArg(0, buf_a); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage1.setArg(1, buf_b); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage1.setArg(2, buf_n); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage1.setArg(3, buf_a_filtered); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage1.setArg(4, buf_b_filtered); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage1.setArg(5, buf_edge_test); + CHECK_CL_ERROR(err, "setArg"); kernel_processPixelStage2 = cl::Kernel(program, "processPixelStage2", &err); - kernel_processPixelStage2.setArg(0, config.EnableBilateralFilter ? buf_a_filtered : buf_a); - kernel_processPixelStage2.setArg(1, config.EnableBilateralFilter ? buf_b_filtered : buf_b); - kernel_processPixelStage2.setArg(2, buf_x_table); - kernel_processPixelStage2.setArg(3, buf_z_table); - kernel_processPixelStage2.setArg(4, buf_depth); - kernel_processPixelStage2.setArg(5, buf_ir_sum); + CHECK_CL_ERROR(err, "cl::Kernel"); + err = kernel_processPixelStage2.setArg(0, config.EnableBilateralFilter ? buf_a_filtered : buf_a); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage2.setArg(1, config.EnableBilateralFilter ? buf_b_filtered : buf_b); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage2.setArg(2, buf_x_table); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage2.setArg(3, buf_z_table); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage2.setArg(4, buf_depth); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_processPixelStage2.setArg(5, buf_ir_sum); + CHECK_CL_ERROR(err, "setArg"); kernel_filterPixelStage2 = cl::Kernel(program, "filterPixelStage2", &err); - kernel_filterPixelStage2.setArg(0, buf_depth); - kernel_filterPixelStage2.setArg(1, buf_ir_sum); - kernel_filterPixelStage2.setArg(2, buf_edge_test); - kernel_filterPixelStage2.setArg(3, buf_filtered); + CHECK_CL_ERROR(err, "cl::Kernel"); + err = kernel_filterPixelStage2.setArg(0, buf_depth); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage2.setArg(1, buf_ir_sum); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage2.setArg(2, buf_edge_test); + CHECK_CL_ERROR(err, "setArg"); + err = kernel_filterPixelStage2.setArg(3, buf_filtered); + CHECK_CL_ERROR(err, "setArg"); cl::Event event0, event1, event2, event3; - queue.enqueueWriteBuffer(buf_lut11to16, CL_FALSE, 0, buf_lut11to16_size, lut11to16, NULL, &event0); - queue.enqueueWriteBuffer(buf_p0_table, CL_FALSE, 0, buf_p0_table_size, p0_table, NULL, &event1); - queue.enqueueWriteBuffer(buf_x_table, CL_FALSE, 0, buf_x_table_size, x_table, NULL, &event2); - queue.enqueueWriteBuffer(buf_z_table, CL_FALSE, 0, buf_z_table_size, z_table, NULL, &event3); - - event0.wait(); - event1.wait(); - event2.wait(); - event3.wait(); - } - catch(const cl::Error &err) - { - LOG_ERROR << err.what() << "(" << err.err() << ")"; - throw; + err = queue.enqueueWriteBuffer(buf_lut11to16, CL_FALSE, 0, buf_lut11to16_size, lut11to16, NULL, &event0); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); + err = queue.enqueueWriteBuffer(buf_p0_table, CL_FALSE, 0, buf_p0_table_size, p0_table, NULL, &event1); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); + err = queue.enqueueWriteBuffer(buf_x_table, CL_FALSE, 0, buf_x_table_size, x_table, NULL, &event2); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); + err = queue.enqueueWriteBuffer(buf_z_table, CL_FALSE, 0, buf_z_table_size, z_table, NULL, &event3); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); + + err = event0.wait(); + CHECK_CL_ERROR(err, "wait"); + err = event1.wait(); + CHECK_CL_ERROR(err, "wait"); + err = event2.wait(); + CHECK_CL_ERROR(err, "wait"); + err = event3.wait(); + CHECK_CL_ERROR(err, "wait"); } + programInitialized = true; return true; } - void run(const DepthPacket &packet) + bool run(const DepthPacket &packet) { - try + cl_int err; { std::vector eventWrite(1), eventPPS1(1), eventFPS1(1), eventPPS2(1), eventFPS2(1); cl::Event event0, event1; - queue.enqueueWriteBuffer(buf_packet, CL_FALSE, 0, buf_packet_size, packet.buffer, NULL, &eventWrite[0]); + err = queue.enqueueWriteBuffer(buf_packet, CL_FALSE, 0, buf_packet_size, packet.buffer, NULL, &eventWrite[0]); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); - queue.enqueueNDRangeKernel(kernel_processPixelStage1, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventWrite, &eventPPS1[0]); - queue.enqueueReadBuffer(buf_ir, CL_FALSE, 0, buf_ir_size, ir_frame->data, &eventPPS1, &event0); + err = queue.enqueueNDRangeKernel(kernel_processPixelStage1, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventWrite, &eventPPS1[0]); + CHECK_CL_ERROR(err, "enqueueNDRangeKernel"); + err = queue.enqueueReadBuffer(buf_ir, CL_FALSE, 0, buf_ir_size, ir_frame->data, &eventPPS1, &event0); + CHECK_CL_ERROR(err, "enqueueReadBuffer"); if(config.EnableBilateralFilter) { - queue.enqueueNDRangeKernel(kernel_filterPixelStage1, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventPPS1, &eventFPS1[0]); + err = queue.enqueueNDRangeKernel(kernel_filterPixelStage1, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventPPS1, &eventFPS1[0]); + CHECK_CL_ERROR(err, "enqueueNDRangeKernel"); } else { eventFPS1[0] = eventPPS1[0]; } - queue.enqueueNDRangeKernel(kernel_processPixelStage2, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventFPS1, &eventPPS2[0]); + err = queue.enqueueNDRangeKernel(kernel_processPixelStage2, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventFPS1, &eventPPS2[0]); + CHECK_CL_ERROR(err, "enqueueNDRangeKernel"); if(config.EnableEdgeAwareFilter) { - queue.enqueueNDRangeKernel(kernel_filterPixelStage2, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventPPS2, &eventFPS2[0]); + err = queue.enqueueNDRangeKernel(kernel_filterPixelStage2, cl::NullRange, cl::NDRange(image_size), cl::NullRange, &eventPPS2, &eventFPS2[0]); + CHECK_CL_ERROR(err, "enqueueWriteBuffer"); } else { eventFPS2[0] = eventPPS2[0]; } - queue.enqueueReadBuffer(config.EnableEdgeAwareFilter ? buf_filtered : buf_depth, CL_FALSE, 0, buf_depth_size, depth_frame->data, &eventFPS2, &event1); - event0.wait(); - event1.wait(); - } - catch(const cl::Error &err) - { - LOG_ERROR << err.what() << " (" << err.err() << ")"; - throw; + err = queue.enqueueReadBuffer(config.EnableEdgeAwareFilter ? buf_filtered : buf_depth, CL_FALSE, 0, buf_depth_size, depth_frame->data, &eventFPS2, &event1); + CHECK_CL_ERROR(err, "enqueueReadBuffer"); + err = event0.wait(); + CHECK_CL_ERROR(err, "wait"); + err = event1.wait(); + CHECK_CL_ERROR(err, "wait"); } + return true; } bool readProgram(std::string &source) const @@ -499,7 +536,7 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging bool buildProgram(const std::string& sources) { - try + cl_int err; { LOG_INFO << "building OpenCL program..."; @@ -507,24 +544,22 @@ class OpenCLDepthPacketProcessorImpl: public WithPerfLogging generateOptions(options); cl::Program::Sources source(1, std::make_pair(sources.c_str(), sources.length())); - program = cl::Program(context, source); - program.build(options.c_str()); - LOG_INFO << "OpenCL program built successfully"; - } - catch(const cl::Error &err) - { - LOG_ERROR << err.what() << "(" << err.err() << ")"; + program = cl::Program(context, source, &err); + CHECK_CL_ERROR(err, "cl::Program"); - if(err.err() == CL_BUILD_PROGRAM_FAILURE) + err = program.build(options.c_str()); + if (err != CL_SUCCESS) { + LOG_ERROR << "failed to build program: " << err; LOG_ERROR << "Build Status: " << program.getBuildInfo(device); LOG_ERROR << "Build Options:\t" << program.getBuildInfo(device); LOG_ERROR << "Build Log:\t " << program.getBuildInfo(device); + programBuilt = false; + return false; } - - programBuilt = false; - return false; } + + LOG_INFO << "OpenCL program built successfully"; programBuilt = true; return true; } @@ -645,11 +680,11 @@ void OpenCLDepthPacketProcessor::process(const DepthPacket &packet) impl_->ir_frame->sequence = packet.sequence; impl_->depth_frame->sequence = packet.sequence; - impl_->run(packet); + bool r = impl_->run(packet); impl_->stopTiming(LOG_INFO); - if(has_listener) + if(has_listener && r) { if(this->listener_->onNewFrame(Frame::Ir, impl_->ir_frame)) { diff --git a/examples/protonect/src/tinythread/tinythread.cpp b/examples/protonect/src/tinythread/tinythread.cpp index 690eceea1..60308facc 100644 --- a/examples/protonect/src/tinythread/tinythread.cpp +++ b/examples/protonect/src/tinythread/tinythread.cpp @@ -163,17 +163,8 @@ void * thread::wrapper_function(void * aArg) // Get thread startup information _thread_start_info * ti = (_thread_start_info *) aArg; - try - { // Call the actual client thread function ti->mFunction(ti->mArg); - } - catch(...) - { - // Uncaught exceptions will terminate the application (default behavior - // according to C++11) - std::terminate(); - } // The thread is no longer executing lock_guard guard(ti->mThread->mDataMutex);