Files
flexnbd-c/src/util.c
Matthew Bloch b546539ab8 Rewrote error & log functions to be more general, use longjmp to get out of
trouble and into predictable cleanup functions (one for each of serve,
client & control contexts).  We use 'fatal' to mean 'kill the thread' and
'error' to mean 'don't kill the thread', assuming some recovery action,
except I don't use error anywhere yet.
2012-06-09 02:25:12 +01:00

59 lines
940 B
C

#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"
pthread_key_t cleanup_handler_key;
int log_level = 1;
void error_init()
{
pthread_key_create(&cleanup_handler_key, free);
}
void error_handler(int fatal)
{
DECLARE_ERROR_CONTEXT(context);
if (!context) {
pthread_exit((void*) 1);
}
longjmp(context->jmp, 1);
}
void mylog(int line_level, const char* format, ...)
{
va_list argptr;
if (line_level < log_level)
return;
va_start(argptr, format);
vfprintf(stderr, format, argptr);
va_end(argptr);
fprintf(stderr, "\n");
}
void* xrealloc(void* ptr, size_t size)
{
void* p = realloc(ptr, size);
FATAL_IF_NULL(p, "couldn't xrealloc %d bytes", ptr ? "realloc" : "malloc", size);
return p;
}
void* xmalloc(size_t size)
{
void* p = xrealloc(NULL, size);
memset(p, 0, size);
return p;
}