-
Notifications
You must be signed in to change notification settings - Fork 87
Description
llogging.h
https://github.com/CMA-ES/libcmaes/blob/master/include/libcmaes/llogging.h
Looks like this:
#ifndef LLOGGING_H
#define LLOGGING_H
#include <libcmaes/libcmaes_config.h>
#ifdef HAVE_GLOG // HAVE_LIB_GLOG
#include <glog/logging.h>
#else
#include <iostream>
namespace libcmaes
{
static std::string INFO="INFO";
static std::string WARNING="WARNING";
static std::string ERROR="ERROR";
static std::string FATAL="FATAL";
static std::ostream nullstream(0);
inline std::ostream& LOG(const std::string &severity,std::ostream &out=std::cout)
{
out << severity << " - ";
return out;
}
inline std::ostream& LOG_IF(const std::string &severity,const bool &condition,std::ostream &out=std::cout)
{
if (condition)
return LOG(severity,out);
else return nullstream;
}
}
#endif
#endif
I am writing here as I currently connect libcmaes to R, see #252.
(Probably a normal usecase for such a lib)
The problem is: I have to redirect output. I cannot have code that directly prints to stderr / stdout.
Otherwise my package gets "flagged" on CRAN (package distribution server for R).
I think, a general and good pattern here is to allow ppl like me, to hook in their own code for "LOG" -- without touching your code.
Side comment: I also found (at least) one instance in cmaparamters.cc where you dont go thru your own logger funs, but directly print to cerr:
std::cerr << "[Warning]: set_vd on non VD algorithm " << this->_algo << ". Not activating VD update\n";
If you currently dont have a lot of time: Its not the most pressing isse for me, I can manually edit your code in my vendored version.
But: not ideal IMHO. OTOH that is really easy to change on your side IMHO, and I also could do a PR.
Probably something like this would be enough
#ifndef LOG
// Default to libcmaes' own LOG(...) if client didn't override
#define LOG(sev) ::libcmaes::LOG(sev)
#endif
#ifndef LOG_IF
// Derive LOG_IF from LOG so clients don't need to provide a second macro
#define LOG_IF(sev, cond) ((cond) ? LOG(sev) : ::libcmaes::nullstream)
#endif
Clients can then do this in an extra header:
#pragma once
#define LOG(sev) bla
and in cmake
DCMAKE_CXX_FLAGS="-include /abs/path/my_log_provider.h"
?