forked from gjedeer/tuntox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
225 additions
and
96 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <time.h> | ||
|
||
#include "log.h" | ||
|
||
int min_log_level = 666; | ||
|
||
/* Turn log level number to a printable string */ | ||
char *log_printable_level(int level) | ||
{ | ||
switch(level) | ||
{ | ||
case L_ERROR: | ||
return "ERROR"; | ||
case L_WARNING: | ||
return "WARNING"; | ||
case L_NOTICE: | ||
return "NOTICE"; | ||
case L_INFO: | ||
return "INFO"; | ||
case L_DEBUG: | ||
return "DEBUG"; | ||
} | ||
return "UNKNOWN"; | ||
} | ||
|
||
/* Output the log to the console */ | ||
void log_printf(int level, const char *fmt, ...) | ||
{ | ||
va_list args; | ||
char logfmt[2048]; | ||
char logtime[100]; | ||
char *level_str; | ||
time_t rawtime; | ||
struct tm *timeinfo; | ||
|
||
if(level > min_log_level) | ||
{ | ||
return; | ||
} | ||
|
||
time(&rawtime); | ||
timeinfo = localtime(&rawtime); | ||
strftime(logtime, 100, "%F %X", timeinfo); | ||
|
||
level_str = log_printable_level(level); | ||
|
||
if(fmt[strlen(fmt)-1] == '\n') | ||
{ | ||
snprintf(logfmt, 2048, "%s: [%s]\t%s", logtime, level_str, fmt); | ||
} | ||
else | ||
{ | ||
snprintf(logfmt, 2048, "%s: [%s]\t%s\n", logtime, level_str, fmt); | ||
} | ||
|
||
va_start(args, fmt); | ||
vfprintf(stderr, logfmt, args); | ||
va_end(args); | ||
} | ||
|
||
|
||
void log_test(void) | ||
{ | ||
int i = 112; | ||
char *x = "test"; | ||
|
||
log_printf(L_WARNING, "Testing"); | ||
log_printf(L_ERROR, "Number stodwadziesciatrzy: %d", 123); | ||
d(beenthere); | ||
dd(i); | ||
|
||
dp(&i); | ||
ds(x); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#define L_ERROR 3 | ||
#define L_WARNING 4 | ||
#define L_NOTICE 5 | ||
#define L_INFO 6 | ||
#define L_DEBUG 7 | ||
|
||
#define L_UNSET 0x29a | ||
|
||
void log_printf(int level, const char *fmt, ...); | ||
|
||
extern int min_log_level; | ||
|
||
#define d(x) log_printf(L_DEBUG, "%s:%d %s", __FILE__, __LINE__, #x); | ||
|
||
/* Debug-log the int variable x */ | ||
#define dd(x) log_printf(L_DEBUG, "%s:%d %s=%d", __FILE__, __LINE__, #x, (x)); | ||
|
||
/* Debug-log the pointer variable x */ | ||
#define dp(x) log_printf(L_DEBUG, "%s:%d %s=%p", __FILE__, __LINE__, #x, (x)); | ||
|
||
/* Debug-log the string variable x */ | ||
#define ds(x) log_printf(L_DEBUG, "%s:%d %s=%s", __FILE__, __LINE__, #x, (x)); |
Oops, something went wrong.