2014-11-01 09:20:54 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* spdlog - an extremely fast and easy to use c++11 logging library. */
|
|
|
|
/* Copyright (c) 2014 Gabi Melman. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-10-26 07:29:50 +08:00
|
|
|
|
2014-11-01 09:20:54 +08:00
|
|
|
#pragma once
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
// Helper class for file sink
|
|
|
|
// When failing to open a file, retry several times(5) with small delay between the tries(10 ms)
|
|
|
|
// Flush to file every X writes (or never if X==0)
|
2014-11-01 09:20:54 +08:00
|
|
|
// Throw spdlog_ex exception on errors
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
2014-10-31 22:34:48 +08:00
|
|
|
#include "os.h"
|
|
|
|
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-31 07:13:27 +08:00
|
|
|
namespace spdlog
|
2014-10-26 07:29:50 +08:00
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
|
|
|
|
class file_helper
|
|
|
|
{
|
|
|
|
public:
|
2014-10-31 22:34:48 +08:00
|
|
|
const int open_tries = 5;
|
|
|
|
const int open_interval = 10;
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
explicit file_helper(const std::size_t flush_inverval):
|
|
|
|
_fd(nullptr),
|
|
|
|
_flush_inverval(flush_inverval),
|
|
|
|
_flush_countdown(flush_inverval) {};
|
|
|
|
|
|
|
|
file_helper(const file_helper&) = delete;
|
2014-11-05 07:47:28 +08:00
|
|
|
file_helper& operator=(const file_helper&) = delete;
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
~file_helper()
|
|
|
|
{
|
2014-10-31 07:13:27 +08:00
|
|
|
close();
|
2014-10-26 07:29:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-31 22:34:48 +08:00
|
|
|
void open(const std::string& fname)
|
2014-10-26 07:29:50 +08:00
|
|
|
{
|
|
|
|
|
2014-10-31 07:13:27 +08:00
|
|
|
close();
|
2014-10-26 07:29:50 +08:00
|
|
|
|
2014-10-31 22:34:48 +08:00
|
|
|
_filename = fname;
|
|
|
|
for (int tries = 0; tries < open_tries; ++tries)
|
2014-10-26 07:29:50 +08:00
|
|
|
{
|
2014-10-31 22:34:48 +08:00
|
|
|
if(!os::fopen_s(&_fd, fname, "wb"))
|
2014-10-26 07:29:50 +08:00
|
|
|
return;
|
|
|
|
|
2014-10-31 22:34:48 +08:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(open_interval));
|
2014-10-26 07:29:50 +08:00
|
|
|
}
|
|
|
|
|
2014-10-31 22:34:48 +08:00
|
|
|
throw spdlog_ex("Failed opening file " + fname + " for writing");
|
2014-10-26 07:29:50 +08:00
|
|
|
}
|
|
|
|
|
2014-11-16 00:42:53 +08:00
|
|
|
void reopen()
|
|
|
|
{
|
2014-11-22 16:29:06 +08:00
|
|
|
if(_filename.empty())
|
|
|
|
throw spdlog_ex("Failed re opening file - was not opened before");
|
|
|
|
open(_filename);
|
|
|
|
|
2014-11-16 00:42:53 +08:00
|
|
|
}
|
|
|
|
|
2014-10-26 07:29:50 +08:00
|
|
|
void close()
|
|
|
|
{
|
2014-10-31 07:13:27 +08:00
|
|
|
if (_fd)
|
|
|
|
{
|
|
|
|
std::fclose(_fd);
|
|
|
|
_fd = nullptr;
|
|
|
|
}
|
2014-10-26 07:29:50 +08:00
|
|
|
}
|
|
|
|
|
2014-11-21 06:21:50 +08:00
|
|
|
void write(const log_msg& msg)
|
2014-10-26 07:29:50 +08:00
|
|
|
{
|
2014-11-21 06:21:50 +08:00
|
|
|
auto& buf = msg.formatted.buf();
|
|
|
|
size_t size = buf.size();
|
|
|
|
if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size)
|
2014-10-31 09:17:40 +08:00
|
|
|
throw spdlog_ex("Failed writing to file " + _filename);
|
2014-10-26 07:29:50 +08:00
|
|
|
|
|
|
|
if(--_flush_countdown == 0)
|
|
|
|
{
|
|
|
|
std::fflush(_fd);
|
|
|
|
_flush_countdown = _flush_inverval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 06:11:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 07:29:50 +08:00
|
|
|
private:
|
|
|
|
FILE* _fd;
|
|
|
|
std::string _filename;
|
|
|
|
const std::size_t _flush_inverval;
|
|
|
|
std::size_t _flush_countdown;
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|