2012-05-17 20:14:22 +01:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2012-06-21 18:01:56 +01:00
|
|
|
pthread_key_t cleanup_handler_key;
|
2012-06-09 02:25:12 +01:00
|
|
|
|
2012-06-11 14:59:26 +01:00
|
|
|
int log_level = 2;
|
2012-05-17 20:14:22 +01:00
|
|
|
|
2012-06-11 13:57:03 +01:00
|
|
|
void error_init(void)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2012-06-09 02:25:12 +01:00
|
|
|
pthread_key_create(&cleanup_handler_key, free);
|
|
|
|
}
|
|
|
|
|
2012-06-11 13:57:03 +01:00
|
|
|
void error_handler(int fatal __attribute__ ((unused)) )
|
2012-06-09 02:25:12 +01:00
|
|
|
{
|
|
|
|
DECLARE_ERROR_CONTEXT(context);
|
|
|
|
|
|
|
|
if (!context) {
|
2012-06-21 17:12:06 +01:00
|
|
|
/* FIXME: This can't be right - by default we exit()
|
|
|
|
* with a status of 0 in this case.
|
|
|
|
*/
|
2012-06-09 02:25:12 +01:00
|
|
|
pthread_exit((void*) 1);
|
|
|
|
}
|
|
|
|
|
2012-06-21 18:01:56 +01:00
|
|
|
longjmp(context->jmp, fatal ? 1 : 2 );
|
2012-05-17 20:14:22 +01:00
|
|
|
}
|
|
|
|
|
2012-06-21 17:11:12 +01:00
|
|
|
|
2012-06-27 15:45:33 +01:00
|
|
|
void exit_err( const char *msg )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "%s\n", msg );
|
|
|
|
exit( 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-09 02:25:12 +01:00
|
|
|
void mylog(int line_level, const char* format, ...)
|
2012-05-17 20:14:22 +01:00
|
|
|
{
|
2012-06-11 14:34:17 +01:00
|
|
|
if (line_level < log_level) { return; }
|
2012-07-13 12:18:19 +01:00
|
|
|
|
|
|
|
va_list argptr;
|
2012-07-12 14:14:46 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
va_start(argptr, format);
|
2012-07-13 12:18:19 +01:00
|
|
|
vfprintf(stderr, format, argptr);
|
2012-05-17 20:14:22 +01:00
|
|
|
va_end(argptr);
|
|
|
|
}
|
|
|
|
|
2012-07-13 12:18:19 +01:00
|
|
|
|
2012-05-17 20:14:22 +01:00
|
|
|
void* xrealloc(void* ptr, size_t size)
|
|
|
|
{
|
|
|
|
void* p = realloc(ptr, size);
|
2012-06-09 02:25:12 +01:00
|
|
|
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes", ptr ? "realloc" : "malloc", size);
|
2012-05-17 20:14:22 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void* p = xrealloc(NULL, size);
|
|
|
|
memset(p, 0, size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|