Skip to content

Commit

Permalink
support log level and two print method
Browse files Browse the repository at this point in the history
  • Loading branch information
kaija authored and kevin_chang committed Jun 12, 2014
1 parent 2bc65dc commit 80d423f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
clog
====

A useful log
A useful log with color

Advantage:
* Small footprint.
* Enable color print with a define in CFLAGS -DCOLOR_LOG.
* Display the log time unit in ms.
* Support log to file
* Support log level filter
* Support LOG($LEVEL, ""...) and LOG_$LEVEL(""...) both method
15 changes: 14 additions & 1 deletion log.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ static int log2screen = 1;
static int log2file;
static char log_file_name[128];
static FILE *log_fp = NULL;
static int log_level = DEBUG;

struct {
char text[8];
Expand All @@ -27,6 +28,7 @@ static char *print_lv_text(int lv, int color){
if(color) return log_text[lv].ctext;
else return log_text[lv].text;
}

static char *print_time()
{
time_t now;
Expand All @@ -40,10 +42,19 @@ static char *print_time()
snprintf(curr_time, 32, "%s %06ld",timebuf, (long)tv.tv_usec);
return curr_time;
}

int log_set_level(int level)
{
if(level < DEBUG || level > FATAL) return -1;
log_level = level;
return 0;
}
int log_set_opt(int opt);

static int log_open_file()
{
log_fp = fopen(log_file_name, "w");
LOG(LOG_INFO, "Log file save to %s\n", log_file_name);
LOG(INFO, "Log file save to %s\n", log_file_name);
return 0;
}

Expand Down Expand Up @@ -72,6 +83,7 @@ void log_print(int level, char *file, int line, char *fmt, ...)
vsnprintf(buf, sizeof(buf),fmt, vl);
va_end(vl);
if(log2screen){
if(level < log_level) return;
#ifdef COLOR_LOG
fprintf(stderr, "%16s| %s ( %s:%d ) %s", print_lv_text(level, 1), print_time(), file, line, buf);
fprintf(stderr, "\033[0m");
Expand All @@ -80,6 +92,7 @@ void log_print(int level, char *file, int line, char *fmt, ...)
#endif
}
if(log2file && log_fp){
if(level < log_level) return;
fprintf(log_fp, "%s %s ( %s:%d ) %s", print_lv_text(level, 0), print_time(), file, line, buf);
}
}
Expand Down
21 changes: 16 additions & 5 deletions log.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
#ifndef __LOG_H
#define __LOG_H

#define LOG_FATAL 4
#define LOG_ERROR 3
#define LOG_WARN 2
#define LOG_INFO 1
#define LOG_DEBUG 0
#define FATAL 4
#define ERROR 3
#define WARN 2
#define INFO 1
#define DEBUG 0
void log_print(int level, char *file, int line, char *fmt, ...);
#define LOG(level, fmt, args...) log_print(level, __FILE__,__LINE__, fmt, ##args)

#define LOG_DEBUG(fmt, args...) log_print(DEBUG, __FILE__,__LINE__, fmt, ##args)
#define LOG_INFO(fmt, args...) log_print(INFO, __FILE__,__LINE__, fmt, ##args)
#define LOG_WARN(fmt, args...) log_print(WARN, __FILE__,__LINE__, fmt, ##args)
#define LOG_ERROR(fmt, args...) log_print(ERROR, __FILE__,__LINE__, fmt, ##args)
#define LOG_FATAL(fmt, args...) log_print(FATAL, __FILE__,__LINE__, fmt, ##args)


int log_set_file(char *file);
int log_close_file();

int log_set_level(int level);
int log_set_opt(int opt);

#endif
27 changes: 22 additions & 5 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@
#include "log.h"
int main()
{
//Set log file to /tmp/a.log
log_set_file("/tmp/a.log");
LOG(LOG_DEBUG, "line1\n");
LOG(LOG_INFO, "line2\n");
LOG(LOG_WARN, "line3\n");
LOG(LOG_ERROR, "line4\n");
LOG(LOG_FATAL, "line5\n");
LOG(DEBUG, "line1\n");
LOG(INFO, "line2\n");
LOG(WARN, "line3\n");
LOG(ERROR, "line4\n");
LOG(FATAL, "line5\n");

//use another method log
LOG_DEBUG("line1\n");
LOG_INFO("line2\n");
LOG_WARN("line3\n");
LOG_ERROR("line4\n");
LOG_FATAL("line5\n");

//set log level to warning
log_set_level(WARN);
LOG_DEBUG("line1\n");
LOG_INFO("line2\n");
LOG_WARN("line3\n");
LOG_ERROR("line4\n");
LOG_FATAL("line5\n");

log_close_file();
return 0;
}

0 comments on commit 80d423f

Please sign in to comment.