$treeview $search $mathjax $extrastylesheet
librsync
2.3.1
$projectbrief
|
$projectbrief
|
$searchbox |
00001 /*= -*- c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 * 00003 * librsync -- library for network deltas 00004 * 00005 * Copyright (C) 2000, 2001 by Martin Pool <mbp@sourcefrog.net> 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU Lesser General Public License as published by 00009 * the Free Software Foundation; either version 2.1 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00020 */ 00021 00022 /*= 00023 | Finality is death. 00024 | Perfection is finality. 00025 | Nothing is perfect. 00026 | There are lumps in it. 00027 */ 00028 00029 /** \file trace.c 00030 * logging and debugging output. 00031 * 00032 * \todo Have a bit set in the log level that says not to include the function 00033 * name. */ 00034 00035 #include "config.h" 00036 #include <assert.h> 00037 #include <stdlib.h> 00038 #include <stdio.h> 00039 #include <stdarg.h> 00040 #include "librsync.h" 00041 #include "trace.h" 00042 #include "util.h" 00043 00044 rs_trace_fn_t *rs_trace_impl = rs_trace_stderr; 00045 00046 int rs_trace_level = RS_LOG_INFO; 00047 00048 #define MY_NAME "librsync" 00049 00050 static void rs_log_va(int level, char const *fn, char const *fmt, va_list va); 00051 00052 /** Log severity strings, if any. Must match ordering in ::rs_loglevel. */ 00053 static const char *rs_severities[] = { 00054 "EMERGENCY! ", "ALERT! ", "CRITICAL! ", "ERROR: ", "Warning: ", 00055 "", "", "" 00056 }; 00057 00058 /** Set the destination of trace information. 00059 * 00060 * The callback scheme allows for use within applications that may have their 00061 * own particular ways of reporting errors: log files for a web server, 00062 * perhaps, and an error dialog for a browser. 00063 * 00064 * \todo Do we really need such fine-grained control, or just yes/no tracing? */ 00065 LIBRSYNC_EXPORT void rs_trace_to(rs_trace_fn_t *new_impl) 00066 { 00067 rs_trace_impl = new_impl; 00068 } 00069 00070 LIBRSYNC_EXPORT void rs_trace_set_level(rs_loglevel level) 00071 { 00072 rs_trace_level = level; 00073 } 00074 00075 static void rs_log_va(int flags, char const *fn, char const *fmt, va_list va) 00076 { 00077 int level = flags & RS_LOG_PRIMASK; 00078 00079 if (rs_trace_impl && level <= rs_trace_level) { 00080 char buf[1000]; 00081 char full_buf[1040]; 00082 00083 vsnprintf(buf, sizeof(buf), fmt, va); 00084 if (flags & RS_LOG_NONAME || !(*fn)) { 00085 snprintf(full_buf, sizeof(full_buf), "%s: %s%s\n", MY_NAME, 00086 rs_severities[level], buf); 00087 } else { 00088 snprintf(full_buf, sizeof(full_buf), "%s: %s(%s) %s\n", MY_NAME, 00089 rs_severities[level], fn, buf); 00090 } 00091 rs_trace_impl(level, full_buf); 00092 } 00093 } 00094 00095 /* Called by a macro that prepends the calling function name, etc. */ 00096 void rs_log0(int level, char const *fn, char const *fmt, ...) 00097 { 00098 va_list va; 00099 00100 va_start(va, fmt); 00101 rs_log_va(level, fn, fmt, va); 00102 va_end(va); 00103 } 00104 00105 LIBRSYNC_EXPORT void rs_trace_stderr(rs_loglevel UNUSED(level), char const *msg) 00106 { 00107 fputs(msg, stderr); 00108 } 00109 00110 LIBRSYNC_EXPORT int rs_supports_trace(void) 00111 { 00112 #ifdef DO_RS_TRACE 00113 return 1; 00114 #else 00115 return 0; 00116 #endif /* !DO_RS_TRACE */ 00117 }