Merge pull request #957 from ailin-nemui/rawlog

Improve rawlog performance
This commit is contained in:
ailin-nemui 2018-11-12 13:02:41 +01:00 committed by GitHub
commit 6c40567dab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 18 deletions

View file

@ -41,15 +41,16 @@ RAWLOG_REC *rawlog_create(void)
RAWLOG_REC *rec;
rec = g_new0(RAWLOG_REC, 1);
return rec;
rec->lines = g_queue_new();
return rec;
}
void rawlog_destroy(RAWLOG_REC *rawlog)
{
g_return_if_fail(rawlog != NULL);
g_slist_foreach(rawlog->lines, (GFunc) g_free, NULL);
g_slist_free(rawlog->lines);
g_queue_foreach(rawlog->lines, (GFunc) g_free, NULL);
g_queue_free(rawlog->lines);
if (rawlog->logging) {
write_buffer_flush();
@ -61,12 +62,8 @@ void rawlog_destroy(RAWLOG_REC *rawlog)
/* NOTE! str must be dynamically allocated and must not be freed after! */
static void rawlog_add(RAWLOG_REC *rawlog, char *str)
{
if (rawlog->nlines < rawlog_lines || rawlog_lines <= 2)
rawlog->nlines++;
else {
void *tmp = rawlog->lines->data;
rawlog->lines = g_slist_remove(rawlog->lines,
rawlog->lines->data);
if (rawlog->lines->length >= rawlog_lines && rawlog_lines > 0) {
void *tmp = g_queue_pop_head(rawlog->lines);
g_free(tmp);
}
@ -75,7 +72,7 @@ static void rawlog_add(RAWLOG_REC *rawlog, char *str)
write_buffer(rawlog->handle, "\n", 1);
}
rawlog->lines = g_slist_append(rawlog->lines, str);
g_queue_push_tail(rawlog->lines, str);
signal_emit_id(signal_rawlog, 2, rawlog, str);
}
@ -105,10 +102,10 @@ void rawlog_redirect(RAWLOG_REC *rawlog, const char *str)
static void rawlog_dump(RAWLOG_REC *rawlog, int f)
{
GSList *tmp;
GList *tmp;
ssize_t ret = 0;
for (tmp = rawlog->lines; ret != -1 && tmp != NULL; tmp = tmp->next) {
for (tmp = rawlog->lines->head; ret != -1 && tmp != NULL; tmp = tmp->next) {
ret = write(f, tmp->data, strlen((char *) tmp->data));
if (ret != -1)
ret = write(f, "\n", 1);

View file

@ -5,8 +5,7 @@ struct _RAWLOG_REC {
int logging;
int handle;
int nlines;
GSList *lines;
GQueue *lines;
};
RAWLOG_REC *rawlog_create(void);