Files
flipper/xplat/Flipper/FlipperState.h
Lorenzo Blasa a040fb8d4e Ensure logs don't indefinitely append past capacity
Summary:
^

There could be cases, albeit unlikely, that logs could be appended for the current state indefintely that would ultimate fail due to not having enough memory.

This change puts a cap on that.

Reviewed By: mweststrate

Differential Revision: D42313904

fbshipit-source-id: 7fd96be822c9427720bccb41c6c32a39213c7652
2023-01-10 06:48:53 -08:00

65 lines
1.7 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <map>
#include <memory>
#include <mutex>
#include <sstream>
#include <string>
#include <vector>
class FlipperStep;
class FlipperStateUpdateListener;
namespace facebook {
namespace flipper {
enum State { success, in_progress, failed };
class StateElement {
public:
StateElement(std::string name, State state) : name_(name), state_(state){};
std::string name_;
State state_;
};
} // namespace flipper
} // namespace facebook
class FlipperState {
friend FlipperStep;
public:
FlipperState();
// Update listeners are responsible for their own
// synchronization. There is no guarantee about which thread they
// may be called on.
void setUpdateListener(std::shared_ptr<FlipperStateUpdateListener>);
std::string getState();
std::vector<facebook::flipper::StateElement> getStateElements();
/* To record a state update, call start() with the name of the step to get a
FlipperStep object. Call complete on this to register it successful,
the absense of the completion call when it is destructed will register as a
step failure. */
std::shared_ptr<FlipperStep> start(std::string step);
private:
void success(std::string);
void failed(std::string, std::string);
void started(std::string);
void ensureLogsCapacity();
std::mutex mutex; // Protects all our member variables.
std::shared_ptr<FlipperStateUpdateListener> mListener = nullptr;
std::stringstream logs;
std::vector<std::string> insertOrder;
std::map<std::string, facebook::flipper::State> stateMap;
};