diff options
Diffstat (limited to 'src/log.c')
-rw-r--r-- | src/log.c | 23 |
1 files changed, 18 insertions, 5 deletions
@@ -13,6 +13,7 @@ const char * sc_log_str (int t) { } /* interestingly, gcc figures out there's no way for code to reach this section, therefore there's no warning "-Wreturn-type" */ } +#ifdef SC_LOGMEM int sc_logentry_free (struct sc_logentry * l) { free(l->message); l->message = NULL; free(l); @@ -22,8 +23,11 @@ struct sc_logentry * sc_logentry_init () { struct sc_logentry * l = calloc(1, sizeof(struct sc_logentry)); return l; } +#endif 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] + char * compiled_message = NULL; +#ifdef SC_LOGMEM if (!c) return -1; pthread_rwlock_t * lock = c->logentries_lock; @@ -34,6 +38,7 @@ int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f if (c->logentries_sizeof <= c->logentries_length) SC_BIGGER_ARRAY(c->logentries, sc_logentry, 1); c->logentries_length++; +#endif size_t strlenm = strlen(m); size_t va_count = parse_printf_format(m, 0, NULL); if (isf && va_count > 0) { @@ -41,21 +46,29 @@ int sc_push_log (unsigned char t, struct sc_cache * c, const char * ca, char * f 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); + compiled_message = malloc(sizeof(char)*strlenm+1); + vsnprintf(compiled_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); + compiled_message = malloc(sizeof(char)*strlenm+1); + strcpy(compiled_message, m); } +#ifdef SC_LOGMEM SC_PLL->file = f; SC_PLL->line = l; SC_PLL->function = ca; SC_PLL->time = time(NULL); SC_PLL->type = t; - 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 */ + SC_PLL->message = compiled_message; +#endif + fprintf(stderr, "[sear.c] %s %s()@%s:%zu: %s\n", sc_log_str(t), ca, f, l, compiled_message); /* in posix, this is thread safe */ +#ifdef SC_LOGMEM if (lock && pthread_rwlock_unlock(lock)) return -4; +#endif +#ifndef SC_LOGMEM + free(compiled_message); +#endif return 1; } |