Add packaging/epson-inkjet-printer-escpr.changes file
[platform/upstream/epson-inkjet-printer-escpr.git] / src / err.c
1 /*
2  * Epson Inkjet Printer Driver (ESC/P-R) for Linux
3  * Copyright (C) 2002-2005 AVASYS CORPORATION.
4  * Copyright (C) Seiko Epson Corporation 2002-2012.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "err.h"
31
32 #define HAVE_DEBUG 0
33
34 /* global  */
35 static char err_pname[256] = "";
36 static FILE *debug_f = NULL;
37
38 /* static functions */
39 static void err_doit (enum msgtype, int, const char *, va_list);
40
41 void debug_msg(const char *fmt, ...){
42 #if (HAVE_DEBUG)        
43         va_list ap;
44         
45         if(!debug_f){
46                 debug_f = fopen(DEBUG_PATH, "wb");
47                 if(debug_f == NULL){
48                         return;
49                 }
50                 fchmod (fileno (debug_f), 0777);
51         }
52         
53         va_start (ap, fmt);
54         vfprintf (debug_f, fmt, ap);
55         fflush(debug_f);
56         va_end (ap);
57 #endif
58         return;
59 }
60
61 void
62 err_init (const char *name)
63 {
64         if (name && strlen (name) < 256)
65                 strcpy (err_pname, name);
66         return;
67 }
68
69 void
70 err_msg (enum msgtype type, const char *fmt, ...)
71 {
72         va_list ap;
73
74         va_start (ap, fmt);
75         err_doit (type, 0, fmt, ap);
76
77         va_end (ap);
78         return;
79 }
80
81 void
82 err_fatal (const char *fmt, ...)
83 {
84         va_list ap;
85
86         va_start (ap, fmt);
87         err_doit (MSGTYPE_ERROR, 0, fmt, ap);
88
89         va_end (ap);
90         exit (1);
91 }
92
93 void
94 err_system (const char *fmt,...)
95 {
96         int e;
97         va_list ap;
98
99         e = errno;
100         va_start (ap, fmt);
101         err_doit (MSGTYPE_ERROR, e, fmt, ap);
102
103         va_end (ap);
104         exit (1);
105 }
106
107 static void
108 err_doit (enum msgtype type, int e, const char *fmt, va_list ap)
109 {
110         if (err_pname[0] != '\0')
111                 fprintf (stderr, "%s : ", err_pname);
112
113         if (type == MSGTYPE_ERROR)
114                 fprintf (stderr, "**** ERROR **** : ");
115         else if (type == MSGTYPE_WARNING)
116                 fprintf (stderr, "**** WARNING **** : ");
117         else if (type == MSGTYPE_INFO)
118                 fprintf (stderr, "**** INFO **** : ");
119                 
120         vfprintf (stderr, fmt, ap);
121
122         if (e)
123                 fprintf (stderr, " : %s", strerror (e));
124         
125         fprintf (stderr, "\n");
126         fflush (stderr);
127         return;
128 }