1 /* Copyright (C) 1997,1999,2000-2003,2005 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <bits/libc-lock.h>
25 #include <sys/syslog.h>
31 /* We have global data, protect the modification. */
32 __libc_lock_define_initialized (static, lock
)
42 all_mask
= label_mask
| severity_mask
| text_mask
| action_mask
| tag_mask
48 /* Adjust the size if new elements are added. */
58 #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0]))
65 struct severity_info
*next
;
69 /* List of known severities. */
70 static const struct severity_info nosev
=
74 static const struct severity_info haltsev
=
76 MM_HALT
, "HALT", (struct severity_info
*) &nosev
78 static const struct severity_info errorsev
=
80 MM_ERROR
, "ERROR", (struct severity_info
*) &haltsev
82 static const struct severity_info warningsev
=
84 MM_WARNING
, "WARNING", (struct severity_info
*) &errorsev
86 static const struct severity_info infosev
=
88 MM_INFO
, "INFO", (struct severity_info
*) &warningsev
91 /* Start of the list. */
92 static struct severity_info
*severity_list
= (struct severity_info
*) &infosev
;
94 /* Mask of values we will print. */
97 /* Prototypes for local functions. */
98 static void init (void);
99 static int internal_addseverity (int severity
, const char *string
)
104 fmtmsg (long int classification
, const char *label
, int severity
,
105 const char *text
, const char *action
, const char *tag
)
107 __libc_once_define (static, once
);
109 struct severity_info
*severity_rec
;
111 /* Make sure everything is initialized. */
112 __libc_once (once
, init
);
114 /* Start the real work. First check whether the input is ok. */
115 if (label
!= MM_NULLLBL
)
117 /* Must be two fields, separated by a colon. */
118 const char *cp
= strchr (label
, ':');
122 /* The first field must not contain more then 10 bytes. */
124 /* The second field must not have more then 14 bytes. */
125 || strlen (cp
+ 1) > 14)
129 for (severity_rec
= severity_list
; severity_rec
!= NULL
;
130 severity_rec
= severity_rec
->next
)
131 if (severity
== severity_rec
->severity
)
135 /* If we don't know anything about the severity level return an error. */
136 if (severity_rec
== NULL
)
140 #ifdef __libc_ptf_call
141 /* We do not want this call to be cut short by a thread
142 cancellation. Therefore disable cancellation for now. */
143 int state
= PTHREAD_CANCEL_ENABLE
;
144 __libc_ptf_call (pthread_setcancelstate
, (PTHREAD_CANCEL_DISABLE
, &state
),
148 /* Now we can print. */
149 if (classification
& MM_PRINT
)
151 int do_label
= (print
& label_mask
) && label
!= MM_NULLLBL
;
152 int do_severity
= (print
& severity_mask
) && severity
!= MM_NULLSEV
;
153 int do_text
= (print
& text_mask
) && text
!= MM_NULLTXT
;
154 int do_action
= (print
& action_mask
) && action
!= MM_NULLACT
;
155 int do_tag
= (print
& tag_mask
) && tag
!= MM_NULLTAG
;
157 if (__fxprintf (stderr
, "%s%s%s%s%s%s%s%s%s%s\n",
158 do_label
? label
: "",
159 do_label
&& (do_severity
| do_text
| do_action
| do_tag
)
161 do_severity
? severity_rec
->string
: "",
162 do_severity
&& (do_text
| do_action
| do_tag
)
165 do_text
&& (do_action
| do_tag
) ? "\n" : "",
166 do_action
? "TO FIX: " : "",
167 do_action
? action
: "",
168 do_action
&& do_tag
? " " : "",
169 do_tag
? tag
: "") < 0)
170 /* Oh, oh. An error occurred during the output. */
174 if (classification
& MM_CONSOLE
)
176 int do_label
= label
!= MM_NULLLBL
;
177 int do_severity
= severity
!= MM_NULLSEV
;
178 int do_text
= text
!= MM_NULLTXT
;
179 int do_action
= action
!= MM_NULLACT
;
180 int do_tag
= tag
!= MM_NULLTAG
;
182 syslog (LOG_ERR
, "%s%s%s%s%s%s%s%s%s%s\n",
183 do_label
? label
: "",
184 do_label
&& (do_severity
| do_text
| do_action
| do_tag
)
186 do_severity
? severity_rec
->string
: "",
187 do_severity
&& (do_text
| do_action
| do_tag
) ? ": " : "",
189 do_text
&& (do_action
| do_tag
) ? "\n" : "",
190 do_action
? "TO FIX: " : "",
191 do_action
? action
: "",
192 do_action
&& do_tag
? " " : "",
196 #ifdef __libc_ptf_call
197 __libc_ptf_call (pthread_setcancelstate
, (state
, NULL
), 0);
204 /* Initialize from environment variable content. */
208 const char *msgverb_var
= getenv ("MSGVERB");
209 const char *sevlevel_var
= getenv ("SEV_LEVEL");
211 if (msgverb_var
!= NULL
&& msgverb_var
[0] != '\0')
213 /* Using this extra variable allows us to work without locking. */
218 for (cnt
= 0; cnt
< NKEYWORDS
; ++cnt
)
219 if (memcmp (msgverb_var
,
220 keywords
[cnt
].name
, keywords
[cnt
].len
) == 0
221 && (msgverb_var
[keywords
[cnt
].len
] == ':'
222 || msgverb_var
[keywords
[cnt
].len
] == '\0'))
229 msgverb_var
+= keywords
[cnt
].len
;
230 if (msgverb_var
[0] == ':')
235 /* We found an illegal keyword in the environment
236 variable. The specifications say that we print all
242 while (msgverb_var
[0] != '\0');
248 if (sevlevel_var
!= NULL
)
250 __libc_lock_lock (lock
);
252 while (sevlevel_var
[0] != '\0')
254 const char *end
= __strchrnul (sevlevel_var
, ':');
257 /* First field: keyword. This is not used here but it must be
259 while (sevlevel_var
< end
)
260 if (*sevlevel_var
++ == ',')
263 if (sevlevel_var
< end
)
265 /* Second field: severity level, a number. */
268 level
= strtol (sevlevel_var
, &cp
, 0);
269 if (cp
!= sevlevel_var
&& cp
< end
&& *cp
++ == ','
272 const char *new_string
;
274 new_string
= __strndup (cp
, end
- cp
);
276 if (new_string
!= NULL
277 && (internal_addseverity (level
, new_string
)
279 free ((char *) new_string
);
283 sevlevel_var
= end
+ (*end
== ':' ? 1 : 0);
289 /* Add the new entry to the list. */
292 internal_addseverity (int severity
, const char *string
)
294 struct severity_info
*runp
, *lastp
;
297 /* First see if there is already a record for the severity level. */
298 for (runp
= severity_list
, lastp
= NULL
; runp
!= NULL
; runp
= runp
->next
)
299 if (runp
->severity
== severity
)
307 /* Change the string. */
308 runp
->string
= string
;
311 /* Remove the severity class. */
313 severity_list
= runp
->next
;
315 lastp
->next
= runp
->next
;
320 else if (string
!= NULL
)
322 runp
= malloc (sizeof (*runp
));
327 runp
->severity
= severity
;
328 runp
->next
= severity_list
;
329 runp
->string
= string
;
330 severity_list
= runp
;
334 /* We tried to remove a non-existing severity class. */
341 /* Add new severity level or remove old one. */
343 addseverity (int severity
, const char *string
)
347 /* Prevent illegal SEVERITY values. */
348 if (severity
<= MM_INFO
)
351 /* Protect the global data. */
352 __libc_lock_lock (lock
);
354 /* Do the real work. */
355 result
= internal_addseverity (severity
, string
);
357 /* Release the lock. */
358 __libc_lock_unlock (lock
);
364 libc_freeres_fn (free_mem
)
366 struct severity_info
*runp
= severity_list
;
369 if (runp
->severity
> MM_INFO
)
371 /* This is data we have to release. */
372 struct severity_info
*here
= runp
;