From ceed357001a7543c62468c5b943cf80c2a644252 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 20 Jan 2010 02:35:46 +0100 Subject: [PATCH] improve dump output --- job.c | 16 +++++++++------- job.h | 2 +- manager.c | 26 +++++++++++++------------- name.c | 40 +++++++++++++++++++++++++--------------- name.h | 2 +- 5 files changed, 49 insertions(+), 37 deletions(-) diff --git a/job.c b/job.c index 3e8cca1e8f..ec0baba5ee 100644 --- a/job.c +++ b/job.c @@ -52,7 +52,7 @@ JobDependency* job_dependency_new(Job *subject, Job *object, bool matters) { * this means the 'anchor' job (i.e. the one the user * explcitily asked for) is the requester. */ - if (!(l = new(JobDependency, 1))) + if (!(l = new0(JobDependency, 1))) return NULL; l->subject = subject; @@ -127,7 +127,7 @@ void job_dependency_delete(Job *subject, Job *object, bool *matters) { job_dependency_free(l); } -void job_dump(Job *j, FILE*f) { +void job_dump(Job *j, FILE*f, const char *prefix) { static const char* const job_type_table[_JOB_TYPE_MAX] = { [JOB_START] = "start", @@ -148,11 +148,13 @@ void job_dump(Job *j, FILE*f) { assert(j); assert(f); - fprintf(f, "Job %u (%s) → %s in state %s\n", - j->id, - name_id(j->name), - job_type_table[j->type], - job_state_table[j->state]); + fprintf(f, + "%sJob %u:\n" + "%s\tAction: %s → %s\n" + "%s\tState: %s\n", + prefix, j->id, + prefix, name_id(j->name), job_type_table[j->type], + prefix, job_state_table[j->state]); } bool job_is_anchor(Job *j) { diff --git a/job.h b/job.h index f337307f04..7a44ae0776 100644 --- a/job.h +++ b/job.h @@ -80,7 +80,7 @@ struct Job { Job* job_new(Manager *m, JobType type, Name *name); void job_free(Job *job); -void job_dump(Job *j, FILE*f); +void job_dump(Job *j, FILE*f, const char *prefix); JobDependency* job_dependency_new(Job *subject, Job *object, bool matters); void job_dependency_free(JobDependency *l); diff --git a/manager.c b/manager.c index a870db6cce..6470e1c1a8 100644 --- a/manager.c +++ b/manager.c @@ -421,7 +421,7 @@ rollback: return r; } -static Job* transaction_add_job(Manager *m, JobType type, Name *name, bool *is_new) { +static Job* transaction_add_one_job(Manager *m, JobType type, Name *name, bool *is_new) { Job *j, *f; int r; @@ -491,7 +491,7 @@ void manager_transaction_delete_job(Manager *m, Job *j) { job_dependency_free(j->object_list); } -static int real_add_job(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) { +static int transaction_add_job_and_dependencies(Manager *m, JobType type, Name *name, Job *by, bool matters, bool force, Job **_ret) { Job *ret; void *state; Name *dep; @@ -503,7 +503,7 @@ static int real_add_job(Manager *m, JobType type, Name *name, Job *by, bool matt assert(name); /* First add the job. */ - if (!(ret = transaction_add_job(m, type, name, &is_new))) + if (!(ret = transaction_add_one_job(m, type, name, &is_new))) return -ENOMEM; /* Then, add a link to the job. */ @@ -514,28 +514,28 @@ static int real_add_job(Manager *m, JobType type, Name *name, Job *by, bool matt /* Finally, recursively add in all dependencies. */ if (type == JOB_START || type == JOB_RELOAD_OR_START) { SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRES], state) - if ((r = real_add_job(m, JOB_START, dep, ret, true, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, force, NULL)) < 0) goto fail; SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUIRES], state) - if ((r = real_add_job(m, JOB_START, dep, ret, !force, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !force, force, NULL)) < 0) goto fail; SET_FOREACH(dep, ret->name->meta.dependencies[NAME_WANTS], state) - if ((r = real_add_job(m, JOB_START, dep, ret, false, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, force, NULL)) < 0) goto fail; SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUISITE], state) - if ((r = real_add_job(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, true, force, NULL)) < 0) goto fail; SET_FOREACH(dep, ret->name->meta.dependencies[NAME_SOFT_REQUISITE], state) - if ((r = real_add_job(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_STARTED, dep, ret, !force, force, NULL)) < 0) goto fail; SET_FOREACH(dep, ret->name->meta.dependencies[NAME_CONFLICTS], state) - if ((r = real_add_job(m, JOB_STOP, dep, ret, true, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, force, NULL)) < 0) goto fail; } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) { SET_FOREACH(dep, ret->name->meta.dependencies[NAME_REQUIRED_BY], state) - if ((r = real_add_job(m, type, dep, ret, true, force, NULL)) < 0) + if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, force, NULL)) < 0) goto fail; } @@ -557,7 +557,7 @@ int manager_add_job(Manager *m, JobType type, Name *name, JobMode mode, bool for assert(name); assert(mode < _JOB_MODE_MAX); - if ((r = real_add_job(m, type, name, NULL, true, force, &ret))) { + if ((r = transaction_add_job_and_dependencies(m, type, name, NULL, true, force, &ret))) { transaction_abort(m); return r; } @@ -786,7 +786,7 @@ void manager_dump_jobs(Manager *s, FILE *f) { assert(f); HASHMAP_FOREACH(j, s->jobs, state) - job_dump(j, f); + job_dump(j, f, NULL); } void manager_dump_names(Manager *s, FILE *f) { @@ -799,5 +799,5 @@ void manager_dump_names(Manager *s, FILE *f) { HASHMAP_FOREACH_KEY(n, t, s->names, state) if (name_id(n) == t) - name_dump(n, f); + name_dump(n, f, NULL); } diff --git a/name.c b/name.c index f4ad176870..ace5529753 100644 --- a/name.c +++ b/name.c @@ -405,7 +405,7 @@ const char* name_id(Name *n) { return set_first(n->meta.names); } -void name_dump(Name *n, FILE *f) { +void name_dump(Name *n, FILE *f, const char *prefix) { static const char* const state_table[_NAME_STATE_MAX] = { [NAME_STUB] = "stub", @@ -432,15 +432,18 @@ void name_dump(Name *n, FILE *f) { assert(n); - fprintf(f, - "Name %s\n" - "\tDescription: %s\n" - "\tName State: %s\n", - name_id(n), - n->meta.description ? n->meta.description : name_id(n), - state_table[n->meta.state]); + if (!prefix) + prefix = ""; - fprintf(f, "\tNames: "); + fprintf(f, + "%sName %s:\n" + "%s\tDescription: %s\n" + "%s\tName State: %s\n", + prefix, name_id(n), + prefix, n->meta.description ? n->meta.description : name_id(n), + prefix, state_table[n->meta.state]); + + fprintf(f, "%s\tNames: ", prefix); SET_FOREACH(t, n->meta.names, state) fprintf(f, "%s ", t); fprintf(f, "\n"); @@ -457,10 +460,10 @@ void name_dump(Name *n, FILE *f) { t = s; fprintf(f, - "\tAddress: %s\n" - "\tSocket State: %s\n", - t, - socket_state_table[n->socket.state]); + "%s\tAddress: %s\n" + "%s\tSocket State: %s\n", + prefix, t, + prefix, socket_state_table[n->socket.state]); free(s); break; @@ -471,7 +474,14 @@ void name_dump(Name *n, FILE *f) { } if (n->meta.job) { - fprintf(f, "\t"); - job_dump(n->meta.job, f); + char *p; + + if (asprintf(&p, "%s\t", prefix) >= 0) + prefix = p; + else + p = NULL; + + job_dump(n->meta.job, f, prefix); + free(p); } } diff --git a/name.h b/name.h index c67e839d4f..6c3e5ba3cd 100644 --- a/name.h +++ b/name.h @@ -284,6 +284,6 @@ int name_merge(Name *name, Name *other); int name_augment(Name *n); const char* name_id(Name *n); -void name_dump(Name *n, FILE *f); +void name_dump(Name *n, FILE *f, const char *prefix); #endif