Improvements to logParser and ResizeablePointer::append

This commit is contained in:
Thulinma 2022-03-02 10:36:54 +01:00
parent 5d0861d5ca
commit 94b6976dfa

View file

@ -214,8 +214,8 @@ namespace Util{
} }
bool ResizeablePointer::append(const void *p, uint32_t l){ bool ResizeablePointer::append(const void *p, uint32_t l){
// We're writing to ourselves or from null pointer - assume outside write (e.g. fread or socket operation) and update the size // We're writing from null pointer - assume outside write (e.g. fread or socket operation) and update the size
if (!p || p == ((char *)ptr) + currSize){ if (!p){
if (currSize + l > maxSize){ if (currSize + l > maxSize){
FAIL_MSG("Pointer write went beyond allocated size! Memory corruption likely."); FAIL_MSG("Pointer write went beyond allocated size! Memory corruption likely.");
BACKTRACE; BACKTRACE;
@ -306,8 +306,6 @@ namespace Util{
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;} if (getenv("MIST_COLOR")){colored = true;}
bool sysd_log = getenv("MIST_LOG_SYSTEMD"); bool sysd_log = getenv("MIST_LOG_SYSTEMD");
char buf[1024];
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,
*WARN_msg, *INFO_msg; *WARN_msg, *INFO_msg;
if (colored){ if (colored){
@ -337,86 +335,125 @@ namespace Util{
WARN_msg = (char *)""; WARN_msg = (char *)"";
INFO_msg = (char *)""; INFO_msg = (char *)"";
} }
while (fgets(buf, 1024, output)){
unsigned int i = 0; Socket::Connection O(-1, in);
char *kind = buf; // type of message, at begin of string O.setBlocking(true);
char *progname = 0; Util::ResizeablePointer buf;
char *progpid = 0; while (O){
char *lineno = 0; if (O.spool()){
char *strmNm = 0; while (O.Received().size()){
char *message = 0; std::string & t = O.Received().get();
while (i < 9 && buf[i] != '|' && buf[i] != 0 && buf[i] < 128){++i;} buf.append(t);
if (buf[i] != '|'){continue;}// on parse error, skip to next message if (buf.size() && (buf.size() > 1024 || *t.rbegin() == '\n')){
buf[i] = 0; // insert null byte unsigned int i = 0;
++i; char *kind = buf; // type of message, at begin of string
progname = buf + i; // progname starts here char *progname = 0;
while (i < 40 && buf[i] != '|' && buf[i] != 0){++i;} char *progpid = 0;
if (buf[i] != '|'){continue;}// on parse error, skip to next message char *lineno = 0;
buf[i] = 0; // insert null byte char *strmNm = 0;
++i; char *message = 0;
progpid = buf + i; // progpid starts here while (i < 9 && buf[i] != '|' && buf[i] != 0 && buf[i] < 128){++i;}
while (i < 60 && buf[i] != '|' && buf[i] != 0){++i;} if (buf[i] != '|'){
if (buf[i] != '|'){continue;}// on parse error, skip to next message // on parse error, skip to next message
buf[i] = 0; // insert null byte t.clear();
++i; buf.truncate(0);
lineno = buf + i; // lineno starts here continue;
while (i < 180 && buf[i] != '|' && buf[i] != 0){++i;} }
if (buf[i] != '|'){continue;}// on parse error, skip to next message buf[i] = 0; // insert null byte
buf[i] = 0; // insert null byte ++i;
++i; progname = buf + i; // progname starts here
strmNm = buf + i; // stream name starts here while (i < 40 && buf[i] != '|' && buf[i] != 0){++i;}
while (i < 380 && buf[i] != '|' && buf[i] != 0){++i;} if (buf[i] != '|'){
if (buf[i] != '|'){continue;}// on parse error, skip to next message // on parse error, skip to next message
buf[i] = 0; // insert null byte t.clear();
++i; buf.truncate(0);
message = buf + i; // message starts here continue;
// find end of line, insert null byte }
unsigned int j = i; buf[i] = 0; // insert null byte
while (j < 1023 && buf[j] != '\n' && buf[j] != 0){++j;} ++i;
buf[j] = 0; progpid = buf + i; // progpid starts here
// print message while (i < 60 && buf[i] != '|' && buf[i] != 0){++i;}
if (callback){callback(kind, message, strmNm, JSON::Value(progpid).asInt(), true);} if (buf[i] != '|'){
color_msg = color_end; // on parse error, skip to next message
if (colored){ t.clear();
if (!strcmp(kind, "CONF")){color_msg = CONF_msg;} buf.truncate(0);
if (!strcmp(kind, "FAIL")){color_msg = FAIL_msg;} continue;
if (!strcmp(kind, "ERROR")){color_msg = ERROR_msg;} }
if (!strcmp(kind, "WARN")){color_msg = WARN_msg;} buf[i] = 0; // insert null byte
if (!strcmp(kind, "INFO")){color_msg = INFO_msg;} ++i;
} lineno = buf + i; // lineno starts here
if (sysd_log){ while (i < 180 && buf[i] != '|' && buf[i] != 0){++i;}
if (!strcmp(kind, "CONF")){dprintf(out, "<5>");} if (buf[i] != '|'){
if (!strcmp(kind, "FAIL")){dprintf(out, "<0>");} // on parse error, skip to next message
if (!strcmp(kind, "ERROR")){dprintf(out, "<1>");} t.clear();
if (!strcmp(kind, "WARN")){dprintf(out, "<2>");} buf.truncate(0);
if (!strcmp(kind, "INFO")){dprintf(out, "<5>");} continue;
if (!strcmp(kind, "VERYHIGH") || !strcmp(kind, "EXTREME") || !strcmp(kind, "INSANE") || !strcmp(kind, "DONTEVEN")){ }
dprintf(out, "<7>"); buf[i] = 0; // insert null byte
++i;
strmNm = buf + i; // stream name starts here
while (i < 380 && buf[i] != '|' && buf[i] != 0){++i;}
if (buf[i] != '|'){
// on parse error, skip to next message
t.clear();
buf.truncate(0);
continue;
}
buf[i] = 0; // insert null byte
++i;
message = buf + i; // message starts here
// insert null byte for end of line
buf[buf.size()-1] = 0;
// print message
if (callback){callback(kind, message, strmNm, JSON::Value(progpid).asInt(), true);}
color_msg = color_end;
if (colored){
if (!strcmp(kind, "CONF")){color_msg = CONF_msg;}
if (!strcmp(kind, "FAIL")){color_msg = FAIL_msg;}
if (!strcmp(kind, "ERROR")){color_msg = ERROR_msg;}
if (!strcmp(kind, "WARN")){color_msg = WARN_msg;}
if (!strcmp(kind, "INFO")){color_msg = INFO_msg;}
}
if (sysd_log){
if (!strcmp(kind, "CONF")){dprintf(out, "<5>");}
if (!strcmp(kind, "FAIL")){dprintf(out, "<0>");}
if (!strcmp(kind, "ERROR")){dprintf(out, "<1>");}
if (!strcmp(kind, "WARN")){dprintf(out, "<2>");}
if (!strcmp(kind, "INFO")){dprintf(out, "<5>");}
if (!strcmp(kind, "VERYHIGH") || !strcmp(kind, "EXTREME") || !strcmp(kind, "INSANE") || !strcmp(kind, "DONTEVEN")){
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 (strmNm && strlen(strmNm)){
dprintf(out, "%s:%s%s%s (%s) ", progname, color_strm, strmNm, color_time, progpid);
}else{
dprintf(out, "%s (%s) ", progname, progpid);
}
}else{
if (strmNm && strlen(strmNm)){dprintf(out, "%s%s%s ", color_strm, strmNm, color_time);}
}
dprintf(out, "%s%s: %s%s", color_msg, kind, message, color_end);
if (lineno && strlen(lineno)){dprintf(out, " (%s) ", lineno);}
dprintf(out, "\n");
buf.truncate(0);
}
t.clear();
} }
}else{ }else{
time_t rawtime; Util::sleep(50);
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 (strmNm && strlen(strmNm)){
dprintf(out, "%s:%s%s%s (%s) ", progname, color_strm, strmNm, color_time, progpid);
}else{
dprintf(out, "%s (%s) ", progname, progpid);
}
}else{
if (strmNm && strlen(strmNm)){dprintf(out, "%s%s%s ", color_strm, strmNm, color_time);}
}
dprintf(out, "%s%s: %s%s", color_msg, kind, message, color_end);
if (lineno && strlen(lineno)){dprintf(out, " (%s) ", lineno);}
dprintf(out, "\n");
} }
fclose(output); close(out);
close(in); close(in);
} }