Implemented thread specific error-downgrading to events and wrote corresponding unit test

This commit is contained in:
EricLiu2000
2019-07-24 12:52:15 -04:00
parent 42a5d525ce
commit 6f8d307850
5 changed files with 87 additions and 4 deletions
+2
View File
@@ -102,6 +102,8 @@ public:
std::string getDescription() const noexcept { return std::string(eventStruct.description); }
const Device* getDevice() const noexcept { return device; } // Will return nullptr if this is an API-wide event
EventTimePoint getTimestamp() const noexcept { return timepoint; }
void downgradeFromError() noexcept;
bool isForDevice(const Device* forDevice) const noexcept { return forDevice == device; }
bool isForDevice(std::string serial) const noexcept;
+17 -4
View File
@@ -2,11 +2,13 @@
#define __ICSNEO_API_EVENTMANAGER_H_
#include <vector>
#include <set>
#include <list>
#include <mutex>
#include <functional>
#include<map>
#include <thread>
#include <algorithm>
#include "icsneo/api/event.h"
namespace icsneo {
@@ -19,6 +21,10 @@ public:
static void ResetInstance();
void downgradeErrorsOnCurrentThread();
void cancelErrorDowngradingOnCurrentThread();
size_t eventCount(EventFilter filter = EventFilter()) const {
std::lock_guard<std::mutex> lk(mutex);
return count_internal(filter);
@@ -67,13 +73,15 @@ public:
}
private:
EventManager() : mutex(), events(), lastUserErrors(), eventLimit(10000) {}
EventManager() : mutex(), downgradedThreads(), events(), lastUserErrors(), eventLimit(10000) {}
EventManager(const EventManager &other);
EventManager& operator=(const EventManager &other);
// Used by functions for threadsafety
mutable std::mutex mutex;
std::set<std::thread::id> downgradedThreads;
// Stores all events
std::list<APIEvent> events;
std::map<std::thread::id, APIEvent> lastUserErrors;
@@ -82,9 +90,14 @@ private:
size_t count_internal(EventFilter filter = EventFilter()) const;
void add_internal(APIEvent event) {
if(event.getSeverity() == APIEvent::Severity::Error)
add_internal_error(event);
else
if(event.getSeverity() == APIEvent::Severity::Error) {
// if the error was added on a thread that downgrades errors (non-user thread)
if(std::find(downgradedThreads.begin(), downgradedThreads.end(), std::this_thread::get_id()) != downgradedThreads.end()) {
event.downgradeFromError();
add_internal_event(event);
} else
add_internal_error(event);
} else
add_internal_event(event);
}