Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/protonect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ SET(SOURCES
src/resource.cpp
src/command_transaction.cpp
src/registration.cpp
src/logging.cpp
src/libfreenect2.cpp

${LIBFREENECT2_THREADING_SOURCE}
Expand Down
3 changes: 3 additions & 0 deletions examples/protonect/Protonect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stdint.h>

#include <libfreenect2/config.h>
#include <libfreenect2/logging.h>

#include <libfreenect2/double_buffer.h>
#include <libfreenect2/depth_packet_processor.h>
Expand All @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion examples/protonect/include/libfreenect2/libfreenect2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <libfreenect2/config.h>
#include <libfreenect2/frame_listener.hpp>
#include <libfreenect2/logging.h>

namespace libfreenect2
{
Expand Down Expand Up @@ -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);
Expand Down
122 changes: 122 additions & 0 deletions examples/protonect/include/libfreenect2/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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 <string>
#include <sstream>

#include <libfreenect2/config.h>

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;
};

template<class T>
void trySetLogger(T *obj, Logger *logger)
{
WithLogger *obj_with_logger = dynamic_cast<WithLogger *>(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();
};

} /* namespace libfreenect2 */

#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_ */
5 changes: 4 additions & 1 deletion examples/protonect/include/libfreenect2/packet_pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define PACKET_PIPELINE_H_

#include <libfreenect2/config.h>
#include <libfreenect2/logging.h>
#include <libfreenect2/data_callback.h>
#include <libfreenect2/rgb_packet_stream_parser.h>
#include <libfreenect2/depth_packet_stream_parser.h>
Expand All @@ -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_;
Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
#define COMMAND_TRANSACTION_H_

#include <libusb.h>
#include <libfreenect2/logging.h>
#include <libfreenect2/protocol/command.h>

namespace libfreenect2
{
namespace protocol
{

class CommandTransaction
class CommandTransaction : public WithLoggerImpl
{
public:
static const int ResponseCompleteLength = 16;
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define USB_CONTROL_H_

#include <libusb.h>
#include <libfreenect2/logging.h>

namespace libfreenect2
{
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <stddef.h>

#include <libfreenect2/config.h>
#include <libfreenect2/logging.h>
#include <libfreenect2/double_buffer.h>
#include <libfreenect2/rgb_packet_processor.h>

Expand All @@ -38,7 +39,7 @@
namespace libfreenect2
{

class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback
class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback, public WithLoggerImpl
{
public:
RgbPacketStreamParser();
Expand Down
3 changes: 2 additions & 1 deletion examples/protonect/include/libfreenect2/usb/transfer_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <vector>
#include <libusb.h>

#include <libfreenect2/logging.h>
#include <libfreenect2/data_callback.h>
#include <libfreenect2/threading.h>

Expand All @@ -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);
Expand Down
13 changes: 6 additions & 7 deletions examples/protonect/src/command_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <libfreenect2/protocol/command_transaction.h>

#include <stdint.h>
#include <iostream>

namespace libfreenect2
{
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}
}
Expand All @@ -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];
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions examples/protonect/src/depth_packet_stream_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/

#include <libfreenect2/depth_packet_stream_parser.h>
#include <iostream>
#include <memory.h>

namespace libfreenect2
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
{
Expand All @@ -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;
Expand All @@ -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
{
Expand Down
Loading