1 /* Copyright (C) 1997, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
21 #include <bits/libc-lock.h>
25 #include <sys/syslog.h>
28 /* We have global data, protect the modification. */
29 __libc_lock_define_initialized (static, lock
)
39 all_mask
= label_mask
| severity_mask
| text_mask
| action_mask
| tag_mask
45 /* Adjust the size if new elements are added. */
55 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
62 struct severity_info
*next
;
66 /* List of known severities. */
67 static const struct severity_info nosev
=
71 static const struct severity_info haltsev
=
73 MM_HALT
, "HALT", (struct severity_info
*) &nosev
75 static const struct severity_info errorsev
=
77 MM_ERROR
, "ERROR", (struct severity_info
*) &haltsev
79 static const struct severity_info warningsev
=
81 MM_WARNING
, "WARNING", (struct severity_info
*) &errorsev
83 static const struct severity_info infosev
=
85 MM_INFO
, "INFO", (struct severity_info
*) &warningsev
88 /* Start of the list. */
89 static struct severity_info
*severity_list
= (struct severity_info
*) &infosev
;
91 /* Mask of values we will print. */
94 /* Prototypes for local functions. */
95 static void init (void);
96 static int internal_addseverity (int severity
, const char *string
)
101 fmtmsg (long int classification
, const char *label
, int severity
,
102 const char *text
, const char *action
, const char *tag
)
104 __libc_once_define (static, once
);
106 struct severity_info
*severity_rec
;
108 /* make sure everything is initialized. */
109 __libc_once (once
, init
);
111 /* Start the real work. First check whether the input is ok. */
112 if (label
!= MM_NULLLBL
)
114 /* Must be two fields, separated by a colon. */
115 const char *cp
= strchr (label
, ':');
119 /* The first field must not contain more then 10 bytes. */
121 /* The second field must not have more then 14 bytes. */
122 || strlen (cp
+ 1) > 14)
126 for (severity_rec
= severity_list
; severity_rec
!= NULL
;
127 severity_rec
= severity_rec
->next
)
128 if (severity
== severity_rec
->severity
)
132 /* If we don't know anything about the severity level return an error. */
133 if (severity_rec
== NULL
)
137 /* Now we can print. */
138 if (classification
& MM_PRINT
)
140 int do_label
= (print
& label_mask
) && label
!= MM_NULLLBL
;
141 int do_severity
= (print
& severity_mask
) && severity
!= MM_NULLSEV
;
142 int do_text
= (print
& text_mask
) && text
!= MM_NULLTXT
;
143 int do_action
= (print
& action_mask
) && action
!= MM_NULLACT
;
144 int do_tag
= (print
& tag_mask
) && tag
!= MM_NULLTAG
;
146 if (fprintf (stderr
, "%s%s%s%s%s%s%s%s%s%s\n",
147 do_label
? label
: "",
148 do_label
&& (do_severity
| do_text
) ? ": " : "",
149 do_severity
? severity_rec
->string
: "",
150 do_severity
&& do_text
? ": " : "",
152 (do_label
| do_severity
| do_text
) && (do_action
| do_tag
)
154 do_action
? "TO FIX: " : "",
155 do_action
? action
: "",
156 do_action
&& do_tag
? " " : "",
157 do_tag
? tag
: "") == EOF
)
158 /* Oh, oh. An error occurred during the output. */
162 if (classification
& MM_CONSOLE
)
164 int do_label
= label
!= MM_NULLLBL
;
165 int do_severity
= severity
!= MM_NULLSEV
;
166 int do_text
= text
!= MM_NULLTXT
;
167 int do_action
= action
!= MM_NULLACT
;
168 int do_tag
= tag
!= MM_NULLTAG
;
170 syslog (LOG_ERR
, "%s%s%s%s%s%s%s%s%s%s\n",
171 do_label
? label
: "",
172 do_label
&& (do_severity
| do_text
) ? ": " : "",
173 do_severity
? severity_rec
->string
: "",
174 do_severity
&& do_text
? ": " : "",
176 (do_label
| do_severity
| do_text
) && (do_action
| do_tag
)
178 do_action
? "TO FIX: " : "",
179 do_action
? action
: "",
180 do_action
&& do_tag
? " " : "",
188 /* Initialize from environment variable content. */
192 const char *msgverb_var
= getenv ("MSGVERB");
193 const char *sevlevel_var
= getenv ("SEV_LEVEL");
195 if (msgverb_var
!= NULL
&& msgverb_var
[0] != '\0')
197 /* Using this extra variable allows us to work without locking. */
202 for (cnt
= 0; cnt
< NKEYWORDS
; ++cnt
)
203 if (memcmp (msgverb_var
,
204 keywords
[cnt
].name
, keywords
[cnt
].len
) == 0
205 && (msgverb_var
[keywords
[cnt
].len
] == ':'
206 || msgverb_var
[keywords
[cnt
].len
] == '\0'))
213 msgverb_var
+= keywords
[cnt
].len
;
214 if (msgverb_var
[0] == ':')
219 /* We found an illegal keyword in the environment
220 variable. The specifications say that we print all
226 while (msgverb_var
[0] != '\0');
232 if (sevlevel_var
!= NULL
)
234 __libc_lock_lock (lock
);
236 while (sevlevel_var
[0] != '\0')
238 const char *end
= __strchrnul (sevlevel_var
, ':');
241 /* First field: keyword. This is not used here but it must be
243 while (sevlevel_var
< end
)
244 if (*sevlevel_var
++ == ',')
247 if (sevlevel_var
< end
)
249 /* Second field: severity level, a number. */
252 level
= strtol (sevlevel_var
, &cp
, 0);
253 if (cp
!= sevlevel_var
&& cp
< end
&& *cp
++ == ','
256 const char *new_string
;
258 new_string
= __strndup (cp
, end
- cp
);
260 if (new_string
!= NULL
261 && (internal_addseverity (level
, new_string
)
263 free ((char *) new_string
);
267 sevlevel_var
= end
+ (*end
== ':' ? 1 : 0);
273 /* Add the new entry to the list. */
276 internal_addseverity (int severity
, const char *string
)
278 struct severity_info
*runp
, *lastp
;
281 /* First see if there is already a record for the severity level. */
282 for (runp
= severity_list
, lastp
= NULL
; runp
!= NULL
; runp
= runp
-> next
)
283 if (runp
->severity
== severity
)
290 /* Release old string. */
291 free ((char *) runp
->string
);
294 /* Change the string. */
295 runp
->string
= string
;
298 /* Remove the severity class. */
300 severity_list
= runp
->next
;
302 lastp
->next
= runp
->next
;
307 else if (string
!= NULL
)
309 runp
= malloc (sizeof (*runp
));
314 runp
->severity
= severity
;
315 runp
->next
= severity_list
;
316 runp
->string
= string
;
317 severity_list
= runp
;
321 /* We tried to remove a non-existing severity class. */
328 /* Add new severity level or remove old one. */
330 addseverity (int severity
, const char *string
)
333 const char *new_string
;
335 /* Prevent illegal SEVERITY values. */
336 if (severity
<= MM_INFO
)
340 /* We want to remove the severity class. */
344 new_string
= __strdup (string
);
346 if (new_string
== NULL
)
347 /* Allocation failed or illegal value. */
351 /* Protect the global data. */
352 __libc_lock_lock (lock
);
354 /* Do the real work. */
355 result
= internal_addseverity (severity
, string
);
358 /* Free the allocated string. */
359 free ((char *) new_string
);
361 /* Release the lock. */
362 __libc_lock_unlock (lock
);
368 static void __attribute__ ((unused
))
371 struct severity_info
*runp
= severity_list
;
374 if (runp
->severity
> MM_INFO
)
376 /* This is data we have to release. */
377 struct severity_info
*here
= runp
;
378 free ((char *) runp
->string
);
385 text_set_element (__libc_subfreeres
, free_mem
);