1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Moved mg_cry to own file

This commit is contained in:
Lammert Bies
2016-12-11 15:20:08 +01:00
parent 5374f58a52
commit 8e5aec7eb7
3 changed files with 89 additions and 60 deletions

View File

@@ -60,6 +60,7 @@ LIB_SOURCES = src/libhttp.c \
src/httplib_connect_websocket_client.c \
src/httplib_construct_etag.c \
src/httplib_consume_socket.c \
src/httplib_cry.c \
src/httplib_delete_file.c \
src/httplib_difftimespec.c \
src/httplib_dir_scan_callback.c \

87
src/httplib_cry.c Normal file
View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2016 Lammert Bies
* Copyright (c) 2013-2016 the Civetweb developers
* Copyright (c) 2004-2013 Sergey Lyubka
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "libhttp-private.h"
/* Print error message to the opened error log stream. */
void mg_cry(const struct mg_connection *conn, const char *fmt, ...) {
char buf[MG_BUF_LEN];
char src_addr[IP_ADDR_STR_LEN];
va_list ap;
struct file fi;
time_t timestamp;
va_start(ap, fmt);
IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap));
va_end(ap);
buf[sizeof(buf) - 1] = 0;
if (!conn) {
puts(buf);
return;
}
/* Do not lock when getting the callback value, here and below.
* I suppose this is fine, since function cannot disappear in the
* same way string option can. */
if ((conn->ctx->callbacks.log_message == NULL)
|| (conn->ctx->callbacks.log_message(conn, buf) == 0)) {
if (conn->ctx->config[ERROR_LOG_FILE] != NULL) {
if (XX_httplib_fopen(conn, conn->ctx->config[ERROR_LOG_FILE], "a+", &fi)
== 0) {
fi.fp = NULL;
}
} else fi.fp = NULL;
if (fi.fp != NULL) {
flockfile(fi.fp);
timestamp = time(NULL);
XX_httplib_sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
fprintf(fi.fp,
"[%010lu] [error] [client %s] ",
(unsigned long)timestamp,
src_addr);
if (conn->request_info.request_method != NULL) {
fprintf(fi.fp,
"%s %s: ",
conn->request_info.request_method,
conn->request_info.request_uri);
}
fprintf(fi.fp, "%s", buf);
fputc('\n', fi.fp);
fflush(fi.fp);
funlockfile(fi.fp);
XX_httplib_fclose(&fi);
}
}
} /* mg_cry */

View File

@@ -828,68 +828,9 @@ void XX_httplib_set_thread_name(const char *threadName) {
/* Print error message to the opened error log stream. */
void mg_cry(const struct mg_connection *conn, const char *fmt, ...) {
char buf[MG_BUF_LEN];
char src_addr[IP_ADDR_STR_LEN];
va_list ap;
struct file fi;
time_t timestamp;
va_start(ap, fmt);
IGNORE_UNUSED_RESULT(vsnprintf_impl(buf, sizeof(buf), fmt, ap));
va_end(ap);
buf[sizeof(buf) - 1] = 0;
if (!conn) {
puts(buf);
return;
}
/* Do not lock when getting the callback value, here and below.
* I suppose this is fine, since function cannot disappear in the
* same way string option can. */
if ((conn->ctx->callbacks.log_message == NULL)
|| (conn->ctx->callbacks.log_message(conn, buf) == 0)) {
if (conn->ctx->config[ERROR_LOG_FILE] != NULL) {
if (XX_httplib_fopen(conn, conn->ctx->config[ERROR_LOG_FILE], "a+", &fi)
== 0) {
fi.fp = NULL;
}
} else fi.fp = NULL;
if (fi.fp != NULL) {
flockfile(fi.fp);
timestamp = time(NULL);
XX_httplib_sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
fprintf(fi.fp,
"[%010lu] [error] [client %s] ",
(unsigned long)timestamp,
src_addr);
if (conn->request_info.request_method != NULL) {
fprintf(fi.fp,
"%s %s: ",
conn->request_info.request_method,
conn->request_info.request_uri);
}
fprintf(fi.fp, "%s", buf);
fputc('\n', fi.fp);
fflush(fi.fp);
funlockfile(fi.fp);
XX_httplib_fclose(&fi);
}
}
}
/* Return fake connection structure. Used for logging, if connection
* is not applicable at the moment of logging. */
struct mg_connection * XX_httplib_fc( struct mg_context *ctx ) {
struct mg_connection *XX_httplib_fc( struct mg_context *ctx ) {
static struct mg_connection fake_connection;