Skip to content

Instantly share code, notes, and snippets.

@dgski
Created October 28, 2019 14:10
Show Gist options
  • Save dgski/b5fd905a9953243101ac96a7ba3f757d to your computer and use it in GitHub Desktop.
Save dgski/b5fd905a9953243101ac96a7ba3f757d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <chrono>
#include <thread>
#include <sstream>
void tail(std::ifstream& f)
{
std::uint64_t lineCount = 0;
auto sleepTime = std::chrono::seconds(0);
while(true)
{
std::stringstream line;
while(true)
{
std::this_thread::sleep_for(sleepTime);
char c;
f.clear();
f.get(c);
if(f.eof())
{
sleepTime = std::chrono::seconds(1);
continue;
}
sleepTime = std::chrono::seconds(0);
if(c == '\n')
{
std::cout << lineCount++ << ':' << line.str() << std::endl;
break;
}
else
{
line << c;
}
}
}
}
int main(int argc, char** argv)
{
if(argc != 2)
{
std::cout << "usage: dail <filename>" << std::endl;
return -1;
}
std::ifstream f(argv[1]);
if(!f.is_open())
{
std::cout << "File could not be opened" << std::endl;
return -2;
}
tail(f);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment