commit 2d967a6d5f4c6a46a96edcb3e2f5c855fc19913d Author: David Goulet dgoulet@ev0ke.net Date: Sun Jun 2 14:09:15 2013 -0400
Add log.c/.h containing loggin calls from old source
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/common/Makefile.am | 4 +- src/common/log.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++ src/common/log.h | 40 ++++++++++++++++++ 3 files changed, 148 insertions(+), 3 deletions(-)
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index c663a99..4e97ac2 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -3,6 +3,4 @@ AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/src AM_CFLAGS = -fno-strict-aliasing
noinst_LTLIBRARIES = libcommon.la -libcommon_la_SOURCES = - - +libcommon_la_SOURCES = log.c log.h diff --git a/src/common/log.c b/src/common/log.c new file mode 100644 index 0000000..29888c0 --- /dev/null +++ b/src/common/log.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <time.h> +#include <unistd.h> + + +#include "log.h" + +/* Set logging options, the options are as follows: */ +/* level - This sets the logging threshold, messages with */ +/* a higher level (i.e lower importance) will not be */ +/* output. For example, if the threshold is set to */ +/* MSGWARN a call to log a message of level MSGDEBUG */ +/* would be ignored. This can be set to -1 to disable */ +/* messages entirely */ +/* filename - This is a filename to which the messages should */ +/* be logged instead of to standard error */ +/* timestamp - This indicates that messages should be prefixed */ +/* with timestamps (and the process id) */ +void set_log_options(int level, char *filename, int timestamp) +{ + loglevel = level; + if (loglevel < MSGERR) + loglevel = MSGNONE; + + if (filename) { + strncpy(logfilename, filename, sizeof(logfilename)); + logfilename[sizeof(logfilename) - 1] = '\0'; + } + + logstamp = timestamp; +} + +void show_msg(int level, const char *fmt, ...) +{ + va_list ap; + int saveerr; + extern char *torsocks_progname; + char timestring[20]; + time_t timestamp; + + if ((loglevel == MSGNONE) || (level > loglevel)) + return; + + if (!logfile) { + if (logfilename[0]) { + logfile = fopen(logfilename, "a"); + if (logfile == NULL) { + logfile = stderr; + show_msg(MSGERR, "Could not open log file, %s, %s\n", + logfilename, strerror(errno)); + } + } else + logfile = stderr; + } + + if (logstamp) { + timestamp = time(NULL); + strftime(timestring, sizeof(timestring), "%H:%M:%S", + localtime(×tamp)); + fprintf(logfile, "%s ", timestring); + } + + fputs(torsocks_progname, logfile); + + if (logstamp) { + fprintf(logfile, "(%d)", getpid()); + } + + fputs(": ", logfile); + + va_start(ap, fmt); + + /* Save errno */ + saveerr = errno; + + vfprintf(logfile, fmt, ap); + + fflush(logfile); + + errno = saveerr; + + va_end(ap); +} + diff --git a/src/common/log.h b/src/common/log.h new file mode 100644 index 0000000..fd141a0 --- /dev/null +++ b/src/common/log.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2 only, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef TORSOCKS_LOG_H +#define TORSOCKS_LOG_H + +#define MSGNONE -1 +#define MSGERR 0 +#define MSGWARN 1 +#define MSGTEST 2 +#define MSGNOTICE 3 +#define MSGDEBUG 3 + +int loglevel = MSGERR; /* The default logging level is to only log + error messages */ +char logfilename[256]; /* Name of file to which log messages should + be redirected */ +FILE *logfile; /* File to which messages should be logged */ +int logstamp; /* Timestamp (and pid stamp) messages */ + +void set_log_options(int level, char *filename, int timestamp); +void show_msg(int level, const char *fmt, ...); + +#endif /* TORSOCKS_LOG_H */
tor-commits@lists.torproject.org