Skip to content

Instantly share code, notes, and snippets.

@bijanebrahimi
Last active September 17, 2016 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijanebrahimi/77c9fd75ed6772af0151cf46189280a9 to your computer and use it in GitHub Desktop.
Save bijanebrahimi/77c9fd75ed6772af0151cf46189280a9 to your computer and use it in GitHub Desktop.
simple logging in c
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include "log.h"
struct testss{
u_long level;
const char *name;
} log_names[] = {
{LogERROR, "ERROR"},
{LogDEBUG, "DEBUG"},
{LogINFO, "INFO"},
{NULL, NULL}
};
log_levels = (LogERROR|LogDEBUG|LogINFO);
extern void
log_print(int level, const char *fmt, ...)
{
const char *log_name;
if ((log_levels & level)!=log_levels)
return ;
if (level==0||level==1)
log_name = (log_names[level+1]).name;
else
log_name = (log_names[(int)log2((double)level)]).name;
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "%s: ", log_name);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
#ifndef _LOG_H_
#define _LOG_H_
#define LogDEBUG 0x01
#define LogINFO 0x02
#define LogERROR 0x04
int log_levels;
extern void log_print(int, const char *, ...);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment