Skip to content

Logging refactoring continued #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 4, 2015
Merged
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
9 changes: 8 additions & 1 deletion examples/protonect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ SET(SOURCES
src/resource.cpp
src/command_transaction.cpp
src/registration.cpp
src/timer.cpp
src/logging.cpp
src/libfreenect2.cpp

${LIBFREENECT2_THREADING_SOURCE}
Expand Down Expand Up @@ -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)

Expand Down
55 changes: 51 additions & 4 deletions examples/protonect/Protonect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <libfreenect2/threading.h>
#include <libfreenect2/registration.h>
#include <libfreenect2/packet_pipeline.h>
#include <libfreenect2/logger.h>
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
#include "viewer.h"
#endif
Expand All @@ -45,6 +46,29 @@ void sigint_handler(int s)
protonect_shutdown = true;
}

//The following demostrates how to create a custom logger
#include <fstream>
#include <cstdlib>
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]);
Expand All @@ -58,6 +82,12 @@ 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;

Expand All @@ -69,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]);
Expand Down Expand Up @@ -100,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;
Expand Down Expand Up @@ -137,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)
Expand All @@ -151,15 +191,22 @@ int main(int argc, char *argv[])

registration->apply(rgb, depth, &undistorted, &registered);

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", &registered);

protonect_shutdown = viewer.render();
#else
protonect_shutdown = true;
protonect_shutdown = protonect_shutdown || viewer.render();
#endif

listener.release(frames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,46 @@
* either License.
*/

#ifndef TIMER_H_
#define TIMER_H_
#ifndef LIBFREENECT2_LOGGER_H_
#define LIBFREENECT2_LOGGER_H_

#include <string>
#include <sstream>

#include <libfreenect2/config.h>

namespace libfreenect2
{

class TimerImpl;
class LIBFREENECT2_API Logger
{
public:
enum Level
{
None = 0,
Error = 1,
Warning = 2,
Info = 3,
Debug = 4,
};
static Level getDefaultLevel();
static std::string level2str(Level level);

class Timer {
public:
Timer();
virtual ~Timer();
virtual ~Logger();

void start();
double stop();
virtual Level level() const;

private:
TimerImpl *impl_;
virtual void log(Level level, const std::string &message) = 0;
protected:
Level level_;
};

} /* namespace libfreenect2 */
LIBFREENECT2_API Logger *createConsoleLogger(Logger::Level level);
LIBFREENECT2_API Logger *createConsoleLoggerWithDefaultLevel();

#endif /* TIMER_H_ */
//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_ */
83 changes: 83 additions & 0 deletions examples/protonect/include/libfreenect2/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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>
#include <libfreenect2/logger.h>

namespace libfreenect2
{

class WithPerfLoggingImpl;

class WithPerfLogging
{
public:
WithPerfLogging();
virtual ~WithPerfLogging();
void startTiming();
std::ostream &stopTiming(std::ostream &stream);
private:
WithPerfLoggingImpl *impl_;
};

class LogMessage
{
private:
Logger *logger_;
Logger::Level level_;
std::ostringstream stream_;
public:
LogMessage(Logger *logger, Logger::Level level);
~LogMessage();

std::ostream &stream();
};

std::string getShortName(const char *func);

} /* namespace libfreenect2 */

#if defined(__GNUC__) or defined(__clang__)
#define LOG_SOURCE ::libfreenect2::getShortName(__PRETTY_FUNCTION__)
#elif defined(_MSC_VER)
#define LOG_SOURCE ::libfreenect2::getShortName(__FUNCSIG__)
#else
#define LOG_SOURCE ""
#endif

#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)
#define LOG_ERROR LOG(Error)

#endif /* LOGGING_H_ */
2 changes: 0 additions & 2 deletions examples/protonect/include/libfreenect2/packet_processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#ifndef PACKET_PROCESSOR_H_
#define PACKET_PROCESSOR_H_

#include <libfreenect2/timer.h>

namespace libfreenect2
{

Expand Down
14 changes: 7 additions & 7 deletions examples/protonect/src/command_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
*/

#include <libfreenect2/protocol/command_transaction.h>
#include <libfreenect2/logging.h>

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

namespace libfreenect2
{
Expand Down Expand Up @@ -106,7 +106,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 +119,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 +135,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 +157,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 +176,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
Loading