Skip to content

Commit

Permalink
Logging has been moved to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
akukh committed May 1, 2024
1 parent 789d2e0 commit 2b9cf9c
Show file tree
Hide file tree
Showing 16 changed files with 294 additions and 229 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endif()
add_subdirectory("flux-config")
add_subdirectory("flux-foundation")
add_subdirectory("flux-io")
add_subdirectory("flux-logging")
add_subdirectory("flux-meta")

#-----------------------------------------------------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions flux-foundation/flux/foundation.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <flux/io.hpp>

#include <flux/foundation/utility.hpp>
18 changes: 18 additions & 0 deletions flux-foundation/flux/foundation/types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <cstddef>
#include <cstdint>
#include <flux/meta/integer.hpp>

namespace flux::fou {

using i8 = meta::integer_of_size_t<1>;
using i16 = meta::integer_of_size_t<2>;
using i32 = meta::integer_of_size_t<4>;
using i64 = meta::integer_of_size_t<8>;

using u8 = meta::unsigned_integer_of_size_t<1>;
using u16 = meta::unsigned_integer_of_size_t<2>;
using u32 = meta::unsigned_integer_of_size_t<4>;
using u64 = meta::unsigned_integer_of_size_t<8>;

} // namespace flux::fou
2 changes: 1 addition & 1 deletion flux-foundation/flux/foundation/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

#include <flux/foundation/utility/addressof.hpp>
#include <flux/foundation/utility/launder.hpp>
#include <flux/foundation/utility/logger.hpp>
#include <flux/foundation/utility/terminate.hpp>
#include <flux/foundation/utility/unreachable.hpp>
46 changes: 0 additions & 46 deletions flux-foundation/flux/foundation/utility/source_location.hpp

This file was deleted.

17 changes: 8 additions & 9 deletions flux-io/flux/io/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

namespace flux::io {

using open_mode = fast_io::open_mode;
using ::fast_io::ibuf_file;
using ::fast_io::obuf_file;
using ::fast_io::open_mode;

using dir = fast_io::dir_file;
using pipe = fast_io::pipe;
using ::fast_io::ibuf_file_mutex;
using ::fast_io::obuf_file_mutex;

using ibuf_file = fast_io::ibuf_file;
using obuf_file = fast_io::obuf_file;
using ::fast_io::io_flush_guard;

using ibuf_file_mutex = fast_io::ibuf_file_mutex;
using obuf_file_mutex = fast_io::obuf_file_mutex;

using fast_io::io_flush_guard;
using dir = ::fast_io::dir_file;
using pipe = ::fast_io::pipe;

} // namespace flux::io
42 changes: 21 additions & 21 deletions flux-io/flux/io/manipulators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,47 @@

namespace flux::io {

using fast_io::manipulators::left;
using fast_io::manipulators::middle;
using fast_io::manipulators::right;
using fast_io::manipulators::width;
using ::fast_io::manipulators::left;
using ::fast_io::manipulators::middle;
using ::fast_io::manipulators::right;
using ::fast_io::manipulators::width;

using fast_io::manipulators::addrvw;
using fast_io::manipulators::chvw;
using ::fast_io::manipulators::addrvw;
using ::fast_io::manipulators::chvw;

// clang-format off
template <typename T>
inline constexpr auto address(T value) noexcept {
if constexpr (meta::integral<T>) {
return fast_io::manipulators::handlevw(&value);
return ::fast_io::manipulators::handlevw(&value);
} else if constexpr (meta::pointer<T>) {
return fast_io::manipulators::pointervw(value);
return ::fast_io::manipulators::pointervw(value);
} else if constexpr (meta::function<T>) {
return fast_io::manipulators::funcvw(value);
return ::fast_io::manipulators::funcvw(value);
} else if constexpr (meta::member_function<T>) {
return fast_io::manipulators::methodvw(value);
return ::fast_io::manipulators::methodvw(value);
} else {
static_assert(true, "Invalid type passed to function.");
}
}

template <meta::integral T>
inline constexpr auto c_str(T const* cstr) noexcept {
return fast_io::manipulators::os_c_str(cstr);
return ::fast_io::manipulators::os_c_str(cstr);
}
// clang-format on

using fast_io::to;
using ::fast_io::to;

using fast_io::manipulators::bin;
using fast_io::manipulators::dec;
using fast_io::manipulators::hex;
using fast_io::manipulators::hex0x;
using fast_io::manipulators::oct;
using ::fast_io::manipulators::bin;
using ::fast_io::manipulators::dec;
using ::fast_io::manipulators::hex;
using ::fast_io::manipulators::hex0x;
using ::fast_io::manipulators::oct;

using fast_io::manipulators::boolalpha;
using fast_io::manipulators::fixed;
using fast_io::manipulators::hexfloat;
using fast_io::manipulators::scientific;
using ::fast_io::manipulators::boolalpha;
using ::fast_io::manipulators::fixed;
using ::fast_io::manipulators::hexfloat;
using ::fast_io::manipulators::scientific;

} // namespace flux::io
16 changes: 5 additions & 11 deletions flux-io/flux/io/print.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace flux::io {

// clang-format off
using fast_io::out;
using ::fast_io::out;

template <typename... Args>
inline constexpr void print(Args&&... args) noexcept {
fast_io::io::print(std::forward<Args>(args)...);
}
using ::fast_io::io::print;
using ::fast_io::io::println;

template <typename... Args>
inline constexpr void println(Args&&... args) noexcept {
fast_io::io::println(std::forward<Args>(args)...);
}
// clang-format on
using ::fast_io::io::panic;
using ::fast_io::io::panicln;

} // namespace flux::io
6 changes: 6 additions & 0 deletions flux-logging/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
flux_interface_library(logging
COMMON
LINK
flux::io)

# code: language="CMake" insertSpaces=true tabSize=4
4 changes: 4 additions & 0 deletions flux-logging/flux/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include <flux/io.hpp>
#include <flux/logging/logger.hpp>
Original file line number Diff line number Diff line change
Expand Up @@ -4,79 +4,11 @@
// TODO:
// Revisit it.
#pragma once
#include <flux/logging/detail/source_location.hpp>

#include <flux/foundation/utility/detail/strip_path.hpp>
#include <flux/foundation/utility/source_location.hpp>

#include <cstddef>
#include <string_view>

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;
};

namespace details {

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 total_size = (reserve_size * 2 + 3);
return intrinsics::add_or_overflow_die_chain(location.file_name.len, location.function_name.len,
total_size);
}

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>;
*(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)) = ':';
return non_overlapped_copy_n(location.function_name.base, location.function_name.len, ++it);
}

inline constexpr flux_source_location_scatter
print_alias_define_source_location_impl(flux::fou::source_location location) noexcept {
using flux::fou::detail::strip_path;
return {{strip_path(location.file_name()), cstr_len(strip_path(location.file_name()))},
{location.function_name(), cstr_len(location.function_name())},
location.line(),
location.column()};
}

} // namespace details

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);
}

inline constexpr char* print_reserve_define(io_reserve_type_t<char, flux_source_location_scatter>,
char* iter,
flux_source_location_scatter location) noexcept {
return details::print_reserve_define_source_location_impl(iter, location);
}

inline constexpr flux_source_location_scatter
print_alias_define(io_alias_t, flux::fou::source_location location) noexcept {
return details::print_alias_define_source_location_impl(location);
}

namespace manipulators {
inline constexpr auto
cur_src_loc(flux::fou::source_location location = flux::fou::source_location::current()) noexcept {
return location;
}
} // namespace manipulators
} // namespace fast_io

namespace flux::fou {
namespace flux::log {

struct [[nodiscard]] to_file final {};
struct [[nodiscard]] to_console final {};
Expand Down Expand Up @@ -140,17 +72,11 @@ namespace detail {

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

#define FLUX_PRINT_LOGGER_HEADER_TO(output_file) \
io::println(output_file, left("+", 36, '-'), left("+", 63, '-'), left("+", 13, '-'), \
left("+", 32, '-')); \
io::println(output_file, "| ", left("Date / Time", 33), " | ", left("Location", 60), " | ", \
left("Level", 10), " | Message"); \
io::println(output_file, left("+", 36, '-'), left("+", 63, '-'), left("+", 13, '-'), \
left("+", 32, '-'));

// clang-format off
template <>
struct [[maybe_unused]] dummy_logger<to_file> final {
Expand All @@ -166,20 +92,17 @@ struct [[maybe_unused]] dummy_logger<to_file> final {
[[maybe_unused]] io_flush_guard guard{output_file_};
meta::apply(
[&](auto&&... args) {
println(color, output_file_, " ", left(get_local_time(), 36),
left(location, 63), left(prefix, 13), std::forward<Args>(args)...);
println(color, output_file_, get_local_time(), " ", location, " ", prefix, " ",
std::forward<Args>(args)...);
},
tuple);
}

template <typename Output>
friend dummy_logger<Output>& flux::fou::logger() noexcept;
friend dummy_logger<Output>& flux::log::logger() noexcept;

private:
constexpr dummy_logger() noexcept : output_file_{"log.ansi", io::open_mode::app} {
using namespace io;
FLUX_PRINT_LOGGER_HEADER_TO(output_file_)
}
constexpr dummy_logger() noexcept : output_file_{"log.ansi", io::open_mode::app} {}
constexpr ~dummy_logger() = default;

io::obuf_file output_file_;
Expand All @@ -198,14 +121,14 @@ struct [[maybe_unused]] dummy_logger<to_console> final {
using namespace io;
meta::apply(
[&](auto&&... args) noexcept {
println(color, out(), left(get_local_time(), 34), location, " ", prefix, " ",
println(color, out(), get_local_time(), " ", location, " ", prefix, " ",
std::forward<Args>(args)...);
},
tuple);
}

template <typename Output>
friend dummy_logger<Output>& flux::fou::logger() noexcept;
friend dummy_logger<Output>& flux::log::logger() noexcept;

private:
constexpr dummy_logger() noexcept = default;
Expand All @@ -216,4 +139,4 @@ struct [[maybe_unused]] dummy_logger<to_console> final {
#undef FLUX_PRINT_LOGGER_HEADER_TO

} // namespace detail
} // namespace flux::fou
} // namespace flux::log
Loading

0 comments on commit 2b9cf9c

Please sign in to comment.