#ifndef _H_PLUG #define _H_PLUG #include #define BUF_SZ 2048 #ifdef _WIN32 #define PATH_SEP "\\" #else #define PATH_SEP "/" #endif typedef enum log_level_t { ERROR=1, WARN=2, INFO=3, DEBUG=4, } log_level_t; typedef struct plug_info_t { char* name; char* version; char* license; } plug_info_t; typedef struct plug_funcs_ctx_t { void (*log)(char* module, log_level_t level, char* s); } plug_funcs_ctx_t; typedef struct plug_ctx_t { char* config_dir; plug_funcs_ctx_t funcs; char* buf; } plug_ctx_t; typedef struct plug_msg_t { char* name; char* value; } plug_msg_t; // Functions defs // User defined funcs int plug_init(plug_ctx_t*); // Loads when DIM initialises void* plug_pre_reload(); // Return a pointer to save state int plug_post_reload(void*); // returns the same pointer after reload int plug_on_msg(plug_msg_t*); // Write the message to `buf` with max `size` characters int plug_free(); // Free everything before being killed // Lib funcs void setup_ctx(plug_ctx_t* ctx); void _log(char* module, log_level_t level, char* s); // Macros #define PLUG_INFO(_name, _version, _license) \ static plug_info_t PLUG_INFO_VAR = { .name=(_name), .version=(_version), .license=(_license)}; \ void* plug_get_info() { \ return &PLUG_INFO_VAR; \ } #define log(level, ...) do { \ char* buf = (char*)malloc(BUF_SZ*sizeof(char)); \ char* mod = (char*)malloc(BUF_SZ*sizeof(char)); \ snprintf(mod, BUF_SZ*sizeof(char), \ "(plug) %s/%s", PLUG_INFO_VAR.name, __FILE__); \ snprintf(buf, BUF_SZ*sizeof(char), __VA_ARGS__); \ _log(mod, level, buf); \ free(buf); \ free(mod); \ } while (0) #ifdef PLUG_IMPL static plug_ctx_t CTX = {0}; void setup_ctx(plug_ctx_t* ctx) { CTX = *ctx; } void _log(char* module, log_level_t level, char* s) { (CTX.funcs.log)(module, level, s); } #endif #endif