aboutsummaryrefslogtreecommitdiff
path: root/src/diag.c
blob: 03a9c97667a92cd1f53a51223ddde75a69e174e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* This file is part of GNU Pies.
   Copyright (C) 2009 Sergey Poznyakoff

   GNU Pies is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Pies 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 GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#include "pies.h"
#include "xvasprintf.h"

unsigned debug_level;
int source_info_option;
int diag_output = DIAG_TO_STDERR;

void
diag_setup (int flags)
{
  if (flags)
    diag_output = flags;
  if (diag_output & DIAG_TO_SYSLOG)
    openlog (log_tag, LOG_PID, log_facility);
}

void
syslog_printer (int prio, const char *fmt, va_list ap)
{
#if HAVE_VSYSLOG
  vsyslog (prio, fmt, ap);
#else
  char buf[128];
  vsnprintf (buf, sizeof buf, fmt, ap);
  syslog (prio, "%s", buf);
#endif
}

void
vlogmsg (int prio, const char *fmt, va_list ap)
{
  if (DIAG_OUTPUT (DIAG_TO_STDERR))
    {
      va_list aq;
      fprintf (stderr, "%s: ", program_name);
      va_copy (aq, ap);
      vfprintf (stderr, fmt, aq);
      va_end (aq);
      fprintf (stderr, "\n");
    }
  
  if (DIAG_OUTPUT (DIAG_TO_SYSLOG))
    syslog_printer (prio, fmt, ap);
}

void
logmsg (int prio, const char *fmt, ...)
{
  va_list ap;
  va_start (ap, fmt);
  vlogmsg (prio, fmt, ap);
  va_end (ap);
}

void
debug_msg (const char *fmt, ...)
{
  va_list ap;
  va_start (ap, fmt);
  vlogmsg (LOG_DEBUG, fmt, ap);
  va_end (ap);
}


static struct obstack log_stk;
static int log_stk_init;

void
logmsg_vprintf (int prio, const char *fmt, va_list ap)
{
  char *str, *p;
  
  str = xvasprintf (fmt, ap);

  if (!log_stk_init)
    {
      obstack_init (&log_stk);
      log_stk_init = 1;
    }
  for (p = str; *p; )
    {
      size_t len = strcspn (p, "\n");
      if (len)
	obstack_grow (&log_stk, p, len);
      p += len;
      if (*p)
	{
	  char *msg;
	  
	  obstack_1grow (&log_stk, 0);
	  msg = obstack_finish (&log_stk);
	  logmsg (prio, "%s", msg);
	  obstack_free (&log_stk, msg);
	  p++;
	}
    }
  free (str);
}

void
logmsg_printf (int prio, const char *fmt, ...)
{
  va_list ap;
  va_start (ap, fmt);
  logmsg_vprintf (prio, fmt, ap);
  va_end (ap);
}

void
grecs_print_diag (grecs_locus_t *locus, int err, int errcode, const char *msg)
{
  if (locus)
    {
      if (errcode)
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s:%lu: %s: %s",
		locus->file, (unsigned long)locus->line, msg,
		strerror (errcode));
      else
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s:%lu: %s",
		locus->file, (unsigned long)locus->line, msg);
    }
  else
    {
      if (errcode)
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s: %s", msg,
		strerror (errcode));
      else
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s", msg);
    }
}
  

Return to:

Send suggestions and report system problems to the System administrator.