#include <iostream>
#include <string>
#include <fstream>
#include <sys/utsname.h>
std::string getTimestamp(const std::string& line)
{
auto pos = line.rfind(' ');
return line.substr(pos + 1, line.size() - pos);
}
int main(int argc, char* argv[])
{
if(argc < 3)
{
std::cerr << "usage: file timestamp timestamp\n/var/log/syslog 11:00:00 12:00:00\n";
exit(EXIT_FAILURE);
}
struct utsname uts;
if(uname(&uts) == -1)
{
exit(EXIT_FAILURE);
}
if(std::ifstream flog(argv[1]); flog)
{
std::string start{argv[2]};
std::string stop {argv[3]};
std::string line;
while(getline(flog, line))
{
std::string header = getTimestamp(line.substr(0, line.find(uts.nodename) - 1));
if(header >= start && header <= stop)
{
std::cout << line << "\n";
}
}
}
else
{
std::cerr << "Cannot open file: " << argv[1];
}
}