Skip to content

Commit

Permalink
Improve easylog (#167)
Browse files Browse the repository at this point in the history
* optimize performance

* inline

* rename
  • Loading branch information
qicosmos authored Feb 2, 2023
1 parent c79257f commit ead3011
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CoroRpcConan(ConanFile):

# Sources are located in the same place as this recipe, copy them to the recipe
exports_sources = "CMakeLists.txt", \
"asio_util/*", "examples/*", "logging/*", "coro_rpc/*", "struct_pack/*", "util/*", "tests/*"
"asio_util/*", "examples/*", "easylog/*", "coro_rpc/*", "struct_pack/*", "util/*", "tests/*"

def requirements(self):
self.requires("asio/1.23.0")
Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/async_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <vector>

#include "asio_util/asio_coro_util.hpp"
#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "router.hpp"
#include "router_impl.hpp"
#include "rpc_protocol.h"
Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/common_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
#pragma once

#include <logging/easylog.h>
#include <easylog/easylog.h>

#include <filesystem>

Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <type_traits>
#include <utility>

#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "rpc_protocol.h"
#include "util/type_traits.h"

Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/coro_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "asio_util/asio_coro_util.hpp"
#include "asio_util/asio_util.hpp"
#include "async_simple/coro/SyncAwait.h"
#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "router.hpp"
#include "rpc_protocol.h"
#include "struct_pack/struct_pack.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/coro_rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "common_service.hpp"
#include "coro_rpc/coro_rpc/connection.hpp"
#include "coro_rpc/coro_rpc/rpc_protocol.h"
#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "struct_pack/struct_pack.hpp"
#include "util/function_name.h"
#include "util/type_traits.h"
Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/coro_rpc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include "coro_connection.hpp"
#include "coro_rpc/coro_rpc/router.hpp"
#include "coro_rpc/coro_rpc/rpc_protocol.h"
#include "logging/easylog.h"
#include "easylog/easylog.h"

namespace coro_rpc {
/*!
Expand Down
2 changes: 1 addition & 1 deletion include/coro_rpc/coro_rpc/router_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "async_connection.hpp"
#include "connection.hpp"
#include "coro_connection.hpp"
#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "rpc_execute.hpp"
#include "rpc_protocol.h"
#include "struct_pack/struct_pack.hpp"
Expand Down
File renamed without changes.
File renamed without changes.
46 changes: 37 additions & 9 deletions include/logging/record.hpp → include/easylog/record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/
#pragma once
#include <charconv>
#include <chrono>
#include <cstring>
#include <type_traits>
#if __has_include(<memory_resource>)
#include <memory_resource>
#endif
Expand All @@ -25,6 +27,8 @@
#include <util/meta_string.hpp>
#include <utility>

#include "../thirdparty/iguana/iguana/detail//dragonbox_to_chars.h"

#if defined(_WIN32)
#ifndef _WINDOWS_
#ifndef WIN32_LEAN_AND_MEAN // Sorry for the inconvenience. Please include any
Expand Down Expand Up @@ -82,10 +86,7 @@ class record_t {

Severity get_severity() const { return severity_; }

const char *get_message() const {
msg_ = ss_.str();
return msg_.data();
}
const char *get_message() const { return ss_.data(); }

std::string_view get_file_str() const { return {buf_, buf_len_}; }

Expand All @@ -97,7 +98,29 @@ class record_t {

template <typename T>
record_t &operator<<(const T &data) {
ss_ << data;
using U = std::remove_cvref_t<T>;
if constexpr (std::is_floating_point_v<U>) {
char temp[40];
const auto end = jkj::dragonbox::to_chars(data, temp);
ss_.append(temp, std::distance(temp, end));
}
else if constexpr (std::is_same_v<char, U>) {
char buf[2] = {data, 0};
ss_.append(buf);
}
else if constexpr (std::is_enum_v<U>) {
int val = (int)data;
*this << val;
}
else if constexpr (std::is_integral_v<U>) {
char buf[32];
auto [ptr, ec] = std::to_chars(buf, buf + 32, data);
ss_.append(buf, std::distance(buf, ptr));
}
else {
ss_.append(data);
}

return *this;
}

Expand Down Expand Up @@ -125,17 +148,22 @@ class record_t {

snprintf(&buf[0], size + 1, fmt, args...);

ss_ << buf;
ss_.append(buf);
}

std::chrono::system_clock::time_point tm_point_;
Severity severity_;
unsigned int tid_;
char buf_[64] = {};
size_t buf_len_ = 0;
std::ostringstream ss_; // TODO: will replace it with std::string to improve
// the performance.
mutable std::string msg_;

#if __has_include(<memory_resource>)
char arr_[1024];
std::pmr::monotonic_buffer_resource resource_;
std::pmr::string ss_{&resource_};
#else
std::string ss_;
#endif
};

#define TO_STR(s) #s
Expand Down
2 changes: 1 addition & 1 deletion src/coro_rpc/benchmark/client/data_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "coro_rpc/coro_rpc_client.hpp"
#include "coro_rpc/coro_rpc_server.hpp"
#include "logging/easylog.h"
#include "easylog/easylog.h"
#include "server.hpp"
using namespace std::chrono_literals;

Expand Down
2 changes: 1 addition & 1 deletion src/coro_rpc/examples/helloworld/server/rpc_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
#include "rpc_service/rpc_service.h"

#include <logging/easylog.h>
#include <easylog/easylog.h>

#include <thread>

Expand Down
2 changes: 1 addition & 1 deletion src/coro_rpc/tests/ServerTester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include <string>

#include "doctest.h"
#include "easylog/easylog.h"
#include "inject_action.hpp"
#include "logging/easylog.h"
#include "rpc_api.hpp"

#ifdef _MSC_VER
Expand Down
2 changes: 1 addition & 1 deletion src/coro_rpc/tests/rpc_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <logging/easylog.h>
#include <easylog/easylog.h>

#include <coro_rpc/rpc_connection.hpp>

Expand Down
4 changes: 3 additions & 1 deletion src/easylog/tests/test_easylog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <filesystem>

#include "doctest.h"
#include "logging/easylog.h"
#include "easylog/easylog.h"

using namespace easylog;

Expand All @@ -38,6 +38,8 @@ TEST_CASE("test basic") {
std::filesystem::remove(filename);
easylog::init_log(Severity::DEBUG, filename, true, 5000, 1, true);

ELOG_INFO << 42 << " " << 4.5 << 'a' << Severity::DEBUG;

ELOGV(INFO, "test");
ELOGV(INFO, "it is a long string test %d %s", 2, "ok");
CHECK(get_last_line(filename).rfind("test 2 ok") != std::string::npos);
Expand Down
15 changes: 7 additions & 8 deletions thirdparty/iguana/iguana/detail/dragonbox_to_chars.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,8 @@ JKJ_FORCEINLINE static void print_9_digits(std::uint32_t s32, int &exponent,
}

template <>
char *to_chars<float, default_float_traits<float>>(std::uint32_t s32,
int exponent,
char *buffer) noexcept {
inline char *to_chars<float, default_float_traits<float>>(
std::uint32_t s32, int exponent, char *buffer) noexcept {
// Print significand.
print_9_digits(s32, exponent, buffer);

Expand Down Expand Up @@ -317,7 +316,7 @@ char *to_chars<float, default_float_traits<float>>(std::uint32_t s32,
}

template <>
char *to_chars<double, default_float_traits<double>>(
inline char *to_chars<double, default_float_traits<double>>(
std::uint64_t const significand, int exponent, char *buffer) noexcept {
// Print significand by decomposing it into a 9-digit block and a 8-digit
// block.
Expand Down Expand Up @@ -544,8 +543,8 @@ char *to_chars<double, default_float_traits<double>>(

// Avoid needless ABI overhead incurred by tag dispatch.
template <class PolicyHolder, class Float, class FloatTraits>
char *to_chars_n_impl(float_bits<Float, FloatTraits> br,
char *buffer) noexcept {
inline char *to_chars_n_impl(float_bits<Float, FloatTraits> br,
char *buffer) noexcept {
auto const exponent_bits = br.extract_exponent_bits();
auto const s = br.remove_exponent_bits(exponent_bits);

Expand Down Expand Up @@ -588,7 +587,7 @@ char *to_chars_n_impl(float_bits<Float, FloatTraits> br,
// Returns the next-to-end position
template <class Float, class FloatTraits = default_float_traits<Float>,
class... Policies>
char *to_chars_n(Float x, char *buffer, Policies... policies) noexcept {
inline char *to_chars_n(Float x, char *buffer, Policies... policies) noexcept {
using namespace jkj::dragonbox::detail::policy_impl;
using policy_holder = decltype(make_policy_holder(
base_default_pair_list<
Expand All @@ -606,7 +605,7 @@ char *to_chars_n(Float x, char *buffer, Policies... policies) noexcept {
// Null-terminate and bypass the return value of fp_to_chars_n
template <class Float, class FloatTraits = default_float_traits<Float>,
class... Policies>
char *to_chars(Float x, char *buffer, Policies... policies) noexcept {
inline char *to_chars(Float x, char *buffer, Policies... policies) noexcept {
auto ptr = to_chars_n<Float, FloatTraits>(x, buffer, policies...);
*ptr = '\0';
return ptr;
Expand Down

0 comments on commit ead3011

Please sign in to comment.