itch-mold-replay 1.0
Time-accurate NASDAQ TotalView-ITCH 5.0 replay over MoldUDP64
Loading...
Searching...
No Matches
log.h
Go to the documentation of this file.
1#pragma once
2#include <cstring>
3#include <format>
4#include <print>
5#include <source_location>
6#include <unistd.h>
7namespace imr::util::log
8{
9 namespace detail
10 {
11#ifndef IMR_LOG_LEVEL
12#define IMR_LOG_LEVEL 0
13#endif
14 inline constexpr int level{IMR_LOG_LEVEL};
15
16 inline constexpr std::string_view color_error{"\033[1;31m"};
17 inline constexpr std::string_view color_warn{"\033[1;33m"};
18 inline constexpr std::string_view color_info{"\033[36m"};
19 inline constexpr std::string_view color_debug{"\033[35m"};
20
21 template <typename... Args>
22 void base(std::FILE* stream, std::string_view tag, std::string_view color, std::format_string<Args...> fmt, Args&&... args)
23 {
24 if (static_cast<bool>(isatty(fileno(stream))))
25 std::print(stream, "{}[imr:{}]\033[0m ", color, tag);
26 else
27 std::print(stream, "[imr:{}] ", tag);
28 std::println(stream, fmt, std::forward<Args>(args)...);
29 }
30 }
31 template <typename... Args>
32 void error(std::format_string<Args...> fmt, Args&&... args)
33 {
34 if constexpr (detail::level >= 0)
35 {
36 detail::base(stderr, "error", detail::color_error, fmt, std::forward<Args>(args)...);
37 }
38 }
39 // mimics std::perror but never any msg other than loc.function_name
40 inline void perror(std::source_location loc = std::source_location::current())
41 {
42 if constexpr (detail::level >= 0)
43 {
44 error("{}: {}", loc.function_name(), std::strerror(errno));
45 }
46 }
47 template <typename... Args>
48 void warn(std::format_string<Args...> fmt, Args&&... args)
49 {
50 if constexpr (detail::level >= 1)
51 {
52 detail::base(stdout, "warn", detail::color_warn, fmt, std::forward<Args>(args)...);
53 }
54 }
55 template <typename... Args>
56 void info(std::format_string<Args...> fmt, Args&&... args)
57 {
58 if constexpr (detail::level >= 2)
59 {
60 detail::base(stdout, "info", detail::color_info, fmt, std::forward<Args>(args)...);
61 }
62 }
63 template <typename... Args>
64 void debug(std::format_string<Args...> fmt, Args&&... args)
65 {
66 if constexpr (detail::level >= 3)
67 {
68 detail::base(stdout, "debug", detail::color_debug, fmt, std::forward<Args>(args)...);
69 }
70 }
71 template <typename... Args>
72 void debug(std::source_location loc = std::source_location::current())
73 {
74 if constexpr (detail::level >= 3)
75 {
76 debug("{}", loc.function_name());
77 }
78 }
79}
#define IMR_LOG_LEVEL
Definition log.h:12
Definition log.h:10
constexpr std::string_view color_warn
Definition log.h:17
constexpr std::string_view color_info
Definition log.h:18
void base(std::FILE *stream, std::string_view tag, std::string_view color, std::format_string< Args... > fmt, Args &&... args)
Definition log.h:22
constexpr int level
Definition log.h:14
constexpr std::string_view color_error
Definition log.h:16
constexpr std::string_view color_debug
Definition log.h:19
Definition log.h:8
void debug(std::source_location loc=std::source_location::current())
Definition log.h:72
void perror(std::source_location loc=std::source_location::current())
Definition log.h:40
void info(std::format_string< Args... > fmt, Args &&... args)
Definition log.h:56
void debug(std::format_string< Args... > fmt, Args &&... args)
Definition log.h:64
void error(std::format_string< Args... > fmt, Args &&... args)
Definition log.h:32
void warn(std::format_string< Args... > fmt, Args &&... args)
Definition log.h:48
Definition file_descriptor.h:11