]> git.ipfire.org Git - thirdparty/ccache.git/blame - src/storage/local/StatsFile.hpp
enhance: Support updating stats file only if values have changed
[thirdparty/ccache.git] / src / storage / local / StatsFile.hpp
CommitLineData
4d13759b 1// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
a28af38b
JR
2//
3// See doc/AUTHORS.adoc for a complete list of contributors.
4//
5// This program is free software; you can redistribute it and/or modify it
6// under the terms of the GNU General Public License as published by the Free
7// Software Foundation; either version 3 of the License, or (at your option)
8// any later version.
9//
10// This program is distributed in the hope that it will be useful, but WITHOUT
11// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13// more details.
14//
15// You should have received a copy of the GNU General Public License along with
16// this program; if not, write to the Free Software Foundation, Inc., 51
17// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
19#pragma once
20
21#include <core/StatisticsCounters.hpp>
22
a28af38b 23#include <functional>
eea89d4a 24#include <optional>
a28af38b
JR
25#include <string>
26
0cd6f70b 27namespace storage::local {
a28af38b
JR
28
29class StatsFile
30{
31public:
52f0cfce 32 explicit StatsFile(const std::string& path);
a28af38b
JR
33
34 // Read counters. No lock is acquired. If the file doesn't exist all returned
35 // counters will be zero.
36 core::StatisticsCounters read() const;
37
4d13759b
JR
38 enum class OnlyIfChanged { no, yes };
39
a28af38b
JR
40 // Acquire a lock, read counters, call `function` with the counters, write the
41 // counters and release the lock. Returns the resulting counters or nullopt on
42 // error (e.g. if the lock could not be acquired).
eea89d4a 43 std::optional<core::StatisticsCounters>
4d13759b
JR
44 update(std::function<void(core::StatisticsCounters& counters)>,
45 OnlyIfChanged only_if_changed = OnlyIfChanged::no) const;
a28af38b
JR
46
47private:
4a925141 48 std::string m_path;
a28af38b
JR
49};
50
0cd6f70b 51} // namespace storage::local