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.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
@ -50,7 +50,7 @@ int main(int, char*[])
auto rotating_logger = spd::rotating_logger_mt("some_logger_name", "logs/rotating.txt", 1048576 * 5, 3);
for (int i = 0; i < 10; ++i)
rotating_logger->info("{} * {} equals {:>10}", i, i, i*i);
// Create a daily logger - a new file is created every day on 2:30am
auto daily_logger = spd::daily_logger_mt("daily_logger", "logs/daily.txt", 2, 30);
// trigger flush if the log severity is error or higher

View File

@ -108,26 +108,26 @@ public:
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());
}
//
// 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;

View File

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

View File

@ -488,7 +488,7 @@ template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class BasicPrintfArgFormatter;
template <typename CharType,
typename ArgFormatter = fmt::ArgFormatter<CharType> >
typename ArgFormatter = fmt::ArgFormatter<CharType> >
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)>
struct ArgArray;
struct ArgArray;
template <std::size_t N>
struct ArgArray<N, true/*IsPacked*/>
{
typedef Value Type[N > 0 ? N : 1];
template <typename Formatter, typename T>
static Value make(const T &value)
template <typename Formatter, typename T>
static Value make(const T &value)
{
#ifdef __clang__
Value result = MakeValue<Formatter>(value);
@ -2638,7 +2638,7 @@ struct ArgArray<N, true/*IsPacked*/>
return MakeValue<Formatter>(value);
#endif
}
};
};
template <std::size_t N>
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'.
type = 'f';
#endif
// Fall through.
// Fall through.
case 'E':
case 'G':
case 'A':

View File

@ -63,7 +63,7 @@ public:
template <typename T> void warn(const T&);
template <typename T> void error(const T&);
template <typename T> void critical(const T&);
bool should_log(level::level_enum) const;
void set_level(level::level_enum);
level::level_enum level() const;

View File

@ -77,23 +77,23 @@ public:
_current_size = _file_helper.size(); //expensive. called only once
}
// calc filename according to index and file extension if exists.
// 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)
{
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
if (index)
{
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
w.write(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
}
else
{
w.write(SPDLOG_FILENAME_T("{}"), filename);
}
return w.str();
}
// calc filename according to index and file extension if exists.
// 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)
{
std::conditional<std::is_same<filename_t::value_type, char>::value, fmt::MemoryWriter, fmt::WMemoryWriter>::type w;
if (index)
{
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
w.write(SPDLOG_FILENAME_T("{}.{}{}"), basename, index, ext);
}
else
{
w.write(SPDLOG_FILENAME_T("{}"), filename);
}
return w.str();
}
protected:
void _sink_it(const details::log_msg& msg) override
@ -111,9 +111,9 @@ protected:
{
_file_helper.flush();
}
private:
private:
// Rotate files:
// log.txt -> log.1.txt
// log.1.txt -> log.2.txt
@ -161,8 +161,8 @@ struct default_daily_file_name_calculator
static filename_t calc_filename(const filename_t& filename)
{
std::tm tm = spdlog::details::os::localtime();
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
filename_t basename, ext;
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;
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();
@ -178,8 +178,8 @@ struct dateonly_daily_file_name_calculator
static filename_t calc_filename(const filename_t& filename)
{
std::tm tm = spdlog::details::os::localtime();
filename_t basename, ext;
std::tie(basename, ext) = details::file_helper::split_by_extenstion(filename);
filename_t basename, ext;
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;
w.write(SPDLOG_FILENAME_T("{}_{:04d}-{:02d}-{:02d}{}"), basename, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, ext);
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()]]")
{
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.txt");
REQUIRE(basename == "mylog");
REQUIRE(ext == ".txt");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.txt");
REQUIRE(basename == "mylog");
REQUIRE(ext == ".txt");
}
TEST_CASE("file_helper_split_by_extenstion2", "[file_helper::split_by_extenstion()]]")
{
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog");
REQUIRE(basename == "mylog");
REQUIRE(ext == "");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog");
REQUIRE(basename == "mylog");
REQUIRE(ext == "");
}
TEST_CASE("file_helper_split_by_extenstion3", "[file_helper::split_by_extenstion()]]")
{
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz.txt");
REQUIRE(basename == "mylog.xyz");
REQUIRE(ext == ".txt");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz.txt");
REQUIRE(basename == "mylog.xyz");
REQUIRE(ext == ".txt");
}
TEST_CASE("file_helper_split_by_extenstion4", "[file_helper::split_by_extenstion()]]")
{
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz....txt");
REQUIRE(basename == "mylog.xyz...");
REQUIRE(ext == ".txt");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion("mylog.xyz....txt");
REQUIRE(basename == "mylog.xyz...");
REQUIRE(ext == ".txt");
}
TEST_CASE("file_helper_split_by_extenstion5", "[file_helper::split_by_extenstion(hidden_file)]]")
{
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(".mylog");
REQUIRE(basename == ".mylog");
REQUIRE(ext == "");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(".mylog");
REQUIRE(basename == ".mylog");
REQUIRE(ext == "");
}
TEST_CASE("file_helper_split_by_extenstion6", "[file_helper::split_by_extenstion(hidden_file)]]")
{
{
#ifdef _WIN32
auto filename = "folder\\.mylog";
auto expected_basename = "folder\\.mylog";
auto filename = "folder\\.mylog";
auto expected_basename = "folder\\.mylog";
#else
auto filename = "folder/.mylog";
auto expected_basename = "folder/.mylog";
auto filename = "folder/.mylog";
auto expected_basename = "folder/.mylog";
#endif
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename);
REQUIRE(ext == "");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename);
REQUIRE(ext == "");
}
TEST_CASE("file_helper_split_by_extenstion7", "[file_helper::split_by_extenstion(hidden_file)]]")
{
#ifdef _WIN32
auto filename = "folder\\.mylog.txt";
auto expected_basename = "folder\\.mylog";
auto filename = "folder\\.mylog.txt";
auto expected_basename = "folder\\.mylog";
#else
auto filename = "folder/.mylog.txt";
auto expected_basename = "folder/.mylog";
auto filename = "folder/.mylog.txt";
auto expected_basename = "folder/.mylog";
#endif
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename);
REQUIRE(ext == ".txt");
std::string basename, ext;
std::tie(basename, ext) = file_helper::split_by_extenstion(filename);
REQUIRE(basename == expected_basename);
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]]")
{
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
REQUIRE(filename == "rotated.3.txt");
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 3);
REQUIRE(filename == "rotated.3.txt");
}
TEST_CASE("rotating_file_sink::calc_filename2", "[rotating_file_sink]]")
{
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
REQUIRE(filename == "rotated.3");
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated", 3);
REQUIRE(filename == "rotated.3");
}
TEST_CASE("rotating_file_sink::calc_filename3", "[rotating_file_sink]]")
{
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
REQUIRE(filename == "rotated.txt");
auto filename = spdlog::sinks::rotating_file_sink_st::calc_filename("rotated.txt", 0);
REQUIRE(filename == "rotated.txt");
}
TEST_CASE("daily_file_sink::default_daily_file_name_calculator1", "[daily_file_sink]]")
{
// daily.YYYY-MM-DD_hh-mm.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::smatch match;
REQUIRE(std::regex_match(filename, match, re));
// daily.YYYY-MM-DD_hh-mm.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::smatch match;
REQUIRE(std::regex_match(filename, match, re));
}
TEST_CASE("daily_file_sink::default_daily_file_name_calculator2", "[daily_file_sink]]")
{
// daily.YYYY-MM-DD_hh-mm.txt
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::smatch match;
REQUIRE(std::regex_match(filename, match, re));
// daily.YYYY-MM-DD_hh-mm.txt
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::smatch match;
REQUIRE(std::regex_match(filename, match, re));
}
TEST_CASE("daily_file_sink::dateonly_daily_file_name_calculator", "[daily_file_sink]]")
{
// daily.YYYY-MM-DD_hh-mm.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
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;
REQUIRE(std::regex_match(filename, match, re));
// daily.YYYY-MM-DD_hh-mm.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
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;
REQUIRE(std::regex_match(filename, match, re));
}