2 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/trace.h>
14 static int verbosity = LOG_INFO;
16 int log_set_verbosity(const char *prog, int level)
18 if (level < LOG_EMERG || level > LOG_TRACE) {
19 trace_log_message(-1, prog, LOG_ERR,
20 "Invalid verbosity level %d", level);
27 int log_get_verbosity(void)
33 static int print_syslog(const char *str, size_t len, void *levPtr)
35 int level = *(int *)levPtr;
36 int ilen = len > MAXERRLEN ? MAXERRLEN : len;
38 syslog(level, "%.*s", ilen, str);
44 static void log_with_prefix(const char *prog, const char *fmt, va_list ap)
47 BIO *bio, *pre = BIO_new(BIO_f_prefix());
49 (void)BIO_snprintf(prefix, sizeof(prefix), "%s: ", prog);
50 (void)BIO_set_prefix(pre, prefix);
51 bio = BIO_push(pre, bio_err);
52 (void)BIO_vprintf(bio, fmt, ap);
53 (void)BIO_printf(bio, "\n");
60 * Unfortunately, C before C99 does not define va_copy, so we must
61 * check if it can be assumed to be present. We do that with an internal
63 * C versions since C94 define __STDC_VERSION__, so it's enough to
64 * check its existence and value.
67 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ + 0 < 199900L
71 void trace_log_message(int category,
72 const char *prog, int level, const char *fmt, ...)
78 if (verbosity >= level)
79 category = -1; /* disabling trace output in addition to logging */
81 if (category >= 0 && OSSL_trace_enabled(category)) {
82 BIO *out = OSSL_trace_begin(category);
87 (void)BIO_vprintf(out, fmt, ap_copy);
90 (void)BIO_vprintf(out, fmt, ap);
92 (void)BIO_printf(out, "\n");
93 OSSL_trace_end(category, out);
95 if (verbosity < level) {
100 if (n_responders != 0) {
101 vsyslog(level, fmt, ap);
102 if (level <= LOG_ERR)
103 ERR_print_errors_cb(print_syslog, &level);
106 log_with_prefix(prog, fmt, ap);