转自:http://www.open-open.com/lib/view/open1455522194089.html
来自: http://blog.csdn.net/yangzhenping/article/details/50668445
如何编译和安装libevent
编译和安装步骤:
$ apt-get install git
$ git clone https://github.com/libevent/libevent.git
$ cd libevent
$ apt-get install cmake
$ apt-get install libssl-dev
$ mkdir build && cd build
$ cmake .. # Default to Unix Makefiles.
$ make
$ make verify # (optional)
$ make install
一些学习文档:
Fast portable non-blocking network programming with Libevent:http://www.wangafu.net/~nickm/libevent-book/
libevent-examples: https://github.com/jasonish/libevent-examples
multi-thread libevent: https://sourceforge.net/projects/libevent-thread/
针对第一个例子:
http://www.wangafu.net/~nickm/libevent-book/Ref1_libsetup.html
le.c内容:
#include <event2/event.h>
#include <stdio.h>
#define EVENT_LOG_DEBUG 0
#define EVENT_LOG_MSG 1
#define EVENT_LOG_WARN 2
#define EVENT_LOG_ERR 3
/* Deprecated; see note at the end of this section */
#define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG
#define _EVENT_LOG_MSG EVENT_LOG_MSG
#define _EVENT_LOG_WARN EVENT_LOG_WARN
#define _EVENT_LOG_ERR EVENT_LOG_ERR
typedef void (*event_log_cb)(int severity, const char *msg);
void event_set_log_callback(event_log_cb cb);
static void discard_cb(int severity, const char *msg)
{
/* This callback does nothing. */
}
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"; break;
case _EVENT_LOG_MSG: s = "msg"; break;
case _EVENT_LOG_WARN: s = "warn"; break;
case _EVENT_LOG_ERR: s = "error"; break;
default: s = "?"; break; /* never reached */
}
fprintf(logfile, "[%s] %s\n", s, msg);
}
/* Turn off all logging from Libevent. */
void suppress_logging(void)
{
event_set_log_callback(discard_cb);
}
/* Redirect all Libevent log messages to the C stdio file 'f'. */
void set_logfile(FILE *f)
{
logfile = f;
event_set_log_callback(write_to_file_cb);
}
int main(int argc, char **argv)
{
FILE *fp=fopen("/tmp/test.txt", "w+");
set_logfile(fp);
fclose(fp);
return 0;
}
$ gcc le.c -o le -levent
$ ./le
没有输出任何文本到/tmp/test.txt,因为main函数中没有调用log,当然文中建议我们不要直接使用用户提供的event_log_cb回调函数,因为这样做是不安全的。
有人翻译了这本文档,可以参考下:
http://popozhu.github.io/page/7/
http://popozhu.github.io/page/6/