Skip to content

Instantly share code, notes, and snippets.

@kfsone
Created January 9, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kfsone/bdd08413ffc5d0a6039d849a64d55e9e to your computer and use it in GitHub Desktop.
Save kfsone/bdd08413ffc5d0a6039d849a64d55e9e to your computer and use it in GitHub Desktop.
// this is pseudo-c
struct Pointers {
const char *timestampStart;
const char *timestampEnd;
const char *pidStart;
const char *pidEnd;
const char *levelStart;
const char *levelEnd;
const char *prefixStart;
const char *prefixEnd;
const char *textStart;
const char *textEnd;
};
#pragma pack(push, 1)
struct Header {
time_t timestamp;
char level[3];
uint8_t prefixLen;
uint16_t pid;
};
#pragma pack(pop)
bool
getPointers(const char *input, Pointers *into)
{
into->timestampStart = input;
into->timestampEnd = input + 15;
into->pidStart = timestampEnd + 2;
into->pidEnd = strchr(into->pidStart, ']');
into->levelStart = into->pidEnd + 2;
into->levelEnd = into->levelStart + 4;
into->prefixStart = into->levelEnd + 1;
into->prefixEnd = strchr(into->prefixStart + ' ');
if (into->prefixEnd == nullptr) {
return false;
}
into->textStart = strchr(into->prefixEnd, ': ');
if (into->prefixStart == nullptr) {
return false;
}
into->textEnd = strchr(into->textStart, '\n');
return into->textEnd != nullptr;
}
void v1(const char *input, const Pointers *pointers)
{
struct iovec vecs[5] {
{ into->timestampStart, into->timestampEnd - into->timestampStart },
{ into->pidStart, into->pidEnd - into->pidStart },
{ into->levelStart, into->levelEnd - into->levelStart },
{ into->prefixStart, into->prefixEnd - into->prefixStart },
{ into->textStart, into->textEnd - into->textStart },
};
... writev(fd, vecs, 5);
}
void v2(const char *input, const Pointers * pointers)
{
Header hdr;
hdr.timestamp = parseTime(into->timestampStart, into->timestampEnd);
memcpy(hdr.level, into->levelStart, 3);
hdr.prefixLen = pointers->prefixEnd - pointers->prefixStart;
hdr.pid = atoi(into->pidStart);
struct iovec vecs[2] {
{ &hdr, sizeof(hdr) },
{ pointers->prefixStart, pointers->textEnd - pointers->prefixStart },
};
... writev(fd, vecs, 2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment