35 template <
int Level>
char const * levelStr();
36 template <>
char const * levelStr<LOG_DEBUG>() {
return "DBG"; }
37 template <>
char const * levelStr<LOG_INFO>() {
return "INF"; }
38 template <>
char const * levelStr<LOG_ERR>() {
return "ERR"; }
39 template <>
char const * levelStr<LOG_CRIT>() {
return "FTL"; }
62#ifdef JEVOIS_USE_SYNC_LOG
66 std::mutex logOutputMutex;
70{
LERROR(
"Cannot set Engine for logs when JeVois has been compiled with -D JEVOIS_USE_SYNC_LOG -- IGNORED"); }
72{
LINFO(
"Terminating Log service"); }
85 LogCore() : itsBuffer(10000), itsRunning(true)
86#ifdef JEVOIS_LOG_TO_FILE
87 , itsStream(
"jevois.log")
100 itsBuffer.push(
"Terminating Log service");
110 std::string msg = itsBuffer.pop();
111#ifdef JEVOIS_LOG_TO_FILE
112 itsStream << msg << std::endl;
114#ifdef JEVOIS_PLATFORM
118 std::cerr << msg <<
'\r' << std::endl;
120 std::cerr << msg << std::endl;
123 if (itsEngine) itsEngine->sendSerial(msg,
true);
131 LINFO(
"Terminating log facility.");
135 volatile bool itsRunning;
136 std::future<void> itsRunFuture;
137#ifdef JEVOIS_LOG_TO_FILE
138 std::ofstream itsStream;
154 std::string
const fn(fullFileName);
155 size_t const lastSlashPos = fn.rfind(
'/');
156 size_t const lastDotPos = fn.rfind(
'.');
157 std::string
const partialFileName = fn.substr(lastSlashPos+1, lastDotPos-lastSlashPos-1);
160 itsLogStream << levelStr<Level>() <<
' ' << partialFileName <<
"::" << functionName <<
": ";
164#ifdef JEVOIS_USE_SYNC_LOG
169 std::lock_guard<std::mutex> guard(jevois::logOutputMutex);
170 std::string
const msg = itsLogStream.str();
171 std::cerr << msg << std::endl;
172 if (itsOutStr) *itsOutStr = msg;
180 std::string
const msg = itsLogStream.str();
181 LogCore::instance().itsBuffer.push(msg);
182 if (itsOutStr) *itsOutStr = msg;
190 itsLogStream << static_cast<int>(out_item);
198 itsLogStream << static_cast<int>(out_item);
206 if (prefix.empty() ==
false) pfx = prefix +
": ";
211 catch (std::exception
const & e)
213 LERROR(pfx <<
"Passing through std::exception:");
214 std::vector<std::string> lines =
jevois::split(e.what(),
"\\n");
215 for (std::string
const & li : lines)
LERROR(li);
219 catch (boost::python::error_already_set & e)
221 LERROR(pfx <<
"Received exception from the Python interpreter:");
224 for (std::string
const & li : lines)
LERROR(li);
230 LERROR(pfx <<
"Passing through unknown exception");
239 if (prefix.empty() ==
false) pfx = prefix +
": ";
241 std::vector<std::string> retvec;
246 catch (std::exception
const & e)
248 retvec.emplace_back(pfx +
"Caught std::exception:");
249 std::vector<std::string> lines =
jevois::split(e.what(),
"\\n");
250 for (std::string
const & li : lines) retvec.emplace_back(li);
253 catch (boost::python::error_already_set & e)
255 retvec.emplace_back(pfx +
"Caught exception from the Python interpreter:");
258 for (std::string
const & li : lines) retvec.emplace_back(li);
263 retvec.emplace_back(pfx +
"Caught unknown exception");
268 for (std::string & m : retvec) {
LERROR(m); ret += m +
"\n"; }
275 std::string
const & strval)
277 LERROR(
"Parameter " << descriptor <<
": Provided value [" << strval <<
"] rejected by callback:");
282 catch (std::exception
const & e)
284 throw std::runtime_error(e.what());
287 catch (boost::python::error_already_set & e)
289 LERROR(
"Python exception:");
295 throw std::runtime_error(
"Caught unknown exception");
302 if (videoerrimg.
valid() ==
false) {
LERROR(
"Cannot draw in empty image -- IGNORED");
return; }
319 if (videoerrimg.
width <= 352 || videoerrimg.
height <= 240)
321 else if (videoerrimg.
width <= 640 || videoerrimg.
height <= 480)
326 std::vector<std::string> lines =
jevois::split(errmsg,
"\\n");
327 for (std::string & m : lines)
330 unsigned int nchar = (videoerrimg.
width - 6) / fw;
331 while (m.size() > nchar)
335 m = m.substr(nchar, m.npos);
345jevois::timed_lock_guard::timed_lock_guard(std::timed_mutex & mtx,
char const * file,
char const * func) :
348 if (itsMutex.try_lock_for(std::chrono::seconds(5)) ==
false)
351 throw std::runtime_error(
"FATAL DEADLOCK ERROR");
356jevois::timed_lock_guard::~timed_lock_guard()
357{ itsMutex.unlock(); }
#define JEVOIS_WAIT_GET_FUTURE(f)
Wait for a future to become ready for 5 seconds, get(), warn and ignore exception,...
Thread-safe synchronized producer/consumer queue.
JeVois processing engine - gets images from camera sensor, processes them, and sends results over USB...
Log< Level > & operator<<(T const &out_item)
Overloaded stream input operator for any type that has operator<< defined for ostream.
~Log()
Close the Log, outputting the aggregated message.
Log(char const *fullFileName, char const *functionName, std::string *outstr=nullptr)
Construct a new Log, adding a prefix to the log stream.
A raw image as coming from a V4L2 Camera and/or being sent out to a USB Gadget.
unsigned int fmt
Pixel format as a V4L2_PIX_FMT_XXX.
unsigned int width
Image width in pixels.
unsigned int height
Image height in pixels.
void clear()
Clear the pixels to all black.
bool valid() const
Check whether the image has a valid pixel buffer.
A generic singleton class to enforce a single instance of an object.
int logLevel
Current log level.
int traceLevel
Current trace level.
void logSetEngine(Engine *e)
Set an Engine so that all log messages will be forwarded to its serial ports.
std::string warnAndIgnoreException(std::string const &prefix="")
Convenience function to catch an exception, issue some LERROR (depending on type),...
void warnAndRethrowParamCallbackException(std::string const &descriptor, std::string const &strval)
Convenience function for parameter callback exceptions.
void warnAndRethrowException(std::string const &prefix="")
Convenience function to catch an exception, issue some LERROR (depending on type),...
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
#define LINFO(msg)
Convenience macro for users to print out console or syslog messages, INFO level.
std::string getPythonExceptionString(boost::python::error_already_set &)
Python exception translation to string so we can print the traceback to our serlog stream.
void writeText(RawImage &img, std::string const &txt, int x, int y, unsigned int col, Font font=Font6x10)
Write some text in an image.
void drawDisk(RawImage &img, int x, int y, unsigned int rad, unsigned int col)
Draw a disk in a YUYV image.
Font
Available fonts for writeText()
void drawLine(RawImage &img, int x1, int y1, int x2, int y2, unsigned int thick, unsigned int col)
Draw a line in a YUYV image.
unsigned int whiteColor(unsigned int fcc)
Return a value that corresponds to white for the given video format.
std::vector< std::string > split(std::string const &input, std::string const ®ex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async_little(Function &&f, Args &&... args)
Async execution using a thread pool.
Main namespace for all JeVois classes and functions.
void drawErrorImage(std::string const &errmsg, RawImage &videoerrimg)
Display an error message into a RawImage.
void logEnd()
Terminate log service.