blob: 90813d4fe832998006f86ca944d7e34d325c4f72 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <cobalt-client/cpp/counter.h>
#include <cobalt-client/cpp/histogram.h>
#include <cobalt-client/cpp/types_internal.h>
namespace cobalt_client {
// This class provides an in memory implementation of the logger, that persists data to a map.
// This is intended to be used in tests, so that users can verify things are logged correctly, by
// instantiating the collector through Collector(InMemoryLogger());
// The class intentionally virtual and not final, to allow customization by unit tests.
class InMemoryLogger : public internal::Logger {
using HistogramBucket = internal::HistogramBucket;
template <typename MetricType>
using MetricMap = std::map<MetricOptions, MetricType, MetricOptions::LessThan>;
using HistogramStorage = std::map<uint32_t, Histogram<1>::Count>;
InMemoryLogger() = default;
InMemoryLogger(const InMemoryLogger&) = delete;
InMemoryLogger(InMemoryLogger&&) = delete;
InMemoryLogger& operator=(const InMemoryLogger&) = delete;
InMemoryLogger& operator=(InMemoryLogger&&) = delete;
~InMemoryLogger() override;
// Adds the contents of buckets and the required info to a buffer.
bool Log(const MetricOptions& metric_info, const HistogramBucket* buckets,
size_t num_buckets) override;
// Adds the count and the required info to a buffer.
bool Log(const MetricOptions& metric_info, int64_t count) override;
bool LogInteger(const MetricOptions& metric_info, int64_t bytes) override;
const MetricMap<Counter::Count>& counters() const { return persisted_counters_; }
const MetricMap<HistogramStorage>& histograms() const { return persisted_histograms_; }
void fail_logging(bool fail) { fail_logging_ = fail; }
bool fail_logging_ = false;
MetricMap<Counter::Count> persisted_counters_;
MetricMap<HistogramStorage> persisted_histograms_;
} // namespace cobalt_client