verbose message about disconnecting client

This commit is contained in:
Devaev Maxim
2020-06-28 13:15:24 +03:00
parent f52d090f9b
commit 98499b6604
2 changed files with 44 additions and 8 deletions

View File

@@ -85,6 +85,8 @@ static void _http_queue_send_stream(struct http_server_t *server, bool stream_up
static bool _expose_new_picture_unsafe(struct http_server_t *server); static bool _expose_new_picture_unsafe(struct http_server_t *server);
static bool _expose_blank_picture(struct http_server_t *server); static bool _expose_blank_picture(struct http_server_t *server);
static void _format_bufferevent_reason(short what, char *reason);
struct http_server_t *http_server_init(struct stream_t *stream) { struct http_server_t *http_server_init(struct stream_t *stream) {
struct http_server_runtime_t *run; struct http_server_runtime_t *run;
@@ -533,7 +535,7 @@ static void _http_callback_stream(struct evhttp_request *request, void *v_server
} }
evhttp_connection_get_peer(conn, &client_addr, &client_port); evhttp_connection_get_peer(conn, &client_addr, &client_port);
LOG_INFO("HTTP: Registered the new stream client: [%s]:%u, id=%s; clients now: %u", LOG_INFO("HTTP: Registered client: [%s]:%u, id=%s; clients now: %u",
client_addr, client_port, client->id, server->run->stream_clients_count); client_addr, client_port, client->id, server->run->stream_clients_count);
buf_event = evhttp_connection_get_bufferevent(conn); buf_event = evhttp_connection_get_bufferevent(conn);
@@ -677,6 +679,9 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
struct evhttp_connection *conn; struct evhttp_connection *conn;
char *client_addr = "???"; char *client_addr = "???";
unsigned short client_port = 0; unsigned short client_port = 0;
char reason[2048] = {0};
_format_bufferevent_reason(what, reason);
# define RUN(_next) client->server->run->_next # define RUN(_next) client->server->run->_next
@@ -697,8 +702,9 @@ static void _http_callback_stream_error(UNUSED struct bufferevent *buf_event, UN
if (conn) { if (conn) {
evhttp_connection_get_peer(conn, &client_addr, &client_port); evhttp_connection_get_peer(conn, &client_addr, &client_port);
} }
LOG_INFO("HTTP: Disconnected the stream client: [%s]:%u; clients now: %u",
client_addr, client_port, RUN(stream_clients_count)); LOG_INFO("HTTP: Disconnected client: [%s]:%u, id=%s, %s; clients now: %u",
client_addr, client_port, client->id, reason, RUN(stream_clients_count));
if (conn) { if (conn) {
evhttp_connection_free(conn); evhttp_connection_free(conn);
} }
@@ -910,3 +916,33 @@ static bool _expose_blank_picture(struct http_server_t *server) {
# undef EXPOSED # undef EXPOSED
} }
static void _format_bufferevent_reason(short what, char *reason) {
char perror_buf[1024] = {0};
char *perror_ptr = errno_to_string(EVUTIL_SOCKET_ERROR(), perror_buf, 1024); // evutil_socket_error_to_string() is not thread-sage
bool first = true;
strcat(reason, perror_ptr);
strcat(reason, " (");
# define FILL_REASON(_bev, _name) { \
if (what & _bev) { \
if (first) { \
first = false; \
} else { \
strcat(reason, ","); \
} \
strcat(reason, _name); \
} \
}
FILL_REASON(BEV_EVENT_READING, "reading");
FILL_REASON(BEV_EVENT_WRITING, "writing");
FILL_REASON(BEV_EVENT_ERROR, "error");
FILL_REASON(BEV_EVENT_TIMEOUT, "timeout");
FILL_REASON(BEV_EVENT_EOF, "eof"); // cppcheck-suppress unreadVariable
# undef FILL_REASON
strcat(reason, ")");
}

View File

@@ -116,7 +116,7 @@ extern pthread_mutex_t log_mutex;
#define LOG_PERROR(_msg, ...) { \ #define LOG_PERROR(_msg, ...) { \
char _perror_buf[1024] = {0}; \ char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(_perror_buf, 1024); \ char *_perror_ptr = errno_to_string(errno, _perror_buf, 1024); \
LOG_ERROR(_msg ": %s", ##__VA_ARGS__, _perror_ptr); \ LOG_ERROR(_msg ": %s", ##__VA_ARGS__, _perror_ptr); \
} }
@@ -149,7 +149,7 @@ extern pthread_mutex_t log_mutex;
#define LOG_VERBOSE_PERROR(_msg, ...) { \ #define LOG_VERBOSE_PERROR(_msg, ...) { \
if (log_level >= LOG_LEVEL_VERBOSE) { \ if (log_level >= LOG_LEVEL_VERBOSE) { \
char _perror_buf[1024] = {0}; \ char _perror_buf[1024] = {0}; \
char *_perror_ptr = errno_to_string(_perror_buf, 1024); \ char *_perror_ptr = errno_to_string(errno, _perror_buf, 1024); \
LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg ": %s", ##__VA_ARGS__, _perror_ptr); \ LOG_PRINTF(COLOR_BLUE, "VERB ", COLOR_BLUE, _msg ": %s", ##__VA_ARGS__, _perror_ptr); \
} \ } \
} }
@@ -161,11 +161,11 @@ extern pthread_mutex_t log_mutex;
} }
INLINE char *errno_to_string(char *buf, size_t size) { INLINE char *errno_to_string(int error, char *buf, size_t size) {
# if defined(__GLIBC__) && defined(_GNU_SOURCE) # if defined(__GLIBC__) && defined(_GNU_SOURCE)
return strerror_r(errno, buf, size); return strerror_r(error, buf, size);
# else # else
strerror_r(errno, buf, size); strerror_r(error, buf, size);
return buf; return buf;
# endif # endif
} }