void initLibraries() { #ifdef _DEBUG event_enable_debug_mode(); // may cause memory leak #endif event_set_fatal_callback(libeventError); #ifdef _WIN32 #ifdef MEM_DEBUG _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); #endif WSADATA data; int err = WSAStartup(0, &data); err = WSAStartup(data.wVersion, &data); assert(err == 0); #endif #ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED evthread_use_windows_threads(); // may cause memory leak #endif #ifdef EVTHREAD_USE_PTHREADS_IMPLEMENTED evthread_use_pthreads(); #endif srand(static_cast<unsigned int>(time(NULL))); // stdlib :) }
void LibeventLog::turnOn(const char *filepath) { if (logfile != NULL) { event_set_log_callback(write_to_file_cv); return; } logfile = fopen(filepath, "a+"); if (logfile == NULL) { event_set_log_callback(discard_log); } else { event_set_log_callback(write_to_file_cv); } event_set_fatal_callback(write_to_file_fatal_cb); }
void LinkScheduler::run() { LOG(INFO, "start"); evthread_use_pthreads(); event_set_log_callback(LinkScheduler::event_log_callback); event_set_fatal_callback(LinkScheduler::event_fatal_callback); _base = event_base_new(); _printStateEvent = evtimer_new(_base, on_print_state, this); struct timeval t = {Conf::instance()->schedulerPrintStateInterval, 0 }; evtimer_add(_printStateEvent, &t); event_base_dispatch(_base); abort(); }
CNetInit() { #if defined _PLATFORM_WINDOWS_ WSADATA WSAData; WSAStartup(0x101, &WSAData); #elif defined _PLATFORM_LINUX_ signal(SIGPIPE, SIG_IGN); #endif event_set_log_callback(write_event_log); event_set_fatal_callback(write_event_fatal); // 内存处理 //#ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED // event_set_mem_functions //#endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED // 2.1.1 版本后才有 //event_enable_debug_logging(); // 检查支持哪些后端方式 //event_get_supported_methods }
static void test_evutil_log(void *ptr) { evutil_socket_t fd = -1; char buf[128]; event_set_log_callback(logfn); event_set_fatal_callback(fatalfn); #define RESET() do { \ logsev = 0; \ if (logmsg) free(logmsg); \ logmsg = NULL; \ } while (0) #define LOGEQ(sev,msg) do { \ tt_int_op(logsev,==,sev); \ tt_assert(logmsg != NULL); \ tt_str_op(logmsg,==,msg); \ } while (0) #ifdef CAN_CHECK_ERR /* We need to disable these tests for now. Previously, the logging * module didn't enforce the requirement that a fatal callback * actually exit. Now, it exits no matter what, so if we wan to * reinstate these tests, we'll need to fork for each one. */ check_error_logging(errx_fn, 2, EVENT_LOG_ERR, "Fatal error; too many kumquats (5)"); RESET(); #endif event_warnx("Far too many %s (%d)", "wombats", 99); LOGEQ(EVENT_LOG_WARN, "Far too many wombats (99)"); RESET(); event_msgx("Connecting lime to coconut"); LOGEQ(EVENT_LOG_MSG, "Connecting lime to coconut"); RESET(); event_debug(("A millisecond passed! We should log that!")); #ifdef USE_DEBUG LOGEQ(EVENT_LOG_DEBUG, "A millisecond passed! We should log that!"); #else tt_int_op(logsev,==,0); tt_ptr_op(logmsg,==,NULL); #endif RESET(); /* Try with an errno. */ errno = ENOENT; event_warn("Couldn't open %s", "/bad/file"); evutil_snprintf(buf, sizeof(buf), "Couldn't open /bad/file: %s",strerror(ENOENT)); LOGEQ(EVENT_LOG_WARN,buf); RESET(); #ifdef CAN_CHECK_ERR evutil_snprintf(buf, sizeof(buf), "Couldn't open /very/bad/file: %s",strerror(ENOENT)); check_error_logging(err_fn, 5, EVENT_LOG_ERR, buf); RESET(); #endif /* Try with a socket errno. */ fd = socket(AF_INET, SOCK_STREAM, 0); #ifdef _WIN32 evutil_snprintf(buf, sizeof(buf), "Unhappy socket: %s", evutil_socket_error_to_string(WSAEWOULDBLOCK)); EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); #else evutil_snprintf(buf, sizeof(buf), "Unhappy socket: %s", strerror(EAGAIN)); errno = EAGAIN; #endif event_sock_warn(fd, "Unhappy socket"); LOGEQ(EVENT_LOG_WARN, buf); RESET(); #ifdef CAN_CHECK_ERR check_error_logging(sock_err_fn, 20, EVENT_LOG_ERR, buf); RESET(); #endif #undef RESET #undef LOGEQ end: if (logmsg) free(logmsg); if (fd >= 0) evutil_closesocket(fd); }
static void test_evutil_log(void *ptr) { evutil_socket_t fd = -1; char buf[128]; event_set_log_callback(logfn); event_set_fatal_callback(fatalfn); #define RESET() do { \ logsev = exited = exitcode = 0; \ if (logmsg) free(logmsg); \ logmsg = NULL; \ } while (0) #define LOGEQ(sev,msg) do { \ tt_int_op(logsev,==,sev); \ tt_assert(logmsg != NULL); \ tt_str_op(logmsg,==,msg); \ } while (0) event_errx(2, "Fatal error; too many kumquats (%d)", 5); LOGEQ(_EVENT_LOG_ERR, "Fatal error; too many kumquats (5)"); tt_int_op(exitcode,==,2); RESET(); event_warnx("Far too many %s (%d)", "wombats", 99); LOGEQ(_EVENT_LOG_WARN, "Far too many wombats (99)"); tt_int_op(exited,==,0); RESET(); event_msgx("Connecting lime to coconut"); LOGEQ(_EVENT_LOG_MSG, "Connecting lime to coconut"); tt_int_op(exited,==,0); RESET(); event_debug(("A millisecond passed! We should log that!")); #ifdef USE_DEBUG LOGEQ(_EVENT_LOG_DEBUG, "A millisecond passed! We should log that!"); #else tt_int_op(logsev,==,0); tt_ptr_op(logmsg,==,NULL); #endif RESET(); /* Try with an errno. */ errno = ENOENT; event_warn("Couldn't open %s", "/bad/file"); evutil_snprintf(buf, sizeof(buf), "Couldn't open /bad/file: %s",strerror(ENOENT)); LOGEQ(_EVENT_LOG_WARN,buf); tt_int_op(exited, ==, 0); RESET(); errno = ENOENT; event_err(5,"Couldn't open %s", "/very/bad/file"); evutil_snprintf(buf, sizeof(buf), "Couldn't open /very/bad/file: %s",strerror(ENOENT)); LOGEQ(_EVENT_LOG_ERR,buf); tt_int_op(exitcode, ==, 5); RESET(); /* Try with a socket errno. */ fd = socket(AF_INET, SOCK_STREAM, 0); #ifdef WIN32 evutil_snprintf(buf, sizeof(buf), "Unhappy socket: %s", evutil_socket_error_to_string(WSAEWOULDBLOCK)); EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); #else evutil_snprintf(buf, sizeof(buf), "Unhappy socket: %s", strerror(EAGAIN)); errno = EAGAIN; #endif event_sock_warn(fd, "Unhappy socket"); LOGEQ(_EVENT_LOG_WARN, buf); tt_int_op(exited,==,0); RESET(); #ifdef WIN32 EVUTIL_SET_SOCKET_ERROR(WSAEWOULDBLOCK); #else errno = EAGAIN; #endif event_sock_err(200, fd, "Unhappy socket"); LOGEQ(_EVENT_LOG_ERR, buf); tt_int_op(exitcode,==,200); RESET(); #undef RESET #undef LOGEQ end: if (logmsg) free(logmsg); if (fd >= 0) EVUTIL_CLOSESOCKET(fd); }
int rproxy_start(idevice_t device, rproxy_client_t * client) { struct rproxy_client_private * new_client = NULL; idevice_connection_t control_connection = NULL; control_thread_context_t * thread_ctx = NULL; struct event_base * ev_base = NULL; #ifdef WIN32 /* Initialize Winsock */ WSADATA wsaData = { 0 }; if (0 != WSAStartup(MAKEWORD(2, 2), &wsaData)) { error("ERROR: Failed to initialize Winsock\n"); return -1; } #endif /* Initialize libevent */ event_set_fatal_callback(libevent_fatal_cb); event_set_log_callback(libevent_log_cb); #ifdef WIN32 if (0 != evthread_use_windows_threads()) { error("ERROR: Failed to initialize libevent's threading support\n"); goto cleanup; } #else if (0 != evthread_use_pthreads()) { error("ERROR: Failed to initialize libevent's threading support\n"); goto cleanup; } #endif #ifdef _DEBUG event_enable_debug_mode(); #endif /* Create a new client struct */ new_client = (rproxy_client_private *)calloc(1, sizeof(rproxy_client_private)); if (NULL == new_client) { error("ERROR: Out of memory\n"); goto cleanup; } /* Create an event base */ new_client->ev_base = event_base_new(); if (NULL == new_client->ev_base) { error("ERROR: Failed to initialize libevent\n"); goto cleanup; } /* Connect to the proxy service */ uint16_t conn_port = 0; int i = 0; for (i = 1; i <= CONTROL_CONNTECTION_ATTEMPTS; i++) { if (0 == rproxy_connect_control_service(device, &control_connection, &conn_port)) { break; } if (CONTROL_CONNTECTION_ATTEMPTS == i) { error("ERROR: Failed to initialize reverse proxy connection\n"); goto cleanup; } sleep(CONTROL_CONNTECTION_ATTEMPTS_INTERVAL); debug("Failed to connect to the proxy, retrying...\n"); } /* Start a new thread for the reverse proxy event loop */ thread_ctx = (control_thread_context_t *)calloc(1, sizeof(control_thread_context_t)); if (NULL == thread_ctx) { error("ERROR: Out of memory\n"); goto cleanup; } thread_ctx->device = device; thread_ctx->control_connection = control_connection; thread_ctx->connection_port = (uint16_t)conn_port; thread_ctx->ev_base = new_client->ev_base; if (0 != thread_create(&(new_client->thread), rproxy_thread_proc, thread_ctx)) { error("ERROR: Failed to start the reverse proxy thread\n"); goto cleanup; } info("Reverse proxy is running\n"); *client = new_client; return 0; cleanup: if (control_connection) { idevice_disconnect(control_connection); } if (new_client) { if (new_client->ev_base) { event_base_free(new_client->ev_base); } free(new_client); } if (thread_ctx) { free(thread_ctx); } #ifdef WIN32 WSACleanup(); #endif return -1; }