Last active
September 17, 2016 19:11
-
-
Save bijanebrahimi/77c9fd75ed6772af0151cf46189280a9 to your computer and use it in GitHub Desktop.
simple logging in c
This file contains hidden or 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
#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); | |
} | |
This file contains hidden or 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
#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