-
-
Save ntavish/1397432 to your computer and use it in GitHub Desktop.
Quicklog: For when printf isn't enough but a full logging library is too much
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//quicklog.h | |
//include it, and just loginfo("hello, %s", "world"); | |
//MIT licence | |
#include <stdio.h> | |
#include <time.h> | |
#ifndef QUICKLOG_H | |
#define QUICKLOG_H | |
/* The different log levels */ | |
enum log_level { | |
LOGLVL_DEBUG, /* Debugging information. */ | |
LOGLVL_INFO, /* Informational messages that might be useful to users; progress information, etc */ | |
LOGLVL_WARN, /* A warning that something might be wrong with the user's input. */ | |
LOGLVL_ERROR, /* Something is wrong with the user's input; cannot continue. */ | |
LOGLVL_FIRE /* Something is wrong with our program or the OS; malloc(2) returning null, etc */ | |
}; | |
/* LOGLVL: Logging messages below this level will not be printed. */ | |
#define LOGLVL LOGLVL_DEBUG | |
/* LOG_TARGET: Where logs go */ | |
#define LOG_TARGET stderr | |
#define LOG_FORMAT "%-10llu: %-7s\t%s\n" | |
#define LOGLINEBUFLEN 60 | |
char loglinebuf[LOGLINEBUFLEN]; | |
time_t loglinetime; | |
#define quicklog(...) snprintf(loglinebuf, LOGLINEBUFLEN, __VA_ARGS__); time(&loglinetime); | |
#define output_loglinebuf(levelname) fprintf(LOG_TARGET, LOG_FORMAT, (long long)loglinetime, levelname, loglinebuf) | |
#if LOGLVL <= LOGLVL_DEBUG | |
#define logdebug(...) quicklog(__VA_ARGS__); output_loglinebuf("DEBUG") | |
#else | |
#define logdebug(...) | |
#endif | |
#if LOGLVL <= LOGLVL_INFO | |
#define loginfo(...) quicklog(__VA_ARGS__); output_loglinebuf("INFO") | |
#else | |
#define loginfo(...) | |
#endif | |
#if LOGLVL <= LOGLVL_WARN | |
#define logwarn(...) quicklog(__VA_ARGS__); output_loglinebuf("WARNING") | |
#else | |
#define logwarn(...) | |
#endif | |
#if LOGLVL <= LOGLVL_ERROR | |
#define logerror(...) quicklog(__VA_ARGS__); output_loglinebuf("ERROR") | |
#else | |
#define logerror(...) | |
#endif | |
#if LOGLVL <= LOGLVL_FIRE | |
#define logfire(...) quicklog(__VA_ARGS__); output_loglinebuf("FIRE!!!") | |
#else | |
#define logfire(...) | |
#endif | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment