1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
24 * fmtmsg implementation
40 #define INIT_CONSOLE 0x2
49 const MM_table_t mm_class
[] =
52 "hard", "HARDWARE", MM_HARD
,
53 "soft", "SOFTWARE", MM_SOFT
,
54 "firm", "FIRMWARE", MM_FIRM
,
55 "appl", "APPLICATION", MM_APPL
,
56 "util", "UTILITY", MM_UTIL
,
57 "opsys", "KERNEL", MM_OPSYS
,
59 "console", 0, MM_CONSOLE
,
60 "recov", "RECOVERABLE", MM_RECOVER
,
61 "nrecov", "PANIC", MM_NRECOV
,
65 static const MM_table_t mm_severity_init
[] =
68 "halt", "HALT", MM_HALT
,
69 "error", "ERROR", MM_ERROR
,
70 "warn", "WARNING", MM_WARNING
,
71 "info", "INFO", MM_INFO
,
75 const MM_table_t mm_verb
[] =
78 "action", 0, MM_action
,
80 "default", 0, MM_default
,
82 "severity", 0, MM_severity
,
83 "source", 0, MM_source
,
92 static MM_table_t
* severity
;
97 register MM_table_t
* p
;
104 if ((s
= getenv(MM_SEVERITY_ENV
)) && *s
)
140 for (p
= (MM_table_t
*)mm_severity_init
; p
->name
; p
++);
141 n
+= p
- (MM_table_t
*)mm_severity_init
+ 1;
142 if (severity
= newof(0, MM_table_t
, n
, s
- e
))
144 s
= (char*)severity
+ n
* sizeof(MM_table_t
);
147 for (q
= (MM_table_t
*)mm_severity_init
; q
->name
; q
++)
162 p
->value
= strtol(s
, NiL
, 0);
183 severity
= (MM_table_t
*)mm_severity_init
;
185 return (const MM_table_t
*)severity
;
189 display(register const MM_table_t
* tab
, int value
, int mask
)
193 if (value
== tab
->value
|| mask
&& (value
& tab
->value
))
194 return (char*)tab
->display
;
201 fmtmsg(long classification
, const char* label
, int severity
, const char* text
, const char* action
, const char* tag
)
206 register MM_table_t
* p
;
213 char lab
[MM_LABEL_1_MAX
+ MM_LABEL_2_MAX
+ 3];
218 if (!(s
= getenv(MM_VERB_ENV
)))
219 mm
.mask
= MM_default
;
222 if (t
= strchr(s
, ':'))
224 if (!(p
= (MM_table_t
*)strlook(mm_verb
, sizeof(MM_table_t
), s
)))
226 mm
.mask
= MM_default
;
238 if (!(classification
& (MM_CONSOLE
|MM_PRINT
)))
240 if (!(sp
= sfstropen()))
243 if (s
= (char*)label
)
245 if (t
= strchr(s
, ':'))
247 if ((n
= t
- s
) > MM_LABEL_1_MAX
)
249 sfprintf(sp
, "%*.*s:", n
, n
, s
);
251 if ((n
= strlen(t
)) > MM_LABEL_2_MAX
)
253 sfprintf(sp
, "%*.*s", n
, n
, s
);
257 if ((n
= strlen(t
)) > MM_LABEL_1_MAX
)
259 sfprintf(sp
, "%*.*s", n
, n
, s
);
261 if (!(s
= sfstruse(sp
)))
270 if (classification
& MM_CONSOLE
)
272 classification
&= ~MM_CONSOLE
;
273 if (!(mm
.init
& INIT_CONSOLE
))
274 mm
.console
= open("/dev/console", O_WRONLY
|O_APPEND
|O_NOCTTY
);
284 else if (classification
& MM_PRINT
)
286 classification
&= ~MM_PRINT
;
292 if ((mask
& MM_label
) && label
)
293 sfprintf(sp
, "%s: ", lab
);
294 if ((mask
& MM_severity
) && (s
= display(mm_severity
, severity
, 0)))
295 sfprintf(sp
, "%s: ", s
);
297 if ((mask
& MM_text
) && text
)
298 sfprintf(sp
, "%s\n", text
);
299 else sfputc(sp
, '\n');
300 if ((mask
& MM_action
) && action
|| (mask
& MM_tag
) && (label
|| tag
))
302 if (fd
!= mm
.console
&& (n
-= 8) > 0)
303 sfprintf(sp
, "%*.*s", n
, n
, "");
304 sfprintf(sp
, "TO FIX:");
305 if ((mask
& MM_action
) && action
)
306 sfprintf(sp
, " %s", action
);
307 if ((mask
& MM_tag
) && (label
|| tag
))
310 if (!tag
|| label
&& !strchr(tag
, ':'))
311 sfprintf(sp
, "%s%s", lab
, tag
? ":" : "");
313 sfprintf(sp
, "%s", tag
);
315 if (mask
& (MM_class
|MM_source
|MM_status
))
318 if ((mask
& MM_source
) && (m
= classification
& (MM_APPL
|MM_UTIL
|MM_OPSYS
)) && (s
= display(mm_class
, m
, 1)))
319 sfprintf(sp
, " %s", s
);
320 if ((mask
& MM_class
) && (m
= classification
& (MM_HARD
|MM_SOFT
|MM_FIRM
)) && (s
= display(mm_class
, m
, 1)))
321 sfprintf(sp
, " %s", s
);
322 if ((mask
& MM_status
) && (m
= classification
& (MM_RECOVER
|MM_NRECOV
)) && (s
= display(mm_class
, m
, 1)))
323 sfprintf(sp
, " %s", s
);
328 if (!(s
= sfstruse(sp
)) || write(fd
, s
, n
) != n
)