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){
// We're writing to ourselves or from null pointer - assume outside write (e.g. fread or socket operation) and update the size
if (!p || p == ((char *)ptr) + currSize){
// We're writing from null pointer - assume outside write (e.g. fread or socket operation) and update the size
if (!p){
if (currSize + l > maxSize){
FAIL_MSG("Pointer write went beyond allocated size! Memory corruption likely.");
BACKTRACE;
@ -306,8 +306,6 @@ namespace Util{
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];
FILE *output = fdopen(in, "r");
char *color_time, *color_msg, *color_end, *color_strm, *CONF_msg, *FAIL_msg, *ERROR_msg,
*WARN_msg, *INFO_msg;
if (colored){
@ -337,7 +335,16 @@ namespace Util{
WARN_msg = (char *)"";
INFO_msg = (char *)"";
}
while (fgets(buf, 1024, output)){
Socket::Connection O(-1, in);
O.setBlocking(true);
Util::ResizeablePointer buf;
while (O){
if (O.spool()){
while (O.Received().size()){
std::string & t = O.Received().get();
buf.append(t);
if (buf.size() && (buf.size() > 1024 || *t.rbegin() == '\n')){
unsigned int i = 0;
char *kind = buf; // type of message, at begin of string
char *progname = 0;
@ -346,34 +353,57 @@ namespace Util{
char *strmNm = 0;
char *message = 0;
while (i < 9 && buf[i] != '|' && buf[i] != 0 && buf[i] < 128){++i;}
if (buf[i] != '|'){continue;}// on parse error, skip to next message
if (buf[i] != '|'){
// on parse error, skip to next message
t.clear();
buf.truncate(0);
continue;
}
buf[i] = 0; // insert null byte
++i;
progname = buf + i; // progname starts here
while (i < 40 && buf[i] != '|' && buf[i] != 0){++i;}
if (buf[i] != '|'){continue;}// on parse error, skip to next message
if (buf[i] != '|'){
// on parse error, skip to next message
t.clear();
buf.truncate(0);
continue;
}
buf[i] = 0; // insert null byte
++i;
progpid = buf + i; // progpid starts here
while (i < 60 && buf[i] != '|' && buf[i] != 0){++i;}
if (buf[i] != '|'){continue;}// on parse error, skip to next message
if (buf[i] != '|'){
// on parse error, skip to next message
t.clear();
buf.truncate(0);
continue;
}
buf[i] = 0; // insert null byte
++i;
lineno = buf + i; // lineno starts here
while (i < 180 && buf[i] != '|' && buf[i] != 0){++i;}
if (buf[i] != '|'){continue;}// on parse error, skip to next message
if (buf[i] != '|'){
// on parse error, skip to next message
t.clear();
buf.truncate(0);
continue;
}
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] != '|'){continue;}// on parse error, skip to next message
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
// find end of line, insert null byte
unsigned int j = i;
while (j < 1023 && buf[j] != '\n' && buf[j] != 0){++j;}
buf[j] = 0;
// 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;
@ -415,8 +445,15 @@ namespace Util{
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);
}
fclose(output);
t.clear();
}
}else{
Util::sleep(50);
}
}
close(out);
close(in);
}