Elog Tutorial: Add some Log (using)

You might not use the log directly. The reson is simple:

  • It is designed to be replaced by an other log library.

This permit you to use custom log library just replacing Macro and basic functions

Declaring the list of macro

debug.hpp

#pragma once
#include <elog/log.hpp>
namespace appl {
int32_t getLogId();
};
#define APPL_BASE(info,data) ELOG_BASE(appl::getLogId(),info,data)
#define APPL_PRINT(data) APPL_BASE(-1, data)
#define APPL_CRITICAL(data) APPL_BASE(1, data)
#define APPL_ERROR(data) APPL_BASE(2, data)
#define APPL_WARNING(data) APPL_BASE(3, data)
#ifdef DEBUG
#define APPL_INFO(data) APPL_BASE(4, data)
#define APPL_DEBUG(data) APPL_BASE(5, data)
#define APPL_VERBOSE(data) APPL_BASE(6, data)
#define APPL_TODO(data) APPL_BASE(4, "TODO : " << data)
#else
#define APPL_INFO(data) do { } while(false)
#define APPL_DEBUG(data) do { } while(false)
#define APPL_VERBOSE(data) do { } while(false)
#define APPL_TODO(data) do { } while(false)
#endif
#define APPL_ASSERT(cond,data) \
do { \
if (!(cond)) { \
APPL_CRITICAL(data); \
assert(!#cond); \
} \
} while (0)

debug.cpp

#include "debug.hpp"
int32_t appl::getLogId() {
static int32_t g_val = elog::registerInstance("your application name");
return g_val;
}
  • on your main application:
#include "debug.hpp"
#include <elog/elog.hpp>
int main(int _argc, const char *_argv[]) {
// if you use etk/ewol/gale, elog init in contain in it.
elog::init(_argc, _argv);

Using it

You just need to call the macro whe you want to add debug log:

APPL_VERBOSE("VERBOSE display");
APPL_DEBUG("DEBUG display");
APPL_INFO("INFO display");
APPL_WARNING("WARNING display");
APPL_ERROR("ERROR display");
APPL_PRINT("PRINT display");
//APPL_CRITICAL("CRITICAL display"); // Disable critical because it create an assert ...

Specification of logs

  • *_CRITICAL(***); This will log the data and asert just after (display backtrace if possible)
  • *_PRINT(***); display on console (can not be removed with the log-level)

Log in an external logger

You must specify an external function that is receiving the logs:

static void myExternalLogCallback(const char* _libName, enum elog::level _level, int32_t _ligne, const char* _funcName, const char* _log) {

Now you must connect it on the elog backend:

// Set a callback:
elog::setCallbackLog(&myExternalLogCallback);
elog::setCallbackLog(&myExternalLogCallback);

The full code of the callback:

static void myExternalLogCallback(const char* _libName, enum elog::level _level, int32_t _ligne, const char* _funcName, const char* _log) {
switch(_level) {
default:
std::cout << "[?] ";
break;
std::cout << "[P] ";
break;
std::cout << "[C] ";
break;
std::cout << "[E] ";
break;
std::cout << "[W] ";
break;
std::cout << "[I] ";
break;
std::cout << "[D] ";
break;
std::cout << "[V] ";
break;
}
std::cout << _libName << " (" << _ligne << ") " << _funcName << " | " << _log << std::endl;
}

you can test the program:

lutin -C -P -mdebug elog-sample?build?run:--elog-level=2