Skip to content

Commit

Permalink
Merge pull request #18 from arcsine-project/update-io-n-logging
Browse files Browse the repository at this point in the history
Small refactoring of the `io` and `logging` modules
  • Loading branch information
akukh authored May 23, 2024
2 parents 7fbd081 + 7e46df0 commit 07d8128
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 73 deletions.
6 changes: 6 additions & 0 deletions flux-io/flux/io/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace flux::io {
using ::fast_io::ibuf_file;
using ::fast_io::obuf_file;
using ::fast_io::open_mode;
using ::fast_io::output_stream;

using ::fast_io::ibuf_file_mutex;
using ::fast_io::obuf_file_mutex;
Expand All @@ -15,4 +16,9 @@ using ::fast_io::io_flush_guard;
using dir = ::fast_io::dir_file;
using pipe = ::fast_io::pipe;

// clang-format off
template <typename CharT, typename T>
using reserve_type_t = ::fast_io::io_reserve_type_t<CharT, T>;
// clang-format on

} // namespace flux::io
4 changes: 2 additions & 2 deletions flux-io/flux/io/manipulators-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
struct test_io_device {
using char_type = char;

std::string buffer;
::std::string buffer;
};

template <typename Iter>
Expand Down Expand Up @@ -68,5 +68,5 @@ TEST_CASE("io::c_str", "[flux-io/manipulators.hpp]") {

char const* p = "hello";
io::print(device, io::c_str(p));
CHECK(device.buffer == std::string{"hello"});
CHECK(device.buffer == ::std::string{"hello"});
}
10 changes: 5 additions & 5 deletions flux-io/flux/io/print-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
struct test_io_device {
using char_type = char;

std::string buffer;
::std::string buffer;
};

template <typename Iter>
Expand All @@ -20,7 +20,7 @@ TEST_CASE("io::print", "[flux-io/print.hpp]") {
test_io_device device;

io::print(device, "test");
CHECK(device.buffer == std::string{"test"});
CHECK(device.buffer == ::std::string{"test"});
}

SECTION("integer values") {
Expand All @@ -29,7 +29,7 @@ TEST_CASE("io::print", "[flux-io/print.hpp]") {
int a = 1;
int b = 2;
io::print(device, a, b, 3);
CHECK(device.buffer == std::string{"123"});
CHECK(device.buffer == ::std::string{"123"});
}
}

Expand All @@ -40,7 +40,7 @@ TEST_CASE("io::println", "[flux-io/print.hpp]") {
test_io_device device;

io::println(device, "test");
CHECK(device.buffer == std::string{"test\n"});
CHECK(device.buffer == ::std::string{"test\n"});
}

SECTION("integer values") {
Expand All @@ -49,6 +49,6 @@ TEST_CASE("io::println", "[flux-io/print.hpp]") {
int a = 1;
int b = 2;
io::println(device, a, b, 3);
CHECK(device.buffer == std::string{"123\n"});
CHECK(device.buffer == ::std::string{"123\n"});
}
}
44 changes: 22 additions & 22 deletions flux-logging/flux/logging/detail/logger_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,44 @@ struct [[nodiscard]] to_file final {};
struct [[nodiscard]] to_console final {};

struct [[nodiscard]] ansi_color final {
std::uint8_t r = 255;
std::uint8_t g = 255;
std::uint8_t b = 255;
std::uint8_t a = 255; // << Unused, exists only for alignment.
::std::uint8_t r = 255;
::std::uint8_t g = 255;
::std::uint8_t b = 255;
::std::uint8_t a = 255; // << Unused, exists only for alignment.

struct [[nodiscard]] escape_code final {
static constexpr std::string_view begin() noexcept {
static constexpr ::std::string_view begin() noexcept {
return "\033[38;2;";
}
static constexpr std::string_view end() noexcept {
static constexpr ::std::string_view end() noexcept {
return "\033[0;00m";
}
};
};

constexpr auto print_reserve_size(fast_io::io_reserve_type_t<char, ansi_color>) noexcept {
constexpr auto print_reserve_size(io::reserve_type_t<char, ansi_color>) noexcept {
using namespace fast_io;
constexpr auto reserve_size = print_reserve_size(io_reserve_type<char, std::uint8_t>);
constexpr auto reserve_size = print_reserve_size(io_reserve_type<char, ::std::uint8_t>);
constexpr auto total_size = reserve_size * 3;
return total_size;
}

constexpr auto print_reserve_define(fast_io::io_reserve_type_t<char, ansi_color>, char* it,
constexpr auto print_reserve_define(io::reserve_type_t<char, ansi_color>, char* it,
ansi_color color) noexcept {
using namespace fast_io;
// clang-format off
*(it = print_reserve_define(io_reserve_type<char, std::uint8_t>, it, color.r)) = ';';
*(it = print_reserve_define(io_reserve_type<char, std::uint8_t>, ++it, color.g)) = ';';
*(it = print_reserve_define(io_reserve_type<char, std::uint8_t>, ++it, color.b)) = 'm';
*(it = print_reserve_define(io_reserve_type<char, ::std::uint8_t>, it, color.r)) = ';';
*(it = print_reserve_define(io_reserve_type<char, ::std::uint8_t>, ++it, color.g)) = ';';
*(it = print_reserve_define(io_reserve_type<char, ::std::uint8_t>, ++it, color.b)) = 'm';
// clang-format on
return ++it;
}
static_assert(fast_io::reserve_printable<char, ansi_color>);

template <typename T, typename... Args>
constexpr void println(ansi_color color, T&& device, Args&&... args) noexcept {
io::println(std::forward<T>(device), ansi_color::escape_code::begin(), color,
std::forward<Args>(args)..., ansi_color::escape_code::end());
io::println(::std::forward<T>(device), ansi_color::escape_code::begin(), color,
::std::forward<Args>(args)..., ansi_color::escape_code::end());
}

namespace detail {
Expand All @@ -71,10 +71,10 @@ detail::dummy_logger<Output>& logger() noexcept;
namespace detail {

inline auto get_local_time() noexcept {
[[maybe_unused]] static bool once = ([] { fast_io::posix_tzset(); }(), true);
auto timestamp = local(fast_io::posix_clock_gettime(fast_io::posix_clock_id::realtime));
timestamp.subseconds = 0;
return timestamp;
[[maybe_unused]] static bool once = ([] noexcept { ::fast_io::posix_tzset(); }(), true);
auto ts = local(::fast_io::posix_clock_gettime(::fast_io::posix_clock_id::realtime));
ts.subseconds = 0;
return ts;
}

// clang-format off
Expand All @@ -86,14 +86,14 @@ struct [[maybe_unused]] dummy_logger<to_file> final {
constexpr dummy_logger& operator=(dummy_logger&&) = delete;

template <typename... Args>
constexpr void log(ansi_color color, std::string_view prefix, meta::tuple<Args&&...> tuple,
constexpr void log(ansi_color color, ::std::string_view prefix, meta::tuple<Args&&...> tuple,
source_location location) noexcept {
using namespace io;
[[maybe_unused]] io_flush_guard guard{output_file_};
meta::apply(
[&](auto&&... args) {
println(color, output_file_, get_local_time(), " ", location, " ", prefix, " ",
std::forward<Args>(args)...);
::std::forward<Args>(args)...);
},
tuple);
}
Expand All @@ -116,13 +116,13 @@ struct [[maybe_unused]] dummy_logger<to_console> final {
constexpr dummy_logger& operator=(dummy_logger&&) = delete;

template <typename... Args>
constexpr void log(ansi_color color, std::string_view prefix, meta::tuple<Args&&...> tuple,
constexpr void log(ansi_color color, ::std::string_view prefix, meta::tuple<Args&&...> tuple,
source_location location) noexcept {
using namespace io;
meta::apply(
[&](auto&&... args) noexcept {
println(color, out(), get_local_time(), " ", location, " ", prefix, " ",
std::forward<Args>(args)...);
::std::forward<Args>(args)...);
},
tuple);
}
Expand Down
34 changes: 17 additions & 17 deletions flux-logging/flux/logging/detail/source_location.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
namespace flux::log {

// This is a custom implementation of `source_location` specifically for logging.
// If you want standard behavior please use `std::source_location`.
// If you want standard behavior please use `::std::source_location`.
struct [[nodiscard]] source_location final {
static consteval source_location
current(std::uint_least32_t line = __builtin_LINE(),
std::uint_least32_t column = __builtin_COLUMN(),
char const* file_name = __builtin_FILE(),
char const* function_name = __builtin_FUNCTION()) noexcept {
current(::std::uint_least32_t line = __builtin_LINE(),
::std::uint_least32_t column = __builtin_COLUMN(),
char const* file_name = __builtin_FILE(),
char const* function_name = __builtin_FUNCTION()) noexcept {
source_location location{};
location.line_ = line;
location.column_ = column;
Expand All @@ -22,11 +22,11 @@ struct [[nodiscard]] source_location final {

constexpr source_location() noexcept = default;

constexpr std::uint_least32_t line() const noexcept {
constexpr ::std::uint_least32_t line() const noexcept {
return line_;
}

constexpr std::uint_least32_t column() const noexcept {
constexpr ::std::uint_least32_t column() const noexcept {
return column_;
}

Expand All @@ -39,10 +39,10 @@ struct [[nodiscard]] source_location final {
}

private:
std::uint_least32_t line_ = {};
std::uint_least32_t column_ = {};
char const* file_name_ = "";
char const* function_name_ = "";
::std::uint_least32_t line_ = {};
::std::uint_least32_t column_ = {};
char const* file_name_ = "";
char const* function_name_ = "";
};

} // namespace flux::log
Expand All @@ -52,15 +52,15 @@ namespace fast_io {
struct flux_source_location_scatter {
basic_io_scatter_t<char> file_name;
basic_io_scatter_t<char> function_name;
std::uint_least32_t line;
std::uint_least32_t column;
::std::uint_least32_t line;
::std::uint_least32_t column;
};

namespace details {

inline constexpr std::size_t
inline constexpr ::std::size_t
print_reserve_size_source_location_impl(flux_source_location_scatter location) noexcept {
constexpr auto reserve_size = print_reserve_size(io_reserve_type<char, std::uint_least32_t>);
constexpr auto reserve_size = print_reserve_size(io_reserve_type<char, ::std::uint_least32_t>);
constexpr auto total_size = (reserve_size * 2 + 3);
return intrinsics::add_or_overflow_die_chain(location.file_name.len, location.function_name.len,
total_size);
Expand All @@ -69,7 +69,7 @@ print_reserve_size_source_location_impl(flux_source_location_scatter location) n
inline constexpr char*
print_reserve_define_source_location_impl(char* it,
flux_source_location_scatter location) noexcept {
constexpr auto io_reserve = io_reserve_type<char, std::uint_least32_t>;
constexpr auto io_reserve = io_reserve_type<char, ::std::uint_least32_t>;
*(it = non_overlapped_copy_n(location.file_name.base, location.file_name.len, it)) = ':';
*(it = print_reserve_define(io_reserve, ++it, location.line)) = ':';
*(it = print_reserve_define(io_reserve, ++it, location.column)) = ':';
Expand All @@ -86,7 +86,7 @@ print_alias_define_source_location_impl(flux::log::source_location location) noe

} // namespace details

inline constexpr std::size_t
inline constexpr ::std::size_t
print_reserve_size(io_reserve_type_t<char, flux_source_location_scatter>,
flux_source_location_scatter location) noexcept {
return details::print_reserve_size_source_location_impl(location);
Expand Down
Loading

0 comments on commit 07d8128

Please sign in to comment.