diff --git a/README b/README index 2a8691a..08c03b5 100644 --- a/README +++ b/README @@ -96,6 +96,7 @@ Options are: -F, --pidfile=FILE Specifies a file to write the process-id to. -h, --help Show this text. -i, --monitor-interval=TIME Set the monitor interval (default: 5.00s). + -L, --access-log=MODE Specify the mode of access logging (default: "ALL"). -l, --logfile=[MODE:]FILE Log all messages to logfile (default: stdout/stderr). -m, --max-frame-size=VALUE Specify the maximum frame size (default: 16384 bytes). -n, --num-workers=VALUE Specify the number of workers (default: 10). @@ -119,6 +120,8 @@ for the mode, then line buffering is used when writing to the log file. The time delay/interval is specified in milliseconds by default, but can be in any other unit if the number is suffixed by a unit (us, ms, s, m, h, d). +Access logging modes are 'NONE', 'ERROR', 'SUCCESS', 'ALL'. + Copyright 2018-2020 HAProxy Technologies SPDX-License-Identifier: GPL-2.0-or-later --- help output ------- diff --git a/include/types/main.h b/include/types/main.h index 5ab393e..fe3f205 100644 --- a/include/types/main.h +++ b/include/types/main.h @@ -75,6 +75,7 @@ struct config_data { int pidfile_fd; uint ev_backend; #ifdef HAVE_LIBCURL + int access_log_mode; char *mir_url; const char *mir_address; int mir_port[2]; diff --git a/src/curl.c b/src/curl.c index b06ee94..fca2266 100644 --- a/src/curl.c +++ b/src/curl.c @@ -180,11 +180,12 @@ static void mir_curl_check_multi_info(struct curl_data *curl) if ((rc = curl_easy_getinfo(msg->easy_handle, CURL_v075500(CURLINFO_SIZE_DOWNLOAD_T, CURLINFO_SIZE_DOWNLOAD), &size_download)) != CURLE_OK) CURL_ERR_EASY("Failed to get number of downloaded bytes", rc); - w_log(NULL, "\"%s %s %s\" %ld " CURL_v075500("%ld/%ld", "%.0f/%.0f") " %.3f %s", - con->mir->method, url, mir_curl_get_http_version(version), - response_code, size_upload, size_download, - CURL_v076100(total_time / 1000.0, total_time * 1000.0), - (msg->data.result != CURLE_OK) ? con->error : "ok"); + if (msg->data.result == CURLE_OK ? (cfg.access_log_mode & 1) : (cfg.access_log_mode & 2)) + w_log(NULL, "\"%s %s %s\" %ld " CURL_v075500("%ld/%ld", "%.0f/%.0f") " %.3f %s", + con->mir->method, url, mir_curl_get_http_version(version), + response_code, size_upload, size_download, + CURL_v076100(total_time / 1000.0, total_time * 1000.0), + (msg->data.result != CURLE_OK) ? con->error : "ok"); CURL_DBG("Done: %s => (%d) %s", url, msg->data.result, con->error); diff --git a/src/main.c b/src/main.c index dabc1b4..c277321 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,9 @@ struct config_data cfg = { .runtime_us = DEFAULT_RUNTIME, .pidfile_fd = -1, .ev_backend = EVFLAG_AUTO, +#ifdef LIBCURL + .access_log_mode = 3, +#endif }; struct program_data prg; @@ -71,6 +74,9 @@ static void usage(const char *program_name, bool_t flag_verbose) (void)printf(" -F, --pidfile=FILE Specifies a file to write the process-id to.\n"); (void)printf(" -h, --help Show this text.\n"); (void)printf(" -i, --monitor-interval=TIME Set the monitor interval (default: %s).\n", str_delay(DEFAULT_MONITOR_INTERVAL)); +#ifdef HAVE_LIBCURL + (void)printf(" -L, --access-log=MODE Specify the mode of access logging (default: \"ALL\").\n"); +#endif (void)printf(" -l, --logfile=[MODE:]FILE Log all messages to logfile (default: stdout/stderr).\n"); (void)printf(" -m, --max-frame-size=VALUE Specify the maximum frame size (default: %d bytes).\n", DEFAULT_MAX_FRAME_SIZE); (void)printf(" -n, --num-workers=VALUE Specify the number of workers (default: %d).\n", DEFAULT_NUM_WORKERS); @@ -91,6 +97,7 @@ static void usage(const char *program_name, bool_t flag_verbose) (void)printf("for the mode, then line buffering is used when writing to the log file.\n\n"); (void)printf("The time delay/interval is specified in milliseconds by default, but can be\n"); (void)printf("in any other unit if the number is suffixed by a unit (us, ms, s, m, h, d).\n\n"); + (void)printf("Access logging modes are 'NONE', 'ERROR', 'SUCCESS', 'ALL'.\n\n"); (void)printf("Copyright 2018-2020 HAProxy Technologies\n"); (void)printf("SPDX-License-Identifier: GPL-2.0-or-later\n\n"); } else { @@ -326,6 +333,55 @@ static int getopt_set_ports(const char *ports, int *range) } +/*** + * NAME + * getopt_set_access_log_mode - + * + * ARGUMENTS + * mode_txt - + * mode_val - + * + * DESCRIPTION + * - + * + * RETURN VALUE + * - + */ +static int getopt_set_access_log_mode(const char *mode_txt, int *mode_val) +{ + int retval = FUNC_RET_ERROR, mode=0; + + DBG_FUNC(NULL, "\"%s\", %p", mode_txt, mode_val); + + if (*mode_txt == 'A' || *mode_txt == 'a') { + mode = 3; + retval = FUNC_RET_OK; + } + else if (*mode_txt == 'E' || *mode_txt == 'e') { + mode = 2; + retval = FUNC_RET_OK; + } + else if (*mode_txt == 'S' || *mode_txt == 's') { + mode = 1; + retval = FUNC_RET_OK; + } + else if (*mode_txt == 'N' || *mode_txt == 'n') { + mode = 0; + retval = FUNC_RET_OK; + } + else + (void)fprintf(stderr, "ERROR: access log mode must be one of 'ALL', 'ERROR', 'SUCCESS', or 'NONE'\n"); + + /* If everything is fine, set the mode. */ + if (_OK(retval)) { + *mode_val = mode; + W_DBG(NOTICE, NULL, " mode set to %d", mode); + } + + return retval; +} + + /*** * NAME * main - @@ -360,6 +416,7 @@ int main(int argc, char **argv, char **envp __maybe_unused) { "runtime", required_argument, NULL, 'r' }, { "processing-delay", required_argument, NULL, 't' }, #ifdef HAVE_LIBCURL + { "access-log", required_argument, NULL, 'L' }, { "mirror-url", required_argument, NULL, 'u' }, { "mirror-interface", required_argument, NULL, 'I' }, { "mirror-local-port", required_argument, NULL, 'P' }, @@ -422,6 +479,8 @@ int main(int argc, char **argv, char **envp __maybe_unused) else if (c == 't') flag_error |= _OK(getopt_set_time(optarg, &(cfg.processing_delay_us), 0, TIMEINT_S(1))) ? 0 : 1; #ifdef HAVE_LIBCURL + else if (c == 'L') + flag_error |= _OK(getopt_set_access_log_mode(optarg, &(cfg.access_log_mode))) ? 0 : 1; else if (c == 'u') mir_url = optarg; else if (c == 'I')