X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Futil.cpp;h=c16ba1c6c8d541164fc5ff7fd540aadb74d1e7fa;hb=ea0b37d1586c82d6be3f8e3951f8d6a6bfef4cec;hp=dc03e9dfb133c60e15a830711214fb5015c1fb15;hpb=e8f8107bdd0d1149117f06e10b145ef00d5543fb;p=cassiopeia.git diff --git a/src/util.cpp b/src/util.cpp index dc03e9d..c16ba1c 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -181,3 +181,21 @@ std::pair parseYearInterval( std::time_t t, const std::string& dat return std::pair( false, 0 ); } } + +std::shared_ptr openLogfile( const std::string name) { + struct stat buffer; + std::string tname = name; + int ctr = 2; + while(stat (tname.c_str(), &buffer) == 0) { + tname = name + "_" + std::to_string(ctr++); + } + auto res = std::shared_ptr(new std::ofstream( tname ), + [](std::ofstream *p){ + p->close(); + delete p; + }); + if(! res->good() ){ + throw std::string("Failed to open file for logging: ") + name; + } + return res; +}