Skip to content

Commit 45132c9

Browse files
christiankerlxlz
authored andcommitted
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
1 parent 6f9ec3f commit 45132c9

File tree

6 files changed

+364
-45
lines changed

6 files changed

+364
-45
lines changed

examples/protonect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SET(SOURCES
102102
src/command_transaction.cpp
103103
src/registration.cpp
104104
src/timer.cpp
105+
src/logging.cpp
105106
src/libfreenect2.cpp
106107

107108
${LIBFREENECT2_THREADING_SOURCE}

examples/protonect/Protonect.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ int main(int argc, char *argv[])
5858
}
5959

6060
libfreenect2::Freenect2 freenect2;
61+
// create a console logger with debug level (default is console logger with info level)
62+
freenect2.setLogger(libfreenect2::createConsoleLogger(libfreenect2::Logger::Debug));
63+
6164
libfreenect2::Freenect2Device *dev = 0;
6265
libfreenect2::PacketPipeline *pipeline = 0;
6366

examples/protonect/include/libfreenect2/libfreenect2.hpp

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

3030
#include <libfreenect2/config.h>
3131
#include <libfreenect2/frame_listener.hpp>
32+
#include <libfreenect2/logging.h>
3233

3334
namespace libfreenect2
3435
{
@@ -95,12 +96,15 @@ class LIBFREENECT2_API Freenect2Device
9596

9697
class Freenect2Impl;
9798

98-
class LIBFREENECT2_API Freenect2
99+
class LIBFREENECT2_API Freenect2 : public WithLogger
99100
{
100101
public:
101102
Freenect2(void *usb_context = 0);
102103
virtual ~Freenect2();
103104

105+
virtual void setLogger(Logger *logger);
106+
virtual Logger *logger();
107+
104108
int enumerateDevices();
105109

106110
std::string getDeviceSerialNumber(int idx);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* This file is part of the OpenKinect Project. http://www.openkinect.org
3+
*
4+
* Copyright (c) 2014 individual OpenKinect contributors. See the CONTRIB file
5+
* for details.
6+
*
7+
* This code is licensed to you under the terms of the Apache License, version
8+
* 2.0, or, at your option, the terms of the GNU General Public License,
9+
* version 2.0. See the APACHE20 and GPL2 files for the text of the licenses,
10+
* or the following URLs:
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
* http://www.gnu.org/licenses/gpl-2.0.txt
13+
*
14+
* If you redistribute this file in source form, modified or unmodified, you
15+
* may:
16+
* 1) Leave this header intact and distribute it under the same terms,
17+
* accompanying it with the APACHE20 and GPL20 files, or
18+
* 2) Delete the Apache 2.0 clause and accompany it with the GPL2 file, or
19+
* 3) Delete the GPL v2 clause and accompany it with the APACHE20 file
20+
* In all cases you must keep the copyright notice intact and include a copy
21+
* of the CONTRIB file.
22+
*
23+
* Binary distributions must follow the binary distribution requirements of
24+
* either License.
25+
*/
26+
27+
#ifndef LOGGING_H_
28+
#define LOGGING_H_
29+
30+
#include <string>
31+
#include <sstream>
32+
33+
#include <libfreenect2/config.h>
34+
35+
namespace libfreenect2
36+
{
37+
38+
class LIBFREENECT2_API Logger
39+
{
40+
public:
41+
enum Level
42+
{
43+
Debug = 1,
44+
Info = 2,
45+
Warning = 3,
46+
Error = 4,
47+
};
48+
static Level getDefaultLevel();
49+
50+
virtual ~Logger();
51+
52+
virtual Level level() const;
53+
54+
virtual void log(Level level, const std::string &message) = 0;
55+
protected:
56+
Level level_;
57+
};
58+
59+
LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level);
60+
LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel();
61+
LIBFREENECT2_API Logger *createNoopLogger();
62+
63+
class LIBFREENECT2_API LogMessage
64+
{
65+
private:
66+
Logger *logger_;
67+
Logger::Level level_;
68+
std::ostringstream stream_;
69+
public:
70+
LogMessage(Logger *logger, Logger::Level level);
71+
~LogMessage();
72+
73+
std::ostream &stream();
74+
};
75+
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+
class LIBFREENECT2_API WithLoggerImpl : public WithLogger
85+
{
86+
protected:
87+
Logger *logger_;
88+
89+
virtual void onLoggerChanged(Logger *logger);
90+
public:
91+
WithLoggerImpl();
92+
virtual ~WithLoggerImpl();
93+
virtual void setLogger(Logger *logger);
94+
virtual Logger *logger();
95+
};
96+
97+
} /* namespace libfreenect2 */
98+
99+
#define LOG_DEBUG (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Debug).stream())
100+
#define LOG_INFO (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Info).stream())
101+
#define LOG_WARNING (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Warning).stream())
102+
#define LOG_ERROR (::libfreenect2::LogMessage(logger(), ::libfreenect2::Logger::Error).stream())
103+
104+
#endif /* LOGGING_H_ */

0 commit comments

Comments
 (0)