Commit 5bbcd614 authored by Bryan Berthou's avatar Bryan Berthou
Browse files

refs#16

In ElementaryUtils (trunk) :
- Refactor FileUtils : add open file mode.
parent 8c58af81
......@@ -28,17 +28,28 @@ public:
static bool isReadable(const std::string & filePath);
/**
* By default open a file in write mode.
* If the file exists its content is deleted.
*
* @param fileOutputStream
* @param filePath
* @param openMode : by default open in write mode and content is deleted.z
* @return
*/
static bool open(std::ofstream &fileOutputStream,
const std::string & filePath);
static void writef(std::ofstream & fileOutputStream,
const std::string & filePath,
const std::ios_base::openmode &openMode = std::ios_base::out
| std::ios_base::trunc);
static void writeAndFlush(std::ofstream & fileOutputStream,
const std::string & str);
static void write(std::ofstream& fileOutputStream, const std::string& str);
static void flush(std::ofstream& fileOutputStream);
static void close(std::ofstream & fileOutputStream);
static void write(const std::string & filePath, const std::string & str);
static void writeLine(const std::string & filePath, const std::string & str);
// static void write(const std::string & filePath, const std::string & str);
// static void writeLine(const std::string & filePath, const std::string & str);
static std::string read(const std::string & filePath);
static std::vector<std::string> readByLine(const std::string & filePath);
static std::ifstream::pos_type getFileSize(const std::string & filetPath);
......
......@@ -13,21 +13,21 @@ bool FileUtils::isReadable(const std::string & filePath) {
return !file.fail();
}
void FileUtils::write(const std::string & filePath, const std::string & str) {
std::ofstream file(filePath.c_str(), std::ios_base::app);
if (file) {
file << str;
}
}
void FileUtils::writeLine(const std::string & filePath, const std::string & str) {
std::ofstream file(filePath.c_str(), std::ios_base::app);
if (file) {
file << str << '\n';
}
}
//void FileUtils::write(const std::string & filePath, const std::string & str) {
// std::ofstream file(filePath.c_str(), std::ios_base::app);
//
// if (file) {
// file << str;
// }
//}
//
//void FileUtils::writeLine(const std::string & filePath, const std::string & str) {
// std::ofstream file(filePath.c_str(), std::ios_base::app);
//
// if (file) {
// file << str << '\n';
// }
//}
std::string FileUtils::read(const std::string & filePath) {
std::string str = StringUtils::EMPTY;
......@@ -79,9 +79,8 @@ std::ifstream::pos_type FileUtils::getFileSize(const std::string & filetPath) {
}
bool FileUtils::open(std::ofstream &fileOutputStream,
const std::string & filePath) {
fileOutputStream.open(filePath.c_str(),
std::ofstream::out | std::ofstream::app);
const std::string & filePath, const std::ios_base::openmode &openMode) {
fileOutputStream.open(filePath.c_str(), openMode);
return fileOutputStream.is_open();
}
......@@ -89,7 +88,7 @@ void FileUtils::write(std::ofstream& fileOutputStream, const std::string& str) {
fileOutputStream << str;
}
void FileUtils::writef(std::ofstream& fileOutputStream,
void FileUtils::writeAndFlush(std::ofstream& fileOutputStream,
const std::string& str) {
fileOutputStream << str;
FileUtils::flush(fileOutputStream);
......
......@@ -40,8 +40,6 @@ LoggerManager::~LoggerManager() {
// flush remaining buffer
flushBuffer();
std::cerr << "(LoggerManager::flushBuffer()) Buffer flushed" << std::endl;
//TODO close file when exception throw
FileUtils::close(m_fileOutputStream);
......@@ -250,7 +248,7 @@ void LoggerManager::writeConsole() {
}
void LoggerManager::writeFile() {
FileUtils::writef(m_fileOutputStream, m_buffer);
FileUtils::writeAndFlush(m_fileOutputStream, m_buffer);
}
void LoggerManager::run() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment