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