1 /*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <fmt/core.h>
20 #include <fmt/format.h>
21 #include <fmt/printf.h>
22
23 #include <optional>
24
25 namespace rootcanal::log {
26
27 enum Verbosity {
28 kDebug,
29 kInfo,
30 kWarning,
31 kError,
32 kFatal,
33 };
34
35 void SetLogColorEnable(bool);
36
37 void VLog(Verbosity verb, char const* file, int line,
38 std::optional<int> instance, char const* format,
39 fmt::format_args args);
40
41 template <typename... Args>
Log(Verbosity verb,char const * file,int line,int instance,char const * format,const Args &...args)42 static void Log(Verbosity verb, char const* file, int line, int instance,
43 char const* format, const Args&... args) {
44 VLog(verb, file, line, instance, format, fmt::make_format_args(args...));
45 }
46
47 template <typename... Args>
Log(Verbosity verb,char const * file,int line,char const * format,const Args &...args)48 static void Log(Verbosity verb, char const* file, int line, char const* format,
49 const Args&... args) {
50 VLog(verb, file, line, {}, format, fmt::make_format_args(args...));
51 }
52
53 #define DEBUG(...) \
54 rootcanal::log::Log(rootcanal::log::Verbosity::kDebug, __FILE__, __LINE__, \
55 __VA_ARGS__)
56
57 #define INFO(...) \
58 rootcanal::log::Log(rootcanal::log::Verbosity::kInfo, __FILE__, __LINE__, \
59 __VA_ARGS__)
60
61 #define WARNING(...) \
62 rootcanal::log::Log(rootcanal::log::Verbosity::kWarning, __FILE__, __LINE__, \
63 __VA_ARGS__)
64
65 #define ERROR(...) \
66 rootcanal::log::Log(rootcanal::log::Verbosity::kError, __FILE__, __LINE__, \
67 __VA_ARGS__)
68
69 #define FATAL(...) \
70 rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
71 __VA_ARGS__)
72
73 #define ASSERT(x) \
74 __builtin_expect((x) != 0, true) || \
75 (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, \
76 __LINE__, "Check failed: {}", #x), \
77 false)
78
79 #define ASSERT_LOG(x, ...) \
80 __builtin_expect((x) != 0, true) || \
81 (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, \
82 __LINE__, "Check failed: {}, {}", #x, \
83 fmt::sprintf(__VA_ARGS__)), \
84 false)
85
86 } // namespace rootcanal::log
87