Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2011 10:18
Show Gist options
  • Save anonymous/1397351 to your computer and use it in GitHub Desktop.
Save anonymous/1397351 to your computer and use it in GitHub Desktop.
Quicklog: For when printf isn't enough but a full logging library is too much
//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
/* Check loglevel at logtime on top of compiled loglevel? */
#define CHECK_LOGLEVEL_AT_RUNTIME 0
enum log_level runtime_log_level = LOGLVL;
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 CHECK_LOGLEVEL_AT_RUNTIME
/* Check loglevel at runtime */
#if LOGLVL <= LOGLVL_DEBUG
#define logdebug(...) if (runtime_log_level >= LOGLVL_DEBUG) {quicklog(__VA_ARGS__); output_loglinebuf("DEBUG");}
#else
#define logdebug(...)
#endif
#if LOGLVL <= LOGLVL_INFO
#define loginfo(...) if (runtime_log_level >= LOGLVL_INFO) {quicklog(__VA_ARGS__); output_loglinebuf("INFO");}
#else
#define loginfo(...)
#endif
#if LOGLVL <= LOGLVL_WARN
#define logwarn(...) if (runtime_log_level >= LOGLVL_WARN) {quicklog(__VA_ARGS__); output_loglinebuf("WARNING");}
#else
#define logwarn(...)
#endif
#if LOGLVL <= LOGLVL_ERROR
#define logerror(...) if (runtime_log_level >= LOGLVL_ERROR) {quicklog(__VA_ARGS__); output_loglinebuf("ERROR");}
#else
#define logerror(...)
#endif
#if LOGLVL <= LOGLVL_FIRE
#define logfire(...) if (runtime_log_level >= LOGLVL_FIRE) {quicklog(__VA_ARGS__); output_loglinebuf("FIRE!!!");}
#else
#define logfire(...)
#endif
#else
/* Don't check loglevel at runtime */
#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
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment