Skip to content

Commit f68a2fa

Browse files
bigzachattackdtrugman
authored andcommitted
Minor improvements and cleaned up styling to keep code consistent
1 parent b4a6aed commit f68a2fa

File tree

6 files changed

+46
-44
lines changed

6 files changed

+46
-44
lines changed

include/pfs/net.hpp

-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class net final
6363
net(const std::string& procfs_root);
6464

6565
private:
66-
std::vector<net_device> get_net_devices(const std::string& file) const;
6766
std::vector<net_socket> get_net_sockets(const std::string& file) const;
6867

6968
static std::string build_net_root(const std::string& procfs_root);

include/pfs/types.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -456,18 +456,18 @@ struct ip
456456

457457
struct net_device
458458
{
459-
std::string name;
459+
std::string interface;
460460
uint64_t rx_bytes;
461461
uint64_t rx_packets;
462-
uint64_t rx_errors;
462+
uint64_t rx_errs;
463463
uint64_t rx_drop;
464464
uint64_t rx_fifo;
465465
uint64_t rx_frame;
466466
uint64_t rx_compressed;
467467
uint64_t rx_multicast;
468468
uint64_t tx_bytes;
469469
uint64_t tx_packets;
470-
uint64_t tx_errors;
470+
uint64_t tx_errs;
471471
uint64_t tx_drop;
472472
uint64_t tx_fifo;
473473
uint64_t tx_colls;

sample/format.hpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,26 @@ inline std::ostream& operator<<(std::ostream& out,
153153
inline std::ostream& operator<<(std::ostream& out,
154154
const pfs::net_device& device)
155155
{
156-
out << "name[" << device.name << "] ";
156+
out << "interface[" << device.interface << "] ";
157+
157158
out << "rx_bytes[" << device.rx_bytes << "] ";
158159
out << "rx_packets[" << device.rx_packets << "] ";
159-
out << "rx_errors[" << device.rx_errors << "] ";
160+
out << "rx_errs[" << device.rx_errs << "] ";
160161
out << "rx_drop[" << device.rx_drop << "] ";
161162
out << "rx_fifo[" << device.rx_fifo << "] ";
162163
out << "rx_frame[" << device.rx_frame << "] ";
163164
out << "rx_compressed[" << device.rx_compressed << "] ";
164165
out << "rx_multicast[" << device.rx_multicast << "] ";
166+
165167
out << "tx_bytes[" << device.tx_bytes << "] ";
166168
out << "tx_packets[" << device.tx_packets << "] ";
167-
out << "tx_errors[" << device.tx_errors << "] ";
169+
out << "tx_errs[" << device.tx_errs << "] ";
168170
out << "tx_drop[" << device.tx_drop << "] ";
169171
out << "tx_fifo[" << device.tx_fifo << "] ";
170172
out << "tx_colls[" << device.tx_colls << "] ";
171173
out << "tx_carrier[" << device.tx_carrier << "] ";
172174
out << "tx_compressed[" << device.tx_compressed << "] ";
175+
173176
return out;
174177
}
175178

src/net.cpp

+8-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@ std::string net::build_net_root(const std::string& procfs_root)
3434
std::vector<net_device> net::get_dev() const
3535
{
3636
static const std::string DEV_FILE("dev");
37-
return get_net_devices(DEV_FILE);
37+
auto path = _net_root + DEV_FILE;
38+
39+
static const size_t HEADER_LINES = 2;
40+
41+
std::vector<net_device> output;
42+
parsers::parse_lines(path, std::back_inserter(output),
43+
parsers::parse_net_device_line, HEADER_LINES);
44+
return output;
3845
}
3946

4047
std::vector<net_socket> net::get_icmp() const
@@ -135,14 +142,4 @@ std::vector<net_socket> net::get_net_sockets(const std::string& file) const
135142
return output;
136143
}
137144

138-
std::vector<net_device> net::get_net_devices(const std::string& file) const
139-
{
140-
auto path = _net_root + file;
141-
142-
static const size_t HEADER_LINES = 2;
143-
std::vector<net_device> output;
144-
parsers::parse_lines(path, std::back_inserter(output), parsers::parse_net_device_line, HEADER_LINES);
145-
return output;
146-
}
147-
148145
} // namespace pfs

src/parsers/net_device.cpp

+16-13
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,26 @@ net_device parse_net_device_line(const std::string& line)
2525
{
2626
// Example:
2727
// clang-format off
28-
// Inter-| Receive | Transmit
29-
// face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
30-
// lo: 27267010 48045 0 0 0 0 0 0 27267010 48045 0 0 0 0 0 0
31-
// eth0: 335754274 58179 0 0 0 0 0 0 9805218 48519 0 0 0 0 0 0
28+
// Inter-| Receive |Transmit
29+
// face | bytes packets errs drop fifo frame compressed multicast| bytes packets errs drop fifo colls carrier compressed
30+
// lo: 93144 1366 0 0 0 0 0 0 93144 1366 0 0 0 0 0 0
31+
// eth0: 2893258 9219 0 0 0 0 0 556 1029533 7276 0 0 0 0 0 0
3232
// clang-format on
3333

34-
enum token {
35-
NAME = 0,
34+
enum token
35+
{
36+
INTERFACE = 0,
3637
RX_BYTES = 1,
3738
RX_PACKETS = 2,
38-
RX_ERRORS = 3,
39+
RX_ERRS = 3,
3940
RX_DROP = 4,
4041
RX_FIFO = 5,
4142
RX_FRAME = 6,
4243
RX_COMPRESSED = 7,
4344
RX_MULTICAST = 8,
4445
TX_BYTES = 9,
4546
TX_PACKETS = 10,
46-
TX_ERRORS = 11,
47+
TX_ERRS = 11,
4748
TX_DROP = 12,
4849
TX_FIFO = 13,
4950
TX_COLLS = 14,
@@ -59,23 +60,25 @@ net_device parse_net_device_line(const std::string& line)
5960
line);
6061
}
6162

62-
try {
63+
try
64+
{
6365
net_device dev;
6466

65-
dev.name = tokens[NAME];
66-
dev.name.pop_back(); // Remove ':';
67+
dev.interface = tokens[INTERFACE];
68+
dev.interface.pop_back(); // Remove ':';
6769

6870
utils::stot(tokens[RX_BYTES], dev.rx_bytes, utils::base::decimal);
6971
utils::stot(tokens[RX_PACKETS], dev.rx_packets, utils::base::decimal);
70-
utils::stot(tokens[RX_ERRORS], dev.rx_errors, utils::base::decimal);
72+
utils::stot(tokens[RX_ERRS], dev.rx_errs, utils::base::decimal);
7173
utils::stot(tokens[RX_DROP], dev.rx_drop, utils::base::decimal);
7274
utils::stot(tokens[RX_FIFO], dev.rx_fifo, utils::base::decimal);
7375
utils::stot(tokens[RX_FRAME], dev.rx_frame, utils::base::decimal);
7476
utils::stot(tokens[RX_COMPRESSED], dev.rx_compressed, utils::base::decimal);
7577
utils::stot(tokens[RX_MULTICAST], dev.rx_multicast, utils::base::decimal);
78+
7679
utils::stot(tokens[TX_BYTES], dev.tx_bytes, utils::base::decimal);
7780
utils::stot(tokens[TX_PACKETS], dev.tx_packets, utils::base::decimal);
78-
utils::stot(tokens[TX_ERRORS], dev.tx_errors, utils::base::decimal);
81+
utils::stot(tokens[TX_ERRS], dev.tx_errs, utils::base::decimal);
7982
utils::stot(tokens[TX_DROP], dev.tx_drop, utils::base::decimal);
8083
utils::stot(tokens[TX_FIFO], dev.tx_fifo, utils::base::decimal);
8184
utils::stot(tokens[TX_COLLS], dev.tx_colls, utils::base::decimal);

test/net_device.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ using namespace pfs::impl::parsers;
99

1010
TEST_CASE("Parse corrupted net device", "[net][net_device]")
1111
{
12-
1312
SECTION("Missing token")
1413
{
15-
std::string line =
14+
std::string line =
1615
"lo: 27267010 48045 0 0 0 0 0 0 "
1716
"27267010 48045 0 0 0 0 0";
1817

@@ -21,36 +20,36 @@ TEST_CASE("Parse corrupted net device", "[net][net_device]")
2120

2221
SECTION("Extra token")
2322
{
24-
std::string line =
23+
std::string line =
2524
"lo: 27267010 48045 0 0 0 0 0 0 "
2625
"27267010 48045 0 0 0 0 0 0 0";
2726

2827
REQUIRE_THROWS_AS(parse_net_device_line(line), pfs::parser_error);
2928
}
30-
3129
}
3230

3331
TEST_CASE("Parse net device", "[net][net_device]")
3432
{
35-
36-
pfs::net_device expected;
33+
pfs::net_device expected;
3734

3835
std::string line =
3936
"eth0: 335754274 58179 1 2 3 4 5 "
4037
"6 9805218 48519 11 12 13 14 15 16";
4138

42-
expected.name = "eth0";
39+
expected.interface = "eth0";
40+
4341
expected.rx_bytes = 335754274;
4442
expected.rx_packets = 58179;
45-
expected.rx_errors = 1;
43+
expected.rx_errs = 1;
4644
expected.rx_drop = 2;
4745
expected.rx_fifo = 3;
4846
expected.rx_frame = 4;
4947
expected.rx_compressed = 5;
5048
expected.rx_multicast = 6;
49+
5150
expected.tx_bytes = 9805218;
5251
expected.tx_packets = 48519;
53-
expected.tx_errors = 11;
52+
expected.tx_errs = 11;
5453
expected.tx_drop = 12;
5554
expected.tx_fifo = 13;
5655
expected.tx_colls = 14;
@@ -59,21 +58,22 @@ TEST_CASE("Parse net device", "[net][net_device]")
5958

6059
auto device = parse_net_device_line(line);
6160

62-
REQUIRE(device.name == expected.name);
61+
REQUIRE(device.interface == expected.interface);
62+
6363
REQUIRE(device.rx_bytes == expected.rx_bytes);
6464
REQUIRE(device.rx_packets == expected.rx_packets);
65-
REQUIRE(device.rx_errors == expected.rx_errors);
65+
REQUIRE(device.rx_errs == expected.rx_errs);
6666
REQUIRE(device.rx_drop == expected.rx_drop);
6767
REQUIRE(device.rx_fifo == expected.rx_fifo);
6868
REQUIRE(device.rx_compressed == expected.rx_compressed);
6969
REQUIRE(device.rx_multicast == expected.rx_multicast);
70+
7071
REQUIRE(device.tx_bytes == expected.tx_bytes);
7172
REQUIRE(device.tx_packets == expected.tx_packets);
72-
REQUIRE(device.tx_errors == expected.tx_errors);
73+
REQUIRE(device.tx_errs == expected.tx_errs);
7374
REQUIRE(device.tx_drop == expected.tx_drop);
7475
REQUIRE(device.tx_fifo == expected.tx_fifo);
7576
REQUIRE(device.tx_colls == expected.tx_colls);
7677
REQUIRE(device.tx_carrier == expected.tx_carrier);
7778
REQUIRE(device.tx_compressed == expected.tx_compressed);
78-
7979
}

0 commit comments

Comments
 (0)