blob: c2d0f861c6cbb4f9c8565557d544167c055867c2 [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 <lib/zx/time.h>
#include <zircon/compiler.h>
#include <future>
#include <optional>
#include <thread>
// Print a log message every |logging_interval| units of time.
// A thread will be started that will log the string |message| after
// |logging_interval| has passed, and then continue to print |message|
// every |logging_interval|.
class __WARN_UNUSED_CONSTRUCTOR PeriodicLogger {
PeriodicLogger() = default;
PeriodicLogger(std::string message, zx::duration logging_interval);
// Prevent copy and move.
PeriodicLogger(const PeriodicLogger&) = delete;
PeriodicLogger& operator=(const PeriodicLogger&) = delete;
// Start logging the given message.
// If a message is already being logged, this new message and interval
// will replace it.
void Start(std::string message, zx::duration logging_interval);
// Stop logging.
void Stop();
std::promise<void> should_stop_;
std::optional<std::thread> logging_thread_;