µhttp-util: setup gnutls logs in one function

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2015-03-13 21:07:45 -05:00
parent 1b14c3cfbe
commit d357562c48
4 changed files with 49 additions and 50 deletions

View file

@ -982,10 +982,9 @@ int main(int argc, char *argv[]) {
sigbus_install();
#ifdef HAVE_GNUTLS
gnutls_global_set_log_function(log_func_gnutls);
log_reset_gnutls_level();
#endif
r = setup_gnutls_logger(NULL);
if (r < 0)
return EXIT_FAILURE;
n = sd_listen_fds(1);
if (n < 0) {

View file

@ -1503,31 +1503,6 @@ static int load_certificates(char **key, char **cert, char **trust) {
return 0;
}
static int setup_gnutls_logger(char **categories) {
if (!arg_listen_http && !arg_listen_https)
return 0;
#ifdef HAVE_GNUTLS
{
char **cat;
int r;
gnutls_global_set_log_function(log_func_gnutls);
if (categories) {
STRV_FOREACH(cat, categories) {
r = log_enable_gnutls_category(*cat);
if (r < 0)
return r;
}
} else
log_reset_gnutls_level();
}
#endif
return 0;
}
int main(int argc, char **argv) {
RemoteServer s = {};
int r;
@ -1544,9 +1519,12 @@ int main(int argc, char **argv) {
if (r <= 0)
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
r = setup_gnutls_logger(arg_gnutls_log);
if (r < 0)
return EXIT_FAILURE;
if (arg_listen_http || arg_listen_https) {
r = setup_gnutls_logger(arg_gnutls_log);
if (r < 0)
return EXIT_FAILURE;
}
if (arg_listen_https || https_socket >= 0)
if (load_certificates(&key, &cert, &trust) < 0)

View file

@ -121,7 +121,7 @@ static struct {
{ {"9", "enc", "int"}, LOG_DEBUG },
};
void log_func_gnutls(int level, const char *message) {
static void log_func_gnutls(int level, const char *message) {
assert_se(message);
if (0 <= level && level < (int) ELEMENTSOF(gnutls_log_map)) {
@ -133,7 +133,18 @@ void log_func_gnutls(int level, const char *message) {
}
}
int log_enable_gnutls_category(const char *cat) {
static void log_reset_gnutls_level(void) {
int i;
for (i = ELEMENTSOF(gnutls_log_map) - 1; i >= 0; i--)
if (gnutls_log_map[i].enabled) {
log_debug("Setting gnutls log level to %d", i);
gnutls_global_set_log_level(i);
break;
}
}
static int log_enable_gnutls_category(const char *cat) {
unsigned i;
if (streq(cat, "all")) {
@ -152,15 +163,22 @@ int log_enable_gnutls_category(const char *cat) {
return -EINVAL;
}
void log_reset_gnutls_level(void) {
int i;
int setup_gnutls_logger(char **categories) {
char **cat;
int r;
for (i = ELEMENTSOF(gnutls_log_map) - 1; i >= 0; i--)
if (gnutls_log_map[i].enabled) {
log_debug("Setting gnutls log level to %d", i);
gnutls_global_set_log_level(i);
break;
gnutls_global_set_log_function(log_func_gnutls);
if (categories) {
STRV_FOREACH(cat, categories) {
r = log_enable_gnutls_category(*cat);
if (r < 0)
return r;
}
} else
log_reset_gnutls_level();
return 0;
}
static int verify_cert_authorized(gnutls_session_t session) {
@ -300,4 +318,10 @@ int check_permissions(struct MHD_Connection *connection, int *code, char **hostn
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname) {
return -EPERM;
}
int setup_gnutls_logger(char **categories) {
if (categories)
log_notice("Ignoring specified gnutls logging categories — gnutls not available.");
return 0;
}
#endif

View file

@ -43,13 +43,11 @@ int mhd_respond_oom(struct MHD_Connection *connection);
int check_permissions(struct MHD_Connection *connection, int *code, char **hostname);
#ifdef HAVE_GNUTLS
void log_func_gnutls(int level, const char *message);
int log_enable_gnutls_category(const char *cat);
void log_reset_gnutls_level(void);
/* This is additionally filtered by our internal log level, so it
* should be set fairly high to capture all potentially interesting
* events without overwhelming detail.
/* Set gnutls internal logging function to a callback which uses our
* own logging framework.
*
* gnutls categories are additionally filtered by our internal log
* level, so it should be set fairly high to capture all potentially
* interesting events without overwhelming detail.
*/
#endif
int setup_gnutls_logger(char **categories);