Esempio n. 1
0
DataLog::DataLog(std::string filename_)
{
    filename = filename_ + "_LOG.txt";

    // create the file or move and erase the last file
    FILE *tmp;
    tmp = fopen(filename.c_str(), "r");
    if (tmp != NULL)
    {
        std::string filePath = "logs/" + currDateTime() + filename;
        rename(filename.c_str(), filePath.c_str());
    }
    fclose(tmp);

    FILE *file;
    file = fopen(filename.c_str(), "w");
    fclose(file);
}
SimpleFileLogger::SimpleFileLogger(const std::string& fileNamePrefix, const std::string& logPath) :
_fileNamePrefix(fileNamePrefix),
_logPath(logPath)
{
    if(_logPath.empty()){
        _logPath = ".";
    }
    if( *_logPath.rbegin() != '/'){
        _logPath += "/";
    }
    boost::posix_time::ptime currDateTime(boost::posix_time::microsec_clock::local_time());
    std::stringstream fullFileName;
    fullFileName << _logPath << _fileNamePrefix << "_" << currDateTime << ".log" ;
    _absFileName = fullFileName.str();
    _outputFileStream.open(_absFileName.c_str(), std::ofstream::out);
    if(!_outputFileStream.good()){
        throw std::runtime_error("SimpleFileLogger::SimpleFileLogger() - Failed to open file for writing :: " + _absFileName);
    }
}