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 2872358 : StatusWithSize ToString<CHIP_ERROR>(const CHIP_ERROR & err, pw::span<char> buffer)
22 : {
23 2872358 : if (CHIP_ERROR::IsSuccess(err))
24 : {
25 : // source location probably does not matter
26 1808133 : return pw::string::Format(buffer, "CHIP_NO_ERROR");
27 : }
28 1064225 : return pw::string::Format(buffer, "CHIP_ERROR:<%" CHIP_ERROR_FORMAT ">", err.Format());
29 : }
30 :
31 : template <>
32 329 : StatusWithSize ToString<std::chrono::duration<uint64_t, std::milli>>(const std::chrono::duration<uint64_t, std::milli> & time,
33 : pw::span<char> buffer)
34 : {
35 : // Cast to llu because some platforms use lu and others use llu.
36 329 : return pw::string::Format(buffer, "%llums", static_cast<long long unsigned int>(time.count()));
37 : }
38 :
39 : template <>
40 10 : StatusWithSize ToString<std::chrono::duration<uint64_t, std::micro>>(const std::chrono::duration<uint64_t, std::micro> & time,
41 : pw::span<char> buffer)
42 : {
43 : // Cast to llu because some platforms use lu and others use llu.
44 10 : return pw::string::Format(buffer, "%lluus", static_cast<long long unsigned int>(time.count()));
45 : }
46 :
47 : } // namespace pw
48 :
49 : #if CHIP_CONFIG_TEST_GOOGLETEST
50 : namespace chip {
51 :
52 : void PrintTo(const CHIP_ERROR & err, std::ostream * os)
53 : {
54 : if (CHIP_ERROR::IsSuccess(err))
55 : {
56 : *os << "CHIP_NO_ERROR";
57 : return;
58 : }
59 : *os << "CHIP_ERROR:<" << err.Format() << ">";
60 : }
61 :
62 : void PrintTo(const std::chrono::duration<uint64_t, std::milli> & time, std::ostream * os)
63 : {
64 : *os << time.count() << "ms";
65 : }
66 :
67 : void PrintTo(const std::chrono::duration<uint64_t, std::micro> & time, std::ostream * os)
68 : {
69 : *os << time.count() << "us";
70 : }
71 :
72 : } // namespace chip
73 : #endif // CHIP_CONFIG_TEST_GOOGLETEST
|