std::ifstream does not take std::string_view as param
| 27 | constexpr const char* NETDEV_FILE = |
| 28 | "/proc/net/dev"; // std::ifstream does not take std::string_view as param |
| 29 | std::optional<std::pair<unsigned long long, unsigned long long>> |
| 30 | waybar::modules::Network::readBandwidthUsage() { |
| 31 | std::ifstream netdev(NETDEV_FILE); |
| 32 | if (!netdev) { |
| 33 | spdlog::warn("Failed to open netdev file {}", NETDEV_FILE); |
| 34 | return {}; |
| 35 | } |
| 36 | |
| 37 | std::string line; |
| 38 | // skip the headers (first two lines) |
| 39 | std::getline(netdev, line); |
| 40 | std::getline(netdev, line); |
| 41 | |
| 42 | unsigned long long receivedBytes = 0ull; |
| 43 | unsigned long long transmittedBytes = 0ull; |
| 44 | while (std::getline(netdev, line)) { |
| 45 | std::istringstream iss(line); |
| 46 | |
| 47 | std::string ifacename; |
| 48 | iss >> ifacename; // ifacename contains "eth0:" |
| 49 | ifacename.pop_back(); // remove trailing ':' |
| 50 | if (ifacename != ifname_) { |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | // The rest of the line consists of whitespace separated counts divided |
| 55 | // into two groups (receive and transmit). Each group has the following |
| 56 | // columns: bytes, packets, errs, drop, fifo, frame, compressed, multicast |
| 57 | // |
| 58 | // We only care about the bytes count, so we'll just ignore the 7 other |
| 59 | // columns. |
| 60 | unsigned long long r = 0ull; |
| 61 | unsigned long long t = 0ull; |
| 62 | // Read received bytes |
| 63 | iss >> r; |
| 64 | // Skip all the other columns in the received group |
| 65 | for (int colsToSkip = 7; colsToSkip > 0; colsToSkip--) { |
| 66 | // skip whitespace between columns |
| 67 | while (iss.peek() == ' ') { |
| 68 | iss.ignore(); |
| 69 | } |
| 70 | // skip the irrelevant column |
| 71 | while (iss.peek() != ' ') { |
| 72 | iss.ignore(); |
| 73 | } |
| 74 | } |
| 75 | // Read transmit bytes |
| 76 | iss >> t; |
| 77 | |
| 78 | receivedBytes += r; |
| 79 | transmittedBytes += t; |
| 80 | } |
| 81 | |
| 82 | return {{receivedBytes, transmittedBytes}}; |
| 83 | } |
| 84 | |
| 85 | waybar::modules::Network::Network(const std::string& id, const Json::Value& config) |
| 86 | : ALabel(config, "network", id, DEFAULT_FORMAT, 60) { |
nothing calls this directly
no outgoing calls
no test coverage detected