dim/sdk/c_cxx/dim_sdk.h

87 lines
2.3 KiB
C
Raw Normal View History

2024-06-14 15:22:20 +00:00
#ifndef _H_PLUG
#define _H_PLUG
#include <stdio.h>
#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;
2024-06-14 15:22:20 +00:00
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;
2024-06-20 20:43:16 +00:00
typedef struct plug_ctx_t {
char* config_dir;
plug_funcs_ctx_t funcs;
char* buf;
2024-06-20 20:43:16 +00:00
} 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) \
2024-06-14 15:22:20 +00:00
static plug_info_t PLUG_INFO_VAR = { .name=(_name), .version=(_version), .license=(_license)}; \
void* plug_get_info() { \
return &PLUG_INFO_VAR; \
2024-06-14 15:22:20 +00:00
}
#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)
2024-06-14 15:22:20 +00:00
#ifdef PLUG_IMPL
static plug_ctx_t CTX = {0};
2024-06-14 15:22:20 +00:00
void setup_ctx(plug_ctx_t* ctx) {
CTX = *ctx;
}
2024-06-14 15:22:20 +00:00
void _log(char* module, log_level_t level, char* s) {
(CTX.funcs.log)(module, level, s);
}
2024-06-14 15:22:20 +00:00
#endif
#endif