librsync  2.3.2
trace.c
Go to the documentation of this file.
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 /** \file trace.c
30  * logging and debugging output.
31  *
32  * \todo Have a bit set in the log level that says not to include the function
33  * name. */
34 
35 #include "config.h"
36 #include <assert.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include "librsync.h"
41 #include "trace.h"
42 #include "util.h"
43 
44 rs_trace_fn_t *rs_trace_impl = rs_trace_stderr;
45 
46 int rs_trace_level = RS_LOG_INFO;
47 
48 #define MY_NAME "librsync"
49 
50 static void rs_log_va(int level, char const *fn, char const *fmt, va_list va);
51 
52 /** Log severity strings, if any. Must match ordering in ::rs_loglevel. */
53 static const char *rs_severities[] = {
54  "EMERGENCY! ", "ALERT! ", "CRITICAL! ", "ERROR: ", "Warning: ",
55  "", "", ""
56 };
57 
58 /** Set the destination of trace information.
59  *
60  * The callback scheme allows for use within applications that may have their
61  * own particular ways of reporting errors: log files for a web server,
62  * perhaps, and an error dialog for a browser.
63  *
64  * \todo Do we really need such fine-grained control, or just yes/no tracing? */
65 void rs_trace_to(rs_trace_fn_t *new_impl)
66 {
67  rs_trace_impl = new_impl;
68 }
69 
71 {
72  rs_trace_level = level;
73 }
74 
75 static void rs_log_va(int flags, char const *fn, char const *fmt, va_list va)
76 {
77  int level = flags & RS_LOG_PRIMASK;
78 
79  if (rs_trace_impl && level <= rs_trace_level) {
80  char buf[1000];
81  char full_buf[1040];
82 
83  vsnprintf(buf, sizeof(buf), fmt, va);
84  if (flags & RS_LOG_NONAME || !(*fn)) {
85  snprintf(full_buf, sizeof(full_buf), "%s: %s%s\n", MY_NAME,
86  rs_severities[level], buf);
87  } else {
88  snprintf(full_buf, sizeof(full_buf), "%s: %s(%s) %s\n", MY_NAME,
89  rs_severities[level], fn, buf);
90  }
91  rs_trace_impl(level, full_buf);
92  }
93 }
94 
95 /* Called by a macro that prepends the calling function name, etc. */
96 void rs_log0(int level, char const *fn, char const *fmt, ...)
97 {
98  va_list va;
99 
100  va_start(va, fmt);
101  rs_log_va(level, fn, fmt, va);
102  va_end(va);
103 }
104 
105 void rs_trace_stderr(rs_loglevel UNUSED(level), char const *msg)
106 {
107  fputs(msg, stderr);
108 }
109 
111 {
112 #ifdef DO_RS_TRACE
113  return 1;
114 #else
115  return 0;
116 #endif /* !DO_RS_TRACE */
117 }
Public header for librsync.
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
static const char * rs_severities[]
Log severity strings, if any.
Definition: trace.c:53
void rs_trace_to(rs_trace_fn_t *new_impl)
Set the destination of trace information.
Definition: trace.c:65
int rs_supports_trace(void)
Check whether the library was compiled with debugging trace.
Definition: trace.c:110
void rs_trace_set_level(rs_loglevel level)
Set the least important message severity that will be output.
Definition: trace.c:70
logging functions.
@ RS_LOG_PRIMASK
Mask to extract priority part.
Definition: trace.h:82
@ RS_LOG_NONAME
Don't show function name in message.
Definition: trace.h:83