alistair
5 years ago
9 changed files with 96 additions and 61 deletions
Binary file not shown.
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
#include "logger.h" |
||||
|
||||
|
||||
// Defaults
|
||||
|
||||
loggerlevel LOGLEVEL = DEBUG; |
||||
loggerlevel STDOUTLEVEL = INFO; |
||||
|
||||
int copy_str(char * out_string, char * string) { |
||||
/* A drop in replacement for strcpy() from <string.h>*/ |
||||
int len = 0; |
||||
while (*(string + len) != '\0') { |
||||
*(out_string + len) = *(string + len); |
||||
len++; |
||||
} |
||||
*(out_string + len) = '\0'; |
||||
return (1); |
||||
} |
||||
|
||||
void logwrite(loggerlevel level, char message[]) { |
||||
// write a string message
|
||||
char time_str[100]; |
||||
time_t current_time; |
||||
time(¤t_time);
|
||||
|
||||
copy_str(time_str, asctime(localtime(¤t_time))); |
||||
|
||||
// delete the \n
|
||||
time_str[24] = ' '; |
||||
|
||||
char prepend[15]; |
||||
copy_str(prepend, time_str); |
||||
|
||||
|
||||
switch (level) { |
||||
case DEBUG: |
||||
strcat(prepend, "DEBUG"); |
||||
break; |
||||
case INFO: |
||||
strcat(prepend, "INFO "); |
||||
break; |
||||
case WARN: |
||||
strcat(prepend, "WARN "); |
||||
break; |
||||
case ERROR: |
||||
strcat(prepend, "ERROR"); |
||||
break; |
||||
} |
||||
|
||||
strcat(prepend, ": "); |
||||
|
||||
if (level <= LOGLEVEL) { |
||||
FILE * logfile = fopen("debug.log", "a"); |
||||
fprintf(logfile, "%s", prepend); |
||||
fprintf(logfile, message); |
||||
fclose(logfile); |
||||
} |
||||
|
||||
if (level <= STDOUTLEVEL) { |
||||
printf("%s", prepend); |
||||
printf(message); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
#include <time.h> |
||||
#include <string.h> |
||||
#include <stdio.h> |
||||
|
||||
typedef enum { |
||||
// high-low priority
|
||||
// low-high verbosity
|
||||
SILENT, ERROR, WARN, INFO, DEBUG |
||||
} loggerlevel; |
||||
|
||||
|
||||
extern loggerlevel LOGLEVEL; |
||||
extern loggerlevel STDOUTLEVEL; |
||||
|
||||
void logwrite(loggerlevel level, char message[]); |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue