librsync  2.3.4
trace.c
1/*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 *
3 * librsync -- library for network deltas
4 *
5 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /*=
23 | Finality is death.
24 | Perfection is finality.
25 | Nothing is perfect.
26 | There are lumps in it.
27 */
28
29#include "config.h" /* IWYU pragma: keep */
30#include <stdio.h>
31#include <stdarg.h>
32#include "librsync.h"
33#include "trace.h"
34#include "util.h"
35
36rs_trace_fn_t *rs_trace_impl = rs_trace_stderr;
37
38int rs_trace_level = RS_LOG_INFO;
39
40#define MY_NAME "librsync"
41
42static void rs_log_va(int level, char const *fn, char const *fmt, va_list va);
43
44/** Log severity strings, if any. Must match ordering in ::rs_loglevel. */
45static const char *rs_severities[] = {
46 "EMERGENCY! ", "ALERT! ", "CRITICAL! ", "ERROR: ", "Warning: ",
47 "", "", ""
48};
49
50/** Set the destination of trace information.
51 *
52 * The callback scheme allows for use within applications that may have their
53 * own particular ways of reporting errors: log files for a web server,
54 * perhaps, and an error dialog for a browser.
55 *
56 * \todo Do we really need such fine-grained control, or just yes/no tracing? */
58{
59 rs_trace_impl = new_impl;
60}
61
63{
64 rs_trace_level = level;
65}
66
67static void rs_log_va(int flags, char const *fn, char const *fmt, va_list va)
68{
69 int level = flags & RS_LOG_PRIMASK;
70
71 if (rs_trace_impl && level <= rs_trace_level) {
72 char buf[1000];
73 char full_buf[1040];
74
75 vsnprintf(buf, sizeof(buf), fmt, va);
76 if (flags & RS_LOG_NONAME || !(*fn)) {
77 snprintf(full_buf, sizeof(full_buf), "%s: %s%s\n", MY_NAME,
78 rs_severities[level], buf);
79 } else {
80 snprintf(full_buf, sizeof(full_buf), "%s: %s(%s) %s\n", MY_NAME,
81 rs_severities[level], fn, buf);
82 }
83 rs_trace_impl(level, full_buf);
84 }
85}
86
87/* Called by a macro that prepends the calling function name, etc. */
88void rs_log0(int level, char const *fn, char const *fmt, ...)
89{
90 va_list va;
91
92 va_start(va, fmt);
93 rs_log_va(level, fn, fmt, va);
94 va_end(va);
95}
96
97void rs_trace_stderr(rs_loglevel UNUSED(level), char const *msg)
98{
99 fputs(msg, stderr);
100}
101
103{
104#ifdef DO_RS_TRACE
105 return 1;
106#else
107 return 0;
108#endif /* !DO_RS_TRACE */
109}
Public header for librsync.
LIBRSYNC_EXPORT void rs_trace_to(rs_trace_fn_t *)
Set trace callback.
Definition: trace.c:57
LIBRSYNC_EXPORT int rs_supports_trace(void)
Check whether the library was compiled with debugging trace.
Definition: trace.c:102
LIBRSYNC_EXPORT void rs_trace_stderr(rs_loglevel level, char const *msg)
Default trace callback that writes to stderr.
void rs_trace_fn_t(rs_loglevel level, char const *msg)
Callback to write out log messages.
Definition: librsync.h:136
rs_loglevel
Log severity levels.
Definition: librsync.h:118
@ RS_LOG_INFO
Informational.
Definition: librsync.h:125
LIBRSYNC_EXPORT void rs_trace_set_level(rs_loglevel level)
Set the least important message severity that will be output.
Definition: trace.c:62
logging functions.
@ RS_LOG_PRIMASK
Mask to extract priority part.
Definition: trace.h:86
@ RS_LOG_NONAME
Don't show function name in message.
Definition: trace.h:87
Misc utility functions used by librsync.