Created
October 28, 2019 14:10
-
-
Save dgski/b5fd905a9953243101ac96a7ba3f757d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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