Debian Codegraph @

c/src/core/log.c

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "log_private.h"
21 #include "logger_private.h"
22 #include "util.h"
23
24 #include <proton/error.h>
25 #include <proton/log.h>
26 #include <proton/object.h>
27
28 static int enabled_env = -1; /* Set from environment variable. */
29 static int enabled_call = -1; /* set by pn_log_enable */
30
31 void pn_log_enable(bool value) {
32 enabled_call = value;
33 }
34
35 bool pni_log_enabled(void) {
36 if (enabled_call != -1) return enabled_call; /* Takes precedence */
37 if (enabled_env == -1)
38 enabled_env = pn_env_bool("PN_TRACE_LOG");
39 return enabled_env;
40 }
41
42 void pn_log_logger(void (*new_logger)(const char* message)) {
43 if (!new_logger) pn_log_enable(false);
44 }
45
46 void pni_vlogf_impl(const char *fmt, va_list ap) {
47 pni_logger_vlogf(pn_default_logger(), PN_SUBSYSTEM_ALL, PN_LEVEL_TRACE, fmt, ap);
48 }
49
50 /**@internal
51 *
52 * Note: We check pni_log_enabled() in the pn_logf macro *before* calling
53 * pni_logf_impl because evaluating the arguments to that call could have
54 * side-effects with performance impact (e.g. calling functions to construct
55 * complicated messages.) It is important that a disabled log statement results
56 * in nothing more than a call to pni_log_enabled().
57 */
58 void pni_logf_impl(const char *fmt, ...) {
59 va_list ap;
60 va_start(ap, fmt);
61 pni_vlogf_impl(fmt, ap);
62 va_end(ap);
63 }
64
65 void pn_log_data(const char *msg, const char *bytes, size_t size)
66 {
67 if (!pni_log_enabled()) return;
68
69 char buf[256];
70 ssize_t n = pn_quote_data(buf, 256, bytes, size);
71 if (n >= 0) {
72 pni_logf_impl("%s: %s", msg, buf);
73 } else if (n == PN_OVERFLOW) {
74 pni_logf_impl("%s: %s (truncated)", msg, buf);
75 } else {
76 pni_logf_impl("%s: cannot log data: %s", msg, pn_code(n));
77 }
78 }