If you don't want to use an existing logging lib, you can try to use macros. I would recommend to provide your own lib and to make it available with macros with printf like formatting mechanism.
I've done something like that in the past. The macro called a log object which encapsulate the real logging which was possible to extend via plugin.
But I think that something similar is already done by Log4xxx so maybe it is good to look at it.
Here is an proposal (sorry no time to test, I hope it works)
header:
#ifdef _MYAPI_IMPL
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
class MY_API Log
{
public:
enum Level {Error, Warning, Info, Debug};
Log(Level level, const char* file, int line );
void operator()(const char* Format, ... );
private:
const char* m_file;
Level m_level;
int m_line;
};
#define __LOG(lvl) (Log(lvl, __FILE__, __LINE__ ))
#define LOG_ERR __LOG(Log::Error)
#define LOG_WRN __LOG(Log::Warning)
#define LOG_INF __LOG(Log::Info)
#define LOG_DBG __LOG(Log::Debug)
class My_API Logger
{
public:
virtual void log(const char* message)=0;
};
class MY_API LoggerManager
{
private:
static LoggerManager* s_inst;
LoggerManager() {}
virtual ~LoggerManager() {}
public:
static LoggerManager* Instance();
static void Clean();
addLogger(Logger* newLogger, Log::Level minlevel = Log::Info);
log(const char* file, int line, Log::Level level, const char* message);
};
Cpp:
Log::Log(Level level, const char* file, int line)
: m_file(file), m_level(level), m_line(line)
{
}
void Log::operator()(const char* format, ... )
{
va_list va;
va_start(va, format);
char message[LENGTH+1]={0};
_vsnprintf(message, LENGTH, format, va);
va_end(va);
LoggerManager::Instance()->log(m_file, m_line, m_level, message);
};
Other libs and exe should be able to call like this. They just have to include the .h and link with the lib.
LOG_INF("Hello %s!", "world");
Update: I've added the explanation needed for the logging mechanism. One way is to use a singleton and to provide a interface to be subclassed for the actual logging.
The benefit of using macros is that it gives you the ability to get the location of the log which may be very interesting infomation in some cases.
It is also possible to turn the macros into regular printf when you don't want to implement all the logging mechanism.