TRIQS/TRIQS 4.0.0
Researching Interacting Quantum Systems
Loading...
Searching...
No Matches
crash_logger.hpp
Go to the documentation of this file.
1// Copyright (c) 2016-2018 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2016-2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2020 Simons Foundation
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You may obtain a copy of the License at
16// https://www.gnu.org/licenses/gpl-3.0.txt
17//
18// Authors: Olivier Parcollet, Nils Wentzell
19
24
25#pragma once
26
27#include "./scope_guard.hpp"
28#include "./typeid_name.hpp"
29
30#include <h5/h5.hpp>
31
32#include <functional>
33#include <iostream>
34#include <string>
35#include <utility>
36#include <vector>
37
38namespace triqs::utility {
39
47 std::string filename_;
48 std::vector<scope_guard<std::function<void()>>> guards;
49 std::vector<std::string> names;
50
51 public:
56 crash_logger(std::string filename) : filename_(std::move(filename)) {}
57
59 crash_logger(const crash_logger &) = delete;
60
63
66
74 using std::swap;
75 swap(guards, x.guards);
76 swap(names, x.names);
77 return *this;
78 }
79
88 template <typename T> crash_logger &operator()(T const &obj, std::string name) {
89 names.push_back(name);
90 guards.emplace_back([&obj, this, name]() {
91 using h5::h5_write; // ensure proper ADL for scalar types
92 try {
93 h5_write(h5::group(h5::file(this->filename_.c_str(), 'a')), name, obj);
94 } catch (...) {
95 std::cerr << "An exception has occurred in crash_logger for an object of type " << typeid_name(obj) << " named " << name << std::endl;
96 }
97 });
98 return *this;
99 }
100
102 ~crash_logger() noexcept {
103 if ((guards.size() > 0) && (guards.front().active())) {
104 std::cerr << "crash_logger : I am destroyed without being dismissed. Dumping the objects : ";
105 for (auto &x : names) std::cerr << "\"" << x << "\" ";
106 std::cerr << std::endl;
107 h5::file(this->filename_.c_str(), 'w'); // create the file
108 }
109 }
110
112 void dismiss() {
113 for (auto &g : guards) g.dismiss();
114 }
115 };
116
117} // namespace triqs::utility
friend void h5_write(h5::group fg, std::string const &subgroup_name, this_t const &g)
Write a block Green's function to HDF5.
crash_logger & operator=(crash_logger &&x) noexcept
Move-assignment takes over guards and names from x.
~crash_logger() noexcept
Destructor writes all registered objects to the dump file if there has not been a call to dismiss().
crash_logger & operator=(const crash_logger &)=delete
Deleted copy-assignment.
crash_logger(std::string filename)
Construct a crash logger that will write to an HDF5 file on abnormal exit.
crash_logger(crash_logger &&)=default
Default move constructor.
crash_logger(const crash_logger &)=delete
Deleted copy constructor.
void dismiss()
Dismiss the logger by suppressing scope-exit dumps and by releasing registered objects.
crash_logger & operator()(T const &obj, std::string name)
Register an object to be dumped to a given HDF5 path on abnormal exit.
RAII wrapper that invokes a callable when it goes out of scope, unless dismissed.
std::string typeid_name()
Human-readable name of a given static type.
RAII helper that runs a user-provided cleanup on scope exit.
Helper functions built around std::type_info.