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