1 /* Public domain fmtmsg()
2 * Written by Isaac Dunham, 2014
13 * If lstr is the first part of bstr, check that the next char in bstr
16 static int _strcolcmp(const char *lstr
, const char *bstr
)
19 while (lstr
[i
] && bstr
[i
] && (bstr
[i
] == lstr
[i
])) i
++;
20 if ( lstr
[i
] || (bstr
[i
] && bstr
[i
] != ':')) return 1;
24 int fmtmsg(long classification
, const char *label
, int severity
,
25 const char *text
, const char *action
, const char *tag
)
27 int ret
= 0, i
, consolefd
, verb
= 0;
28 char *errstring
= MM_NULLSEV
, *cmsg
= getenv("MSGVERB");
29 char *const msgs
[] = {
30 "label", "severity", "text", "action", "tag", NULL
34 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &cs
);
36 if (severity
== MM_HALT
) errstring
= "HALT: ";
37 else if (severity
== MM_ERROR
) errstring
= "ERROR: ";
38 else if (severity
== MM_WARNING
) errstring
= "WARNING: ";
39 else if (severity
== MM_INFO
) errstring
= "INFO: ";
41 if (classification
& MM_CONSOLE
) {
42 consolefd
= open("/dev/console", O_WRONLY
);
46 if (dprintf(consolefd
, "%s%s%s%s%s%s%s%s\n",
47 label
?label
:"", label
?": ":"",
48 severity
?errstring
:"", text
?text
:"",
49 action
?"\nTO FIX: ":"",
50 action
?action
:"", action
?" ":"",
57 if (classification
& MM_PRINT
) {
58 while (cmsg
&& cmsg
[0]) {
59 for(i
=0; msgs
[i
]; i
++) {
60 if (!_strcolcmp(msgs
[i
], cmsg
)) break;
62 if (msgs
[i
] == NULL
) {
63 //ignore MSGVERB-unrecognized component
68 cmsg
= strchr(cmsg
, ':');
72 if (!verb
) verb
= 0xFF;
73 if (dprintf(2, "%s%s%s%s%s%s%s%s\n",
74 (verb
&1 && label
) ? label
: "",
75 (verb
&1 && label
) ? ": " : "",
76 (verb
&2 && severity
) ? errstring
: "",
77 (verb
&4 && text
) ? text
: "",
78 (verb
&8 && action
) ? "\nTO FIX: " : "",
79 (verb
&8 && action
) ? action
: "",
80 (verb
&8 && action
) ? " " : "",
81 (verb
&16 && tag
) ? tag
: "" ) < 1)
84 if ((ret
& (MM_NOCON
|MM_NOMSG
)) == (MM_NOCON
|MM_NOMSG
))
87 pthread_setcancelstate(cs
, 0);