dnsjit.core.log(3) | Library Functions Manual | dnsjit.core.log(3) |
dnsjit.core.log - Core logging facility
local log = require("dnsjit.core.log")
log.enable("all")
log.disable("debug")
local example = require("example") -- Example as below
example.log():enable("all")
example.log():disable("debug")
local example = require("example") -- Example as below
local obj = example.new()
obj:log():enable("all")
obj:log():disable("debug")
NOTE naming of variables and module only globals are required to exactly as described in order for the macros to work; self is the pointer to the object instance, self->_log is the object instance logging configuration struct, _log is the module logging configuration struct.
Include logging:
#include "core/log.h"
Add the logging struct to the module struct:
typedef struct example {
core_log_t _log;
...
} example_t;
Add a module logging configuration and a struct default:
static core_log_t _log = LOG_T_INIT("example");
static example_t _defaults = {
LOG_T_INIT_OBJ("example"),
...
};
Use new/free and/or init/destroy functions (depends if you create
the object in Lua or not):
example_t* example_new() {
example_t* self = calloc(1, sizeof(example_t));
*self = _defaults;
ldebug("new()");
return self;
}
void example_free(example_t* self) {
ldebug("free()");
free(self);
}
int example_init(example_t* self) {
*self = _defaults;
ldebug("init()");
return 0;
}
void example_destroy(example_t* self) {
ldebug("destroy()");
...
}
In the Lua part of the C module you need to create a function that returns either the object instance Log or the modules Log.
Add C function to get module only Log:
core_log_t* example_log() {
return &_log;
}
For the structures metatable add the following function:
local ffi = require("ffi")
local C = ffi.C
function Example:log()
if self == nil then
return C.example_log()
end
return self._log
end
local log = require("dnsjit.core.log")
local ffi = require("ffi")
local C = ffi.C
local Example = {}
local module_log = log.new("example")
function Example.new()
local self = setmetatable({
_log = log.new("example", module_log),
}, { __index = Example })
self._log:debug("new()")
return self
end
function Example:log()
if self == nil then
return module_log
end
return self._log
end
Core logging facility used by all modules.
Jerry Lundström (DNS-OARC), Tomáš Křížek (CZ.NIC), Petr Špaček (ISC)
Maintained by DNS-OARC
For issues and feature requests please use:
For question and help please use:
1.3.0 | dnsjit |