From 4fde20b657cd3546dad03261b89b479a2a1006cd Mon Sep 17 00:00:00 2001 From: Matt Hill Date: Thu, 10 Jul 2014 18:17:18 -0400 Subject: [PATCH] Improved daemon error logging --- src/daemon.cpp | 4 +-- src/daemon/logging_videobuffer.h | 50 ++++++++++++++++++++++++++++++++ src/videobuffer.cpp | 38 ++++++++++++++++++++---- src/videobuffer.h | 15 ++++++++++ 4 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 src/daemon/logging_videobuffer.h diff --git a/src/daemon.cpp b/src/daemon.cpp index 2b2d525..57bb544 100644 --- a/src/daemon.cpp +++ b/src/daemon.cpp @@ -5,13 +5,13 @@ #include "daemon/beanstalk.hpp" #include "daemon/uuid.h" +#include "daemon/logging_videobuffer.h" #include "tclap/CmdLine.h" #include "alpr.h" #include "openalpr/simpleini/simpleini.h" #include "openalpr/cjson.h" #include "support/tinythread.h" -#include "videobuffer.h" #include #include "support/timing.h" @@ -230,7 +230,7 @@ void streamRecognitionThread(void* arg) int framenum = 0; - VideoBuffer videoBuffer; + LoggingVideoBuffer videoBuffer(logger); videoBuffer.connect(tdata->stream_url, 5); diff --git a/src/daemon/logging_videobuffer.h b/src/daemon/logging_videobuffer.h new file mode 100644 index 0000000..460ffcf --- /dev/null +++ b/src/daemon/logging_videobuffer.h @@ -0,0 +1,50 @@ +#ifndef OPENALPR_LOGGING_VIDEOBUFFER_H +#define OPENALPR_LOGGING_VIDEOBUFFER_H +#include "../videobuffer.h" + +#include + + +class LoggingVideoDispatcher : public VideoDispatcher +{ + public: + LoggingVideoDispatcher(std::string mjpeg_url, int fps, log4cplus::Logger logger) : + VideoDispatcher(mjpeg_url, fps) + { + this->logger = logger; + } + + virtual void log_info(std::string message) + { + LOG4CPLUS_INFO(logger, message); + } + virtual void log_error(std::string error) + { + LOG4CPLUS_WARN(logger, error ); + } + + private: + log4cplus::Logger logger; +}; + + +class LoggingVideoBuffer : public VideoBuffer +{ + public: + LoggingVideoBuffer(log4cplus::Logger logger) : VideoBuffer() + { + this->logger = logger; + } + + protected: + + virtual VideoDispatcher* createDispatcher(std::string mjpeg_url, int fps) + { + return new LoggingVideoDispatcher(mjpeg_url, fps, logger); + } + + private: + log4cplus::Logger logger; +}; + +#endif // OPENALPR_LOGGING_VIDEOBUFFER_H \ No newline at end of file diff --git a/src/videobuffer.cpp b/src/videobuffer.cpp index 51da51b..a1fa33c 100644 --- a/src/videobuffer.cpp +++ b/src/videobuffer.cpp @@ -38,6 +38,10 @@ VideoBuffer::~VideoBuffer() } } +VideoDispatcher* VideoBuffer::createDispatcher(std::string mjpeg_url, int fps) +{ + return new VideoDispatcher(mjpeg_url, fps); +} void VideoBuffer::connect(std::string mjpeg_url, int fps) { @@ -62,7 +66,7 @@ void VideoBuffer::connect(std::string mjpeg_url, int fps) } - dispatcher = new VideoDispatcher(mjpeg_url, fps); + dispatcher = createDispatcher(mjpeg_url, fps); tthread::thread* t = new tthread::thread(imageCollectionThread, (void*) dispatcher); @@ -103,14 +107,33 @@ void imageCollectionThread(void* arg) cv::VideoCapture cap=cv::VideoCapture(); cap.open(dispatcher->mjpeg_url); - getALPRImages(cap, dispatcher); + if (cap.isOpened()) + { + getALPRImages(cap, dispatcher); + } + else + { + std::stringstream ss; + ss << "Stream " << dispatcher->mjpeg_url << " failed to open."; + dispatcher->log_error(ss.str()); + } } catch (const std::runtime_error& error) { // Error occured while trying to gather image. Retry, don't exit. - std::cerr << "VideoBuffer exception: " << error.what() << std::endl; + std::stringstream ss; + ss << "VideoBuffer exception: " << error.what(); + dispatcher->log_error( ss.str() ); } + catch (cv::Exception e) + { + // OpenCV Exception occured while trying to gather image. Retry, don't exit. + std::stringstream ss; + ss << "VideoBuffer OpenCV exception: " << e.what(); + dispatcher->log_error( ss.str() ); + } + // Delay 1 second usleep(1000000); @@ -137,9 +160,12 @@ void getALPRImages(cv::VideoCapture cap, VideoDispatcher* dispatcher) { hasImage = cap.read(frame); // Double check the image to make sure it's valid. - if (frame.cols == 0 || frame.rows == 0) + if (!frame.data || frame.empty()) { dispatcher->mMutex.unlock(); + std::stringstream ss; + ss << "Stream " << dispatcher->mjpeg_url << " received invalid frame"; + dispatcher->log_error(ss.str()); return; } @@ -148,7 +174,9 @@ void getALPRImages(cv::VideoCapture cap, VideoDispatcher* dispatcher) catch (const std::runtime_error& error) { // Error occured while trying to gather image. Retry, don't exit. - std::cerr << "Exception happened " << error.what() << std::endl; + std::stringstream ss; + ss << "Exception happened " << error.what(); + dispatcher->log_error(ss.str()); dispatcher->mMutex.unlock(); return; } diff --git a/src/videobuffer.h b/src/videobuffer.h index d7e7f57..3959261 100644 --- a/src/videobuffer.h +++ b/src/videobuffer.h @@ -3,6 +3,7 @@ #include #include +#include #include "opencv2/highgui/highgui.hpp" @@ -47,6 +48,15 @@ class VideoDispatcher this->latestFrameNumber++; } + virtual void log_info(std::string message) + { + std::cout << message << std::endl; + } + virtual void log_error(std::string error) + { + std::cerr << error << std::endl; + } + bool active; int latestFrameNumber; @@ -69,6 +79,7 @@ class VideoBuffer virtual ~VideoBuffer(); void connect(std::string mjpeg_url, int fps); + // If a new frame is available, the function sets "frame" to it and returns the frame number // If no frames are available, or the latest has already been grabbed, returns -1. @@ -76,6 +87,10 @@ class VideoBuffer void disconnect(); + protected: + + virtual VideoDispatcher* createDispatcher(std::string mjpeg_url, int fps); + private: