1/*	$NetBSD$	*/
2
3/*++
4/* NAME
5/*	msg_vstream 3
6/* SUMMARY
7/*	report diagnostics to VSTREAM
8/* SYNOPSIS
9/*	#include <msg_vstream.h>
10/*
11/*	void	msg_vstream_init(progname, stream)
12/*	const char *progname;
13/*	VSTREAM	*stream;
14/* DESCRIPTION
15/*	This module implements support to report msg(3) diagnostics
16/*	to a VSTREAM.
17/*
18/*	msg_vstream_init() sets the program name that appears in each output
19/*	record, and directs diagnostics (see msg(3)) to the specified
20/*	VSTREAM. The \fIprogname\fR argument is not copied.
21/* SEE ALSO
22/*	msg(3)
23/* BUGS
24/*	No guarantee that long records are written atomically.
25/*	Only the last msg_vstream_init() call takes effect.
26/* LICENSE
27/* .ad
28/* .fi
29/*	The Secure Mailer license must be distributed with this software.
30/* AUTHOR(S)
31/*	Wietse Venema
32/*	IBM T.J. Watson Research
33/*	P.O. Box 704
34/*	Yorktown Heights, NY 10598, USA
35/*--*/
36
37/* System libraries. */
38
39#include <sys_defs.h>
40#include <errno.h>
41#include <stdlib.h>			/* 44BSD stdarg.h uses abort() */
42#include <stdarg.h>
43
44/* Utility library. */
45
46#include "vstream.h"
47#include "msg.h"
48#include "msg_output.h"
49#include "msg_vstream.h"
50
51 /*
52  * Private state.
53  */
54static const char *msg_tag;
55static VSTREAM *msg_stream;
56
57/* msg_vstream_print - log diagnostic to VSTREAM */
58
59static void msg_vstream_print(int level, const char *text)
60{
61    static const char *level_text[] = {
62	"info", "warning", "error", "fatal", "panic",
63    };
64
65    if (level < 0 || level >= (int) (sizeof(level_text) / sizeof(level_text[0])))
66	msg_panic("invalid severity level: %d", level);
67    if (level == MSG_INFO) {
68	vstream_fprintf(msg_stream, "%s: %s\n",
69			msg_tag, text);
70    } else {
71	vstream_fprintf(msg_stream, "%s: %s: %s\n",
72			msg_tag, level_text[level], text);
73    }
74    vstream_fflush(msg_stream);
75}
76
77/* msg_vstream_init - initialize */
78
79void    msg_vstream_init(const char *name, VSTREAM *vp)
80{
81    static int first_call = 1;
82
83    msg_tag = name;
84    msg_stream = vp;
85    if (first_call) {
86	first_call = 0;
87	msg_output(msg_vstream_print);
88    }
89}
90