Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
int main() {
std::string filename = "easylog.txt";
std::filesystem::remove(filename);
easylog::init_log(easylog::Severity::DEBUG, filename, true, 5000, 1, true);
easylog::init_log(easylog::Severity::DEBUG, filename, true, true, 500000, 1,
true);

ELOGFMT(INFO, "test {}", 42);
std::string str = "world";
ELOGFMT(INFO, "test {} {} {}", 42, "hello", str);

ELOG_INFO << 42;
ELOG_INFO << 42 << " " << 4.5 << 'a' << easylog::Severity::DEBUG;
Expand Down
6 changes: 4 additions & 2 deletions include/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ inline void add_appender(std::function<void(std::string_view)> fn) {
easylog::logger<Id>::instance() += \
easylog::record_t(std::chrono::system_clock::now(), severity, \
GET_STRING(__FILE__, __LINE__)) \
.sprintf(fmt, __VA_ARGS__); \
.format<easylog::FormatType::SPRINTF>(easylog::get_async<Id>(), \
fmt, __VA_ARGS__); \
if (severity == easylog::Severity::CRITICAL) { \
easylog::flush<Id>(); \
std::exit(EXIT_FAILURE); \
Expand All @@ -201,7 +202,8 @@ inline void add_appender(std::function<void(std::string_view)> fn) {
easylog::logger<Id>::instance() += \
easylog::record_t(std::chrono::system_clock::now(), severity, \
GET_STRING(__FILE__, __LINE__)) \
.format(prefix::format(format_str, __VA_ARGS__)); \
.format<easylog::FormatType::FORMAT>(easylog::get_async<Id>(), \
format_str, __VA_ARGS__); \
if (severity == easylog::Severity::CRITICAL) { \
easylog::flush<Id>(); \
std::exit(EXIT_FAILURE); \
Expand Down
8 changes: 7 additions & 1 deletion include/easylog/appender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <condition_variable>
#include <filesystem>
#include <fstream>
#include <future>
#include <iostream>
#include <shared_mutex>
#include <string>
Expand Down Expand Up @@ -77,7 +78,10 @@ class appender {
void enable_console(bool b) { enable_console_ = b; }

void start_thread() {
write_thd_ = std::thread([this] {
std::promise<void> promise;
auto future = promise.get_future();
write_thd_ = std::thread([this, promise = std::move(promise)]() mutable {
promise.set_value();
while (!stop_) {
if (max_files_ > 0 && file_size_ > max_file_size_ &&
static_cast<size_t>(-1) != file_size_) {
Expand Down Expand Up @@ -110,6 +114,7 @@ class appender {
}
}
});
future.wait();
}

std::string_view get_tid_buf(unsigned int tid) {
Expand Down Expand Up @@ -140,6 +145,7 @@ class appender {

template <bool sync = false, bool enable_console = false>
void write_record(record_t &record) {
record.gen_content();
std::lock_guard guard(get_mutex<sync>());
if constexpr (sync == true) {
if (max_files_ > 0 && file_size_ > max_file_size_ &&
Expand Down
65 changes: 59 additions & 6 deletions include/easylog/record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#include <charconv>
#include <chrono>
#include <cstddef>
#include <cstring>
#include <functional>
#include <type_traits>

#include "util/time_util.hpp"
#ifdef __linux__
Expand Down Expand Up @@ -68,6 +71,11 @@ template <typename T>
constexpr inline bool has_str_v = has_str<remove_cvref_t<T>>::value;
} // namespace detail

enum class FormatType {
SPRINTF,
FORMAT,
};

enum class Severity {
NONE,
TRACE,
Expand Down Expand Up @@ -170,14 +178,58 @@ class record_t {
return *this;
}

template <typename... Args>
record_t &sprintf(const char *fmt, Args &&...args) {
ss_.append(fmt::sprintf(fmt, std::forward<Args>(args)...));
return *this;
void gen_content() {
if (args_cache_) {
ss_.append(args_cache_());
args_cache_ = {};
}
}

template <typename T> decltype(auto) transform(T &t) {
using U = std::remove_reference_t<T>;

if constexpr (std::is_same_v<U, const char *> || fixed_array_v<U> ||
string_view_v<U>) {
return std::string(t);
} else if constexpr (string_v<U>) {
return std::move(t);
} else {
return t;
}
}

template <typename String> record_t &format(String &&str) {
ss_.append(str.data());
template <typename Tuple, size_t... I>
auto get_args_impl(Tuple &&tp, std::index_sequence<I...>) {
std::tuple<> t;
return std::tuple_cat(t, std::make_tuple(transform(std::get<I>(tp)))...);
}

template <FormatType fmt_type, typename... Args>
record_t &format(bool async, const char *fmt, Args &&...args) {
size_t len = strlen(fmt);
std::string str;
str.reserve(len);
str.append(fmt);
args_cache_ = [str = std::move(str),
args1 = get_args_impl(
std::make_tuple(std::forward<Args>(args)...),
std::make_index_sequence<sizeof...(Args)>{})]() mutable {
return std::apply(
[str = std::move(str)](auto &&...args) mutable {
if constexpr (fmt_type == FormatType::FORMAT) {
return fmt::format(str.data(), std::move(args)...);
} else {
return fmt::sprintf(str.data(), std::move(args)...);
}
},
args1);
};

if (!async) {
ss_.append(args_cache_());
args_cache_ = {};
}

return *this;
}

Expand Down Expand Up @@ -242,6 +294,7 @@ class record_t {
#else
std::string ss_;
#endif
std::function<std::string()> args_cache_;
};

#define TO_STR(s) #s
Expand Down
38 changes: 35 additions & 3 deletions include/util/type_traits.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <string_view>

template <class T> struct remove_cvref {
typedef std::remove_cv_t<std::remove_reference_t<T>> type;
};
template <class T> using remove_cvref_t = typename remove_cvref<T>::type;

template <class T>
constexpr inline bool c_array_v =
std::is_array_v<remove_cvref_t<T>> &&std::extent_v<remove_cvref_t<T>> > 0;

template <typename Type, typename = void> struct is_array : std::false_type {};

template <typename T>
struct is_array<
T, std::void_t<decltype(std::declval<T>().size()),
typename std::enable_if_t<(std::tuple_size<T>::value != 0)>>>
: std::true_type {};

template <typename T>
constexpr inline bool array_v = is_array<remove_cvref_t<T>>::value;

template <typename Type>
constexpr inline bool fixed_array_v = c_array_v<Type> || array_v<Type>;

template <template <typename...> class U, typename T>
struct is_template_instant_of : std::false_type {};

template <template <typename...> class U, typename... args>
struct is_template_instant_of<U, U<args...>> : std::true_type {};

template <typename T>
constexpr inline bool string_view_v =
is_template_instant_of<std::basic_string_view, remove_cvref_t<T>>::value;

template <typename T>
constexpr inline bool string_v =
is_template_instant_of<std::basic_string, remove_cvref_t<T>>::value;

namespace coro_rpc {
template <typename Function> struct function_traits;

Expand Down Expand Up @@ -150,9 +184,7 @@ struct is_invocable
template <typename F, typename... Args>
inline constexpr bool is_invocable_v = is_invocable<F, Args...>::value;

template <typename T> struct remove_first {
using type = T;
};
template <typename T> struct remove_first { using type = T; };

template <class First, class... Second>
struct remove_first<std::tuple<First, Second...>> {
Expand Down
3 changes: 2 additions & 1 deletion src/benchmark/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ void test_glog() {

void test_easylog() {
std::filesystem::remove("easylog.txt");
easylog::init_log(Severity::DEBUG, "easylog.txt", false, 1024 * 1024, 1);
easylog::init_log(Severity::DEBUG, "easylog.txt", false, false, 1024 * 1024,
1);
{
ScopedTimer timer("easylog");
for (int i = 0; i < 5000; i++)
Expand Down