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, std::optional<int> instance,
38 char const* format, fmt::format_args args);
39
40 template <typename... Args>
Log(Verbosity verb,char const * file,int line,int instance,char const * format,const Args &...args)41 static void Log(Verbosity verb, char const* file, int line, int instance, char const* format,
42 const Args&... args) {
43 VLog(verb, file, line, instance, format, fmt::make_format_args(args...));
44 }
45
46 template <typename... Args>
Log(Verbosity verb,char const * file,int line,char const * format,const Args &...args)47 static void Log(Verbosity verb, char const* file, int line, char const* format,
48 const Args&... args) {
49 VLog(verb, file, line, {}, format, fmt::make_format_args(args...));
50 }
51
52 #define DEBUG(...) \
53 rootcanal::log::Log(rootcanal::log::Verbosity::kDebug, __FILE__, __LINE__, __VA_ARGS__)
54
55 #define INFO(...) \
56 rootcanal::log::Log(rootcanal::log::Verbosity::kInfo, __FILE__, __LINE__, __VA_ARGS__)
57
58 #define WARNING(...) \
59 rootcanal::log::Log(rootcanal::log::Verbosity::kWarning, __FILE__, __LINE__, __VA_ARGS__)
60
61 #define ERROR(...) \
62 rootcanal::log::Log(rootcanal::log::Verbosity::kError, __FILE__, __LINE__, __VA_ARGS__)
63
64 #define FATAL(...) \
65 rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, __VA_ARGS__)
66
67 #define ASSERT(x) \
68 __builtin_expect((x) != 0, true) || \
69 (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
70 "Check failed: {}", #x), \
71 false)
72
73 #define ASSERT_LOG(x, ...) \
74 __builtin_expect((x) != 0, true) || \
75 (rootcanal::log::Log(rootcanal::log::Verbosity::kFatal, __FILE__, __LINE__, \
76 "Check failed: {}, {}", #x, fmt::sprintf(__VA_ARGS__)), \
77 false)
78
79 } // namespace rootcanal::log
80