Skip to content

Commit

Permalink
Add the ability to show or hide access logging with a command-line ar…
Browse files Browse the repository at this point in the history
…gument
  • Loading branch information
Matthew Byng-Maddick committed Sep 21, 2021
1 parent 92d85c3 commit 1d5529e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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 -------
Expand Down
1 change: 1 addition & 0 deletions include/types/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
11 changes: 6 additions & 5 deletions src/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
59 changes: 59 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 1d5529e

Please sign in to comment.