blob: eef8030981b3c269eb848dee42b9941ebb2b09ee [file] [log] [blame]
michaeltfcea39d2017-04-20 05:39:30 -07001/*
2 * Copyright 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020010#include "rtc_tools/network_tester/packet_logger.h"
michaeltfcea39d2017-04-20 05:39:30 -070011
Mirko Bonadeie45c6882019-02-16 09:59:29 +010012#include <string>
13
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
michaeltfcea39d2017-04-20 05:39:30 -070015
16namespace webrtc {
17
18PacketLogger::PacketLogger(const std::string& log_file_path)
19 : packet_logger_stream_(log_file_path,
20 std::ios_base::out | std::ios_base::binary) {
21 RTC_DCHECK(packet_logger_stream_.is_open());
22 RTC_DCHECK(packet_logger_stream_.good());
23}
24
25PacketLogger::~PacketLogger() = default;
26
27void PacketLogger::LogPacket(const NetworkTesterPacket& packet) {
28 // The protobuffer message will be saved in the following format to the file:
29 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30 // | 1 byte | X byte | 1 byte | ... | X byte |
31 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 // | Size of the next | proto | Size of the next | ... | proto |
33 // | proto message | message | proto message | | message |
34 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Mirko Bonadeie45c6882019-02-16 09:59:29 +010035 std::string packet_data;
michaeltfcea39d2017-04-20 05:39:30 -070036 packet.SerializeToString(&packet_data);
37 RTC_DCHECK_LE(packet_data.length(), 255);
38 RTC_DCHECK_GE(packet_data.length(), 0);
39 char proto_size = packet_data.length();
40 packet_logger_stream_.write(&proto_size, sizeof(proto_size));
41 packet_logger_stream_.write(packet_data.data(), packet_data.length());
42}
43
44} // namespace webrtc