Skip to content

Instantly share code, notes, and snippets.

@waveacme
Created March 25, 2016 02:45
Show Gist options
  • Save waveacme/01cb09c4f6edd1f1c11f to your computer and use it in GitHub Desktop.
Save waveacme/01cb09c4f6edd1f1c11f to your computer and use it in GitHub Desktop.
a macro logger for c
#include "logger.h"
int DebugLevel = DEBUG_WARN;
int main(void)
{
DBGPRINT(DEBUG_WARN, ("hello,debug=%s\n", "aaa"));
return 0;
}
#ifndef LOGGER_H
#define LOGGER_H
#include <stdio.h>
//want to enable debug
#ifndef DBG
#define DBG
#endif
// Debug information verbosity: lower values indicate higher urgency
#define DEBUG_OFF 0
#define DEBUG_ERROR 1
#define DEBUG_WARN 2
#define DEBUG_TRACE 3
#define DEBUG_INFO 4
#define DEBUG_VERB 5
#ifdef DBG
extern int DebugLevel;
#define DBGPRINT_RAW(Level, Fmt) \
do { \
if (Level <= DebugLevel) \
{ \
printf Fmt; \
} \
} while(0)
#define DBGPRINT(Level, Fmt) DBGPRINT_RAW(Level, Fmt)
#define DBGPRINT_ERR(Fmt) \
{ \
printf("ERROR!!! "); \
printf Fmt; \
}
#else
#define DBGPRINT(Level, Fmt)
#define DBGPRINT_RAW(Level, Fmt)
#define DBGPRINT_ERR(Fmt)
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment