1 | /* copyright (C) 2001 Sun Microsystems, Inc. and Dan Mueth*/ |
---|
2 | |
---|
3 | /* |
---|
4 | * This library is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU Lesser General Public |
---|
6 | * License as published by the Free Software Foundation; either |
---|
7 | * version 2.1 of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This library is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
12 | * Lesser General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU Lesser General Public |
---|
15 | * License along with this library; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | #include <config.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <stdarg.h> |
---|
22 | #include <scrollkeeper.h> |
---|
23 | #include <errno.h> |
---|
24 | #include <string.h> |
---|
25 | #include <libintl.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <sys/stat.h> |
---|
28 | #include <time.h> |
---|
29 | |
---|
30 | /* |
---|
31 | * sk_warning: |
---|
32 | * This is a general purpose function for sending error and |
---|
33 | * warning messages to STDOUT and/or a log file. |
---|
34 | * |
---|
35 | * verbose: 0 or 1 indicating whether the output goes only to the log |
---|
36 | * file (0) or to both STDOUT and the log file (1). |
---|
37 | * |
---|
38 | * funct_name: A string which is only printed to the log file and not to |
---|
39 | * STDOUT. This is useful for command-line routines where it |
---|
40 | * doesn't make sense to put the name of the routine in the |
---|
41 | * warning/error message. For internal functions, we typically |
---|
42 | * leave this empty and place the function name in paranthesis |
---|
43 | * inside the 'format' string. |
---|
44 | */ |
---|
45 | |
---|
46 | void sk_warning(int verbose, char *funct_name, char *format, ...) |
---|
47 | { |
---|
48 | va_list ap; |
---|
49 | struct stat buf; |
---|
50 | FILE *fid; |
---|
51 | time_t current_time; |
---|
52 | struct tm *tm; |
---|
53 | char datestamp[512]; |
---|
54 | |
---|
55 | /* |
---|
56 | * Write to STDOUT first, since we return if there is a problem |
---|
57 | * accessing the log file. |
---|
58 | */ |
---|
59 | va_start(ap, format); |
---|
60 | if (verbose) |
---|
61 | vfprintf(stderr, format, ap); |
---|
62 | va_end(ap); |
---|
63 | |
---|
64 | /* |
---|
65 | * Open log file, and rotate if necessary. |
---|
66 | */ |
---|
67 | if (stat(SCROLLKEEPERLOGFILE, &buf) == -1) { |
---|
68 | if (errno == ENOENT) { |
---|
69 | fid = fopen(SCROLLKEEPERLOGFILE, "w"); |
---|
70 | if (fid == NULL) { |
---|
71 | printf("Cannot create log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
72 | return; |
---|
73 | } |
---|
74 | } |
---|
75 | else { |
---|
76 | printf("Error accessing log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
77 | return; |
---|
78 | } |
---|
79 | } |
---|
80 | else { |
---|
81 | if (buf.st_size < (1<<24)) { |
---|
82 | fid = fopen(SCROLLKEEPERLOGFILE, "a"); |
---|
83 | if (fid == NULL) { |
---|
84 | printf("Cannot write to log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
85 | return; |
---|
86 | } |
---|
87 | } |
---|
88 | else { /* Rotate log file */ |
---|
89 | rename(SCROLLKEEPERLOGFILE, SCROLLKEEPERLOGFILE_ROT); |
---|
90 | fid = fopen(SCROLLKEEPERLOGFILE, "w"); |
---|
91 | if (fid == NULL) { |
---|
92 | printf("Cannot create log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
93 | return; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /* Write message to log file */ |
---|
99 | time (¤t_time); |
---|
100 | tm = localtime (¤t_time); |
---|
101 | strftime (datestamp, sizeof (datestamp), "%b %d %X", tm); |
---|
102 | va_start(ap, format); |
---|
103 | fprintf(fid,"%s %s :", datestamp, funct_name); |
---|
104 | vfprintf(fid, format, ap); |
---|
105 | va_end(ap); |
---|
106 | fclose(fid); |
---|
107 | } |
---|
108 | |
---|
109 | void |
---|
110 | check_ptr (void *p, char *name) |
---|
111 | { |
---|
112 | if (p == NULL) |
---|
113 | { |
---|
114 | fprintf (stderr, _("%s: out of memory: %s\n"), name, strerror (errno)); |
---|
115 | exit (EXIT_FAILURE); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | static void reconcile_skout_prefs(char outputprefs, int stdout_threshold, int log_threshold, int *do_stdout_message, int *do_log_message) |
---|
120 | { |
---|
121 | int stdout_value, log_value; |
---|
122 | |
---|
123 | /* |
---|
124 | * Up to this point, the various flags have not been checked for self-consistancy. |
---|
125 | * For example, quiet and debug may both be turned on. |
---|
126 | * |
---|
127 | * Order of priority is: |
---|
128 | * DEBUG |
---|
129 | * QUIET |
---|
130 | * VERBOSE |
---|
131 | * DEFAULT |
---|
132 | * |
---|
133 | * We assign a final value according to: |
---|
134 | * DEBUG = SKOUT_DEBUG = 4 |
---|
135 | * VERBOSE = SKOUT_VERBOSE = 3 |
---|
136 | * DEFAULT = SKOUT_DEFAULT = 2 |
---|
137 | * QUIET = SKOUT_QUIET = 1 |
---|
138 | * |
---|
139 | * If the threshold value is less than this number, then the message |
---|
140 | * event will occur. |
---|
141 | */ |
---|
142 | |
---|
143 | stdout_value=SKOUT_DEFAULT; |
---|
144 | if (outputprefs & SKOUT_STD_VERBOSE) stdout_value=SKOUT_VERBOSE; |
---|
145 | if (outputprefs & SKOUT_STD_QUIET) stdout_value=SKOUT_QUIET; |
---|
146 | if (outputprefs & SKOUT_STD_DEBUG) stdout_value=SKOUT_DEBUG; |
---|
147 | |
---|
148 | log_value=SKOUT_DEFAULT; |
---|
149 | if (outputprefs & SKOUT_LOG_VERBOSE) log_value=SKOUT_VERBOSE; |
---|
150 | if (outputprefs & SKOUT_LOG_QUIET) log_value=SKOUT_QUIET; |
---|
151 | if (outputprefs & SKOUT_LOG_DEBUG) log_value=SKOUT_DEBUG; |
---|
152 | |
---|
153 | if (stdout_value >= stdout_threshold) *do_stdout_message=1; |
---|
154 | if (log_value >= log_threshold) *do_log_message=1; |
---|
155 | } |
---|
156 | |
---|
157 | /* |
---|
158 | * sk_message: |
---|
159 | * This is a general purpose function for sending error and |
---|
160 | * warning messages to STDOUT and/or a log file. |
---|
161 | * |
---|
162 | * verbose: 0 or 1 indicating whether the output goes only to the log |
---|
163 | * file (0) or to both STDOUT and the log file (1). |
---|
164 | * |
---|
165 | * funct_name: A string which is only printed to the log file and not to |
---|
166 | * STDOUT. This is useful for command-line routines where it |
---|
167 | * doesn't make sense to put the name of the routine in the |
---|
168 | * warning/error message. For internal functions, we typically |
---|
169 | * leave this empty and place the function name in paranthesis |
---|
170 | * inside the 'format' string. |
---|
171 | */ |
---|
172 | |
---|
173 | extern void sk_message(char outputprefs, int stdout_threshold, int log_threshold, char *funct_name, char *format, ...) |
---|
174 | { |
---|
175 | va_list ap; |
---|
176 | struct stat buf; |
---|
177 | FILE *fid; |
---|
178 | time_t current_time; |
---|
179 | struct tm *tm; |
---|
180 | char datestamp[512]; |
---|
181 | int do_stdout_message=0, do_log_message=0; |
---|
182 | |
---|
183 | reconcile_skout_prefs(outputprefs, stdout_threshold, log_threshold, &do_stdout_message, &do_log_message); |
---|
184 | |
---|
185 | /* |
---|
186 | * Write to STDOUT if do_log_message is set. |
---|
187 | */ |
---|
188 | if (do_stdout_message) { |
---|
189 | /* |
---|
190 | * (We write to STDOUT first, since we return if there is a problem |
---|
191 | * accessing the log file.) |
---|
192 | */ |
---|
193 | va_start(ap, format); |
---|
194 | vfprintf(stderr, format, ap); |
---|
195 | va_end(ap); |
---|
196 | } |
---|
197 | |
---|
198 | /* |
---|
199 | * Write to log file if do_log_message is set. |
---|
200 | */ |
---|
201 | if (do_log_message) { |
---|
202 | /* |
---|
203 | * Open log file, and rotate if necessary. |
---|
204 | */ |
---|
205 | if (stat(SCROLLKEEPERLOGFILE, &buf) == -1) { |
---|
206 | if (errno == ENOENT) { |
---|
207 | fid = fopen(SCROLLKEEPERLOGFILE, "w"); |
---|
208 | if (fid == NULL) { |
---|
209 | printf("Cannot create log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
210 | return; |
---|
211 | } |
---|
212 | } |
---|
213 | else { |
---|
214 | printf("Error accessing log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
215 | return; |
---|
216 | } |
---|
217 | } |
---|
218 | else { |
---|
219 | if (buf.st_size < (1<<24)) { |
---|
220 | fid = fopen(SCROLLKEEPERLOGFILE, "a"); |
---|
221 | if (fid == NULL) { |
---|
222 | printf("Cannot write to log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
223 | return; |
---|
224 | } |
---|
225 | } |
---|
226 | else { /* Rotate log file */ |
---|
227 | rename(SCROLLKEEPERLOGFILE, SCROLLKEEPERLOGFILE_ROT); |
---|
228 | fid = fopen(SCROLLKEEPERLOGFILE, "w"); |
---|
229 | if (fid == NULL) { |
---|
230 | printf("Cannot create log file: %s : %s\n", SCROLLKEEPERLOGFILE, strerror(errno)); |
---|
231 | return; |
---|
232 | } |
---|
233 | } |
---|
234 | } |
---|
235 | |
---|
236 | /* Write message to log file */ |
---|
237 | time (¤t_time); |
---|
238 | tm = localtime (¤t_time); |
---|
239 | strftime (datestamp, sizeof (datestamp), "%b %d %X", tm); |
---|
240 | va_start(ap, format); |
---|
241 | fprintf(fid,"%s %s: ", datestamp, funct_name); |
---|
242 | vfprintf(fid, format, ap); |
---|
243 | va_end(ap); |
---|
244 | fclose(fid); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | /* |
---|
250 | * Grab the warning and error messages from libxml validating the OMF file |
---|
251 | * against the DTD and pass it to sk_message(). |
---|
252 | */ |
---|
253 | void sk_dtd_validation_message(char *outputprefs, char *format, ...) |
---|
254 | { |
---|
255 | va_list ap; |
---|
256 | char message[4096]; |
---|
257 | |
---|
258 | va_start(ap, format); |
---|
259 | vsprintf(message, format, ap); |
---|
260 | va_end(ap); |
---|
261 | |
---|
262 | sk_message(*outputprefs, SKOUT_VERBOSE, SKOUT_DEBUG, "(install)",_("OMF validation error: %s"), message); |
---|
263 | } |
---|