Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2024 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <type_traits>
21 : #include <utility>
22 :
23 : namespace chip {
24 :
25 : /**
26 : * A dumpable object that can log some useful state for debugging in fatal
27 : * error scenarios by exposing a `void DumpToLog() const` method. The method
28 : * should log key details about the state of object using ChipLogError().
29 : */
30 : template <class, class = void>
31 : struct IsDumpable : std::false_type
32 : {
33 : };
34 : template <class T>
35 : struct IsDumpable<T, std::void_t<decltype(std::declval<T>().DumpToLog())>> : std::true_type
36 : {
37 : };
38 :
39 : struct DumpableTypeExample
40 : {
41 : void DumpToLog() const {};
42 : };
43 : static_assert(IsDumpable<DumpableTypeExample>::value);
44 :
45 : /**
46 : * Calls DumpToLog() on the object, if supported.
47 : */
48 : template <class T>
49 0 : void DumpObjectToLog([[maybe_unused]] const T * object)
50 : {
51 : if constexpr (IsDumpable<T>::value)
52 : {
53 0 : object->DumpToLog();
54 : }
55 0 : }
56 :
57 : } // namespace chip
|