That is, “result” as in “increased statistics counter”.
}
core::Statistics statistics(ctx.storage.primary.get_statistics_updates());
- const auto result_message = statistics.get_result_message();
- if (result_message) {
- LOG("Result: {}", *result_message);
+ for (const auto& message : statistics.get_statistics_messages()) {
+ LOG("Result: {}", message);
}
}
}
core::Statistics statistics(ctx.storage.primary.get_statistics_updates());
- const auto result_id = statistics.get_result_id();
- if (!result_id) {
+ const auto ids = statistics.get_statistics_ids();
+ if (ids.empty()) {
return;
}
core::StatsLog(ctx.config.stats_log())
- .log_result(ctx.args_info.input_file, *result_id);
+ .log_result(ctx.args_info.input_file, ids);
}
static void
{
}
-static const StatisticsField*
-get_result(const core::StatisticsCounters& counters)
+static std::vector<std::string>
+get_statistics_fields(const core::StatisticsCounters& counters, bool id)
{
+ std::vector<std::string> result;
for (const auto& field : k_statistics_fields) {
if (counters.get(field.statistic) != 0 && !(field.flags & FLAG_NOZERO)) {
- return &field;
+ result.emplace_back(id ? field.id : field.message);
}
}
- return nullptr;
+ std::sort(result.begin(), result.end());
+ return result;
}
-nonstd::optional<std::string>
-Statistics::get_result_id() const
+std::vector<std::string>
+Statistics::get_statistics_ids() const
{
- const auto result = get_result(m_counters);
- if (result) {
- return result->id;
- }
- return nonstd::nullopt;
+ return get_statistics_fields(m_counters, true);
}
-nonstd::optional<std::string>
-Statistics::get_result_message() const
+std::vector<std::string>
+Statistics::get_statistics_messages() const
{
- const auto result = get_result(m_counters);
- if (result) {
- return result->message;
- }
- return nonstd::nullopt;
+ return get_statistics_fields(m_counters, false);
}
std::string
#include <core/StatisticsCounters.hpp>
-#include <third_party/nonstd/optional.hpp>
-
#include <string>
#include <unordered_map>
+#include <vector>
class Config;
public:
Statistics(const StatisticsCounters& counters);
- // Return a machine-readable string representing the final ccache result, or
- // nullopt if there was no result.
- nonstd::optional<std::string> get_result_id() const;
+ // Return machine-readable strings representing the statistics counters.
+ std::vector<std::string> get_statistics_ids() const;
- // Return a human-readable string representing the final ccache result, or
- // nullopt if there was no result.
- nonstd::optional<std::string> get_result_message() const;
+ // Return human-readable strings representing the statistics counters.
+ std::vector<std::string> get_statistics_messages() const;
// Format config header in human-readable format.
static std::string format_config_header(const Config& config);
void
StatsLog::log_result(const std::string& input_file,
- const std::string& result_id)
+ const std::vector<std::string>& result_ids)
{
File file(m_path, "ab");
- if (file) {
- PRINT(*file, "# {}\n", input_file);
- PRINT(*file, "{}\n", result_id);
- } else {
+ if (!file) {
LOG("Failed to open {}: {}", m_path, strerror(errno));
+ return;
+ }
+
+ PRINT(*file, "# {}\n", input_file);
+ for (const auto& id : result_ids) {
+ PRINT(*file, "{}\n", id);
}
}
#include "StatisticsCounters.hpp"
#include <string>
+#include <vector>
namespace core {
StatsLog(const std::string& path);
StatisticsCounters read() const;
- void log_result(const std::string& input_file, const std::string& result_id);
+ void log_result(const std::string& input_file,
+ const std::vector<std::string>& result_ids);
private:
const std::string m_path;
TEST_SUITE_BEGIN("core::Statistics");
-TEST_CASE("get_result_id")
+TEST_CASE("get_statistics_ids")
{
TestContext test_context;
StatisticsCounters counters;
+ counters.increment(Statistic::cache_size_kibibyte);
counters.increment(Statistic::cache_miss);
+ counters.increment(Statistic::direct_cache_hit);
+ counters.increment(Statistic::autoconf_test);
- CHECK(*Statistics(counters).get_result_id() == "cache_miss");
+ std::vector<std::string> expected = {
+ "autoconf_test", "cache_miss", "direct_cache_hit"};
+ CHECK(Statistics(counters).get_statistics_ids() == expected);
}
-TEST_CASE("get_result_message")
+TEST_CASE("get_statistics_messages")
{
TestContext test_context;
StatisticsCounters counters;
+ counters.increment(Statistic::cache_size_kibibyte);
counters.increment(Statistic::cache_miss);
+ counters.increment(Statistic::direct_cache_hit);
+ counters.increment(Statistic::autoconf_test);
- CHECK(*Statistics(counters).get_result_message() == "cache miss");
+ std::vector<std::string> expected = {
+ "autoconf compile/link", "cache hit (direct)", "cache miss"};
+ CHECK(Statistics(counters).get_statistics_messages() == expected);
}
TEST_SUITE_END();
TestContext test_context;
StatsLog stats_log("stats.log");
- stats_log.log_result("foo.c", "cache_miss");
- stats_log.log_result("bar.c", "preprocessed_cache_hit");
+ stats_log.log_result("foo.c", {"cache_miss"});
+ stats_log.log_result("bar.c", {"preprocessed_cache_hit"});
CHECK(Util::read_file("stats.log")
== "# foo.c\ncache_miss\n# bar.c\npreprocessed_cache_hit\n");