diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-22 18:22:39 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-22 18:22:39 +0100 |
commit | b2ad8f785e801a35cadf254d827f56d648be510c (patch) | |
tree | 4eeb1e3eefb02c40dac40469c0fae5ad91feb3e3 /common/store.cpp | |
parent | 1fe8664ec74165fc3f250098609ea0e049e3adc8 (diff) | |
download | sink-b2ad8f785e801a35cadf254d827f56d648be510c.tar.gz sink-b2ad8f785e801a35cadf254d827f56d648be510c.zip |
Introduced Log::Context
To have hierarchical debug output we have to pass around something at
run-time, there is no reasonable alternative. Log::Context provides the
identifier to do just that and largely replaces the debug component
idea.
Diffstat (limited to 'common/store.cpp')
-rw-r--r-- | common/store.cpp | 48 |
1 files changed, 26 insertions, 22 deletions
diff --git a/common/store.cpp b/common/store.cpp index a70be05..8007626 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -107,20 +107,21 @@ static QMap<QByteArray, QByteArray> getResources(const Sink::Query::Filter &quer | |||
107 | 107 | ||
108 | 108 | ||
109 | template <class DomainType> | 109 | template <class DomainType> |
110 | KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray &resourceInstanceIdentifier, const Query &query, typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr aggregatingEmitter) | 110 | KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray &resourceInstanceIdentifier, const Query &query, typename AggregatingResultEmitter<typename DomainType::Ptr>::Ptr aggregatingEmitter, const Sink::Log::Context &ctx_) |
111 | { | 111 | { |
112 | auto ctx = ctx_.subContext(resourceInstanceIdentifier); | ||
112 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | 113 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); |
113 | if (facade) { | 114 | if (facade) { |
114 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | 115 | SinkTraceCtx(ctx) << "Trying to fetch from resource " << resourceInstanceIdentifier; |
115 | auto result = facade->load(query); | 116 | auto result = facade->load(query, ctx); |
116 | if (result.second) { | 117 | if (result.second) { |
117 | aggregatingEmitter->addEmitter(result.second); | 118 | aggregatingEmitter->addEmitter(result.second); |
118 | } else { | 119 | } else { |
119 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; | 120 | SinkWarningCtx(ctx) << "Null emitter for resource " << resourceInstanceIdentifier; |
120 | } | 121 | } |
121 | return result.first; | 122 | return result.first; |
122 | } else { | 123 | } else { |
123 | SinkTrace() << "Couldn' find a facade for " << resourceInstanceIdentifier; | 124 | SinkTraceCtx(ctx) << "Couldn' find a facade for " << resourceInstanceIdentifier; |
124 | // Ignore the error and carry on | 125 | // Ignore the error and carry on |
125 | return KAsync::null<void>(); | 126 | return KAsync::null<void>(); |
126 | } | 127 | } |
@@ -129,8 +130,9 @@ KAsync::Job<void> queryResource(const QByteArray resourceType, const QByteArray | |||
129 | template <class DomainType> | 130 | template <class DomainType> |
130 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | 131 | QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) |
131 | { | 132 | { |
133 | Log::Context ctx{query.id()}; | ||
132 | query.setType(ApplicationDomain::getTypeName<DomainType>()); | 134 | query.setType(ApplicationDomain::getTypeName<DomainType>()); |
133 | SinkTrace() << "Loading model: " << query; | 135 | SinkTraceCtx(ctx) << "Loading model: " << query; |
134 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties); | 136 | auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties); |
135 | 137 | ||
136 | //* Client defines lifetime of model | 138 | //* Client defines lifetime of model |
@@ -145,18 +147,19 @@ QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | |||
145 | model->setEmitter(aggregatingEmitter); | 147 | model->setEmitter(aggregatingEmitter); |
146 | 148 | ||
147 | if (query.liveQuery() && query.getResourceFilter().ids.isEmpty() && !ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) { | 149 | if (query.liveQuery() && query.getResourceFilter().ids.isEmpty() && !ApplicationDomain::isGlobalType(ApplicationDomain::getTypeName<DomainType>())) { |
148 | SinkTrace() << "Listening for new resources"; | 150 | SinkTraceCtx(ctx) << "Listening for new resources"; |
151 | auto resourceCtx = ctx.subContext("resourceQuery"); | ||
149 | auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>(); | 152 | auto facade = FacadeFactory::instance().getFacade<ApplicationDomain::SinkResource>(); |
150 | Q_ASSERT(facade); | 153 | Q_ASSERT(facade); |
151 | Sink::Query resourceQuery; | 154 | Sink::Query resourceQuery; |
152 | query.setFlags(Query::LiveQuery); | 155 | query.setFlags(Query::LiveQuery); |
153 | auto result = facade->load(resourceQuery); | 156 | auto result = facade->load(resourceQuery, resourceCtx); |
154 | auto emitter = result.second; | 157 | auto emitter = result.second; |
155 | emitter->onAdded([query, aggregatingEmitter](const ApplicationDomain::SinkResource::Ptr &resource) { | 158 | emitter->onAdded([query, aggregatingEmitter, resourceCtx](const ApplicationDomain::SinkResource::Ptr &resource) { |
156 | SinkTrace() << "Found new resources: " << resource->identifier(); | 159 | SinkTraceCtx(resourceCtx) << "Found new resources: " << resource->identifier(); |
157 | const auto resourceType = ResourceConfig::getResourceType(resource->identifier()); | 160 | const auto resourceType = ResourceConfig::getResourceType(resource->identifier()); |
158 | Q_ASSERT(!resourceType.isEmpty()); | 161 | Q_ASSERT(!resourceType.isEmpty()); |
159 | queryResource<DomainType>(resourceType, resource->identifier(), query, aggregatingEmitter).exec(); | 162 | queryResource<DomainType>(resourceType, resource->identifier(), query, aggregatingEmitter, resourceCtx).exec(); |
160 | }); | 163 | }); |
161 | emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) { | 164 | emitter->onModified([](const ApplicationDomain::SinkResource::Ptr &) { |
162 | }); | 165 | }); |
@@ -164,8 +167,8 @@ QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | |||
164 | }); | 167 | }); |
165 | emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &, bool) { | 168 | emitter->onInitialResultSetComplete([](const ApplicationDomain::SinkResource::Ptr &, bool) { |
166 | }); | 169 | }); |
167 | emitter->onComplete([query, aggregatingEmitter]() { | 170 | emitter->onComplete([query, aggregatingEmitter, resourceCtx]() { |
168 | SinkTrace() << "Resource query complete"; | 171 | SinkTraceCtx(resourceCtx) << "Resource query complete"; |
169 | 172 | ||
170 | }); | 173 | }); |
171 | model->setProperty("resourceEmitter", QVariant::fromValue(emitter)); | 174 | model->setProperty("resourceEmitter", QVariant::fromValue(emitter)); |
@@ -173,9 +176,9 @@ QSharedPointer<QAbstractItemModel> Store::loadModel(Query query) | |||
173 | } | 176 | } |
174 | 177 | ||
175 | KAsync::value(resources.keys()) | 178 | KAsync::value(resources.keys()) |
176 | .template each([query, aggregatingEmitter, resources](const QByteArray &resourceInstanceIdentifier) { | 179 | .template each([query, aggregatingEmitter, resources, ctx](const QByteArray &resourceInstanceIdentifier) { |
177 | const auto resourceType = resources.value(resourceInstanceIdentifier); | 180 | const auto resourceType = resources.value(resourceInstanceIdentifier); |
178 | return queryResource<DomainType>(resourceType, resourceInstanceIdentifier, query, aggregatingEmitter); | 181 | return queryResource<DomainType>(resourceType, resourceInstanceIdentifier, query, aggregatingEmitter, ctx); |
179 | }) | 182 | }) |
180 | .exec(); | 183 | .exec(); |
181 | model->fetchMore(QModelIndex()); | 184 | model->fetchMore(QModelIndex()); |
@@ -370,30 +373,31 @@ DomainType Store::readOne(const Sink::Query &query) | |||
370 | template <class DomainType> | 373 | template <class DomainType> |
371 | QList<DomainType> Store::read(const Sink::Query &q) | 374 | QList<DomainType> Store::read(const Sink::Query &q) |
372 | { | 375 | { |
376 | Log::Context ctx{q.id()}; | ||
373 | auto query = q; | 377 | auto query = q; |
374 | query.setFlags(Query::SynchronousQuery); | 378 | query.setFlags(Query::SynchronousQuery); |
375 | QList<DomainType> list; | 379 | QList<DomainType> list; |
376 | auto resources = getResources(query.getResourceFilter(), ApplicationDomain::getTypeName<DomainType>()); | 380 | auto resources = getResources(query.getResourceFilter(), ApplicationDomain::getTypeName<DomainType>()); |
377 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); | 381 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); |
378 | aggregatingEmitter->onAdded([&list](const typename DomainType::Ptr &value){ | 382 | aggregatingEmitter->onAdded([&list, ctx](const typename DomainType::Ptr &value){ |
379 | SinkTrace() << "Found value: " << value->identifier(); | 383 | SinkTraceCtx(ctx) << "Found value: " << value->identifier(); |
380 | list << *value; | 384 | list << *value; |
381 | }); | 385 | }); |
382 | for (const auto &resourceInstanceIdentifier : resources.keys()) { | 386 | for (const auto &resourceInstanceIdentifier : resources.keys()) { |
383 | const auto resourceType = resources.value(resourceInstanceIdentifier); | 387 | const auto resourceType = resources.value(resourceInstanceIdentifier); |
384 | SinkTrace() << "Querying resource: " << resourceType << resourceInstanceIdentifier; | 388 | SinkTraceCtx(ctx) << "Querying resource: " << resourceType << resourceInstanceIdentifier; |
385 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | 389 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); |
386 | if (facade) { | 390 | if (facade) { |
387 | SinkTrace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | 391 | SinkTraceCtx(ctx) << "Trying to fetch from resource " << resourceInstanceIdentifier; |
388 | auto result = facade->load(query); | 392 | auto result = facade->load(query, ctx); |
389 | if (result.second) { | 393 | if (result.second) { |
390 | aggregatingEmitter->addEmitter(result.second); | 394 | aggregatingEmitter->addEmitter(result.second); |
391 | } else { | 395 | } else { |
392 | SinkWarning() << "Null emitter for resource " << resourceInstanceIdentifier; | 396 | SinkWarningCtx(ctx) << "Null emitter for resource " << resourceInstanceIdentifier; |
393 | } | 397 | } |
394 | result.first.exec(); | 398 | result.first.exec(); |
395 | } else { | 399 | } else { |
396 | SinkTrace() << "Couldn't find a facade for " << resourceInstanceIdentifier; | 400 | SinkTraceCtx(ctx) << "Couldn't find a facade for " << resourceInstanceIdentifier; |
397 | // Ignore the error and carry on | 401 | // Ignore the error and carry on |
398 | } | 402 | } |
399 | } | 403 | } |