Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_app.c @ master

History | View | Annotate | Download (8.39 KB)

1 a901db09 Eric Allman
/* vim: set ai sw=8 sts=8 ts=8 :*/
2 0c663d10 Eric Allman
3 47c6ea64 Eric Allman
/***********************************************************************
4 055d3009 Eric Allman
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7 c87dd166 Eric Allman
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9 6bd5476b Eric Allman
**        All rights reserved.
10 055d3009 Eric Allman
**
11 6bd5476b Eric Allman
**        Permission is hereby granted, without written agreement and without
12
**        license or royalty fees, to use, copy, modify, and distribute this
13
**        software and its documentation for any purpose, provided that the above
14
**        copyright notice and the following two paragraphs appear in all copies
15
**        of this software.
16 055d3009 Eric Allman
**
17 6bd5476b Eric Allman
**        IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
18
**        SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
19
**        PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
20
**        EVEN IF REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21 055d3009 Eric Allman
**
22 6bd5476b Eric Allman
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23 055d3009 Eric Allman
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 6bd5476b Eric Allman
**        FOR A PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION,
25
**        IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO
26
**        OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
27
**        OR MODIFICATIONS.
28 055d3009 Eric Allman
**  ----- END LICENSE BLOCK -----
29 47c6ea64 Eric Allman
***********************************************************************/
30
31
#include <ep.h>
32
#include <ep_app.h>
33 b73e1ad7 Eric Allman
#include <ep_log.h>
34 19a09ae1 Eric Allman
#include <ep_string.h>
35 b73e1ad7 Eric Allman
36 47c6ea64 Eric Allman
#include <stdlib.h>
37 34a5c900 Eric Allman
#include <sys/errno.h>
38 47c6ea64 Eric Allman
39
40 9ab0fd1a Eric Allman
static uint32_t        OperationFlags = EP_APP_FLAG_LOG_ABORT;
41 b73e1ad7 Eric Allman
42
43 47c6ea64 Eric Allman
////////////////////////////////////////////////////////////////////////
44
//
45
//  Application support
46
//
47
//        Just to make life easier for app writers
48
//
49
////////////////////////////////////////////////////////////////////////
50
51
const char *
52
ep_app_getprogname(void)
53
{
54
#if EP_OSCF_HAS_GETPROGNAME
55
        return getprogname();
56 7af4d080 Eric Allman
#elif __linux__
57 47c6ea64 Eric Allman
        extern char *program_invocation_short_name;
58
        return program_invocation_short_name;
59
#else
60
        return NULL;
61
# endif
62
}
63
64 6c598524 Eric Allman
65 19a09ae1 Eric Allman
struct msginfo
66
{
67
        const char        *tag;                // message tag (e.g., "ERROR")
68
        const char        *colors;        // colors (foreground, background)
69
        uint32_t        flag;                // flag to test for logging
70
};
71
72
#define MSGINFO(tag, colors, logflag)        \
73 9ab0fd1a Eric Allman
                { tag, colors, EP_APP_FLAG_LOG_ ## logflag }
74 19a09ae1 Eric Allman
75 9ab0fd1a Eric Allman
static struct msginfo        MsgInfo[10] =
76 19a09ae1 Eric Allman
{
77 9ab0fd1a Eric Allman
        MSGINFO("OK",                "gk",                OK        ),
78
        MSGINFO("OK",                "gk",                OK        ),
79
        MSGINFO("OK",                "gk",                OK        ),
80
        MSGINFO("OK",                "gk",                OK        ),
81 19a09ae1 Eric Allman
        MSGINFO("WARN",                "yk",                WARNING        ),
82
        MSGINFO("ERROR",        "ry",                ERROR        ),
83 9fd2a718 Eric Allman
        MSGINFO("SEVERE",        "yr",                SEVERE        ),
84 19a09ae1 Eric Allman
        MSGINFO("ABORT",        "wr",                ABORT        ),
85
        MSGINFO("FATAL",        "wr",                FATAL        ),
86 9ab0fd1a Eric Allman
        MSGINFO("INFO",                "ck",                INFO        ),
87 19a09ae1 Eric Allman
};
88
89
#define EP_STAT_SEV_FATAL        8        // pseudo-value
90 9ab0fd1a Eric Allman
#define EP_STAT_SEV_INFO        9        // pseudo-value
91 19a09ae1 Eric Allman
92
93 6c598524 Eric Allman
94
////////////////////////////////////////////////////////////////////////
95
//
96
//  PRINTMESSAGE -- helper routine for message printing
97
//
98
99
static void
100 19a09ae1 Eric Allman
printmessage(struct msginfo *mi,
101
                const char *emsg,
102 778d2c65 Eric Allman
                const char *fmt,
103
                va_list av)
104 6c598524 Eric Allman
{
105
        const char *progname;
106 19a09ae1 Eric Allman
        const char *fg;
107
        const char *bg;
108
109
        switch (mi->colors[0])
110
        {
111
          case 'b': fg = EpVid->vidfgblue;        break;
112
          case 'c': fg = EpVid->vidfgcyan;        break;
113
          case 'g': fg = EpVid->vidfggreen;        break;
114
          case 'k': fg = EpVid->vidfgblack;        break;
115
          case 'm': fg = EpVid->vidfgmagenta;        break;
116
          case 'r': fg = EpVid->vidfgred;        break;
117
          case 'w': fg = EpVid->vidfgwhite;        break;
118
          case 'y': fg = EpVid->vidfgyellow;        break;
119
          default:  fg = NULL;                        break;
120
        }
121
        switch (mi->colors[1])
122
        {
123
          case 'b': bg = EpVid->vidbgblue;        break;
124
          case 'c': bg = EpVid->vidbgcyan;        break;
125
          case 'g': bg = EpVid->vidbggreen;        break;
126
          case 'k': bg = EpVid->vidbgblack;        break;
127
          case 'm': bg = EpVid->vidbgmagenta;        break;
128
          case 'r': bg = EpVid->vidbgred;        break;
129
          case 'w': bg = EpVid->vidbgwhite;        break;
130
          case 'y': bg = EpVid->vidbgyellow;        break;
131
          default:  bg = NULL;                        break;
132
        }
133 6c598524 Eric Allman
134 778d2c65 Eric Allman
        if (fg == NULL)
135
                fg = "";
136
        if (bg == NULL)
137
                bg = "";
138
139 1aa4fc2c Eric Allman
        flockfile(stderr);
140 19a09ae1 Eric Allman
        fprintf(stderr, "[%s%s%s%s] ",
141
                        fg, bg, mi->tag, EpVid->vidnorm);
142 6c598524 Eric Allman
        if ((progname = ep_app_getprogname()) != NULL)
143
                fprintf(stderr, "%s: ", progname);
144
        if (fmt != NULL)
145
                vfprintf(stderr, fmt, av);
146
        else
147 19a09ae1 Eric Allman
                fprintf(stderr, "unknown %s", mi->tag);
148
        if (emsg != NULL)
149
                fprintf(stderr, ":\n\t%s", emsg);
150 6c598524 Eric Allman
        if (errno != 0)
151
        {
152
                char nbuf[40];
153
154 bfecf573 Eric Allman
                (void) (0 == strerror_r(errno, nbuf, sizeof nbuf));
155 6c598524 Eric Allman
                fprintf(stderr, "\n\t(%s)", nbuf);
156
        }
157 19a09ae1 Eric Allman
        fprintf(stderr, "\n");
158 1aa4fc2c Eric Allman
        funlockfile(stderr);
159 19a09ae1 Eric Allman
}
160
161
162
////////////////////////////////////////////////////////////////////////
163
//
164
//  EP_APP_MESSAGE -- print a message (severity from EP_STAT)
165
//
166
//        Parameters:
167
//                estat -- status code (printed)
168
//                fmt -- format for a message
169
//                ... -- arguments
170
//
171
172
void
173 a8edf5d7 Eric Allman
ep_app_messagev(
174 19a09ae1 Eric Allman
        EP_STAT estat,
175
        const char *fmt,
176 a8edf5d7 Eric Allman
        va_list av)
177 19a09ae1 Eric Allman
{
178
        struct msginfo *mi = &MsgInfo[EP_STAT_SEVERITY(estat)];
179
        char ebuf[100];
180 a8edf5d7 Eric Allman
        va_list av2;
181 19a09ae1 Eric Allman
182
        ep_stat_tostr(estat, ebuf, sizeof ebuf);
183
        errno = 0;
184 a8edf5d7 Eric Allman
        if (EP_UT_BITSET(mi->flag, OperationFlags))
185
                va_copy(av2, av);
186 19a09ae1 Eric Allman
        printmessage(mi, ebuf, fmt, av);
187
        if (EP_UT_BITSET(mi->flag, OperationFlags))
188
        {
189 a8edf5d7 Eric Allman
                ep_logv(estat, fmt, av2);
190
                va_end(av2);
191 19a09ae1 Eric Allman
        }
192 6c598524 Eric Allman
}
193
194 a8edf5d7 Eric Allman
void
195
ep_app_message(
196
        EP_STAT estat,
197
        const char *fmt,
198
        ...)
199
{
200
        va_list av;
201
202
        errno = 0;
203
        va_start(av, fmt);
204
        ep_app_messagev(estat, fmt, av);
205
        va_end(av);
206
}
207
208 6c598524 Eric Allman
209 47c6ea64 Eric Allman
////////////////////////////////////////////////////////////////////////
210
//
211 174526d7 Eric Allman
//  EP_APP_INFO -- print an informational message
212
//
213
//        Parameters:
214
//                fmt -- format for a message
215
//                ... -- arguments
216
//
217
218
void
219
ep_app_info(
220
        const char *fmt,
221
        ...)
222
{
223
        va_list av;
224
225
        errno = 0;
226
        va_start(av, fmt);
227 9ab0fd1a Eric Allman
        printmessage(&MsgInfo[EP_STAT_SEV_INFO], NULL, fmt, av);
228 174526d7 Eric Allman
        va_end(av);
229
230 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_INFO, OperationFlags))
231 174526d7 Eric Allman
        {
232
                va_start(av, fmt);
233
                ep_logv(EP_STAT_OK, fmt, av);
234
                va_end(av);
235
        }
236
}
237
238
239
////////////////////////////////////////////////////////////////////////
240
//
241 47c6ea64 Eric Allman
//  EP_APP_WARN -- print a warning message
242
//
243
//        Parameters:
244
//                fmt -- format for a message
245
//                ... -- arguments
246
//
247
//        Returns:
248
//                never
249
//
250
251
void
252
ep_app_warn(
253
        const char *fmt,
254
        ...)
255
{
256
        va_list av;
257
258
        va_start(av, fmt);
259 19a09ae1 Eric Allman
        printmessage(&MsgInfo[EP_STAT_SEV_WARN], NULL, fmt, av);
260 47c6ea64 Eric Allman
        va_end(av);
261 b73e1ad7 Eric Allman
262 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_WARNING, OperationFlags))
263 b73e1ad7 Eric Allman
        {
264
                va_start(av, fmt);
265
                ep_logv(EP_STAT_WARN, fmt, av);
266
                va_end(av);
267
        }
268 47c6ea64 Eric Allman
}
269
270
271
////////////////////////////////////////////////////////////////////////
272
//
273
//  EP_APP_ERROR -- print an error message
274
//
275
//        Parameters:
276
//                fmt -- format for a message
277 34a5c900 Eric Allman
//                        If NULL it prints errno.
278 47c6ea64 Eric Allman
//                ... -- arguments
279
//
280
//        Returns:
281
//                never
282
//
283
284
void
285
ep_app_error(
286
        const char *fmt,
287
        ...)
288
{
289
        va_list av;
290
291
        va_start(av, fmt);
292 19a09ae1 Eric Allman
        printmessage(&MsgInfo[EP_STAT_SEV_ERROR], NULL, fmt, av);
293 6c598524 Eric Allman
        va_end(av);
294 b73e1ad7 Eric Allman
295 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_ERROR, OperationFlags))
296 b73e1ad7 Eric Allman
        {
297
                va_start(av, fmt);
298
                ep_logv(EP_STAT_ERROR, fmt, av);
299
                va_end(av);
300
        }
301 6c598524 Eric Allman
}
302 5bc1cdfb Eric Allman
303 6c598524 Eric Allman
304
////////////////////////////////////////////////////////////////////////
305
//
306 19a09ae1 Eric Allman
//  EP_APP_SEVERE -- print a severe message
307
//
308
//        Parameters:
309
//                fmt -- format for a message
310
//                        If NULL it prints errno.
311
//                ... -- arguments
312
//
313
//        Returns:
314
//                never
315
//
316
317
void
318
ep_app_severe(
319
        const char *fmt,
320
        ...)
321
{
322
        va_list av;
323
324
        va_start(av, fmt);
325
        printmessage(&MsgInfo[EP_STAT_SEV_SEVERE], NULL, fmt, av);
326
        va_end(av);
327
328 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_SEVERE, OperationFlags))
329 19a09ae1 Eric Allman
        {
330
                va_start(av, fmt);
331
                ep_logv(EP_STAT_SEVERE, fmt, av);
332
                va_end(av);
333
        }
334
}
335
336
337
////////////////////////////////////////////////////////////////////////
338
//
339 6c598524 Eric Allman
//  EP_APP_FATAL -- print a fatal error message and exit
340
//
341
//        Just uses a generic exit status.
342
//
343
//        Parameters:
344
//                fmt -- format for a message
345
//                ... -- arguments
346
//
347
//        Returns:
348
//                never
349
//
350
351
void
352
ep_app_fatal(
353
        const char *fmt,
354
        ...)
355
{
356
        va_list av;
357
358
        va_start(av, fmt);
359 19a09ae1 Eric Allman
        printmessage(&MsgInfo[EP_STAT_SEV_FATAL], NULL, fmt, av);
360 47c6ea64 Eric Allman
        va_end(av);
361 6c598524 Eric Allman
362 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_FATAL, OperationFlags))
363 b73e1ad7 Eric Allman
        {
364
                va_start(av, fmt);
365
                ep_logv(EP_STAT_SEVERE, fmt, av);
366
                va_end(av);
367
        }
368
369 6c598524 Eric Allman
        fprintf(stderr, "\t(exiting)\n");
370
        exit(1);
371
        /*NOTREACHED*/
372 47c6ea64 Eric Allman
}
373
374
375
////////////////////////////////////////////////////////////////////////
376
//
377
//  EP_APP_ABORT -- print an error message and abort
378
//
379
//        Parameters:
380
//                fmt -- format for a message
381
//                ... -- arguments
382
//
383
//        Returns:
384
//                never
385
//
386
387
void
388
ep_app_abort(
389
        const char *fmt,
390
        ...)
391
{
392
        va_list av;
393
394
        va_start(av, fmt);
395 19a09ae1 Eric Allman
        printmessage(&MsgInfo[EP_STAT_SEV_ABORT], NULL, fmt, av);
396 47c6ea64 Eric Allman
        va_end(av);
397
398 9ab0fd1a Eric Allman
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_ABORT, OperationFlags))
399 b73e1ad7 Eric Allman
        {
400
                va_start(av, fmt);
401
                ep_logv(EP_STAT_ABORT, fmt, av);
402
                va_end(av);
403
        }
404
405 6c598524 Eric Allman
        fprintf(stderr, "\n\t(exiting)\n");
406 34a5c900 Eric Allman
        abort();
407 47c6ea64 Eric Allman
        /*NOTREACHED*/
408
}