const char * sc_log_str (int t) {
switch (t) {
case SC_LOG_ERROR:
return "SC_LOG_ERROR";
case SC_LOG_WARNING:
return "SC_LOG_WARNING";
case SC_LOG_INFO:
return "SC_LOG_INFO";
case SC_LOG_DEBUG:
return "SC_LOG_DEBUG";
default:
return "SC_LOG_UNKNOWN";
}
/* interestingly, gcc figures out there's no way for code to reach this section, therefore there's no warning "-Wreturn-type" */
}
int sc_logentry_free (struct sc_logentry * l) {
free(l->message); l->message = NULL;
free(l);
return 1;
}
struct sc_logentry * sc_logentry_init () {
struct sc_logentry * l = calloc(1, sizeof(struct sc_logentry));
return l;
}
int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f, size_t l, unsigned short int isf, char * m, ...) {
#define SC_PLL c->logentries[c->logentries_length-1]
if (!c)
return -1;
pthread_rwlock_t * lock = c->logentries_lock;
if (!lock)
return -2;
if (pthread_rwlock_wrlock(lock))
return -3;
if (c->logentries_sizeof - c->logentries_length != 0)
SC_BIGGER_ARRAY(c->logentries, sc_logentry);
c->logentries_length++;
size_t strlenm = strlen(m);
size_t va_count = parse_printf_format(m, 0, NULL);
if (isf && va_count > 0) {
va_list ap, ap2;
va_start(ap, m);
va_copy(ap2, ap);
strlenm = vsnprintf(NULL, 0, m, ap);
SC_PLL->message = malloc(sizeof(char)*strlenm+1);
vsnprintf(SC_PLL->message, strlenm+1, m, ap2);
va_end(ap);
va_end(ap2);
} else {
SC_PLL->message = malloc(sizeof(char)*strlenm+1);
strcpy(SC_PLL->message, m);
}
SC_PLL->file = f;
SC_PLL->line = l;
SC_PLL->function = ca;
SC_PLL->time = time(NULL);
fprintf(stderr, "[sear.c] %s %s()@%s:%lu: %s\n", sc_log_str(t), ca, f, l, SC_PLL->message); /* in posix, this is thread safe */
if (lock && pthread_rwlock_unlock(lock))
return -4;
return 1;
}