OISEAU
A modern DGTD framework
Loading...
Searching...
No Matches
logging.hpp
1// Copyright (C) 2025 Tiago V. L. Amorim (@tiagovla)
2//
3// This file is part of oiseau (https://github.com/tiagovla/oiseau)
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6
7#pragma once
8
9#include <fmt/format.h>
10#include <spdlog/cfg/argv.h>
11#include <spdlog/common.h>
12#include <spdlog/fmt/bundled/base.h>
13#include <spdlog/sinks/stdout_color_sinks.h>
14#include <spdlog/spdlog.h>
15
16#include <sstream>
17#include <xtensor/containers/xarray.hpp>
18#include <xtensor/core/xtensor_forward.hpp>
19#include <xtensor/io/xio.hpp>
20
21#include "fmt/base.h"
22
23#undef SPDLOG_TRACE
24#undef SPDLOG_DEBUG
25#undef SPDLOG_INFO
26#undef SPDLOG_WARN
27#undef SPDLOG_ERROR
28#undef SPDLOG_CRITICAL
29
30namespace oiseau::logging {
31
32enum class Verbosity {
33 OFF = SPDLOG_LEVEL_OFF,
34 FATAL = SPDLOG_LEVEL_CRITICAL,
35 ERROR = SPDLOG_LEVEL_ERROR,
36 WARNING = SPDLOG_LEVEL_WARN,
37 INFO = SPDLOG_LEVEL_INFO,
38 DEBUG = SPDLOG_LEVEL_DEBUG,
39};
40
41void set_verbosity(Verbosity level);
42void init(int argc, char** argv);
43
44#define LOG_TRACE(...) \
45 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::trace, \
46 __VA_ARGS__)
47#define LOG_DEBUG(...) \
48 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::debug, \
49 __VA_ARGS__)
50#define LOG_INFO(...) \
51 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::info, \
52 __VA_ARGS__)
53#define LOG_WARN(...) \
54 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::warn, \
55 __VA_ARGS__)
56#define LOG_ERROR(...) \
57 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::err, \
58 __VA_ARGS__)
59#define LOG_FATAL(...) \
60 spdlog::log(spdlog::source_loc{__FILE__, __LINE__, SPDLOG_FUNCTION}, spdlog::level::critical, \
61 __VA_ARGS__)
62#define LOG_OFF(...) \
63 do { \
64 } while (false)
65
66} // namespace oiseau::logging
67
68template <typename T>
69class fmt::formatter<xt::xarray<T>> {
70 public:
71 constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
72 template <typename Context>
73 constexpr auto format(xt::xarray<T> const& arr, Context& ctx) const {
74 std::stringstream ss;
75 ss << arr;
76 return format_to(ctx.out(), "{}", ss.str());
77 }
78};