Skip to content

Commit 4024897

Browse files
committed
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.
1 parent 81e1151 commit 4024897

17 files changed

+57
-129
lines changed

examples/protonect/Protonect.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <libfreenect2/threading.h>
3434
#include <libfreenect2/registration.h>
3535
#include <libfreenect2/packet_pipeline.h>
36+
#include <libfreenect2/logging.h>
3637
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
3738
#include "viewer.h"
3839
#endif
@@ -59,7 +60,7 @@ int main(int argc, char *argv[])
5960

6061
libfreenect2::Freenect2 freenect2;
6162
// create a console logger with debug level (default is console logger with info level)
62-
freenect2.setLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug));
63+
libfreenect2::setGlobalLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug));
6364

6465
libfreenect2::Freenect2Device *dev = 0;
6566
libfreenect2::PacketPipeline *pipeline = 0;

examples/protonect/include/libfreenect2/depth_packet_stream_parser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
#include <stdint.h>
3232

3333
#include <libfreenect2/config.h>
34-
#include <libfreenect2/logging.h>
3534

3635
#include <libfreenect2/double_buffer.h>
3736
#include <libfreenect2/depth_packet_processor.h>
@@ -52,7 +51,7 @@ LIBFREENECT2_PACK(struct LIBFREENECT2_API DepthSubPacketFooter
5251
uint32_t fields[32];
5352
});
5453

55-
class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback, public WithLoggerImpl
54+
class LIBFREENECT2_API DepthPacketStreamParser : public DataCallback
5655
{
5756
public:
5857
DepthPacketStreamParser();

examples/protonect/include/libfreenect2/libfreenect2.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
#include <libfreenect2/config.h>
3131
#include <libfreenect2/frame_listener.hpp>
32-
#include <libfreenect2/logging.h>
3332

3433
namespace libfreenect2
3534
{
@@ -96,15 +95,12 @@ class LIBFREENECT2_API Freenect2Device
9695

9796
class Freenect2Impl;
9897

99-
class LIBFREENECT2_API Freenect2 : public WithLogger
98+
class LIBFREENECT2_API Freenect2
10099
{
101100
public:
102101
Freenect2(void *usb_context = 0);
103102
virtual ~Freenect2();
104103

105-
virtual void setLogger(Logger *logger);
106-
virtual Logger *logger();
107-
108104
int enumerateDevices();
109105

110106
std::string getDeviceSerialNumber(int idx);

examples/protonect/include/libfreenect2/logging.h

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level);
6060
LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel();
6161
LIBFREENECT2_API Logger *createNoopLogger();
6262

63+
//libfreenect2 frees the memory of the logger passed in.
64+
LIBFREENECT2_API Logger *getGlobalLogger();
65+
LIBFREENECT2_API void setGlobalLogger(Logger *logger);
66+
6367
class LIBFREENECT2_API LogMessage
6468
{
6569
private:
@@ -73,50 +77,22 @@ class LIBFREENECT2_API LogMessage
7377
std::ostream &stream();
7478
};
7579

76-
class LIBFREENECT2_API WithLogger
77-
{
78-
public:
79-
virtual ~WithLogger();
80-
virtual void setLogger(Logger *logger) = 0;
81-
virtual Logger *logger() = 0;
82-
};
83-
84-
template<class T>
85-
void trySetLogger(T *obj, Logger *logger)
86-
{
87-
WithLogger *obj_with_logger = dynamic_cast<WithLogger *>(obj);
88-
if(obj_with_logger != 0)
89-
{
90-
obj_with_logger->setLogger(logger);
91-
}
92-
}
93-
94-
class LIBFREENECT2_API WithLoggerImpl : public WithLogger
95-
{
96-
protected:
97-
Logger *logger_;
98-
99-
virtual void onLoggerChanged(Logger *logger);
100-
public:
101-
WithLoggerImpl();
102-
virtual ~WithLoggerImpl();
103-
virtual void setLogger(Logger *logger);
104-
virtual Logger *logger();
105-
};
80+
std::string getShortName(const char *func);
10681

10782
} /* namespace libfreenect2 */
10883

10984
#if defined(__GNUC__) or defined(__clang__)
110-
#define LIBFREENECT2_LOG_SOURCE __PRETTY_FUNCTION__
85+
#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__PRETTY_FUNCTION__)
11186
#elif defined(_MSC_VER)
112-
#define LIBFREENECT2_LOG_SOURCE __FUNCSIG__
87+
#define LIBFREENECT2_LOG_SOURCE ::libfreenect2::getShortName(__FUNCSIG__)
11388
#else
11489
#define LIBFREENECT2_LOG_SOURCE ""
11590
#endif
11691

117-
#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ")
118-
#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ")
119-
#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ")
120-
#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ")
92+
#define LOG(LEVEL) (::libfreenect2::LogMessage(::libfreenect2::getGlobalLogger(), ::libfreenect2::Logger::LEVEL).stream() << "[" << LIBFREENECT2_LOG_SOURCE << "] ")
93+
#define LOG_DEBUG LOG(Debug)
94+
#define LOG_INFO LOG(Info)
95+
#define LOG_WARNING LOG(Warning)
96+
#define LOG_ERROR LOG(Error)
12197

12298
#endif /* LOGGING_H_ */

examples/protonect/include/libfreenect2/packet_pipeline.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#define PACKET_PIPELINE_H_
2929

3030
#include <libfreenect2/config.h>
31-
#include <libfreenect2/logging.h>
3231
#include <libfreenect2/data_callback.h>
3332
#include <libfreenect2/rgb_packet_stream_parser.h>
3433
#include <libfreenect2/depth_packet_stream_parser.h>
@@ -51,7 +50,7 @@ class LIBFREENECT2_API PacketPipeline
5150
virtual DepthPacketProcessor *getDepthPacketProcessor() const = 0;
5251
};
5352

54-
class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline, public WithLoggerImpl
53+
class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline
5554
{
5655
protected:
5756
RgbPacketStreamParser *rgb_parser_;
@@ -64,8 +63,6 @@ class LIBFREENECT2_API BasePacketPipeline : public PacketPipeline, public WithLo
6463

6564
virtual void initialize();
6665
virtual DepthPacketProcessor *createDepthPacketProcessor() = 0;
67-
68-
virtual void onLoggerChanged(Logger *logger);
6966
public:
7067
virtual ~BasePacketPipeline();
7168

examples/protonect/include/libfreenect2/protocol/command_transaction.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,14 @@
2828
#define COMMAND_TRANSACTION_H_
2929

3030
#include <libusb.h>
31-
#include <libfreenect2/logging.h>
3231
#include <libfreenect2/protocol/command.h>
3332

3433
namespace libfreenect2
3534
{
3635
namespace protocol
3736
{
3837

39-
class CommandTransaction : public WithLoggerImpl
38+
class CommandTransaction
4039
{
4140
public:
4241
static const int ResponseCompleteLength = 16;
@@ -64,7 +63,7 @@ class CommandTransaction : public WithLoggerImpl
6463
};
6564

6665
CommandTransaction(libusb_device_handle *handle, int inbound_endpoint, int outbound_endpoint);
67-
virtual ~CommandTransaction();
66+
~CommandTransaction();
6867

6968
void execute(const CommandBase& command, Result& result);
7069
private:

examples/protonect/include/libfreenect2/protocol/usb_control.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#define USB_CONTROL_H_
2929

3030
#include <libusb.h>
31-
#include <libfreenect2/logging.h>
3231

3332
namespace libfreenect2
3433
{
@@ -62,7 +61,7 @@ namespace protocol
6261
*
6362
* The 2. interface can be enabled/disabled by changing its alternate setting to 1/0
6463
*/
65-
class UsbControl : public WithLoggerImpl
64+
class UsbControl
6665
{
6766
public:
6867
UsbControl(libusb_device_handle *handle);

examples/protonect/include/libfreenect2/rgb_packet_stream_parser.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <stddef.h>
3131

3232
#include <libfreenect2/config.h>
33-
#include <libfreenect2/logging.h>
3433
#include <libfreenect2/double_buffer.h>
3534
#include <libfreenect2/rgb_packet_processor.h>
3635

@@ -39,7 +38,7 @@
3938
namespace libfreenect2
4039
{
4140

42-
class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback, public WithLoggerImpl
41+
class LIBFREENECT2_API RgbPacketStreamParser : public DataCallback
4342
{
4443
public:
4544
RgbPacketStreamParser();

examples/protonect/include/libfreenect2/usb/transfer_pool.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <vector>
3131
#include <libusb.h>
3232

33-
#include <libfreenect2/logging.h>
3433
#include <libfreenect2/data_callback.h>
3534
#include <libfreenect2/threading.h>
3635

@@ -40,7 +39,7 @@ namespace libfreenect2
4039
namespace usb
4140
{
4241

43-
class TransferPool : public WithLoggerImpl
42+
class TransferPool
4443
{
4544
public:
4645
TransferPool(libusb_device_handle *device_handle, unsigned char device_endpoint);

examples/protonect/src/command_transaction.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include <libfreenect2/protocol/command_transaction.h>
28+
#include <libfreenect2/logging.h>
2829

2930
#include <stdint.h>
3031

0 commit comments

Comments
 (0)