blob: e45e5416e48d1fc01d65520d1309278b8a542283 [file] [log] [blame]
Dan Stozaa79f1fa2016-10-19 16:11:29 -07001/*
2 * Copyright 2016 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#pragma clang diagnostic push
20#pragma clang diagnostic ignored "-Weverything"
21#include <fmt/format.h>
22#include <log/log.h>
23#pragma clang diagnostic pop
24
25#include <type_traits>
26
27namespace android {
28namespace gfx {
29
30/* SafeLog is a mix-in that can be used to easily add typesafe logging using fmtlib to any class.
31 * To use it, inherit from it using CRTP and implement the getLogTag method.
32 *
33 * For example:
34 *
35 * class Frobnicator : private SafeLog<Frobnicator> {
36 * friend class SafeLog<Frobnicator>; // Allows getLogTag to be private
37 *
38 * public:
39 * void frobnicate(int32_t i);
40 *
41 * private:
42 * // SafeLog does member access on the object calling alog*, so this tag can theoretically vary
43 * // by instance unless getLogTag is made static
44 * const char* getLogTag() { return "Frobnicator"; }
45 * };
46 *
47 * void Frobnicator::frobnicate(int32_t i) {
48 * // Logs something like "04-16 21:35:46.811 3513 3513 I Frobnicator: frobnicating 42"
49 * alogi("frobnicating {}", i);
50 * }
51 *
52 * See http://fmtlib.net for more information on the formatting API.
53 */
54
55template <typename T>
56class SafeLog {
57 protected:
58 template <typename... Args>
59#if LOG_NDEBUG
60 void alogv(Args&&... /*unused*/) const {
61 }
62#else
63 void alogv(Args&&... args) const {
64 alog<ANDROID_LOG_VERBOSE>(std::forward<Args>(args)...);
65 }
66#endif
67
68 template <typename... Args>
69 void alogd(Args&&... args) const {
70 alog<ANDROID_LOG_DEBUG>(std::forward<Args>(args)...);
71 }
72
73 template <typename... Args>
74 void alogi(Args&&... args) const {
75 alog<ANDROID_LOG_INFO>(std::forward<Args>(args)...);
76 }
77
78 template <typename... Args>
79 void alogw(Args&&... args) const {
80 alog<ANDROID_LOG_WARN>(std::forward<Args>(args)...);
81 }
82
83 template <typename... Args>
84 void aloge(Args&&... args) const {
85 alog<ANDROID_LOG_ERROR>(std::forward<Args>(args)...);
86 }
87
88 private:
89 // Suppresses clang-tidy check cppcoreguidelines-pro-bounds-array-to-pointer-decay
90 template <size_t strlen, typename... Args>
91 void write(fmt::MemoryWriter* writer, const char (&str)[strlen], Args&&... args) const {
92 writer->write(static_cast<const char*>(str), std::forward<Args>(args)...);
93 }
94
95 template <int priority, typename... Args>
96 void alog(Args&&... args) const {
97 static_assert(std::is_base_of<SafeLog<T>, T>::value, "Can't convert to SafeLog pointer");
98 fmt::MemoryWriter writer;
99 write(&writer, std::forward<Args>(args)...);
100 auto derivedThis = static_cast<const T*>(this);
101 android_writeLog(priority, derivedThis->getLogTag(), writer.c_str());
102 }
103};
104
105} // namespace gfx
106} // namespace android