loolstress: filter trace messages by regex config

Change-Id: I13483cd6614e5753a22408102c9cc310a587db2e
Reviewed-on: https://gerrit.libreoffice.org/27970
Reviewed-by: Ashod Nakashian <ashnakash@gmail.com>
Tested-by: Ashod Nakashian <ashnakash@gmail.com>
private/Ashod/repairactions
Ashod Nakashian 2016-08-04 18:44:24 -04:00 committed by Ashod Nakashian
parent f57964bd05
commit a3e9fc8139
4 changed files with 53 additions and 6 deletions

View File

@ -1402,7 +1402,23 @@ void LOOLWSD::initialize(Application& self)
if (getConfigValue<bool>(conf, "trace[@enable]", false))
{
const auto& path = getConfigValue<std::string>(conf, "trace.path", "");
TraceDumper.reset(new TraceFileWriter(path, getConfigValue<bool>(conf, "trace.outgoing.record", false)));
const auto recordOutgoing = getConfigValue<bool>(conf, "trace.outgoing.record", false);
std::vector<std::string> filters;
for (size_t i = 0; ; ++i)
{
const std::string confPath = "trace.filter.message[" + std::to_string(i) + "]";
const auto regex = config().getString(confPath, "");
if (!regex.empty())
{
filters.push_back(regex);
}
else if (!config().has(confPath))
{
break;
}
}
TraceDumper.reset(new TraceFileWriter(path, recordOutgoing, filters));
Log::info("Command trace dumping enabled to file: " + path);
}

View File

@ -13,6 +13,8 @@
#include <string>
#include <vector>
#include "Util.hpp"
/// Dumps commands and notification trace.
class TraceFileRecord
{
@ -40,11 +42,16 @@ public:
class TraceFileWriter
{
public:
TraceFileWriter(const std::string& path, const bool recordOugoing) :
TraceFileWriter(const std::string& path, const bool recordOugoing, const std::vector<std::string>& filters) :
_epochStart(Poco::Timestamp().epochMicroseconds()),
_recordOutgoing(recordOugoing),
_filter(true),
_stream(path, std::ios::out)
{
for (const auto& f : filters)
{
_filter.deny(f);
}
}
~TraceFileWriter()
@ -59,12 +66,15 @@ public:
void writeIncoming(const std::string& pId, const std::string& sessionId, const std::string& data)
{
write(pId, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Incoming));
if (_filter.match(data))
{
write(pId, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Incoming));
}
}
void writeOutgoing(const std::string& pId, const std::string& sessionId, const std::string& data)
{
if (_recordOutgoing)
if (_recordOutgoing && _filter.match(data))
{
write(pId, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Outgoing));
}
@ -89,6 +99,7 @@ private:
private:
const Poco::Int64 _epochStart;
const bool _recordOutgoing;
Util::RegexListMatcher _filter;
std::fstream _stream;
std::mutex _mutex;
};

View File

@ -114,22 +114,37 @@ namespace Util
class RegexListMatcher
{
public:
RegexListMatcher()
RegexListMatcher() :
_allowByDefault(false)
{
}
RegexListMatcher(const bool allowByDefault) :
_allowByDefault(allowByDefault)
{
}
RegexListMatcher(std::initializer_list<std::string> allowed) :
_allowByDefault(false),
_allowed(allowed)
{
}
RegexListMatcher(std::initializer_list<std::string> allowed,
std::initializer_list<std::string> denied) :
_allowByDefault(false),
_allowed(allowed),
_denied(denied)
{
}
RegexListMatcher(const bool allowByDefault,
std::initializer_list<std::string> denied) :
_allowByDefault(allowByDefault),
_denied(denied)
{
}
void allow(const std::string& pattern) { _allowed.insert(pattern); }
void deny(const std::string& pattern)
{
@ -145,7 +160,8 @@ namespace Util
bool match(const std::string& subject) const
{
return (match(_allowed, subject) && !match(_denied, subject));
return (_allowByDefault || match(_allowed, subject)) &&
!match(_denied, subject);
}
private:
@ -181,6 +197,7 @@ namespace Util
}
private:
const bool _allowByDefault;
std::set<std::string> _allowed;
std::set<std::string> _denied;
};

View File

@ -25,6 +25,9 @@
<trace desc="Dump commands and notifications for replay" enable="true">
<path desc="Output file path">/tmp/dump</path>
<filter>
<message desc="Regex pattern of messages to exlcude">tile.*</message>
</filter>
<outgoing>
<record desc="Whether or not to record outgoing messages" default="false">false</record>
</outgoing>