Skip to content

Removed excessive use of std::endl from examples. #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions Examples/ArpSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

Expand All @@ -33,18 +33,16 @@ static struct option L3FwdOptions[] = {
*/
void printUsage()
{
std::cout << std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hv] -i interface_ip -c victim_ip -g gateway_ip" << std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -i interface_ip : The IPv4 address of interface to use" << std::endl
<< " -c victim_ip : The IPv4 address of the victim" << std::endl
<< " -g gateway_ip : The IPv4 address of the gateway" << std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
std::cout << "\nUsage:"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the strings like here, the performace doesn't matter actually. Personally, I prefer the original std::endl which looks clearer.

Copy link
Collaborator Author

@Dimi1010 Dimi1010 Apr 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the '\n' version significantly cleaner to read and visualize how the string will be formatted on the console. Especially for places where clang-format places the << std::endl on the next line.

"\n------\n"
<< pcpp::AppName::get()
<< " [-hv] -i interface_ip -c victim_ip -g gateway_ip\n"
"\nOptions:\n"
"\n -i interface_ip : The IPv4 address of interface to use"
"\n -c victim_ip : The IPv4 address of the victim"
"\n -g gateway_ip : The IPv4 address of the gateway"
"\n -h : Displays this help message and exits"
"\n -v : Displays the current version and exists"
<< std::endl;
}

Expand All @@ -53,9 +51,8 @@ void printUsage()
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull()
<< "\nBuilt: " << pcpp::getBuildDateTime() << "\nBuilt from: " << pcpp::getGitInfo() << std::endl;
exit(0);
}

Expand Down Expand Up @@ -151,7 +148,7 @@ void doArpSpoofing(pcpp::PcapLiveDevice* pDevice, const pcpp::IPv4Address& gatew
victimArpReply.computeCalculateFields();

// Send ARP replies to gateway and to victim every 5 seconds
std::cout << "Sending ARP replies to victim and to gateway every 5 seconds..." << std::endl << std::endl;
std::cout << "Sending ARP replies to victim and to gateway every 5 seconds...\n" << std::endl;
while (1)
{
pDevice->sendPacket(&gwArpReply);
Expand Down
43 changes: 19 additions & 24 deletions Examples/Arping/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

Expand All @@ -47,24 +47,20 @@ static struct option ArpingOptions[] = {
void printUsage()
{
std::cout
<< std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hvl] [-c count] [-w timeout] [-s mac_addr] [-S ip_addr] -i interface -T ip_addr"
<< std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< " -l : Print the list of interfaces and exists" << std::endl
<< " -c count : Send 'count' requests" << std::endl
<< " -i interface : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 address"
<< std::endl
<< " -s mac_addr : Set source MAC address" << std::endl
<< " -S ip_addr : Set source IP address" << std::endl
<< " -T ip_addr : Set target IP address" << std::endl
<< " -w timeout : How long to wait for a reply (in seconds)" << std::endl
<< "\nUsage:"
<< "\n------\n"
<< pcpp::AppName::get()
<< " [-hvl] [-c count] [-w timeout] [-s mac_addr] [-S ip_addr] -i interface -T ip_addr\n"
"\nOptions:\n"
"\n -h : Displays this help message and exits"
"\n -v : Displays the current version and exists"
"\n -l : Print the list of interfaces and exists"
"\n -c count : Send 'count' requests"
"\n -i interface : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 address"
"\n -s mac_addr : Set source MAC address"
"\n -S ip_addr : Set source IP address"
"\n -T ip_addr : Set target IP address"
"\n -w timeout : How long to wait for a reply (in seconds)"
<< std::endl;
}

Expand All @@ -73,9 +69,8 @@ void printUsage()
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull()
<< "\nBuilt: " << pcpp::getBuildDateTime() << "\nBuilt from: " << pcpp::getGitInfo() << std::endl;
exit(0);
}

Expand All @@ -87,11 +82,11 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList =
pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
std::cout << "\nNetwork interfaces:\n";
for (const auto& dev : devList)
{
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString()
<< std::endl;
<< "\n";
}
exit(0);
}
Expand Down
59 changes: 24 additions & 35 deletions Examples/DNSResolver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

Expand All @@ -32,41 +32,31 @@ static struct option DNSResolverOptions[] = {
*/
void printUsage()
{
std::cout << std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hvl] [-t timeout] [-d dns_server] [-g gateway] [-i interface] -s hostname"
<< std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< " -l : Print the list of interfaces and exists" << std::endl
<< " -s hostname : Hostname to resolve" << std::endl
<< " -i interface : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 "
"address. If not set"
<< std::endl
<< " one of the interfaces that has a default gateway will be used" << std::endl
<< " -d dns_server: IPv4 address of DNS server to send the DNS request to. If not set the DNS request "
"will be sent to the gateway"
<< std::endl
<< " -g gateway : IPv4 address of the gateway to send the DNS request to. If not set the default "
"gateway will be chosen"
<< std::endl
<< " -t timeout : How long to wait for a reply (in seconds). Default timeout is 5 seconds"
<< std::endl
<< std::endl;
std::cout
<< "\nUsage:"
"\n------\n"
<< pcpp::AppName::get()
<< " [-hvl] [-t timeout] [-d dns_server] [-g gateway] [-i interface] -s hostname\n"
"\nOptions:\n"
"\n -h : Displays this help message and exits"
"\n -v : Displays the current version and exists"
"\n -l : Print the list of interfaces and exists"
"\n -s hostname : Hostname to resolve"
"\n -i interface : Use the specified interface. Can be interface name (e.g eth0) or interface IPv4 address. If not set"
"\n one of the interfaces that has a default gateway will be used"
"\n -d dns_server: IPv4 address of DNS server to send the DNS request to. If not set the DNS request will be sent to the gateway"
"\n -g gateway : IPv4 address of the gateway to send the DNS request to. If not set the default gateway will be chosen"
"\n -t timeout : How long to wait for a reply (in seconds). Default timeout is 5 seconds"
<< std::endl;
}

/**
* Print application version
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull()
<< "\nBuilt: " << pcpp::getBuildDateTime() << "\nBuilt from: " << pcpp::getGitInfo() << std::endl;
exit(0);
}

Expand All @@ -78,11 +68,11 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList =
pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
std::cout << std::endl << "Network interfaces:\n";
for (const auto& dev : devList)
{
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString()
<< std::endl;
<< '\n';
}
exit(0);
}
Expand Down Expand Up @@ -221,17 +211,16 @@ int main(int argc, char* argv[])
hostname, dev, responseTime, dnsTTL, timeoutSec, dnsServerIP, gatewayIP);
if (resultIP == pcpp::IPv4Address::Zero)
{
std::cout << std::endl << "Could not resolve hostname [" << hostname << "]" << std::endl;
std::cout << "\nCould not resolve hostname [" << hostname << "]" << std::endl;
}
else
{
std::cout << std::endl
<< "IP address of [" << hostname << "] is: " << resultIP << " DNS-TTL=" << dnsTTL
std::cout << "\nIP address of [" << hostname << "] is: " << resultIP << " DNS-TTL=" << dnsTTL
<< " time=" << (int)responseTime << "ms" << std::endl;
}
}
catch (const std::exception&)
{
std::cout << std::endl << "Could not resolve hostname [" << hostname << "]" << std::endl;
std::cout << "\nCould not resolve hostname [" << hostname << "]" << std::endl;
}
}
62 changes: 26 additions & 36 deletions Examples/DnsSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

Expand Down Expand Up @@ -84,30 +84,23 @@ struct DnsSpoofingArgs
void printUsage()
{
std::cout
<< std::endl
<< "Usage:" << std::endl
<< "------" << std::endl
<< pcpp::AppName::get() << " [-hvl] [-o host1,host2,...,host_n] [-c ip_address] -i interface -d ip_address"
<< std::endl
<< std::endl
<< "Options:" << std::endl
<< std::endl
<< " -h : Displays this help message and exits" << std::endl
<< " -v : Displays the current version and exists" << std::endl
<< " -l : Print the list of available interfaces" << std::endl
<< " -i interface : The interface name or interface IP address to use." << std::endl
<< " Use the -l switch to see all interfaces" << std::endl
<< " -d ip_address : The IP address of the spoofed DNS server. Supports both IPv4 and IPv6"
<< std::endl
<< " (all responses will be sent with this IP address)" << std::endl
<< " -c ip_address : Spoof only DNS requests coming from a specific IP address" << std::endl
<< " -o host1,host2,...,host_n : A comma-separated list of hosts to spoof. If list is not given,"
<< std::endl
<< " all hosts will be spoofed. If an host contains '*' all sub-domains"
<< std::endl
<< " will be spoofed, for example: if '*.google.com' is given" << std::endl
<< " then 'mail.google.com', 'tools.google.com', etc. will be spoofed"
<< std::endl
<< "\nUsage:"
"\n------\n"
<< pcpp::AppName::get()
<< " [-hvl] [-o host1,host2,...,host_n] [-c ip_address] -i interface -d ip_address\n"
"\nOptions:\n"
"\n -h : Displays this help message and exits"
"\n -v : Displays the current version and exists"
"\n -l : Print the list of available interfaces"
"\n -i interface : The interface name or interface IP address to use."
"\n Use the -l switch to see all interfaces"
"\n -d ip_address : The IP address of the spoofed DNS server. Supports both IPv4 and IPv6"
"\n (all responses will be sent with this IP address)"
"\n -c ip_address : Spoof only DNS requests coming from a specific IP address"
"\n -o host1,host2,...,host_n : A comma-separated list of hosts to spoof. If list is not given,"
"\n all hosts will be spoofed. If an host contains '*' all sub-domains"
"\n will be spoofed, for example: if '*.google.com' is given"
"\n then 'mail.google.com', 'tools.google.com', etc. will be spoofed"
<< std::endl;
}

Expand All @@ -116,9 +109,8 @@ void printUsage()
*/
void printAppVersion()
{
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull() << std::endl
<< "Built: " << pcpp::getBuildDateTime() << std::endl
<< "Built from: " << pcpp::getGitInfo() << std::endl;
std::cout << pcpp::AppName::get() << " " << pcpp::getPcapPlusPlusVersionFull()
<< "\nBuilt: " << pcpp::getBuildDateTime() << "\nBuilt from: " << pcpp::getGitInfo() << std::endl;
exit(0);
}

Expand All @@ -130,7 +122,7 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList =
pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
std::cout << "\nNetwork interfaces:\n";
for (const auto& dev : devList)
{
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString()
Expand Down Expand Up @@ -266,16 +258,14 @@ void onApplicationInterrupted(void* cookie)
DnsSpoofingArgs* args = (DnsSpoofingArgs*)cookie;
if (args->stats.spoofedHosts.size() == 0)
{
std::cout << std::endl << "Application closing. No hosts were spoofed." << std::endl;
std::cout << "\nApplication closing. No hosts were spoofed." << std::endl;
}
else
{
std::cout << std::endl
<< "Summary of spoofed hosts:" << std::endl
<< "-------------------------" << std::endl
<< std::endl
<< "Total spoofed: " << args->stats.numOfSpoofedDnsRequests << std::endl
<< "Number of host spoofed: " << args->stats.spoofedHosts.size() << std::endl
std::cout << "\nSummary of spoofed hosts:"
<< "\n-------------------------\n"
<< "\nTotal spoofed: " << args->stats.numOfSpoofedDnsRequests
<< "\nNumber of host spoofed: " << args->stats.spoofedHosts.size() << '\n'
<< std::endl;

// create a table
Expand Down
4 changes: 2 additions & 2 deletions Examples/DpdkBridge/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
#define EXIT_WITH_ERROR(reason) \
do \
{ \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

#define EXIT_WITH_ERROR_AND_PRINT_USAGE(reason) \
do \
{ \
printUsage(); \
std::cout << std::endl << "ERROR: " << reason << std::endl << std::endl; \
std::cout << "\nERROR: " << reason << "\n\n" << std::flush; \
exit(1); \
} while (0)

Expand Down
Loading
Loading