Commit 943c217b authored by Libor Peltan's avatar Libor Peltan

kjournalprint: separated changeset print routine to allow use for debug

parent bce54d00
......@@ -21,6 +21,7 @@
#include "knot/updates/changesets.h"
#include "knot/updates/apply.h"
#include "libknot/libknot.h"
#include "knot/zone/zone-dump.h"
#include "contrib/macros.h"
#include "contrib/mempattern.h"
......@@ -682,3 +683,33 @@ int changeset_walk(const changeset_t *changeset, changeset_walk_callback callbac
return KNOT_EOK;
}
void changeset_print(const changeset_t *changeset, FILE *outfile, bool color)
{
const char * RED = "\x1B[31m", * GRN = "\x1B[32m", * RESET = "\x1B[0m";
size_t buflen = 1024;
char *buff = malloc(buflen);
if (changeset->soa_from != NULL || !zone_contents_is_empty(changeset->remove)) {
fprintf(outfile, "%s;;Removed\n", color ? RED : "");
}
if (changeset->soa_from != NULL && buff != NULL) {
(void)knot_rrset_txt_dump(changeset->soa_from, &buff, &buflen, &KNOT_DUMP_STYLE_DEFAULT);
fprintf(outfile, "%s", buff);
}
(void)zone_dump_text(changeset->remove, outfile, false);
if (changeset->soa_to != NULL || !zone_contents_is_empty(changeset->add)) {
fprintf(outfile, "%s;;Added\n", color ? GRN : "");
}
if (changeset->soa_to != NULL && buff != NULL) {
(void)knot_rrset_txt_dump(changeset->soa_to, &buff, &buflen, &KNOT_DUMP_STYLE_DEFAULT);
fprintf(outfile, "%s", buff);
}
(void)zone_dump_text(changeset->add, outfile, false);
if (color) {
printf("%s", RESET);
}
free(buff);
}
......@@ -276,3 +276,13 @@ typedef int (*changeset_walk_callback)(const knot_rrset_t *rrset, bool addition,
* \return KNOT_E*
*/
int changeset_walk(const changeset_t *changeset, changeset_walk_callback callback, void *ctx);
/*!
*
* \brief Dumps the changeset into text file.
*
* \param changeset Changeset.
* \param outfile File to write into.
* \param color Use unix tty color metacharacters.
*/
void changeset_print(const changeset_t *changeset, FILE *outfile, bool color);
......@@ -74,33 +74,18 @@ static int reconfigure_mapsize(const char *journal_path, size_t *mapsize)
return KNOT_EOK;
}
static void print_changeset(const changeset_t *chs, bool color, char **buff, size_t *buflen)
static void print_changeset(const changeset_t *chs, bool color)
{
printf(color ? YLW : "");
if (chs->soa_from == NULL) {
printf(";; Zone-in-journal, serial: %u\n",
knot_soa_serial(&chs->soa_to->rrs));
printf(color ? GRN : "");
printf("%s", get_rrset(chs->soa_to, buff, buflen));
zone_dump_text(chs->add, stdout, false);
printf(color ? RESET : "");
} else {
printf(";; Changes between zone versions: %u -> %u\n",
knot_soa_serial(&chs->soa_from->rrs),
knot_soa_serial(&chs->soa_to->rrs));
printf(color ? RED : "");
printf(";; Removed\n");
printf("%s", get_rrset(chs->soa_from, buff, buflen));
zone_dump_text(chs->remove, stdout, false);
printf(color ? GRN : "");
printf(";; Added\n");
printf("%s", get_rrset(chs->soa_to, buff, buflen));
zone_dump_text(chs->add, stdout, false);
printf(color ? RESET : "");
}
changeset_print(chs, stdout, color);
}
dynarray_declare(rrtype, uint16_t, DYNARRAY_VISIBILITY_STATIC, 100)
......@@ -192,12 +177,6 @@ int print_journal(char *path, knot_dname_t *name, uint32_t limit, bool color, bo
ret = KNOT_ENOENT;
}
size_t buflen = 8192;
char *buff = malloc(buflen);
if (buff == NULL) {
journal_db_close(&jdb);
return KNOT_ENOMEM;
}
journal_t *j = journal_new();
if (ret == KNOT_EOK) {
......@@ -206,7 +185,6 @@ int print_journal(char *path, knot_dname_t *name, uint32_t limit, bool color, bo
if (ret != KNOT_EOK) {
journal_free(&j);
journal_db_close(&jdb);
free(buff);
return ret;
}
......@@ -237,14 +215,13 @@ int print_journal(char *path, knot_dname_t *name, uint32_t limit, bool color, bo
if (debugmode) {
print_changeset_debugmode(chs);
} else {
print_changeset(chs, color, &buff, &buflen);
print_changeset(chs, color);
}
}
changesets_free(&db);
pj_finally:
free(buff);
journal_close(j);
journal_free(&j);
journal_db_close(&jdb);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment