2 * log.c Functions in the library call radlib_log() which
3 * does internal logging.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Copyright 2000,2006 The FreeRADIUS server project
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/libradius.h>
30 #define FR_STRERROR_BUFSIZE (1024)
32 #ifdef HAVE_THREAD_TLS
34 * GCC on most Linux systems
36 #define THREAD_TLS __thread
38 #elif defined(HAVE_DECLSPEC_THREAD)
42 #define THREAD_TLS __declspec(thread)
46 * We don't have thread-local storage. Ensure we don't
52 * Use pthread keys if we have pthreads. For MAC, which should
56 #define USE_PTHREAD_FOR_TLS (1)
60 #ifndef USE_PTHREAD_FOR_TLS
62 * Try to create a thread-local-storage version of this buffer.
64 static THREAD_TLS char fr_strerror_buffer[FR_STRERROR_BUFSIZE];
69 static pthread_key_t fr_strerror_key;
70 static pthread_once_t fr_strerror_once = PTHREAD_ONCE_INIT;
73 static void fr_strerror_make_key(void)
75 pthread_key_create(&fr_strerror_key, NULL);
80 * Log to a buffer, trying to be thread-safe.
82 void fr_strerror_printf(const char *fmt, ...)
86 #ifdef USE_PTHREAD_FOR_TLS
89 pthread_once(&fr_strerror_once, fr_strerror_make_key);
91 buffer = pthread_getspecific(fr_strerror_key);
93 buffer = malloc(FR_STRERROR_BUFSIZE);
94 if (!buffer) return; /* panic and die! */
96 pthread_setspecific(fr_strerror_key, buffer);
100 vsnprintf(buffer, FR_STRERROR_BUFSIZE, fmt, ap);
104 vsnprintf(fr_strerror_buffer, sizeof(fr_strerror_buffer), fmt, ap);
110 const char *fr_strerror(void)
112 #ifndef USE_PTHREAD_FOR_TLS
113 return fr_strerror_buffer;
118 pthread_once(&fr_strerror_once, fr_strerror_make_key);
120 msg = pthread_getspecific(fr_strerror_key);
123 return "(unknown error)"; /* DON'T return NULL! */
127 void fr_perror(const char *fmt, ...)
132 vfprintf(stderr, fmt, ap);
133 if (strchr(fmt, ':') == NULL)
134 fprintf(stderr, ": ");
135 fprintf(stderr, "%s\n", fr_strerror());