1 /* Copyright (C) 1997 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
54 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
61 struct severity_info
*next
;
65 /* List of known severities. */
66 static const struct severity_info nosev
=
70 static const struct severity_info haltsev
=
72 MM_HALT
, "HALT", (struct severity_info
*) &nosev
74 static const struct severity_info errorsev
=
76 MM_ERROR
, "ERROR", (struct severity_info
*) &haltsev
78 static const struct severity_info warningsev
=
80 MM_WARNING
, "WARNING", (struct severity_info
*) &errorsev
82 static const struct severity_info infosev
=
84 MM_INFO
, "INFO", (struct severity_info
*) &warningsev
87 /* Start of the list. */
88 static struct severity_info
*severity_list
= (struct severity_info
*) &infosev
;
90 /* Mask of values we will print. */
93 /* Prototypes for local functions. */
94 static void init (void);
95 static int internal_addseverity (int severity
, const char *string
)
100 fmtmsg (long int classification
, const char *label
, int severity
,
101 const char *text
, const char *action
, const char *tag
)
103 __libc_once_define (static, once
);
105 struct severity_info
*severity_rec
;
107 /* make sure everything is initialized. */
108 __libc_once (once
, init
);
110 /* Start the real work. First check whether the input is ok. */
111 if (label
!= MM_NULLLBL
)
113 /* Must be two fields, separated by a colon. */
114 const char *cp
= strchr (label
, ':');
118 /* The first field must not contain more then 10 bytes. */
120 /* The second field must not have more then 14 bytes. */
121 || strlen (cp
+ 1) > 14)
125 for (severity_rec
= severity_list
; severity_rec
!= NULL
;
126 severity_rec
= severity_rec
->next
)
127 if (severity
== severity_rec
->severity
)
131 /* If we don't know anything about the severity level return an error. */
132 if (severity_rec
== NULL
)
136 /* Now we can print. */
137 if (classification
& MM_PRINT
)
139 int do_label
= (print
& label_mask
) && label
!= MM_NULLLBL
;
140 int do_severity
= (print
& severity_mask
) && severity
!= MM_NULLSEV
;
141 int do_text
= (print
& text_mask
) && text
!= MM_NULLTXT
;
142 int do_action
= (print
& action_mask
) && action
!= MM_NULLACT
;
143 int do_tag
= (print
& tag_mask
) && tag
!= MM_NULLTAG
;
145 if (fprintf (stderr
, "%s%s%s%s%s%s%s%s%s%s\n",
146 do_label
? label
: "",
147 do_label
&& (do_severity
| do_text
) ? ": " : "",
148 do_severity
? severity_rec
->string
: "",
149 do_severity
&& do_text
? ": " : "",
151 (do_label
| do_severity
| do_text
) && (do_action
| do_tag
)
153 do_action
? "TO FIX: " : "",
154 do_action
? action
: "",
155 do_action
&& do_tag
? " " : "",
156 do_tag
? tag
: "") == EOF
)
157 /* Oh, oh. An error occured during the output. */
161 if (classification
& MM_CONSOLE
)
163 int do_label
= label
!= MM_NULLLBL
;
164 int do_severity
= severity
!= MM_NULLSEV
;
165 int do_text
= text
!= MM_NULLTXT
;
166 int do_action
= action
!= MM_NULLACT
;
167 int do_tag
= tag
!= MM_NULLTAG
;
169 syslog (LOG_ERR
, "%s%s%s%s%s%s%s%s%s%s\n",
170 do_label
? label
: "",
171 do_label
&& (do_severity
| do_text
) ? ": " : "",
172 do_severity
? severity_rec
->string
: "",
173 do_severity
&& do_text
? ": " : "",
175 (do_label
| do_severity
| do_text
) && (do_action
| do_tag
)
177 do_action
? "TO FIX: " : "",
178 do_action
? action
: "",
179 do_action
&& do_tag
? " " : "",
187 /* Initialize from environment variable content. */
191 const char *msgverb_var
= getenv ("MSGVERB");
192 const char *sevlevel_var
= getenv ("SEV_LEVEL");
194 if (msgverb_var
!= NULL
&& msgverb_var
[0] != '\0')
196 /* Using this extra variable allows us to work without locking. */
201 for (cnt
= 0; cnt
< NKEYWORDS
; ++cnt
)
202 if (memcmp (msgverb_var
,
203 keywords
[cnt
].name
, keywords
[cnt
].len
) == 0
204 && (msgverb_var
[keywords
[cnt
].len
] == ':'
205 || msgverb_var
[keywords
[cnt
].len
] == '\0'))
212 msgverb_var
+= keywords
[cnt
].len
;
213 if (msgverb_var
[0] == ':')
218 /* We found an illegal keyword in the environment
219 variable. The specifications say that we print all
225 while (msgverb_var
[0] != '\0');
231 if (sevlevel_var
!= NULL
)
233 __libc_lock_lock (lock
);
235 while (sevlevel_var
[0] != '\0')
237 const char *end
= strchr (sevlevel_var
, ':');
241 end
= strchr (sevlevel_var
, '\0');
243 /* First field: keyword. This is not used here but it must be
245 while (sevlevel_var
< end
)
246 if (*sevlevel_var
++ == ',')
249 if (sevlevel_var
< end
)
251 /* Second field: severity level, a number. */
254 level
= strtol (sevlevel_var
, &cp
, 0);
255 if (cp
!= sevlevel_var
&& cp
< end
&& *cp
++ == ','
258 const char *new_string
;
260 new_string
= __strndup (cp
, end
- cp
);
262 if (new_string
!= NULL
263 && (internal_addseverity (level
, new_string
)
265 free ((char *) new_string
);
269 sevlevel_var
= end
+ (*end
== ':' ? 1 : 0);
275 /* Add the new entry to the list. */
278 internal_addseverity (int severity
, const char *string
)
280 struct severity_info
*runp
, *lastp
;
283 /* First see if there is already a record for the severity level. */
284 for (runp
= severity_list
, lastp
= NULL
; runp
!= NULL
; runp
= runp
-> next
)
285 if (runp
->severity
== severity
)
292 /* Release old string. */
293 free ((char *) runp
->string
);
296 /* Change the string. */
297 runp
->string
= string
;
300 /* Remove the severity class. */
302 severity_list
= runp
->next
;
304 lastp
->next
= runp
->next
;
309 else if (string
!= NULL
)
311 runp
= malloc (sizeof (*runp
));
316 runp
->severity
= severity
;
317 runp
->next
= severity_list
;
318 runp
->string
= string
;
319 severity_list
= runp
;
323 /* We tried to remove a non-existing severity class. */
330 /* Add new severity level or remove old one. */
332 addseverity (int severity
, const char *string
)
335 const char *new_string
;
337 /* Prevent illegal SEVERITY values. */
338 if (severity
<= MM_INFO
)
342 /* We want to remove the severity class. */
346 new_string
= __strdup (string
);
348 if (new_string
== NULL
)
349 /* Allocation failed or illegal value. */
353 /* Protect the global data. */
354 __libc_lock_lock (lock
);
356 /* Do the real work. */
357 result
= internal_addseverity (severity
, string
);
360 /* Free the allocated string. */
361 free ((char *) new_string
);
363 /* Release the lock. */
364 __libc_lock_unlock (lock
);
370 static void __attribute__ ((unused
))
373 struct severity_info
*runp
= severity_list
;
376 if (runp
->severity
> MM_INFO
)
378 /* This is data we have to release. */
379 struct severity_info
*here
= runp
;
380 free ((char *) runp
->string
);
387 text_set_element (__libc_subfreeres
, free_mem
);