Skip to content
Snippets Groups Projects
Commit 055cc40d authored by Ryan Gonzalez's avatar Ryan Gonzalez
Browse files

logger: Parse full lines in the input text before partial lines


This doesn't make a large difference at the moment, but it's an
important refactor for being able to easily partially buffer lines while
parsing the level prefixes.

steamrt/tasks#444

Signed-off-by: default avatarRyan Gonzalez <ryan.gonzalez@collabora.com>
parent c6ad4dd3
No related branches found
No related tags found
1 merge request!724logger: Add support for level prefixes
......@@ -953,6 +953,9 @@ _srt_logger_process (SrtLogger *self,
GError **error)
{
char buf[LINE_MAX + 1] = { 0 };
/* The portion of the filled buffer that has already been given to
* logger_process_partial_line (always <= filled) */
size_t already_processed_partial_line = 0;
size_t filled = 0;
ssize_t res;
......@@ -1009,17 +1012,22 @@ _srt_logger_process (SrtLogger *self,
if (res < 0)
return glnx_throw_errno_prefix (error, "Error reading standard input");
logger_process_partial_line (self, buf + filled, (size_t) res);
/* We never touch the last byte of buf while reading */
filled += (size_t) res;
g_assert (filled < sizeof (buf));
while (filled > 0)
{
char *end_of_line = memchr (buf, '\n', filled);
char *end_of_line = NULL;
size_t len;
g_assert (already_processed_partial_line <= filled);
/* Skip the parts of the line we know don't have a newline */
end_of_line = memchr (buf + already_processed_partial_line,
'\n',
filled - already_processed_partial_line);
/* If we have read LINE_MAX bytes with no newline, or we reached
* EOF with no newline at the end, give up and truncate it;
* otherwise keep reading and wait for a newline to appear. */
......@@ -1037,7 +1045,16 @@ _srt_logger_process (SrtLogger *self,
/* Length of the first logical line, including the newline */
len = end_of_line - buf + 1;
/* Since already_processed_partial_line is covering parts that have
* no newline (because otherwise, it wouldn't be partially
* processed!), it should always be less than len, which includes the
* trailing newline. */
g_assert (already_processed_partial_line < len);
logger_process_partial_line (self,
buf + already_processed_partial_line,
len - already_processed_partial_line);
logger_process_complete_line (self, buf, len);
already_processed_partial_line = 0;
if (filled > len)
{
......@@ -1051,6 +1068,16 @@ _srt_logger_process (SrtLogger *self,
filled = 0;
}
}
if (filled > already_processed_partial_line)
{
/* There is still some leftover content in the buffer that doesn't
* make up a complete line; process it now */
logger_process_partial_line (self,
buf + already_processed_partial_line,
filled - already_processed_partial_line);
already_processed_partial_line = filled;
}
}
while (res > 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment