本文共 2380 字,大约阅读时间需要 7 分钟。
Setting up the Libevent library
#include#include static void discard_cb(int severity, const char *msg) { /* 这个回调函数会丢弃所有Libevent的日志输出 */}static FILE *logfile = NULL;static void write_to_file_cb(int severity, const char *msg) { const char *s; if (!logfile) return; switch (severity) { case _EVENT_LOG_DEBUG: s = "debug"; case _EVENT_LOG_MSG: s = "msg"; case _EVENT_LOG_WARN: s = "warn"; case _EVENT_LOG_ERR: s = "error"; default: s = "?"; } fprintf(logfile, "[%s] %s\n", s, msg);}void suppress_logging(void) { event_set_log_callback(discard_cb);}void set_logfile(FILE *f) { logfile = f; event_set_log_callback(write_to_file_cb);}
typedef void (*event_fatal_cb)(int err);void event_set_fatal_callback(event_fatal_cb cb);
void * (*malloc_fn)(size_t sz);void * (*realloc_fn)(void *ptr, size_t sz);void (*free_fn)(void *ptr);event_set_mem_functions(malloc_fn, realloc_fn, free_fn);
#define EVTHREAD_WRITE 0x04#define EVTHREAD_READ 0x08#define EVTHREAD_TRY 0x10#define EVTHREAD_LOCKTYPE_RECURSIVE 1#define EVTHREAD_LOCKTYPE_READWRITE 2#define EVTHREAD_LOCK_API_VERSION 1struct evthread_lock_callbacks { int lock_api_version; unsigned supported_locktypes; void* (*alloc)(unsigned locktype); void (*free)(void *lock, unsigned locktype); int (*lock)(unsigned mode, void *lock); int (*unlock)(unsigned mode, void *lock);};int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cb);void evthread_set_id_callback(unsigned long (*id_fn)(void));struct evthread_condition_callbacks { int condition_api_version; void* (*alloc_condition)(unsigned condtype); void (*free_condition)(void *cond); int (*signal_condition)(void *cond, int broadcast); int (*wait_condition)(void *cond, void *lock, const struct timeval *timeout);};int evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cb); 通过以上方法,可以对Libevent库进行定制化设置,实现更高级别的功能控制。
转载地址:http://hotfk.baihongyu.com/