Tizen 2.0 Release
[external/epson-laser-printer-escpage.git] / src / err.c
1 /*
2  * EPSON ESC/P-R Printer Driver for Linux
3  * Copyright (C) 2002-2005 AVASYS CORPORATION.
4  * Copyright (C) Seiko Epson Corporation 2002-2005.
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  * As a special exception, AVASYS CORPORATION gives permission to
21  * link the code of this program with libraries which are covered by
22  * the AVASYS Public License and distribute their linked
23  * combinations.  You must obey the GNU General Public License in all
24  * respects for all of the code used other than the libraries which
25  * are covered by AVASYS Public License.
26  */
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "err.h"
37 #include "time.h"
38
39 #define HAVE_DEBUG 0
40
41 /* global  */
42 static char err_pname[256] = "";
43 static FILE *debug_f = NULL;
44 static FILE *debug_f2 = NULL;
45
46 /* static functions */
47 static void err_doit (enum msgtype, int, const char *, va_list);
48
49 void debug_msg(const char *fmt, ...){
50 #if (HAVE_DEBUG)        
51         va_list ap;
52         
53         if(!debug_f){
54                 debug_f = fopen(DEBUG_PATH, "wb");
55                 if(debug_f == NULL){
56                         return;
57                 }
58                 fchmod (fileno (debug_f), 0777);
59         }
60         
61         va_start (ap, fmt);
62         vfprintf (debug_f, fmt, ap);
63         fflush(debug_f);
64         va_end (ap);
65 #endif
66         return;
67 }
68
69 void debugt_msg(char *  fmt, ...)
70 {
71 #if (HAVE_DEBUG_T)
72         if(fmt == NULL || strlen(fmt) == 0){
73                 return;
74         }
75         
76    va_list ap;
77         if(!debug_f2){
78                 debug_f2 = fopen(EPS_DEBUG_PATH2, "wb");
79                 if(debug_f2 == NULL){
80                         return;
81                 }
82                 fchmod (fileno (debug_f2), 0644);
83         }
84         
85         time_t ltime; /* calendar time */
86         struct tm *Tm;
87         struct timespec start;
88         char szBuf[256];
89         char *p = szBuf;
90         int err = 0;
91         
92         ltime=time(NULL); /* get current cal time */
93         Tm = localtime(&ltime);
94    err = clock_gettime(CLOCK_REALTIME, &start);
95    if(err == 0){
96         sprintf(szBuf, "[%02d-%02d-%02d %02d:%02d:%02d::%02d] %s", Tm->tm_year % 10, Tm->tm_mon + 1, Tm->tm_mday, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, (int)start.tv_nsec/1000000, fmt);
97    }else{
98         sprintf(szBuf, "[%02d-%02d-%02d %02d:%02d:%02d] %s", Tm->tm_year % 10, Tm->tm_mon + 1, Tm->tm_mday, Tm->tm_hour, Tm->tm_min, Tm->tm_sec, fmt);
99    }
100         
101         va_start (ap, szBuf);
102         vfprintf (debug_f2, szBuf, ap);
103         fflush(debug_f2);
104         va_end (ap);
105 #endif
106         return 0;
107 }
108
109 void
110 err_init (const char *name)
111 {
112         if (name && strlen (name) < 256)
113                 strcpy (err_pname, name);
114         return;
115 }
116
117 void
118 err_msg (enum msgtype type, const char *fmt, ...)
119 {
120         va_list ap;
121
122         va_start (ap, fmt);
123         err_doit (type, 0, fmt, ap);
124
125         va_end (ap);
126         return;
127 }
128
129 void
130 err_fatal (const char *fmt, ...)
131 {
132         va_list ap;
133
134         va_start (ap, fmt);
135         err_doit (MSGTYPE_ERROR, 0, fmt, ap);
136
137         va_end (ap);
138         exit (1);
139 }
140
141 void
142 err_system (const char *fmt,...)
143 {
144         int e;
145         va_list ap;
146
147         e = errno;
148         va_start (ap, fmt);
149         err_doit (MSGTYPE_ERROR, e, fmt, ap);
150
151         va_end (ap);
152         exit (1);
153 }
154
155 static void
156 err_doit (enum msgtype type, int e, const char *fmt, va_list ap)
157 {
158         if (err_pname[0] != '\0')
159                 fprintf (stderr, "%s : ", err_pname);
160
161         if (type == MSGTYPE_ERROR)
162                 fprintf (stderr, "**** ERROR **** : ");
163         else if (type == MSGTYPE_WARNING)
164                 fprintf (stderr, "**** WARNING **** : ");
165         else if (type == MSGTYPE_INFO)
166                 fprintf (stderr, "**** INFO **** : ");
167                 
168         vfprintf (stderr, fmt, ap);
169
170         if (e)
171                 fprintf (stderr, " : %s", strerror (e));
172         
173         fprintf (stderr, "\n");
174         fflush (stderr);
175         return;
176 }