Added support for MIST_COLOR and MIST_LOG_SYSTEMD environment variables to affect logging

This commit is contained in:
Thulinma 2021-06-02 19:45:10 +02:00
parent 8be233ccb2
commit 98b10473a4

View file

@ -277,7 +277,8 @@ namespace Util{
/// calling the given callback for each valid message. Closes the file descriptor on read error /// calling the given callback for each valid message. Closes the file descriptor on read error
void logParser(int in, int out, bool colored, void logParser(int in, int out, bool colored,
void callback(const std::string &, const std::string &, const std::string &, uint64_t, bool)){ void callback(const std::string &, const std::string &, const std::string &, uint64_t, bool)){
if (getenv("MIST_COLOR")){colored = true;}
bool sysd_log = getenv("MIST_LOG_SYSTEMD");
char buf[1024]; char buf[1024];
FILE *output = fdopen(in, "r"); FILE *output = fdopen(in, "r");
char *color_time, *color_msg, *color_end, *color_strm, *CONF_msg, *FAIL_msg, *ERROR_msg, char *color_time, *color_msg, *color_end, *color_strm, *CONF_msg, *FAIL_msg, *ERROR_msg,
@ -356,14 +357,25 @@ namespace Util{
if (!strcmp(kind, "WARN")){color_msg = WARN_msg;} if (!strcmp(kind, "WARN")){color_msg = WARN_msg;}
if (!strcmp(kind, "INFO")){color_msg = INFO_msg;} if (!strcmp(kind, "INFO")){color_msg = INFO_msg;}
} }
time_t rawtime; if (sysd_log){
struct tm *timeinfo; if (!strcmp(kind, "CONF")){dprintf(out, "<5>");}
struct tm timetmp; if (!strcmp(kind, "FAIL")){dprintf(out, "<0>");}
char buffer[100]; if (!strcmp(kind, "ERROR")){dprintf(out, "<1>");}
time(&rawtime); if (!strcmp(kind, "WARN")){dprintf(out, "<2>");}
timeinfo = localtime_r(&rawtime, &timetmp); if (!strcmp(kind, "INFO")){dprintf(out, "<5>");}
strftime(buffer, 100, "%F %H:%M:%S", timeinfo); if (!strcmp(kind, "VERYHIGH") || !strcmp(kind, "EXTREME") || !strcmp(kind, "INSANE") || !strcmp(kind, "DONTEVEN")){
dprintf(out, "%s[%s] ", color_time, buffer); dprintf(out, "<7>");
}
}else{
time_t rawtime;
struct tm *timeinfo;
struct tm timetmp;
char buffer[100];
time(&rawtime);
timeinfo = localtime_r(&rawtime, &timetmp);
strftime(buffer, 100, "%F %H:%M:%S", timeinfo);
dprintf(out, "%s[%s] ", color_time, buffer);
}
if (progname && progpid && strlen(progname) && strlen(progpid)){ if (progname && progpid && strlen(progname) && strlen(progpid)){
if (strmNm && strlen(strmNm)){ if (strmNm && strlen(strmNm)){
dprintf(out, "%s:%s%s%s (%s) ", progname, color_strm, strmNm, color_time, progpid); dprintf(out, "%s:%s%s%s (%s) ", progname, color_strm, strmNm, color_time, progpid);