Handle file extensions in rotating and daily loggers

This commit is contained in:
gabime
2017-12-01 03:40:49 +02:00
parent 60ce47a814
commit 3c4a2bf531
8 changed files with 200 additions and 37 deletions

View File

@@ -16,6 +16,7 @@
#include <cstdio>
#include <string>
#include <thread>
#include <tuple>
#include <cerrno>
namespace spdlog
@@ -84,14 +85,13 @@ public:
void write(const log_msg& msg)
{
size_t msg_size = msg.formatted.size();
auto data = msg.formatted.data();
if (std::fwrite(data, 1, msg_size, _fd) != msg_size)
throw spdlog_ex("Failed writing to file " + os::filename_to_str(_filename), errno);
}
size_t size()
size_t size() const
{
if (!_fd)
throw spdlog_ex("Cannot use size() on closed file " + os::filename_to_str(_filename));
@@ -103,12 +103,32 @@ public:
return _filename;
}
static bool file_exists(const filename_t& name)
static bool file_exists(const filename_t& fname)
{
return os::file_exists(name);
return os::file_exists(fname);
}
//
// return basename and extension:
//
// "mylog.txt" => ("mylog", ".txt")
// "mylog" => ("mylog", "")
//
// the starting dot in filenames is ignored (hidden files):
//
// "my_folder/.mylog" => ("my_folder/.mylog")
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
static std::tuple<filename_t, filename_t> split_by_extenstion(const filename_t& fname)
{
auto index = fname.rfind('.');
bool found_ext = index != filename_t::npos && index !=0 && fname[index - 1] != details::os::folder_sep;
if (found_ext)
return std::make_tuple(fname.substr(0, index), fname.substr(index));
else
return std::make_tuple(fname, filename_t());
}
private:
FILE* _fd;
filename_t _filename;

View File

@@ -143,6 +143,16 @@ inline bool operator!=(const std::tm& tm1, const std::tm& tm2)
SPDLOG_CONSTEXPR static const char* eol = SPDLOG_EOL;
SPDLOG_CONSTEXPR static int eol_size = sizeof(SPDLOG_EOL) - 1;
// folder separator
#ifdef _WIN32
SPDLOG_CONSTEXPR static const char folder_sep = '\\';
#else
SPDLOG_CONSTEXPR static const char folder_sep = '/';
#endif
inline void prevent_child_fd(FILE *f)
{
#ifdef _WIN32