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
This commit is contained in:
Lorenzo Blasa
2023-01-10 06:48:53 -08:00
committed by Facebook GitHub Bot
parent 882b969931
commit a040fb8d4e
2 changed files with 23 additions and 6 deletions

View File

@@ -10,6 +10,8 @@
#include "FlipperStateUpdateListener.h" #include "FlipperStateUpdateListener.h"
#include "FlipperStep.h" #include "FlipperStep.h"
#define FLIPPER_LOGS_CAPACITY 4096
#if FLIPPER_DEBUG_LOG #if FLIPPER_DEBUG_LOG
#include "Log.h" #include "Log.h"
#endif #endif
@@ -19,7 +21,8 @@ using namespace facebook::flipper;
/* Class responsible for collecting state updates and combining them into a /* Class responsible for collecting state updates and combining them into a
* view of the current state of the flipper client. */ * view of the current state of the flipper client. */
FlipperState::FlipperState() : logs("") {} FlipperState::FlipperState() {}
void FlipperState::setUpdateListener( void FlipperState::setUpdateListener(
std::shared_ptr<FlipperStateUpdateListener> listener) { std::shared_ptr<FlipperStateUpdateListener> listener) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
@@ -46,14 +49,25 @@ void FlipperState::started(std::string step) {
} }
} }
void FlipperState::ensureLogsCapacity() {
if (logs.tellp() > FLIPPER_LOGS_CAPACITY) {
logs.str("");
logs.clear();
logs << "[Truncated]" << std::endl;
}
}
void FlipperState::success(std::string step) { void FlipperState::success(std::string step) {
std::shared_ptr<FlipperStateUpdateListener> localListener; std::shared_ptr<FlipperStateUpdateListener> localListener;
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
std::string message = "[Success] " + step;
#if FLIPPER_DEBUG_LOG #if FLIPPER_DEBUG_LOG
log("[finished] " + step); log(message);
#endif #endif
logs = logs + "[Success] " + step + "\n"; ensureLogsCapacity();
logs << message << std::endl;
stateMap[step] = State::success; stateMap[step] = State::success;
localListener = mListener; localListener = mListener;
} }
@@ -72,7 +86,8 @@ void FlipperState::failed(std::string step, std::string errorMessage) {
#if FLIPPER_DEBUG_LOG #if FLIPPER_DEBUG_LOG
log(message); log(message);
#endif #endif
logs = logs + message + "\n"; ensureLogsCapacity();
logs << message << std::endl;
stateMap[step] = State::failed; stateMap[step] = State::failed;
localListener = mListener; localListener = mListener;
} }
@@ -88,7 +103,7 @@ void FlipperState::failed(std::string step, std::string errorMessage) {
// way // way
std::string FlipperState::getState() { std::string FlipperState::getState() {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
return logs; return logs.str();
} }
std::vector<StateElement> FlipperState::getStateElements() { std::vector<StateElement> FlipperState::getStateElements() {

View File

@@ -10,6 +10,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <sstream>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -53,10 +54,11 @@ class FlipperState {
void success(std::string); void success(std::string);
void failed(std::string, std::string); void failed(std::string, std::string);
void started(std::string); void started(std::string);
void ensureLogsCapacity();
std::mutex mutex; // Protects all our member variables. std::mutex mutex; // Protects all our member variables.
std::shared_ptr<FlipperStateUpdateListener> mListener = nullptr; std::shared_ptr<FlipperStateUpdateListener> mListener = nullptr;
std::string logs; std::stringstream logs;
std::vector<std::string> insertOrder; std::vector<std::string> insertOrder;
std::map<std::string, facebook::flipper::State> stateMap; std::map<std::string, facebook::flipper::State> stateMap;
}; };