VIPRA Documentation
Loading...
Searching...
No Matches
logging.hpp
1
2#pragma once
3
4#include <algorithm>
5#include <cctype>
6#include <iostream>
7#include <string>
8#include <unordered_map>
9
10#define FMT_HEADER_ONLY
11#include <fmt/format.h>
12#include <fmt/ranges.h>
13
14#include "vipra/macros/performance.hpp"
15
16namespace VIPRA {
17
18class Log {
19 public:
20 enum class Level {
21 DEBUG,
22 INFO,
23 WARN,
24 ERROR,
25 };
26
27 static void set_level(Level lvl) { level = lvl; }
28 static void set_level(std::string lvl)
29 {
30 static const std::unordered_map<std::string, Level> LEVELS = {
31 {"debug", Level::DEBUG},
32 {"info", Level::INFO},
33 {"warn", Level::WARN},
34 {"error", Level::ERROR},
35 };
36
37 std::transform(lvl.begin(), lvl.end(), lvl.begin(),
38 [](char letter) { return tolower(letter); });
39
40 level = LEVELS.at(lvl);
41 }
42
50 template <typename... param_ts>
51 VIPRA_INLINE static void warn(fmt::format_string<param_ts...> message,
52 param_ts&&... params)
53 {
54 if ( level <= Level::WARN ) {
55 std::cout << "[WARN] " << fmt::format(message, std::forward<param_ts>(params)...)
56 << '\n';
57 }
58 }
59
67 template <typename... param_ts>
68 VIPRA_INLINE static void info(fmt::format_string<param_ts...> message,
69 param_ts&&... params)
70 {
71 if ( level <= Level::INFO ) {
72 std::cout << "[INFO] " << fmt::format(message, std::forward<param_ts>(params)...)
73 << '\n';
74 }
75 }
76
84 template <typename... param_ts>
85 VIPRA_INLINE static void debug(fmt::format_string<param_ts...> message,
86 param_ts&&... params)
87 {
88 if ( level <= Level::DEBUG ) {
89 std::cout << "[DEBUG] " << fmt::format(message, std::forward<param_ts>(params)...)
90 << '\n';
91 }
92 }
93
101 template <typename... param_ts>
102 VIPRA_INLINE static void error(fmt::format_string<param_ts...> message,
103 param_ts&&... params)
104 {
105 std::cout << "[ERROR] " << fmt::format(message, std::forward<param_ts>(params)...)
106 << '\n';
107 }
108
109 private:
110 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
111 static Level level;
112};
113} // namespace VIPRA
Definition logging.hpp:18
static VIPRA_INLINE void error(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:102
static VIPRA_INLINE void warn(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level WARN.
Definition logging.hpp:51
static VIPRA_INLINE void debug(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:85
static VIPRA_INLINE void info(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level INFO.
Definition logging.hpp:68