File: errormsg.c

package info (click to toggle)
xymon 4.3.30-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,288 kB
  • sloc: ansic: 69,112; sh: 3,595; makefile: 857; javascript: 452; perl: 48
file content (193 lines) | stat: -rw-r--r-- 4,776 bytes parent folder | download | duplicates (2)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*----------------------------------------------------------------------------*/
/* Xymon monitor library.                                                     */
/*                                                                            */
/* This is a library module, part of libxymon.                                */
/* It contains routines for error- and debug-message display.                 */
/*                                                                            */
/* Copyright (C) 2002-2011 Henrik Storner <henrik@storner.dk>                 */
/*                                                                            */
/* This program is released under the GNU General Public License (GPL),       */
/* version 2. See the file "COPYING" for details.                             */
/*                                                                            */
/*----------------------------------------------------------------------------*/

static char rcsid[] = "$Id: errormsg.c 8069 2019-07-23 15:29:06Z jccleaver $";

#include <sys/types.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>

#include "libxymon.h"

SBUF_DEFINE(errbuf);
static char msg[4096];
static char timestr[20];
static size_t timesize = sizeof(timestr);
static struct timeval now;
static time_t then = 0;
int save_errbuf = 1;
static char *errappname = NULL;

int debug = 0;
static FILE *tracefd = NULL;
static FILE *debugfd = NULL;

void errprintf(const char *fmt, ...)
{
	va_list args;
	gettimeofday(&now, NULL);

	if (now.tv_sec != then) {
		strftime(timestr, timesize, "%Y-%m-%d %H:%M:%S", localtime(&now.tv_sec));
		then = now.tv_sec;
	}
	fprintf(stderr, "%s.%06d ", timestr, (int) now.tv_usec);
	if (errappname) fprintf(stderr, "%s ", errappname);

	va_start(args, fmt);
	vsnprintf(msg, sizeof(msg), fmt, args);
	va_end(args);

	fprintf(stderr, "%s", msg);
	fflush(stderr);

	if (save_errbuf) {
		if (errbuf == NULL) {
			SBUF_MALLOC(errbuf, 8192);
			*errbuf = '\0';
		}
		else if ((strlen(errbuf) + strlen(msg)) > errbuf_buflen) {
			SBUF_REALLOC(errbuf, errbuf_buflen+8192);
		}

		strncat(errbuf, msg, (errbuf_buflen - strlen(errbuf)));
	}
}


void real_dbgprintf(const char *fmt, ...)
{
	va_list args;
	gettimeofday(&now, NULL);

	if (!debugfd) debugfd = stdout;
	if (now.tv_sec != then) {
		strftime(timestr, timesize, "%Y-%m-%d %H:%M:%S", localtime(&now.tv_sec));
		then = now.tv_sec;
	}

	fprintf(debugfd, "%lu %s.%06d ", (unsigned long)getpid(), timestr, (int) now.tv_usec);

	va_start(args, fmt);
	vfprintf(debugfd, fmt, args);
	va_end(args);
	fflush(debugfd);
}

void flush_errbuf(void)
{
	if (errbuf) xfree(errbuf);
	errbuf = NULL;
}


void set_debugfile(char *fn, int appendtofile)
{
	/* Always close and reopen when re-setting */
	if (debugfd && (debugfd != stdout) && (debugfd != stderr)) fclose(debugfd);

	if (fn) {
		if (strcasecmp(fn, "stderr") == 0) debugfd = stderr;
		else if (strcasecmp(fn, "stdout") == 0) debugfd = stdout;
		else {
			debugfd = fopen(fn, (appendtofile ? "a" : "w"));
			if (debugfd == NULL) errprintf("Cannot open debug log '%s': %s\n", fn, strerror(errno));
		}
	}

	if (!debugfd) debugfd = stdout;
}


void starttrace(const char *fn)
{
	if (tracefd) fclose(tracefd);
	if (fn) {
		tracefd = fopen(fn, "a"); 
		if (tracefd == NULL) errprintf("Cannot open tracefile %s\n", fn);
	}
	else tracefd = stdout;
}

void stoptrace(void)
{
	if (tracefd) fclose(tracefd);
	tracefd = NULL;
}

void traceprintf(const char *fmt, ...)
{
	va_list args;

	if (tracefd) {
		char timestr[40];
		time_t now = getcurrenttime(NULL);

		MEMDEFINE(timestr);

		strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", localtime(&now));
		fprintf(tracefd, "%08lu %s ", (unsigned long)getpid(), timestr);

		va_start(args, fmt);
		vfprintf(tracefd, fmt, args);
		va_end(args);
		fflush(tracefd);

		MEMUNDEFINE(timestr);
	}
}

void reopen_file(char *fn, char *mode, FILE *fd)
{
	FILE *testfd;

	testfd = fopen(fn, mode);
	if (!testfd) {
		fprintf(stderr, "reopen_file: Cannot open new file: %s\n", strerror(errno));
		return;
	}
	fclose(testfd);

	if (freopen(fn, mode, fd) == NULL) {
		/* Ugh ... lost the filedescriptor :-(( */
	}
}

void redirect_cgilog(char *cginame)
{
	char logfn[PATH_MAX];
	char *cgilogdir;
	FILE *fd;
	
	cgilogdir = getenv("XYMONCGILOGDIR");
	if (!cgilogdir) return;

	if (cginame) errappname = strdup(cginame);
	snprintf(logfn, sizeof(logfn), "%s/cgierror.log", cgilogdir);
	reopen_file(logfn, "a", stderr);

	/* If debugging, setup the debug logfile */
	if (debug) {
		snprintf(logfn, sizeof(logfn), "%s/%s.dbg", cgilogdir, (errappname ? errappname : "cgi"));
		set_debugfile(logfn, 1);
	}
}