Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

gdp / ep / ep_app.c @ master

History | View | Annotate | Download (8.39 KB)

1
/* vim: set ai sw=8 sts=8 ts=8 :*/
2

    
3
/***********************************************************************
4
**  ----- BEGIN LICENSE BLOCK -----
5
**        LIBEP: Enhanced Portability Library (Reduced Edition)
6
**
7
**        Copyright (c) 2008-2019, Eric P. Allman.  All rights reserved.
8
**        Copyright (c) 2015-2019, Regents of the University of California.
9
**        All rights reserved.
10
**
11
**        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
**
17
**        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
**
22
**        REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
23
**        LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
**        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
**  ----- END LICENSE BLOCK -----
29
***********************************************************************/
30

    
31
#include <ep.h>
32
#include <ep_app.h>
33
#include <ep_log.h>
34
#include <ep_string.h>
35

    
36
#include <stdlib.h>
37
#include <sys/errno.h>
38

    
39

    
40
static uint32_t        OperationFlags = EP_APP_FLAG_LOG_ABORT;
41

    
42

    
43
////////////////////////////////////////////////////////////////////////
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
#elif __linux__
57
        extern char *program_invocation_short_name;
58
        return program_invocation_short_name;
59
#else
60
        return NULL;
61
# endif
62
}
63

    
64

    
65
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
                { tag, colors, EP_APP_FLAG_LOG_ ## logflag }
74

    
75
static struct msginfo        MsgInfo[10] =
76
{
77
        MSGINFO("OK",                "gk",                OK        ),
78
        MSGINFO("OK",                "gk",                OK        ),
79
        MSGINFO("OK",                "gk",                OK        ),
80
        MSGINFO("OK",                "gk",                OK        ),
81
        MSGINFO("WARN",                "yk",                WARNING        ),
82
        MSGINFO("ERROR",        "ry",                ERROR        ),
83
        MSGINFO("SEVERE",        "yr",                SEVERE        ),
84
        MSGINFO("ABORT",        "wr",                ABORT        ),
85
        MSGINFO("FATAL",        "wr",                FATAL        ),
86
        MSGINFO("INFO",                "ck",                INFO        ),
87
};
88

    
89
#define EP_STAT_SEV_FATAL        8        // pseudo-value
90
#define EP_STAT_SEV_INFO        9        // pseudo-value
91

    
92

    
93

    
94
////////////////////////////////////////////////////////////////////////
95
//
96
//  PRINTMESSAGE -- helper routine for message printing
97
//
98

    
99
static void
100
printmessage(struct msginfo *mi,
101
                const char *emsg,
102
                const char *fmt,
103
                va_list av)
104
{
105
        const char *progname;
106
        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

    
134
        if (fg == NULL)
135
                fg = "";
136
        if (bg == NULL)
137
                bg = "";
138

    
139
        flockfile(stderr);
140
        fprintf(stderr, "[%s%s%s%s] ",
141
                        fg, bg, mi->tag, EpVid->vidnorm);
142
        if ((progname = ep_app_getprogname()) != NULL)
143
                fprintf(stderr, "%s: ", progname);
144
        if (fmt != NULL)
145
                vfprintf(stderr, fmt, av);
146
        else
147
                fprintf(stderr, "unknown %s", mi->tag);
148
        if (emsg != NULL)
149
                fprintf(stderr, ":\n\t%s", emsg);
150
        if (errno != 0)
151
        {
152
                char nbuf[40];
153

    
154
                (void) (0 == strerror_r(errno, nbuf, sizeof nbuf));
155
                fprintf(stderr, "\n\t(%s)", nbuf);
156
        }
157
        fprintf(stderr, "\n");
158
        funlockfile(stderr);
159
}
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
ep_app_messagev(
174
        EP_STAT estat,
175
        const char *fmt,
176
        va_list av)
177
{
178
        struct msginfo *mi = &MsgInfo[EP_STAT_SEVERITY(estat)];
179
        char ebuf[100];
180
        va_list av2;
181

    
182
        ep_stat_tostr(estat, ebuf, sizeof ebuf);
183
        errno = 0;
184
        if (EP_UT_BITSET(mi->flag, OperationFlags))
185
                va_copy(av2, av);
186
        printmessage(mi, ebuf, fmt, av);
187
        if (EP_UT_BITSET(mi->flag, OperationFlags))
188
        {
189
                ep_logv(estat, fmt, av2);
190
                va_end(av2);
191
        }
192
}
193

    
194
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

    
209
////////////////////////////////////////////////////////////////////////
210
//
211
//  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
        printmessage(&MsgInfo[EP_STAT_SEV_INFO], NULL, fmt, av);
228
        va_end(av);
229

    
230
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_INFO, OperationFlags))
231
        {
232
                va_start(av, fmt);
233
                ep_logv(EP_STAT_OK, fmt, av);
234
                va_end(av);
235
        }
236
}
237

    
238

    
239
////////////////////////////////////////////////////////////////////////
240
//
241
//  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
        printmessage(&MsgInfo[EP_STAT_SEV_WARN], NULL, fmt, av);
260
        va_end(av);
261

    
262
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_WARNING, OperationFlags))
263
        {
264
                va_start(av, fmt);
265
                ep_logv(EP_STAT_WARN, fmt, av);
266
                va_end(av);
267
        }
268
}
269

    
270

    
271
////////////////////////////////////////////////////////////////////////
272
//
273
//  EP_APP_ERROR -- print an error message
274
//
275
//        Parameters:
276
//                fmt -- format for a message
277
//                        If NULL it prints errno.
278
//                ... -- 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
        printmessage(&MsgInfo[EP_STAT_SEV_ERROR], NULL, fmt, av);
293
        va_end(av);
294

    
295
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_ERROR, OperationFlags))
296
        {
297
                va_start(av, fmt);
298
                ep_logv(EP_STAT_ERROR, fmt, av);
299
                va_end(av);
300
        }
301
}
302

    
303

    
304
////////////////////////////////////////////////////////////////////////
305
//
306
//  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
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_SEVERE, OperationFlags))
329
        {
330
                va_start(av, fmt);
331
                ep_logv(EP_STAT_SEVERE, fmt, av);
332
                va_end(av);
333
        }
334
}
335

    
336

    
337
////////////////////////////////////////////////////////////////////////
338
//
339
//  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
        printmessage(&MsgInfo[EP_STAT_SEV_FATAL], NULL, fmt, av);
360
        va_end(av);
361

    
362
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_FATAL, OperationFlags))
363
        {
364
                va_start(av, fmt);
365
                ep_logv(EP_STAT_SEVERE, fmt, av);
366
                va_end(av);
367
        }
368

    
369
        fprintf(stderr, "\t(exiting)\n");
370
        exit(1);
371
        /*NOTREACHED*/
372
}
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
        printmessage(&MsgInfo[EP_STAT_SEV_ABORT], NULL, fmt, av);
396
        va_end(av);
397

    
398
        if (EP_UT_BITSET(EP_APP_FLAG_LOG_ABORT, OperationFlags))
399
        {
400
                va_start(av, fmt);
401
                ep_logv(EP_STAT_ABORT, fmt, av);
402
                va_end(av);
403
        }
404

    
405
        fprintf(stderr, "\n\t(exiting)\n");
406
        abort();
407
        /*NOTREACHED*/
408
}