Skip to content

Instantly share code, notes, and snippets.

@lambda-fairy
Last active December 19, 2015 18:18
Show Gist options
  • Save lambda-fairy/5997186 to your computer and use it in GitHub Desktop.
Save lambda-fairy/5997186 to your computer and use it in GitHub Desktop.
Simple logging system in C
#include <stdio.h>
#include "log.h"
void
PLog_printf(const char *type, const char *filename, unsigned int line, const char *fmt, ...) {
va_list vl;
va_start(vl, fmt);
fprintf(stderr, "%s:%u: %s: ", filename, line, type);
vfprintf(stderr, fmt, vl);
putc('\n', stderr);
va_end(vl);
}
#ifndef log_h
#define log_h
#include <setjmp.h>
#define PLog_error(...) PLog_printf("error", __FILE__, __LINE__, __VA_ARGS__)
#define PLog_warn(...) PLog_printf("warning", __FILE__, __LINE__, __VA_ARGS__)
#define PLog_info(...) PLog_printf("info", __FILE__, __LINE__, __VA_ARGS__)
void PLog_printf(const char *type, const char *filename, unsigned int line, const char *fmt, ...);
/* The horror! The horror! */
#define PLog_panic(buf, ...) \
do { \
PLog_error(__VA_ARGS__); \
longjmp((buf), 1); \
} while (0)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment