summaryrefslogtreecommitdiffhomepage
path: root/compat/warn.hpp
blob: 9b610d4a95d08ece21e39383005fc0847ddb6b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include "macros.hpp"

#include <sstream>
#include <iostream>
#include <locale>
#include <utility>

#include <string>

namespace warn_detail {
template<typename t> using basic_string_stream = std::basic_ostringstream<t, std::char_traits<t>, std::allocator<t>>;
using string_stream = basic_string_stream<wchar_t>;

force_inline void do_warn(string_stream&) {}

template<typename x, typename... xs>
force_inline void do_warn(string_stream& acc, const x& datum, const xs&... rest)
{
    acc << datum;
    if (sizeof...(rest) > 0u)
        acc << L' ';
    do_warn(acc, rest...);
}

template<typename... xs>
never_inline void warn_(const char* file, int line, const char* level, const xs&... seq)
{
    using namespace warn_detail;
    string_stream stream;

    do_warn(stream, seq...);

    std::wcerr << L'[' << level << L' '
               << file << L':' << line
               << L"] "
               << std::boolalpha
               << stream.str()
               << L'\n';
    std::wcerr.flush();
}

} // ns warn_detail

#define otr_impl_warn_base(level, ...) \
    (warn_detail::warn_(__FILE__, __LINE__, (level), __VA_ARGS__))

#define warn(...) \
    otr_impl_warn_base("WARN", __VA_ARGS__)

#define debug(...) \
    otr_impl_warn_base("DEBUG", __VA_ARGS__)

#define crit(...) \
    otr_impl_warn_base("CRIT", __VA_ARGS__)

#if 0
#include <utility>

#define fatal(...)                                  \
    do                                              \
    {                                               \
        otr_impl_warn_base("CRIT", __VA_ARGS__);    \
        *(volatile int*)0 = 0;                      \
        std::quick_exit(255);                       \
    } while (0)
#endif