summaryrefslogtreecommitdiff
path: root/libmailutils/stdstream/strerr.c
blob: 76dd2cb7fbfd8a378959805d12098ed3ea48c44d (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010 Free Software Foundation, Inc.

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

   This library 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 Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <mailutils/types.h>
#include <mailutils/nls.h>
#include <mailutils/io.h>
#include <mailutils/errno.h>
#include <mailutils/log.h>
#include <mailutils/stream.h>
#include <mailutils/stdstream.h>
#include <mailutils/filter.h>

int
mu_stdstream_strerr_create (mu_stream_t *plogger, int type, int facility,
			    int priority, const char *tag,
			    const char *fname MU_ARG_UNUSED)
{
  int rc;
  mu_stream_t transport;
  
  if (!tag)
    tag = mu_program_name;
  switch (type)
    {
    case MU_STRERR_STDERR:
      {
	mu_stream_t str;
	char *fltargs[3] = { "INLINE-COMMENT", };
      
	rc = mu_stdio_stream_create (&str, MU_STDERR_FD, 0);
	if (rc)
	  {
	    fprintf (stderr, _("%s: cannot open error stream: %s\n"),
		     tag, mu_strerror (rc));
	    return MU_ERR_FAILURE;
	  }
	mu_asprintf (&fltargs[1], "%s: ", tag);
	fltargs[2] = NULL;
	rc = mu_filter_create_args (&transport, str,
				    "INLINE-COMMENT", 2, (const char**)fltargs,
				    MU_FILTER_ENCODE, MU_STREAM_WRITE);
	mu_stream_unref (str);
	free (fltargs[1]);
	if (rc)
	  {
	    fprintf (stderr,
		     _("%s: cannot open output filter stream: %s"),
		     tag, mu_strerror (rc));
	    return MU_ERR_FAILURE;
	  }
      }
      break;

    case MU_STRERR_SYSLOG:
      openlog (tag, LOG_PID, facility);
      rc = mu_syslog_stream_create (&transport, priority);
      if (rc)
	{
	  fprintf (stderr, _("%s: cannot create syslog stream: %s\n"),
		   tag, mu_strerror (rc));
	  return MU_ERR_FAILURE;
	}
      break;

    default:
      fprintf (stderr, _("%s: cannot create error stream: %s\n"),
	       tag, mu_strerror (EINVAL));
      return EINVAL;
    }

  rc = mu_log_stream_create (plogger, transport);
  mu_stream_unref (transport);
  if (rc)
    {
      fprintf (stderr, _("%s: cannot open logger stream: %s\n"),
	       tag , mu_strerror (rc));
      return MU_ERR_FAILURE;
    }
  return 0;
}
      

Return to:

Send suggestions and report system problems to the System administrator.