Blame nscd/dbg_log.c

Packit 6c4009
/* Copyright (c) 1998-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1998.
Packit 6c4009
Packit 6c4009
   This program is free software; you can redistribute it and/or modify
Packit 6c4009
   it under the terms of the GNU General Public License as published
Packit 6c4009
   by the Free Software Foundation; version 2 of the License, or
Packit 6c4009
   (at your option) any later version.
Packit 6c4009
Packit 6c4009
   This program is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 6c4009
   GNU General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU General Public License
Packit 6c4009
   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <syslog.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include "dbg_log.h"
Packit 6c4009
#include "nscd.h"
Packit 6c4009
Packit 6c4009
/* if in debug mode and we have a debug file, we write the messages to it,
Packit 6c4009
   if in debug mode and no debug file, we write the messages to stderr,
Packit 6c4009
   else to syslog.  */
Packit 6c4009
Packit 6c4009
static char *logfilename;
Packit 6c4009
FILE *dbgout;
Packit 6c4009
int debug_level;
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
set_logfile (const char *logfile)
Packit 6c4009
{
Packit 6c4009
  logfilename = strdup (logfile);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
init_logfile (void)
Packit 6c4009
{
Packit 6c4009
  if (logfilename)
Packit 6c4009
    {
Packit 6c4009
      dbgout = fopen64 (logfilename, "a");
Packit 6c4009
      return dbgout == NULL ? 0 : 1;
Packit 6c4009
    }
Packit 6c4009
  return 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
dbg_log (const char *fmt,...)
Packit 6c4009
{
Packit 6c4009
  va_list ap;
Packit 6c4009
  char msg2[512];
Packit 6c4009
Packit 6c4009
  va_start (ap, fmt);
Packit 6c4009
  vsnprintf (msg2, sizeof (msg2), fmt, ap);
Packit 6c4009
Packit 6c4009
  if (debug_level > 0)
Packit 6c4009
    {
Packit 6c4009
      time_t t = time (NULL);
Packit 6c4009
Packit 6c4009
      struct tm now;
Packit 6c4009
      localtime_r (&t, &now;;
Packit 6c4009
Packit 6c4009
      char buf[256];
Packit 6c4009
      strftime (buf, sizeof (buf), "%c", &now;;
Packit 6c4009
Packit 6c4009
      char msg[1024];
Packit 6c4009
      snprintf (msg, sizeof (msg), "%s - %d: %s%s", buf, getpid (), msg2,
Packit 6c4009
		msg2[strlen (msg2) - 1] == '\n' ? "" : "\n");
Packit 6c4009
      if (dbgout)
Packit 6c4009
	{
Packit 6c4009
	  fputs (msg, dbgout);
Packit 6c4009
	  fflush (dbgout);
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	fputs (msg, stderr);
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    syslog (LOG_NOTICE, "%d %s", getpid (), msg2);
Packit 6c4009
Packit 6c4009
  va_end (ap);
Packit 6c4009
}