Skip to content

Commit 1d5529e

Browse files
author
Matthew Byng-Maddick
committed
Add the ability to show or hide access logging with a command-line argument
1 parent 92d85c3 commit 1d5529e

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

README

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ Options are:
9696
-F, --pidfile=FILE Specifies a file to write the process-id to.
9797
-h, --help Show this text.
9898
-i, --monitor-interval=TIME Set the monitor interval (default: 5.00s).
99+
-L, --access-log=MODE Specify the mode of access logging (default: "ALL").
99100
-l, --logfile=[MODE:]FILE Log all messages to logfile (default: stdout/stderr).
100101
-m, --max-frame-size=VALUE Specify the maximum frame size (default: 16384 bytes).
101102
-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.
119120
The time delay/interval is specified in milliseconds by default, but can be
120121
in any other unit if the number is suffixed by a unit (us, ms, s, m, h, d).
121122

123+
Access logging modes are 'NONE', 'ERROR', 'SUCCESS', 'ALL'.
124+
122125
Copyright 2018-2020 HAProxy Technologies
123126
SPDX-License-Identifier: GPL-2.0-or-later
124127
--- help output -------

include/types/main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct config_data {
7575
int pidfile_fd;
7676
uint ev_backend;
7777
#ifdef HAVE_LIBCURL
78+
int access_log_mode;
7879
char *mir_url;
7980
const char *mir_address;
8081
int mir_port[2];

src/curl.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,12 @@ static void mir_curl_check_multi_info(struct curl_data *curl)
180180
if ((rc = curl_easy_getinfo(msg->easy_handle, CURL_v075500(CURLINFO_SIZE_DOWNLOAD_T, CURLINFO_SIZE_DOWNLOAD), &size_download)) != CURLE_OK)
181181
CURL_ERR_EASY("Failed to get number of downloaded bytes", rc);
182182

183-
w_log(NULL, "\"%s %s %s\" %ld " CURL_v075500("%ld/%ld", "%.0f/%.0f") " %.3f %s",
184-
con->mir->method, url, mir_curl_get_http_version(version),
185-
response_code, size_upload, size_download,
186-
CURL_v076100(total_time / 1000.0, total_time * 1000.0),
187-
(msg->data.result != CURLE_OK) ? con->error : "ok");
183+
if (msg->data.result == CURLE_OK ? (cfg.access_log_mode & 1) : (cfg.access_log_mode & 2))
184+
w_log(NULL, "\"%s %s %s\" %ld " CURL_v075500("%ld/%ld", "%.0f/%.0f") " %.3f %s",
185+
con->mir->method, url, mir_curl_get_http_version(version),
186+
response_code, size_upload, size_download,
187+
CURL_v076100(total_time / 1000.0, total_time * 1000.0),
188+
(msg->data.result != CURLE_OK) ? con->error : "ok");
188189

189190
CURL_DBG("Done: %s => (%d) %s", url, msg->data.result, con->error);
190191

src/main.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ struct config_data cfg = {
3434
.runtime_us = DEFAULT_RUNTIME,
3535
.pidfile_fd = -1,
3636
.ev_backend = EVFLAG_AUTO,
37+
#ifdef LIBCURL
38+
.access_log_mode = 3,
39+
#endif
3740
};
3841
struct program_data prg;
3942

@@ -71,6 +74,9 @@ static void usage(const char *program_name, bool_t flag_verbose)
7174
(void)printf(" -F, --pidfile=FILE Specifies a file to write the process-id to.\n");
7275
(void)printf(" -h, --help Show this text.\n");
7376
(void)printf(" -i, --monitor-interval=TIME Set the monitor interval (default: %s).\n", str_delay(DEFAULT_MONITOR_INTERVAL));
77+
#ifdef HAVE_LIBCURL
78+
(void)printf(" -L, --access-log=MODE Specify the mode of access logging (default: \"ALL\").\n");
79+
#endif
7480
(void)printf(" -l, --logfile=[MODE:]FILE Log all messages to logfile (default: stdout/stderr).\n");
7581
(void)printf(" -m, --max-frame-size=VALUE Specify the maximum frame size (default: %d bytes).\n", DEFAULT_MAX_FRAME_SIZE);
7682
(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)
9197
(void)printf("for the mode, then line buffering is used when writing to the log file.\n\n");
9298
(void)printf("The time delay/interval is specified in milliseconds by default, but can be\n");
9399
(void)printf("in any other unit if the number is suffixed by a unit (us, ms, s, m, h, d).\n\n");
100+
(void)printf("Access logging modes are 'NONE', 'ERROR', 'SUCCESS', 'ALL'.\n\n");
94101
(void)printf("Copyright 2018-2020 HAProxy Technologies\n");
95102
(void)printf("SPDX-License-Identifier: GPL-2.0-or-later\n\n");
96103
} else {
@@ -326,6 +333,55 @@ static int getopt_set_ports(const char *ports, int *range)
326333
}
327334

328335

336+
/***
337+
* NAME
338+
* getopt_set_access_log_mode -
339+
*
340+
* ARGUMENTS
341+
* mode_txt -
342+
* mode_val -
343+
*
344+
* DESCRIPTION
345+
* -
346+
*
347+
* RETURN VALUE
348+
* -
349+
*/
350+
static int getopt_set_access_log_mode(const char *mode_txt, int *mode_val)
351+
{
352+
int retval = FUNC_RET_ERROR, mode=0;
353+
354+
DBG_FUNC(NULL, "\"%s\", %p", mode_txt, mode_val);
355+
356+
if (*mode_txt == 'A' || *mode_txt == 'a') {
357+
mode = 3;
358+
retval = FUNC_RET_OK;
359+
}
360+
else if (*mode_txt == 'E' || *mode_txt == 'e') {
361+
mode = 2;
362+
retval = FUNC_RET_OK;
363+
}
364+
else if (*mode_txt == 'S' || *mode_txt == 's') {
365+
mode = 1;
366+
retval = FUNC_RET_OK;
367+
}
368+
else if (*mode_txt == 'N' || *mode_txt == 'n') {
369+
mode = 0;
370+
retval = FUNC_RET_OK;
371+
}
372+
else
373+
(void)fprintf(stderr, "ERROR: access log mode must be one of 'ALL', 'ERROR', 'SUCCESS', or 'NONE'\n");
374+
375+
/* If everything is fine, set the mode. */
376+
if (_OK(retval)) {
377+
*mode_val = mode;
378+
W_DBG(NOTICE, NULL, " mode set to %d", mode);
379+
}
380+
381+
return retval;
382+
}
383+
384+
329385
/***
330386
* NAME
331387
* main -
@@ -360,6 +416,7 @@ int main(int argc, char **argv, char **envp __maybe_unused)
360416
{ "runtime", required_argument, NULL, 'r' },
361417
{ "processing-delay", required_argument, NULL, 't' },
362418
#ifdef HAVE_LIBCURL
419+
{ "access-log", required_argument, NULL, 'L' },
363420
{ "mirror-url", required_argument, NULL, 'u' },
364421
{ "mirror-interface", required_argument, NULL, 'I' },
365422
{ "mirror-local-port", required_argument, NULL, 'P' },
@@ -422,6 +479,8 @@ int main(int argc, char **argv, char **envp __maybe_unused)
422479
else if (c == 't')
423480
flag_error |= _OK(getopt_set_time(optarg, &(cfg.processing_delay_us), 0, TIMEINT_S(1))) ? 0 : 1;
424481
#ifdef HAVE_LIBCURL
482+
else if (c == 'L')
483+
flag_error |= _OK(getopt_set_access_log_mode(optarg, &(cfg.access_log_mode))) ? 0 : 1;
425484
else if (c == 'u')
426485
mir_url = optarg;
427486
else if (c == 'I')

0 commit comments

Comments
 (0)