如何編譯和安裝libevent
來自: 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; }</pre>
$ 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/
??