using nonstd string_view enum class Log General Warning Error Alert De

 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
69
70
71
using nonstd::string_view;
enum class Log {
General,
Warning,
Error,
Alert,
Debug,
};
namespace detail {
constexpr string_view logGetColor(Log type) {
switch (type) {
case Log::General: return "\e[m";
case Log::Warning: return "\e[33m";
case Log::Error: return "\e[31m";
case Log::Alert: return "\e[41m";
case Log::Debug: return "\e[2m";
}
return "\e[m";
}
constexpr string_view trimFilePath(const char *filename) {
size_t length = 0;
for (; *filename; ++length, ++filename) {}
for (; length > 0 && *filename != '/'; --length, --filename) {}
if (!length) {
return filename;
}
if (*(filename-1) == '.') {
return filename + 1;
}
for (--filename, --length; length > 0 && *filename != '/'; --length, --filename) {}
return (!length)? filename : filename + 1;
}
}
#define DETAIL_STRINGIZE(x) DETAIL_STRINGIZE2(x)
#define DETAIL_STRINGIZE2(x) #x
#define LINE_STRING DETAIL_STRINGIZE(__LINE__)
#ifndef __HIP_DEVICE_COMPILE__
#define DETAIL_WRITE_LOG(_filename, _line, _type, s_type, _fmt, ...) do {\
static_assert(std::is_convertible<decltype(_filename), const char*>(), "_filename should be convertable to const char*"); \
static_assert(std::is_convertible<decltype(_line), const char*>(), "_line should be convertable to const char*"); \
static_assert(std::is_convertible<decltype(_type), Log>(), "_type should be convertable to Log"); \
static_assert(std::is_convertible<decltype(s_type), const char*>(), "s_type should be convertable to const char*"); \
static_assert(std::is_convertible<decltype(_fmt), const char*>(), "_fmt should be convertable to const char*"); \
static constexpr string_view view_filename = detail::trimFilePath(_filename);\
static constexpr auto color = detail::logGetColor(_type); \
static constexpr auto color_size = color.size(); \
static constexpr auto fmt_ = make_constexpr_string<color_size>(color.data()) + "HOST: %s " + make_constexpr_string(s_type) + ": " + make_constexpr_string<view_filename.size()>(view_filename.data()) + ":" + _line + ": " + _fmt + "\e[m" "\n"; \
time_t t;\
tm now;\
char time_str[256];\
std::time(&t);\
localtime_r(&t, &now);\
std::strftime(time_str, sizeof(time_str), "%Y-%m-%dT%H:%M:%S", &now);\
printf(fmt_.data(), time_str ,##__VA_ARGS__); \
} while(0)
#else
#define DETAIL_WRITE_LOG(_filename, _line, _type, s_type, _fmt, ...) do {\
static constexpr string_view view_filename = detail::trimFilePath(_filename);\
static constexpr auto color = detail::logGetColor(_type); \
static constexpr auto color_size = color.size(); \
static constexpr auto fmt_ = make_constexpr_string<color_size>(color.data()) + "DEVIDE: " + make_constexpr_string(s_type) + ": " + make_constexpr_string<view_filename.size()>(view_filename.data()) + ":" + _line + ": " + _fmt + "\e[m" "\n"; \
printf(fmt_.data() ,##__VA_ARGS__); \
} while(0)
#endif