This commit is contained in:
gabime
2014-12-21 02:42:37 +02:00
parent 58308df33c
commit d163b8c45a
38 changed files with 2999 additions and 2999 deletions

View File

@@ -45,93 +45,93 @@ namespace details
class file_helper
{
public:
const int open_tries = 5;
const int open_interval = 10;
const int open_tries = 5;
const int open_interval = 10;
explicit file_helper(bool auto_flush):
_fd(nullptr),
_auto_flush(auto_flush)
{}
explicit file_helper(bool auto_flush):
_fd(nullptr),
_auto_flush(auto_flush)
{}
file_helper(const file_helper&) = delete;
file_helper& operator=(const file_helper&) = delete;
file_helper(const file_helper&) = delete;
file_helper& operator=(const file_helper&) = delete;
~file_helper()
{
close();
}
~file_helper()
{
close();
}
void open(const std::string& fname, bool truncate=false)
{
void open(const std::string& fname, bool truncate=false)
{
close();
close();
const char* mode = truncate ? "wb" : "ab";
_filename = fname;
for (int tries = 0; tries < open_tries; ++tries)
{
if(!os::fopen_s(&_fd, fname, mode))
return;
_filename = fname;
for (int tries = 0; tries < open_tries; ++tries)
{
if(!os::fopen_s(&_fd, fname, mode))
return;
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
}
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
}
throw spdlog_ex("Failed opening file " + fname + " for writing");
}
throw spdlog_ex("Failed opening file " + fname + " for writing");
}
void reopen(bool truncate)
{
if(_filename.empty())
throw spdlog_ex("Failed re opening file - was not opened before");
open(_filename, truncate);
void reopen(bool truncate)
{
if(_filename.empty())
throw spdlog_ex("Failed re opening file - was not opened before");
open(_filename, truncate);
}
}
void close()
{
if (_fd)
{
std::fclose(_fd);
_fd = nullptr;
}
}
void close()
{
if (_fd)
{
std::fclose(_fd);
_fd = nullptr;
}
}
void write(const log_msg& msg)
{
void write(const log_msg& msg)
{
size_t size = msg.formatted.size();
auto data = msg.formatted.data();
if(std::fwrite(data, 1, size, _fd) != size)
throw spdlog_ex("Failed writing to file " + _filename);
size_t size = msg.formatted.size();
auto data = msg.formatted.data();
if(std::fwrite(data, 1, size, _fd) != size)
throw spdlog_ex("Failed writing to file " + _filename);
if(_auto_flush)
std::fflush(_fd);
if(_auto_flush)
std::fflush(_fd);
}
}
const std::string& filename() const
{
return _filename;
}
const std::string& filename() const
{
return _filename;
}
static bool file_exists(const std::string& name)
{
FILE* file;
if (!os::fopen_s(&file, name.c_str(), "r"))
{
fclose(file);
return true;
}
else
{
return false;
}
}
static bool file_exists(const std::string& name)
{
FILE* file;
if (!os::fopen_s(&file, name.c_str(), "r"))
{
fclose(file);
return true;
}
else
{
return false;
}
}
private:
FILE* _fd;
std::string _filename;
bool _auto_flush;
FILE* _fd;
std::string _filename;
bool _auto_flush;
};