Skip to content

Commit 23bff34

Browse files
committed
Less intrusive warnings, allow disabling them
1 parent 25385a2 commit 23bff34

3 files changed

Lines changed: 53 additions & 18 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1377,7 +1377,7 @@ if(openPMD_BUILD_TESTING)
13771377
--outfile ../samples/git-sample/thetaMode/data%T.json \
13781378
--outconfig ' \
13791379
json.attribute.mode = \"short\" \n\
1380-
json.dataset.mode = \"template\"' \
1380+
json.dataset.mode = \"template_no_warn\"' \
13811381
"
13821382
WORKING_DIRECTORY ${openPMD_RUNTIME_OUTPUT_DIRECTORY}
13831383
)

include/openPMD/IO/JSON/JSONIOHandlerImpl.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,22 @@ class JSONIOHandlerImpl : public AbstractIOHandlerImpl
292292

293293
IOMode m_mode = IOMode::Dataset;
294294
SpecificationVia m_IOModeSpecificationVia = SpecificationVia::DefaultValue;
295+
bool m_printedSkippedWriteWarningAlready = false;
295296

296-
std::pair<IOMode, SpecificationVia>
297-
retrieveDatasetMode(openPMD::json::TracingJSON &config) const;
297+
struct DatasetMode
298+
{
299+
IOMode m_IOMode;
300+
SpecificationVia m_specificationVia;
301+
bool m_skipWarnings;
302+
303+
template <typename A, typename B, typename C>
304+
operator std::tuple<A, B, C>()
305+
{
306+
return std::tuple<A, B, C>{
307+
m_IOMode, m_specificationVia, m_skipWarnings};
308+
}
309+
};
310+
DatasetMode retrieveDatasetMode(openPMD::json::TracingJSON &config) const;
298311

299312
///////////////////////
300313
// Attribute IO mode //

src/IO/JSON/JSONIOHandlerImpl.cpp

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,12 @@ namespace
248248
}
249249
} // namespace
250250

251-
auto JSONIOHandlerImpl::retrieveDatasetMode(openPMD::json::TracingJSON &config)
252-
const -> std::pair<IOMode, SpecificationVia>
251+
auto JSONIOHandlerImpl::retrieveDatasetMode(
252+
openPMD::json::TracingJSON &config) const -> DatasetMode
253253
{
254-
IOMode res = m_mode;
255-
SpecificationVia res_2 = SpecificationVia::DefaultValue;
254+
IOMode ioMode = m_mode;
255+
SpecificationVia specificationVia = SpecificationVia::DefaultValue;
256+
bool skipWarnings = false;
256257
if (auto [configLocation, maybeConfig] = getBackendConfig(config);
257258
maybeConfig.has_value())
258259
{
@@ -274,13 +275,19 @@ auto JSONIOHandlerImpl::retrieveDatasetMode(openPMD::json::TracingJSON &config)
274275
auto mode = modeOption.value();
275276
if (mode == "dataset")
276277
{
277-
res = IOMode::Dataset;
278-
res_2 = SpecificationVia::Manually;
278+
ioMode = IOMode::Dataset;
279+
specificationVia = SpecificationVia::Manually;
279280
}
280281
else if (mode == "template")
281282
{
282-
res = IOMode::Template;
283-
res_2 = SpecificationVia::Manually;
283+
ioMode = IOMode::Template;
284+
specificationVia = SpecificationVia::Manually;
285+
}
286+
else if (mode == "template_no_warn")
287+
{
288+
ioMode = IOMode::Template;
289+
specificationVia = SpecificationVia::Manually;
290+
skipWarnings = true;
284291
}
285292
else
286293
{
@@ -292,7 +299,7 @@ auto JSONIOHandlerImpl::retrieveDatasetMode(openPMD::json::TracingJSON &config)
292299
}
293300
}
294301
}
295-
return std::make_pair(res, res_2);
302+
return DatasetMode{ioMode, specificationVia, skipWarnings};
296303
}
297304

298305
auto JSONIOHandlerImpl::retrieveAttributeMode(
@@ -379,7 +386,9 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
379386
, m_fileFormat{format}
380387
, m_originalExtension{std::move(originalExtension)}
381388
{
382-
std::tie(m_mode, m_IOModeSpecificationVia) = retrieveDatasetMode(config);
389+
std::tie(
390+
m_mode, m_IOModeSpecificationVia, m_printedSkippedWriteWarningAlready) =
391+
retrieveDatasetMode(config);
383392
std::tie(m_attributeMode, m_attributeModeSpecificationVia) =
384393
retrieveAttributeMode(config);
385394

@@ -403,7 +412,9 @@ JSONIOHandlerImpl::JSONIOHandlerImpl(
403412
, m_fileFormat{format}
404413
, m_originalExtension{std::move(originalExtension)}
405414
{
406-
std::tie(m_mode, m_IOModeSpecificationVia) = retrieveDatasetMode(config);
415+
std::tie(
416+
m_mode, m_IOModeSpecificationVia, m_printedSkippedWriteWarningAlready) =
417+
retrieveDatasetMode(config);
407418
std::tie(m_attributeMode, m_attributeModeSpecificationVia) =
408419
retrieveAttributeMode(config);
409420

@@ -548,7 +559,13 @@ void JSONIOHandlerImpl::createDataset(
548559
parameter.options, /* considerFiles = */ false);
549560
// Retrieves mode from dataset-specific configuration, falls back to global
550561
// value if not defined
551-
IOMode localMode = retrieveDatasetMode(config).first;
562+
auto [localMode, _, skipWarnings] = retrieveDatasetMode(config);
563+
(void)_;
564+
// No use in introducing logic to skip warnings only for one particular
565+
// dataset. If warnings are skipped, then they are skipped consistently.
566+
// Use |= since `false` is the default value and we don't wish to reset
567+
// the flag.
568+
m_printedSkippedWriteWarningAlready |= skipWarnings;
552569

553570
parameter.warnUnusedParameters(
554571
config,
@@ -1183,9 +1200,14 @@ void JSONIOHandlerImpl::writeDataset(
11831200
case IOMode::Dataset:
11841201
break;
11851202
case IOMode::Template:
1186-
std::cerr << "[JSON/TOML backend: Warning] Trying to write data to a "
1187-
"template dataset. Will skip."
1188-
<< std::endl;
1203+
if (!m_printedSkippedWriteWarningAlready)
1204+
{
1205+
std::cerr
1206+
<< "[JSON/TOML backend: Warning] Trying to write data to a "
1207+
"template dataset. Will skip."
1208+
<< std::endl;
1209+
m_printedSkippedWriteWarningAlready = true;
1210+
}
11891211
return;
11901212
}
11911213

0 commit comments

Comments
 (0)