Back to index

glibc  2.9
dbg_log.c
Go to the documentation of this file.
00001 /* Copyright (c) 1998, 2000, 2004, 2005 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
00004 
00005    This program is free software; you can redistribute it and/or modify
00006    it under the terms of the GNU General Public License as published
00007    by the Free Software Foundation; version 2 of the License, or
00008    (at your option) any later version.
00009 
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013    GNU General Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License
00016    along with this program; if not, write to the Free Software Foundation,
00017    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
00018 
00019 #include <stdarg.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <syslog.h>
00023 #include <unistd.h>
00024 #include "dbg_log.h"
00025 #include "nscd.h"
00026 
00027 /* if in debug mode and we have a debug file, we write the messages to it,
00028    if in debug mode and no debug file, we write the messages to stderr,
00029    else to syslog.  */
00030 
00031 static char *logfilename;
00032 FILE *dbgout;
00033 int debug_level;
00034 
00035 void
00036 set_logfile (const char *logfile)
00037 {
00038   logfilename = strdup (logfile);
00039 }
00040 
00041 int
00042 init_logfile (void)
00043 {
00044   if (logfilename)
00045     {
00046       dbgout = fopen64 (logfilename, "a");
00047       return dbgout == NULL ? 0 : 1;
00048     }
00049   return 1;
00050 }
00051 
00052 void
00053 dbg_log (const char *fmt,...)
00054 {
00055   va_list ap;
00056   char msg[512], msg2[512];
00057 
00058   va_start (ap, fmt);
00059   vsnprintf (msg2, sizeof (msg), fmt, ap);
00060 
00061   if (debug_level > 0)
00062     {
00063       snprintf (msg, sizeof (msg), "%d: %s%s", getpid (), msg2,
00064               msg2[strlen (msg2) - 1] == '\n' ? "" : "\n");
00065       if (dbgout)
00066        {
00067          fputs (msg, dbgout);
00068          fflush (dbgout);
00069        }
00070       else
00071        fputs (msg, stderr);
00072     }
00073   else
00074     syslog (LOG_NOTICE, "%d %s", getpid (), msg2);
00075 
00076   va_end (ap);
00077 }