This commit is contained in:
gabime 2017-12-01 03:46:19 +02:00
parent 84e307521d
commit f5939f9e56
8 changed files with 112 additions and 112 deletions

View File

@ -1,4 +1,4 @@
// //
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //

View File

@ -108,26 +108,26 @@ public:
return os::file_exists(fname); return os::file_exists(fname);
} }
// //
// return basename and extension: // return basename and extension:
// //
// "mylog.txt" => ("mylog", ".txt") // "mylog.txt" => ("mylog", ".txt")
// "mylog" => ("mylog", "") // "mylog" => ("mylog", "")
// //
// the starting dot in filenames is ignored (hidden files): // the starting dot in filenames is ignored (hidden files):
// //
// "my_folder/.mylog" => ("my_folder/.mylog") // "my_folder/.mylog" => ("my_folder/.mylog")
// "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt") // "my_folder/.mylog.txt" => ("my_folder/.mylog", ".txt")
static std::tuple<filename_t, filename_t> split_by_extenstion(const filename_t& fname) static std::tuple<filename_t, filename_t> split_by_extenstion(const filename_t& fname)
{ {
auto index = fname.rfind('.'); auto index = fname.rfind('.');
bool found_ext = index != filename_t::npos && index !=0 && fname[index - 1] != details::os::folder_sep; bool found_ext = index != filename_t::npos && index !=0 && fname[index - 1] != details::os::folder_sep;
if (found_ext) if (found_ext)
return std::make_tuple(fname.substr(0, index), fname.substr(index)); return std::make_tuple(fname.substr(0, index), fname.substr(index));
else else
return std::make_tuple(fname, filename_t()); return std::make_tuple(fname, filename_t());
} }
private: private:
FILE* _fd; FILE* _fd;

View File

@ -526,7 +526,7 @@ inline void spdlog::pattern_formatter::handle_flag(char flag)
{ {
switch (flag) switch (flag)
{ {
// logger name // logger name
case 'n': case 'n':
_formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter())); _formatters.push_back(std::unique_ptr<details::flag_formatter>(new details::name_formatter()));
break; break;

View File

@ -488,7 +488,7 @@ template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class BasicPrintfArgFormatter; class BasicPrintfArgFormatter;
template <typename CharType, template <typename CharType,
typename ArgFormatter = fmt::ArgFormatter<CharType> > typename ArgFormatter = fmt::ArgFormatter<CharType> >
class BasicFormatter; class BasicFormatter;
/** /**
@ -2618,15 +2618,15 @@ inline uint64_t make_type(const T &arg)
} }
template <std::size_t N, bool/*IsPacked*/= (N < ArgList::MAX_PACKED_ARGS)> template <std::size_t N, bool/*IsPacked*/= (N < ArgList::MAX_PACKED_ARGS)>
struct ArgArray; struct ArgArray;
template <std::size_t N> template <std::size_t N>
struct ArgArray<N, true/*IsPacked*/> struct ArgArray<N, true/*IsPacked*/>
{ {
typedef Value Type[N > 0 ? N : 1]; typedef Value Type[N > 0 ? N : 1];
template <typename Formatter, typename T> template <typename Formatter, typename T>
static Value make(const T &value) static Value make(const T &value)
{ {
#ifdef __clang__ #ifdef __clang__
Value result = MakeValue<Formatter>(value); Value result = MakeValue<Formatter>(value);
@ -2638,7 +2638,7 @@ struct ArgArray<N, true/*IsPacked*/>
return MakeValue<Formatter>(value); return MakeValue<Formatter>(value);
#endif #endif
} }
}; };
template <std::size_t N> template <std::size_t N>
struct ArgArray<N, false/*IsPacked*/> struct ArgArray<N, false/*IsPacked*/>
@ -3473,7 +3473,7 @@ void BasicWriter<Char>::write_double(T value, const Spec &spec)
// MSVC's printf doesn't support 'F'. // MSVC's printf doesn't support 'F'.
type = 'f'; type = 'f';
#endif #endif
// Fall through. // Fall through.
case 'E': case 'E':
case 'G': case 'G':
case 'A': case 'A':

View File

@ -77,23 +77,23 @@ public:
_current_size = _file_helper.size(); //expensive. called only once _current_size = _file_helper.size(); //expensive. called only once
} }
// calc filename according to index and file extension if exists. // calc filename according to index and file extension if exists.
// e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt". // e.g. calc_filename("logs/mylog.txt, 3) => "logs/mylog.3.txt".
static filename_t calc_filename(const filename_t& filename, std::size_t index) static filename_t calc_filename(const filename_t& filename, std::size_t index)
{ {
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
if (index) if (index)
{ {
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename); std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
w.write(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext); w.write(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
} }
else else
{ {
w.write(SPDLOG_FILENAME_T("{}"), filename); w.write(SPDLOG_FILENAME_T("{}"), filename);
} }
return w.str(); return w.str();
} }
protected: protected:
void _sink_it(const details::log_msg& msg) override void _sink_it(const details::log_msg& msg) override
@ -161,8 +161,8 @@ struct default_daily_file_name_calculator
static filename_t calc_filename(const filename_t& filename) static filename_t calc_filename(const filename_t& filename)
{ {
std::tm tm = spdlog::details::os::localtime(); std::tm tm = spdlog::details::os::localtime();
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename); std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, ext); w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}_{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, ext);
return w.str(); return w.str();
@ -178,8 +178,8 @@ struct dateonly_daily_file_name_calculator
static filename_t calc_filename(const filename_t& filename) static filename_t calc_filename(const filename_t& filename)
{ {
std::tm tm = spdlog::details::os::localtime(); std::tm tm = spdlog::details::os::localtime();
filename_t basename, ext; filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename); std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w; std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext); w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext);
return w.str(); return w.str();

View File

@ -75,73 +75,73 @@ TEST_CASE("file_helper_reopen2", "[file_helper::reopen(false)]]")
TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]") TEST_CASE("file_helper_split_by_extenstion", "[file_helper::split_by_extenstion()]]")
{ {
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.txt"); std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.txt");
REQUIRE(basename == "mylog"); REQUIRE(basename == "mylog");
REQUIRE(ext == ".txt"); REQUIRE(ext == ".txt");
} }
TEST_CASE("file_helper_split_by_extenstion2", "[file_helper::split_by_extenstion()]]") TEST_CASE("file_helper_split_by_extenstion2", "[file_helper::split_by_extenstion()]]")
{ {
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog"); std::tie(basename, ext) = file_helper::split_by_extenstion("mylog");
REQUIRE(basename == "mylog"); REQUIRE(basename == "mylog");
REQUIRE(ext == ""); REQUIRE(ext == "");
} }
TEST_CASE("file_helper_split_by_extenstion3", "[file_helper::split_by_extenstion()]]") TEST_CASE("file_helper_split_by_extenstion3", "[file_helper::split_by_extenstion()]]")
{ {
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz.txt"); std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz.txt");
REQUIRE(basename == "mylog.xyz"); REQUIRE(basename == "mylog.xyz");
REQUIRE(ext == ".txt"); REQUIRE(ext == ".txt");
} }
TEST_CASE("file_helper_split_by_extenstion4", "[file_helper::split_by_extenstion()]]") TEST_CASE("file_helper_split_by_extenstion4", "[file_helper::split_by_extenstion()]]")
{ {
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz....txt"); std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz....txt");
REQUIRE(basename == "mylog.xyz..."); REQUIRE(basename == "mylog.xyz...");
REQUIRE(ext == ".txt"); REQUIRE(ext == ".txt");
} }
TEST_CASE("file_helper_split_by_extenstion5", "[file_helper::split_by_extenstion(hidden_file)]]") TEST_CASE("file_helper_split_by_extenstion5", "[file_helper::split_by_extenstion(hidden_file)]]")
{ {
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(".mylog"); std::tie(basename, ext) = file_helper::split_by_extenstion(".mylog");
REQUIRE(basename == ".mylog"); REQUIRE(basename == ".mylog");
REQUIRE(ext == ""); REQUIRE(ext == "");
} }
TEST_CASE("file_helper_split_by_extenstion6", "[file_helper::split_by_extenstion(hidden_file)]]") TEST_CASE("file_helper_split_by_extenstion6", "[file_helper::split_by_extenstion(hidden_file)]]")
{ {
#ifdef _WIN32 #ifdef _WIN32
auto filename = "folder\\.mylog"; auto filename = "folder\\.mylog";
auto expected_basename = "folder\\.mylog"; auto expected_basename = "folder\\.mylog";
#else #else
auto filename = "folder/.mylog"; auto filename = "folder/.mylog";
auto expected_basename = "folder/.mylog"; auto expected_basename = "folder/.mylog";
#endif #endif
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename); std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename); REQUIRE(basename == expected_basename);
REQUIRE(ext == ""); REQUIRE(ext == "");
} }
TEST_CASE("file_helper_split_by_extenstion7", "[file_helper::split_by_extenstion(hidden_file)]]") TEST_CASE("file_helper_split_by_extenstion7", "[file_helper::split_by_extenstion(hidden_file)]]")
{ {
#ifdef _WIN32 #ifdef _WIN32
auto filename = "folder\\.mylog.txt"; auto filename = "folder\\.mylog.txt";
auto expected_basename = "folder\\.mylog"; auto expected_basename = "folder\\.mylog";
#else #else
auto filename = "folder/.mylog.txt"; auto filename = "folder/.mylog.txt";
auto expected_basename = "folder/.mylog"; auto expected_basename = "folder/.mylog";
#endif #endif
std::string basename, ext; std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename); std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename); REQUIRE(basename == expected_basename);
REQUIRE(ext == ".txt"); REQUIRE(ext == ".txt");
} }

View File

@ -195,48 +195,48 @@ TEST_CASE("daily_logger with custom calculator", "[daily_logger_custom]]")
TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename1", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
REQUIRE(filename == "rotated.3.txt"); REQUIRE(filename == "rotated.3.txt");
} }
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
REQUIRE(filename == "rotated.3"); REQUIRE(filename == "rotated.3");
} }
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]") TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
{ {
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0); auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
REQUIRE(filename == "rotated.txt"); REQUIRE(filename == "rotated.txt");
} }
TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]") TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]")
{ {
// daily.YYYY-MM-DD_hh-mm.txt // daily.YYYY-MM-DD_hh-mm.txt
auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily.txt"); auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily.txt");
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)"); std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9].txt$)");
std::smatch match; std::smatch match;
REQUIRE(std::regex_match(filename, match, re)); REQUIRE(std::regex_match(filename, match, re));
} }
TEST_CASE("daily_file_sink::default_daily_file_name_calculator2", "[daily_file_sink]]") TEST_CASE("daily_file_sink::default_daily_file_name_calculator2", "[daily_file_sink]]")
{ {
// daily.YYYY-MM-DD_hh-mm.txt // daily.YYYY-MM-DD_hh-mm.txt
auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily"); auto filename = spdlog::sinks::default_daily_file_name_calculator::calc_filename("daily");
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)"); std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])_\d\d-[0-5][0-9]$)");
std::smatch match; std::smatch match;
REQUIRE(std::regex_match(filename, match, re)); REQUIRE(std::regex_match(filename, match, re));
} }
TEST_CASE("daily_file_sink::dateonly_daily_file_name_calculator", "[daily_file_sink]]") TEST_CASE("daily_file_sink::dateonly_daily_file_name_calculator", "[daily_file_sink]]")
{ {
// daily.YYYY-MM-DD_hh-mm.txt // daily.YYYY-MM-DD_hh-mm.txt
auto filename = spdlog::sinks::dateonly_daily_file_name_calculator::calc_filename("daily.txt"); auto filename = spdlog::sinks::dateonly_daily_file_name_calculator::calc_filename("daily.txt");
// date regex based on https://www.regular-expressions.info/dates.html // date regex based on https://www.regular-expressions.info/dates.html
std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)"); std::regex re(R"(^daily_(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])\.txt$)");
std::smatch match; std::smatch match;
REQUIRE(std::regex_match(filename, match, re)); REQUIRE(std::regex_match(filename, match, re));
} }