1
1
Fork 0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
1.4 KiB

#include "logger.h"
// Defaults
loggerlevel LOGLEVEL = SILENT;
loggerlevel STDOUTLEVEL = SILENT;
void set_loglevel(loggerlevel echo, loggerlevel write) {
LOGLEVEL = write;
STDOUTLEVEL = echo;
}
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(&current_time);
copy_str(time_str, asctime(localtime(&current_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);
}
}