2016-07-30 21:22:28 -05:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
2017-02-07 10:16:59 -06:00
|
|
|
#ifndef INCLUDED_TRACEFILE_HPP
|
|
|
|
#define INCLUDED_TRACEFILE_HPP
|
|
|
|
|
2016-07-30 21:22:28 -05:00
|
|
|
#include <fstream>
|
2016-07-31 13:17:52 -05:00
|
|
|
#include <mutex>
|
2016-07-31 17:06:01 -05:00
|
|
|
#include <sstream>
|
2016-07-31 13:17:52 -05:00
|
|
|
#include <string>
|
2016-07-31 17:06:01 -05:00
|
|
|
#include <vector>
|
2016-07-30 21:22:28 -05:00
|
|
|
|
2017-02-05 11:13:32 -06:00
|
|
|
#include <Poco/DateTime.h>
|
|
|
|
#include <Poco/DateTimeFormatter.h>
|
2016-08-06 20:23:57 -05:00
|
|
|
#include <Poco/DeflatingStream.h>
|
2016-08-17 18:41:58 -05:00
|
|
|
#include <Poco/InflatingStream.h>
|
2017-02-07 10:16:59 -06:00
|
|
|
#include <Poco/URI.h>
|
2016-08-06 20:23:57 -05:00
|
|
|
|
2017-02-05 18:35:54 -06:00
|
|
|
#include "Protocol.hpp"
|
|
|
|
#include "Log.hpp"
|
2016-08-04 17:44:24 -05:00
|
|
|
#include "Util.hpp"
|
|
|
|
|
2016-07-30 21:22:28 -05:00
|
|
|
/// Dumps commands and notification trace.
|
2016-08-02 09:00:37 -05:00
|
|
|
class TraceFileRecord
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Direction : char
|
|
|
|
{
|
|
|
|
Invalid = 0,
|
|
|
|
Incoming = '>',
|
2016-08-02 20:09:01 -05:00
|
|
|
Outgoing = '<',
|
2017-05-01 14:33:37 -05:00
|
|
|
Event = '~'
|
2016-08-02 09:00:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
TraceFileRecord() :
|
2016-09-28 03:20:24 -05:00
|
|
|
Dir(Direction::Invalid),
|
|
|
|
TimestampNs(0),
|
|
|
|
Pid(0)
|
2016-08-02 09:00:37 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:54:52 -06:00
|
|
|
std::string toString() const
|
|
|
|
{
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << static_cast<char>(Dir) << Pid << static_cast<char>(Dir)
|
|
|
|
<< SessionId << static_cast<char>(Dir) << Payload;
|
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2016-08-02 09:00:37 -05:00
|
|
|
Direction Dir;
|
|
|
|
unsigned TimestampNs;
|
2016-08-03 07:15:04 -05:00
|
|
|
unsigned Pid;
|
2016-08-04 09:31:11 -05:00
|
|
|
std::string SessionId;
|
2016-08-02 09:00:37 -05:00
|
|
|
std::string Payload;
|
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Trace-file generator class.
|
|
|
|
/// Writes records into a trace file.
|
2016-07-31 12:56:57 -05:00
|
|
|
class TraceFileWriter
|
2016-07-30 21:22:28 -05:00
|
|
|
{
|
|
|
|
public:
|
2017-02-05 20:59:08 -06:00
|
|
|
TraceFileWriter(const std::string& path,
|
|
|
|
const bool recordOugoing,
|
|
|
|
const bool compress,
|
|
|
|
const bool takeSnapshot,
|
2016-08-06 20:23:57 -05:00
|
|
|
const std::vector<std::string>& filters) :
|
2016-07-30 22:06:12 -05:00
|
|
|
_epochStart(Poco::Timestamp().epochMicroseconds()),
|
2016-08-04 12:52:28 -05:00
|
|
|
_recordOutgoing(recordOugoing),
|
2016-08-06 20:23:57 -05:00
|
|
|
_compress(compress),
|
2017-02-05 20:59:08 -06:00
|
|
|
_takeSnapshot(takeSnapshot),
|
2017-02-05 18:35:54 -06:00
|
|
|
_path(Poco::Path(path).parent().toString()),
|
2016-08-04 17:44:24 -05:00
|
|
|
_filter(true),
|
2017-02-05 11:13:32 -06:00
|
|
|
_stream(processPath(path), compress ? std::ios::binary : std::ios::out),
|
2016-12-07 07:31:42 -06:00
|
|
|
_deflater(_stream, Poco::DeflatingStreamBuf::STREAM_GZIP)
|
2016-07-30 21:22:28 -05:00
|
|
|
{
|
2016-08-04 17:44:24 -05:00
|
|
|
for (const auto& f : filters)
|
|
|
|
{
|
|
|
|
_filter.deny(f);
|
|
|
|
}
|
2016-07-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2016-07-31 12:56:57 -05:00
|
|
|
~TraceFileWriter()
|
2016-07-30 21:22:28 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-08-06 20:23:57 -05:00
|
|
|
_deflater.close();
|
2016-07-30 21:22:28 -05:00
|
|
|
_stream.close();
|
|
|
|
}
|
|
|
|
|
2017-02-05 18:35:54 -06:00
|
|
|
void newSession(const std::string& id, const std::string& sessionId, const std::string& uri, const std::string& localPath)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-02-05 20:59:08 -06:00
|
|
|
std::string snapshot = uri;
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2017-02-05 20:59:08 -06:00
|
|
|
if (_takeSnapshot)
|
2017-02-05 18:35:54 -06:00
|
|
|
{
|
2017-02-10 00:06:17 -06:00
|
|
|
std::string decodedUri;
|
|
|
|
Poco::URI::decode(uri, decodedUri);
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string url = Poco::URI(decodedUri).getPath();
|
2017-02-05 20:59:08 -06:00
|
|
|
const auto it = _urlToSnapshot.find(url);
|
|
|
|
if (it != _urlToSnapshot.end())
|
|
|
|
{
|
|
|
|
snapshot = it->second.Snapshot;
|
|
|
|
it->second.SessionCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Create a snapshot file.
|
|
|
|
const Poco::Path origPath(localPath);
|
|
|
|
std::string filename = origPath.getBaseName();
|
2017-05-01 14:33:37 -05:00
|
|
|
filename += '_' + Poco::DateTimeFormatter::format(Poco::DateTime(), "%Y%m%d_%H-%M-%S");
|
2017-02-05 20:59:08 -06:00
|
|
|
filename += '.' + origPath.getExtension();
|
|
|
|
snapshot = Poco::Path(_path, filename).toString();
|
|
|
|
|
|
|
|
LOG_TRC("TraceFile: Copying local file [" << localPath << "] to snapshot [" << snapshot << "].");
|
|
|
|
Poco::File(localPath).copyTo(snapshot);
|
|
|
|
snapshot = Poco::URI(Poco::URI("file://"), snapshot).toString();
|
|
|
|
|
|
|
|
LOG_TRC("TraceFile: Mapped URL " << url << " to " << snapshot);
|
|
|
|
_urlToSnapshot.emplace(url, SnapshotData(snapshot));
|
|
|
|
}
|
2017-02-05 18:35:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto data = "NewSession: " + snapshot;
|
|
|
|
writeLocked(id, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Event));
|
|
|
|
flushLocked();
|
|
|
|
}
|
|
|
|
|
|
|
|
void endSession(const std::string& id, const std::string& sessionId, const std::string& uri)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-02-05 20:59:08 -06:00
|
|
|
std::string snapshot = uri;
|
2017-02-05 18:35:54 -06:00
|
|
|
|
2018-02-07 03:17:59 -06:00
|
|
|
const std::string url = Poco::URI(uri).getPath();
|
2017-02-05 18:35:54 -06:00
|
|
|
const auto it = _urlToSnapshot.find(url);
|
|
|
|
if (it != _urlToSnapshot.end())
|
|
|
|
{
|
|
|
|
snapshot = it->second.Snapshot;
|
|
|
|
if (it->second.SessionCount == 1)
|
|
|
|
{
|
|
|
|
// Last session, remove the mapping.
|
|
|
|
_urlToSnapshot.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
it->second.SessionCount--;
|
|
|
|
}
|
|
|
|
}
|
2017-02-05 20:59:08 -06:00
|
|
|
|
|
|
|
const auto data = "EndSession: " + snapshot;
|
|
|
|
writeLocked(id, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Event));
|
|
|
|
flushLocked();
|
2017-02-05 18:35:54 -06:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void writeEvent(const std::string& id, const std::string& sessionId, const std::string& data)
|
2016-08-02 20:09:01 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
writeLocked(id, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Event));
|
2016-09-28 04:39:20 -05:00
|
|
|
flushLocked();
|
2016-08-02 20:09:01 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void writeIncoming(const std::string& id, const std::string& sessionId, const std::string& data)
|
2016-07-30 21:22:28 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-08-04 17:44:24 -05:00
|
|
|
if (_filter.match(data))
|
|
|
|
{
|
2017-02-05 18:35:54 -06:00
|
|
|
// Remap the URL to the snapshot.
|
|
|
|
if (LOOLProtocol::matchPrefix("load", data))
|
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
std::vector<std::string> tokens = LOOLProtocol::tokenize(data);
|
2017-02-05 18:35:54 -06:00
|
|
|
if (tokens.size() >= 2)
|
|
|
|
{
|
|
|
|
std::string url;
|
|
|
|
if (LOOLProtocol::getTokenString(tokens[1], "url", url))
|
|
|
|
{
|
|
|
|
std::string decodedUrl;
|
|
|
|
Poco::URI::decode(url, decodedUrl);
|
2018-02-07 03:17:59 -06:00
|
|
|
Poco::URI uriPublic = Poco::URI(decodedUrl);
|
2017-02-05 18:35:54 -06:00
|
|
|
if (uriPublic.isRelative() || uriPublic.getScheme() == "file")
|
|
|
|
{
|
|
|
|
uriPublic.normalize();
|
|
|
|
}
|
|
|
|
|
|
|
|
url = uriPublic.getPath();
|
|
|
|
const auto it = _urlToSnapshot.find(url);
|
|
|
|
if (it != _urlToSnapshot.end())
|
|
|
|
{
|
|
|
|
LOG_TRC("TraceFile: Mapped URL: " << url << " to " << it->second.Snapshot);
|
|
|
|
tokens[1] = "url=" + it->second.Snapshot;
|
|
|
|
std::string newData;
|
|
|
|
for (const auto& token : tokens)
|
|
|
|
{
|
|
|
|
newData += token + ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
writeLocked(id, sessionId, newData, static_cast<char>(TraceFileRecord::Direction::Incoming));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
writeLocked(id, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Incoming));
|
2016-08-04 17:44:24 -05:00
|
|
|
}
|
2016-07-30 21:22:28 -05:00
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void writeOutgoing(const std::string& id, const std::string& sessionId, const std::string& data)
|
2016-08-02 09:00:37 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
std::unique_lock<std::mutex> lock(_mutex);
|
|
|
|
|
2016-08-04 17:44:24 -05:00
|
|
|
if (_recordOutgoing && _filter.match(data))
|
2016-08-04 12:52:28 -05:00
|
|
|
{
|
2017-01-13 06:52:08 -06:00
|
|
|
writeLocked(id, sessionId, data, static_cast<char>(TraceFileRecord::Direction::Outgoing));
|
2016-08-04 12:52:28 -05:00
|
|
|
}
|
2016-08-02 09:00:37 -05:00
|
|
|
}
|
|
|
|
|
2016-09-28 04:39:20 -05:00
|
|
|
private:
|
|
|
|
void flushLocked()
|
2016-09-19 17:48:30 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
|
|
|
|
2016-09-19 17:48:30 -05:00
|
|
|
_deflater.flush();
|
|
|
|
_stream.flush();
|
|
|
|
}
|
|
|
|
|
2017-01-13 06:52:08 -06:00
|
|
|
void writeLocked(const std::string& id, const std::string& sessionId, const std::string& data, const char delim)
|
2016-07-31 06:54:47 -05:00
|
|
|
{
|
2016-09-28 04:39:20 -05:00
|
|
|
Util::assertIsLocked(_mutex);
|
|
|
|
|
2016-07-31 06:54:47 -05:00
|
|
|
const Poco::Int64 usec = Poco::Timestamp().epochMicroseconds() - _epochStart;
|
2016-08-06 20:23:57 -05:00
|
|
|
if (_compress)
|
|
|
|
{
|
|
|
|
_deflater.write(&delim, 1);
|
|
|
|
_deflater << usec;
|
|
|
|
_deflater.write(&delim, 1);
|
2017-01-13 06:52:08 -06:00
|
|
|
_deflater << id;
|
2016-08-06 20:23:57 -05:00
|
|
|
_deflater.write(&delim, 1);
|
|
|
|
_deflater << sessionId;
|
|
|
|
_deflater.write(&delim, 1);
|
|
|
|
_deflater.write(data.c_str(), data.size());
|
|
|
|
_deflater.write("\n", 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_stream.write(&delim, 1);
|
|
|
|
_stream << usec;
|
|
|
|
_stream.write(&delim, 1);
|
2017-01-13 06:52:08 -06:00
|
|
|
_stream << id;
|
2016-08-06 20:23:57 -05:00
|
|
|
_stream.write(&delim, 1);
|
|
|
|
_stream << sessionId;
|
|
|
|
_stream.write(&delim, 1);
|
|
|
|
_stream.write(data.c_str(), data.size());
|
|
|
|
_stream.write("\n", 1);
|
|
|
|
}
|
2016-07-31 06:54:47 -05:00
|
|
|
}
|
|
|
|
|
2017-02-05 11:13:32 -06:00
|
|
|
static std::string processPath(const std::string& path)
|
|
|
|
{
|
2018-02-07 03:17:59 -06:00
|
|
|
const size_t pos = path.find('%');
|
2017-02-05 11:13:32 -06:00
|
|
|
if (pos == std::string::npos)
|
|
|
|
{
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string res = path.substr(0, pos);
|
2017-05-01 14:33:37 -05:00
|
|
|
res += Poco::DateTimeFormatter::format(Poco::DateTime(), "%Y%m%d_%H-%M-%S");
|
2017-02-05 11:13:32 -06:00
|
|
|
res += path.substr(pos + 1);
|
2017-05-01 14:33:37 -05:00
|
|
|
LOG_INF("Command trace dumping enabled to file: " << res);
|
2017-02-05 11:13:32 -06:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-02-05 18:35:54 -06:00
|
|
|
private:
|
|
|
|
struct SnapshotData
|
|
|
|
{
|
|
|
|
SnapshotData(const std::string& snapshot) :
|
|
|
|
Snapshot(snapshot)
|
|
|
|
{
|
|
|
|
SessionCount = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SnapshotData(const SnapshotData& other) :
|
|
|
|
Snapshot(other.Snapshot)
|
|
|
|
{
|
|
|
|
SessionCount = other.SessionCount.load();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Snapshot;
|
|
|
|
std::atomic<size_t> SessionCount;
|
|
|
|
};
|
|
|
|
|
2016-07-30 21:22:28 -05:00
|
|
|
private:
|
2016-07-30 22:06:12 -05:00
|
|
|
const Poco::Int64 _epochStart;
|
2016-08-04 12:52:28 -05:00
|
|
|
const bool _recordOutgoing;
|
2016-08-06 20:23:57 -05:00
|
|
|
const bool _compress;
|
2017-02-05 20:59:08 -06:00
|
|
|
const bool _takeSnapshot;
|
2017-02-05 18:35:54 -06:00
|
|
|
const std::string _path;
|
2016-08-04 17:44:24 -05:00
|
|
|
Util::RegexListMatcher _filter;
|
2016-08-06 20:23:57 -05:00
|
|
|
std::ofstream _stream;
|
|
|
|
Poco::DeflatingOutputStream _deflater;
|
2016-07-30 22:06:12 -05:00
|
|
|
std::mutex _mutex;
|
2017-02-05 18:35:54 -06:00
|
|
|
std::map<std::string, SnapshotData> _urlToSnapshot;
|
2016-07-30 21:22:28 -05:00
|
|
|
};
|
|
|
|
|
2016-08-13 23:01:13 -05:00
|
|
|
/// Trace-file parser class.
|
|
|
|
/// Reads records from a trace file.
|
2016-07-31 13:17:52 -05:00
|
|
|
class TraceFileReader
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TraceFileReader(const std::string& path) :
|
2016-08-17 18:41:58 -05:00
|
|
|
_compressed(path.size() > 2 && path.substr(path.size() - 2) == "gz"),
|
2016-08-04 11:17:22 -05:00
|
|
|
_epochStart(0),
|
2017-02-09 23:54:52 -06:00
|
|
|
_epochEnd(0),
|
2016-08-17 18:41:58 -05:00
|
|
|
_stream(path, _compressed ? std::ios::binary : std::ios::in),
|
|
|
|
_inflater(_stream, Poco::InflatingStreamBuf::STREAM_GZIP),
|
2016-08-04 09:31:11 -05:00
|
|
|
_index(0),
|
2016-07-31 18:32:37 -05:00
|
|
|
_indexIn(-1),
|
|
|
|
_indexOut(-1)
|
2016-07-31 13:17:52 -05:00
|
|
|
{
|
2016-07-31 18:32:37 -05:00
|
|
|
readFile();
|
2016-07-31 13:17:52 -05:00
|
|
|
}
|
|
|
|
|
2016-08-17 18:41:58 -05:00
|
|
|
~TraceFileReader()
|
|
|
|
{
|
|
|
|
_stream.close();
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:54:52 -06:00
|
|
|
Poco::Int64 getEpochStart() const { return _epochStart; }
|
|
|
|
Poco::Int64 getEpochEnd() const { return _epochEnd; }
|
2016-08-04 11:17:22 -05:00
|
|
|
|
2016-08-04 09:31:11 -05:00
|
|
|
TraceFileRecord getNextRecord()
|
|
|
|
{
|
|
|
|
if (_index < _records.size())
|
|
|
|
{
|
|
|
|
return _records[_index++];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid.
|
|
|
|
return TraceFileRecord();
|
|
|
|
}
|
2016-08-01 20:53:09 -05:00
|
|
|
|
2016-07-31 18:32:37 -05:00
|
|
|
TraceFileRecord getNextRecord(const TraceFileRecord::Direction dir)
|
|
|
|
{
|
|
|
|
if (dir == TraceFileRecord::Direction::Incoming)
|
|
|
|
{
|
|
|
|
if (_indexIn < _records.size())
|
|
|
|
{
|
|
|
|
const TraceFileRecord rec = _records[_indexIn];
|
|
|
|
_indexIn = advance(_indexIn, dir);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_indexOut < _records.size())
|
|
|
|
{
|
|
|
|
const TraceFileRecord rec = _records[_indexOut];
|
|
|
|
_indexOut = advance(_indexOut, dir);
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invalid.
|
|
|
|
return TraceFileRecord();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-07-31 17:06:01 -05:00
|
|
|
void readFile()
|
|
|
|
{
|
2016-07-31 18:32:37 -05:00
|
|
|
_records.clear();
|
|
|
|
|
2016-07-31 17:06:01 -05:00
|
|
|
std::string line;
|
2016-08-17 18:41:58 -05:00
|
|
|
for (;;)
|
2016-07-31 17:06:01 -05:00
|
|
|
{
|
2016-08-17 18:41:58 -05:00
|
|
|
if (_compressed)
|
|
|
|
{
|
|
|
|
std::getline(_inflater, line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::getline(_stream, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line.empty())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-06 06:36:54 -06:00
|
|
|
TraceFileRecord rec;
|
|
|
|
if (extractRecord(line, rec))
|
2016-07-31 17:06:01 -05:00
|
|
|
_records.push_back(rec);
|
2016-08-04 09:31:11 -05:00
|
|
|
else
|
|
|
|
fprintf(stderr, "Invalid trace file record, expected 4 tokens. [%s]\n", line.c_str());
|
2016-07-31 17:06:01 -05:00
|
|
|
}
|
2016-07-31 18:32:37 -05:00
|
|
|
|
2016-08-04 09:31:11 -05:00
|
|
|
if (_records.empty() ||
|
|
|
|
_records[0].Dir != TraceFileRecord::Direction::Event ||
|
|
|
|
_records[0].Payload.find("NewSession") != 0)
|
2016-08-01 20:53:09 -05:00
|
|
|
{
|
2016-08-04 09:31:11 -05:00
|
|
|
fprintf(stderr, "Invalid trace file with %ld records. First record: %s\n", _records.size(),
|
|
|
|
_records.empty() ? "<empty>" : _records[0].Payload.c_str());
|
|
|
|
throw std::runtime_error("Invalid trace file.");
|
2016-08-01 20:53:09 -05:00
|
|
|
}
|
2016-08-04 11:17:22 -05:00
|
|
|
|
|
|
|
_indexIn = advance(-1, TraceFileRecord::Direction::Incoming);
|
|
|
|
_indexOut = advance(-1, TraceFileRecord::Direction::Outgoing);
|
|
|
|
|
|
|
|
_epochStart = _records[0].TimestampNs;
|
2017-02-09 23:54:52 -06:00
|
|
|
_epochEnd = _records[_records.size() - 1].TimestampNs;
|
2016-07-31 17:06:01 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 06:36:54 -06:00
|
|
|
static bool extractRecord(const std::string& s, TraceFileRecord& rec)
|
2016-07-31 17:06:01 -05:00
|
|
|
{
|
2017-02-06 06:36:54 -06:00
|
|
|
if (s.length() < 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
char delimiter = s[0];
|
|
|
|
rec.Dir = static_cast<TraceFileRecord::Direction>(delimiter);
|
|
|
|
|
|
|
|
size_t pos = 1;
|
|
|
|
int record = 0;
|
|
|
|
for (; record < 4 && pos < s.length(); ++record)
|
2016-07-31 17:06:01 -05:00
|
|
|
{
|
2017-02-06 06:36:54 -06:00
|
|
|
size_t next = s.find(delimiter, pos);
|
|
|
|
|
|
|
|
switch (record)
|
2016-07-31 17:06:01 -05:00
|
|
|
{
|
2017-02-06 06:36:54 -06:00
|
|
|
case 0:
|
|
|
|
rec.TimestampNs = std::atoi(s.substr(pos, next - pos).c_str());
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
rec.Pid = std::atoi(s.substr(pos, next - pos).c_str());
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rec.SessionId = s.substr(pos, next - pos);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
rec.Payload = s.substr(pos);
|
|
|
|
return true;
|
2016-07-31 17:06:01 -05:00
|
|
|
}
|
2017-02-06 06:36:54 -06:00
|
|
|
|
|
|
|
if (next == std::string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pos = next + 1;
|
2016-07-31 17:06:01 -05:00
|
|
|
}
|
|
|
|
|
2017-02-06 06:36:54 -06:00
|
|
|
return false;
|
2016-07-31 17:06:01 -05:00
|
|
|
}
|
|
|
|
|
2016-07-31 18:32:37 -05:00
|
|
|
unsigned advance(unsigned index, const TraceFileRecord::Direction dir)
|
|
|
|
{
|
|
|
|
while (++index < _records.size())
|
|
|
|
{
|
|
|
|
if (_records[index].Dir == dir)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2016-07-31 13:17:52 -05:00
|
|
|
private:
|
2016-08-17 18:41:58 -05:00
|
|
|
const bool _compressed;
|
2016-08-04 11:17:22 -05:00
|
|
|
Poco::Int64 _epochStart;
|
2017-02-09 23:54:52 -06:00
|
|
|
Poco::Int64 _epochEnd;
|
2016-07-31 17:06:01 -05:00
|
|
|
std::ifstream _stream;
|
2016-08-17 18:41:58 -05:00
|
|
|
Poco::InflatingInputStream _inflater;
|
2016-07-31 17:06:01 -05:00
|
|
|
std::vector<TraceFileRecord> _records;
|
2016-08-04 09:31:11 -05:00
|
|
|
unsigned _index;
|
2016-07-31 18:32:37 -05:00
|
|
|
unsigned _indexIn;
|
|
|
|
unsigned _indexOut;
|
2016-07-31 13:17:52 -05:00
|
|
|
};
|
|
|
|
|
2017-02-07 10:16:59 -06:00
|
|
|
#endif
|
|
|
|
|
2016-07-30 21:22:28 -05:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|