librsync  2.0.1
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 <[email protected]>
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 
30 
31 /*
32  * TODO: Have a bit set in the log level that says not to include the
33  * function name.
34  */
35 
36 #include "config.h"
37 #ifdef HAVE_UNISTD_H
38 #include <unistd.h>
39 #else
40 #define STDERR_FILENO 2
41 #endif
42 #include <stdio.h>
43 #ifdef HAVE_SYS_FILE_H
44 #include <sys/file.h>
45 #endif
46 #include <string.h>
47 #include <errno.h>
48 #include <stdlib.h>
49 #include <assert.h>
50 #include <stdarg.h>
51 
52 #include "librsync.h"
53 #include "util.h"
54 #include "trace.h"
55 
56 rs_trace_fn_t *rs_trace_impl = rs_trace_stderr;
57 
58 int rs_trace_level = RS_LOG_INFO;
59 
60 #ifdef HAVE_PROGRAM_INVOCATION_NAME
61 # define MY_NAME program_invocation_short_name
62 #else
63 # define MY_NAME "librsync"
64 #endif
65 
66 static void rs_log_va(int level, char const *fn, char const *fmt, va_list va);
67 
68 #if SIZEOF_SIZE_T > SIZEOF_LONG
69 # warning size_t is larger than a long integer, values in trace messages may be wrong
70 #endif
71 
72 
73 /**
74  * Log severity strings, if any. Must match ordering in
75  * ::rs_loglevel.
76  */
77 static const char *rs_severities[] = {
78  "EMERGENCY! ", "ALERT! ", "CRITICAL! ", "ERROR: ", "Warning: ",
79  "", "", ""
80 };
81 
82 
83 
84 /**
85  * \brief Set the destination of trace information.
86  *
87  * The callback scheme allows for use within applications that may
88  * have their own particular ways of reporting errors: log files for a
89  * web server, perhaps, and an error dialog for a browser.
90  *
91  * \todo Do we really need such fine-grained control, or just yes/no
92  * tracing?
93  */
94 void
96 {
97  rs_trace_impl = new_impl;
98 }
99 
100 
101 void
103 {
104  rs_trace_level = level;
105 }
106 
107 
108 static void
109 rs_log_va(int flags, char const *fn, char const *fmt, va_list va)
110 {
111  int level = flags & RS_LOG_PRIMASK;
112 
113  if (rs_trace_impl && level <= rs_trace_level) {
114  char buf[1000];
115  char full_buf[1000];
116 
117  vsnprintf(buf, sizeof buf - 1, fmt, va);
118 
119  if (flags & RS_LOG_NONAME) {
120  snprintf(full_buf, sizeof full_buf - 1,
121  "%s: %s%s\n",
122  MY_NAME, rs_severities[level], buf);
123  } else {
124  snprintf(full_buf, sizeof full_buf - 1,
125  "%s: %s(%s) %s\n",
126  MY_NAME, rs_severities[level], fn, buf);
127  }
128 
129  rs_trace_impl(level, full_buf);
130  }
131 }
132 
133 
134 
135 /**
136  * Called by a macro, used on platforms where we can't determine the
137  * calling function name.
138  */
139 void
140 rs_log0_nofn(int level, char const *fmt, ...)
141 {
142  va_list va;
143 
144  va_start(va, fmt);
145  rs_log_va(level, PACKAGE, fmt, va);
146  va_end(va);
147 }
148 
149 
150 /* Called by a macro that prepends the calling function name,
151  * etc. */
152 void
153 rs_log0(int level, char const *fn, char const *fmt, ...)
154 {
155  va_list va;
156 
157  va_start(va, fmt);
158  rs_log_va(level, fn, fmt, va);
159  va_end(va);
160 }
161 
162 
163 void
164 rs_trace_stderr(rs_loglevel UNUSED(level), char const *msg)
165 {
166  /* NOTE NO TRAILING NUL */
167  write(STDERR_FILENO, msg, strlen(msg));
168 }
169 
170 
171 /* This is called directly if the machine doesn't allow varargs
172  * macros. */
173 void
174 rs_fatal0(char const *s, ...)
175 {
176  va_list va;
177 
178  va_start(va, s);
179  rs_log_va(RS_LOG_CRIT, PACKAGE, s, va);
180  va_end(va);
181  abort();
182 }
183 
184 
185 /* This is called directly if the machine doesn't allow varargs
186  * macros. */
187 void
188 rs_error0(char const *s, ...)
189 {
190  va_list va;
191 
192  va_start(va, s);
193  rs_log_va(RS_LOG_ERR, PACKAGE, s, va);
194  va_end(va);
195 }
196 
197 
198 /* This is called directly if the machine doesn't allow varargs
199  * macros. */
200 void
201 rs_trace0(char const *s, ...)
202 {
203 #ifdef DO_RS_TRACE
204  va_list va;
205 
206  va_start(va, s);
207  rs_log_va(RS_LOG_DEBUG, PACKAGE, s, va);
208  va_end(va);
209 #endif /* !DO_RS_TRACE */
210 }
211 
212 
213 int
215 {
216 #ifdef DO_RS_TRACE
217  return 1;
218 #else
219  return 0;
220 #endif /* !DO_RS_TRACE */
221 }
void rs_trace_fn_t(rs_loglevel level, char const *msg)
Callback to write out log messages.
Definition: librsync.h:137
void rs_trace_to(rs_trace_fn_t *)
Set trace callback.
Definition: trace.c:95
int rs_supports_trace(void)
Check whether the library was compiled with debugging trace.
Definition: trace.c:214
Error conditions.
Definition: librsync.h:122
Public header for librsync.
Informational.
Definition: librsync.h:125
void rs_trace_stderr(rs_loglevel level, char const *msg)
Default trace callback that writes to stderr.
Critical conditions.
Definition: librsync.h:121
void rs_trace_set_level(rs_loglevel level)
Set the least important message severity that will be output.
Definition: trace.c:102
rs_loglevel
Log severity levels.
Definition: librsync.h:118
Debug-level messages.
Definition: librsync.h:126