libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
log.hpp
1 #pragma once
2 
3 #include "fly/types/string/literals.hpp"
4 #include "fly/types/string/string.hpp"
5 
6 #include <cstdint>
7 #include <memory>
8 #include <ostream>
9 #include <string>
10 
11 namespace fly::logger {
12 
16 enum class Level : std::uint8_t
17 {
18  Debug,
19  Info,
20  Warn,
21  Error,
22 
23  NumLevels
24 };
25 
29 struct Trace
30 {
31  std::string_view m_file;
32  std::string_view m_function;
33  std::uint32_t m_line {0};
34 };
35 
48 struct Log
49 {
53  Log() = default;
54 
62  Log(Trace &&trace, std::string &&message, std::uint32_t max_message_size) noexcept;
63 
67  Log(Log &&log) noexcept;
68 
72  Log &operator=(Log &&log) noexcept;
73 
74  friend std::ostream &operator<<(std::ostream &stream, const Log &log);
75 
76  std::uintmax_t m_index {0};
77  Level m_level {Level::NumLevels};
78  Trace m_trace {};
79  double m_time {-1.0};
80  std::string m_message;
81 };
82 
83 } // namespace fly::logger
84 
85 //==================================================================================================
86 template <>
88 {
97  template <typename FormatContext>
98  void format(fly::logger::Trace trace, FormatContext &context)
99  {
100  if (!trace.m_file.empty() && !trace.m_function.empty())
101  {
103  context.out(),
104  "{}:{}:{}",
105  trace.m_file,
106  trace.m_function,
107  trace.m_line);
108  }
109  }
110 };
static void format_to(OutputIterator output, FormatString< ParameterTypes... > &&fmt, ParameterTypes &&...parameters)
Definition: string.hpp:939
void format(fly::logger::Trace trace, FormatContext &context)
Definition: log.hpp:98
Definition: formatters.hpp:42
Definition: log.hpp:49
Log & operator=(Log &&log) noexcept
Definition: log.cpp:30
Definition: log.hpp:30