File: log.c

package info (click to toggle)
gup 0.5.17
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 200 kB
  • sloc: ansic: 1,609; sh: 211; makefile: 34
file content (75 lines) | stat: -rw-r--r-- 1,770 bytes parent folder | download | duplicates (3)
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
/* Handle logging to the log file and mail file */

#include "gup.h"

static void log_line(FILE *fp, int lflags, const char *stamp,
	const char *prefix, const char *out_msg, va_list AP)
{
    if (lflags & L_TIMESTAMP)
	fputs(stamp, fp);
    if (prefix)
	fprintf(fp, "%s: ", prefix);
    vfprintf(fp, out_msg, AP);
    fputs("\n", fp);
}

/* Log a message, with a time stamp to the logfile and or mailfile */
void vlogit(int lflags, const char *prefix, const char *log_msg, va_list AP)
{
    time_t secs;
    struct tm *tm;
    char stamp[100];
    va_list AP2;

    static int tried_mail_open = FALSE;

    va_copy(AP2, AP);
    if (lflags & L_TIMESTAMP) {
	time(&secs);
	tm = localtime(&secs);
	strftime(stamp, sizeof(stamp), "%d %h %Y %T: ", tm);
    }
    if (log_fp && (lflags & L_LOG)) {
	log_line(log_fp, lflags, stamp, prefix, log_msg, AP);
	fflush(log_fp);
    }
    if (lflags & L_MAIL) {
	/* If the mail pipe has not been opened yet, then that means that this
	 * is an error prior to a valid site command which means that mail goes
	 * to the address found in the inbound headers, or as a last resort the
	 * BACKSTOP_MAILID.
	 */
	if (!mail_fp && !tried_mail_open) {
	    tried_mail_open = TRUE;
#ifndef TEST
	    mail_fp = mail_open(TRUE, NULL, NULL, NULL);
#endif
	}
	if (mail_fp)
	    /* to prevent ordering problems with parts using stdout */
	    log_line(mail_fp, lflags, stamp, prefix, log_msg, AP2);
    }
    va_end(AP2);
}

void logit(int lflags, const char *log_msg, ...)
{
    va_list ap;

    va_start(ap, log_msg);
    vlogit(lflags, NULL, log_msg, ap);
    va_end(ap);
}

#ifdef TEST
FILE *log_fp;
FILE *mail_fp;

int main(void)
{
    log_fp = stdout;
    mail_fp = stdout;
    logit(L_MAIL, ">%s<>%d<", "string", 1234);
    exit(0);
}
#endif