Line data Source code
1 : /*
2 : * Copyright (c) 2024 Project CHIP Authors
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 : #include <lib/core/StringBuilderAdapters.h>
17 :
18 : namespace pw {
19 :
20 : template <>
21 0 : StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer)
22 : {
23 0 : if (CHIP_ERROR::IsSuccess(err))
24 : {
25 : // source location probably does not matter
26 0 : return pw::string::Format(buffer, "CHIP_NO_ERROR");
27 : }
28 0 : return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format());
29 : }
30 :
31 : template <>
32 0 : StatusWithSize ToString<chip::CriticalFailure>(const chip::CriticalFailure & err, pw::span<char> buffer)
33 : {
34 0 : return ToString<CHIP_ERROR>(err.GetError(), buffer);
35 : }
36 :
37 : template <>
38 0 : StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
39 : pw::span<char> buffer)
40 : {
41 : // Cast to llu because some platforms use lu and others use llu.
42 0 : return pw::string::Format(buffer, "%llums", static_cast<long long unsigned int>(time.count()));
43 : }
44 :
45 : template <>
46 0 : StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
47 : pw::span<char> buffer)
48 : {
49 : // Cast to llu because some platforms use lu and others use llu.
50 0 : return pw::string::Format(buffer, "%lluus", static_cast<long long unsigned int>(time.count()));
51 : }
52 :
53 : } // namespace pw
54 :
55 : #if CHIP_CONFIG_TEST_GOOGLETEST
56 : namespace chip {
57 :
58 : void PrintTo(const CHIP_ERROR & err, std::ostream * os)
59 : {
60 : if (CHIP_ERROR::IsSuccess(err))
61 : {
62 : *os << "CHIP_NO_ERROR";
63 : return;
64 : }
65 : *os << "CHIP_ERROR:<" << err.Format() << ">";
66 : }
67 :
68 : void PrintTo(const CriticalFailure & err, std::ostream * os)
69 : {
70 : PrintTo(err.GetError(), os);
71 : }
72 :
73 : void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os)
74 : {
75 : *os << time.count() << "ms";
76 : }
77 :
78 : void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os)
79 : {
80 : *os << time.count() << "us";
81 : }
82 :
83 : } // namespace chip
84 : #endif // CHIP_CONFIG_TEST_GOOGLETEST
|