1 /******************************************************************************
2 *
3 * Copyright 1999-2012 Broadcom Corporation
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
19 #pragma once
20
21 #include <stdint.h>
22
23 #ifdef __cplusplus
24
25 #include <bluetooth/log.h>
26
27 #include <array>
28 #include <iomanip>
29 #include <sstream>
30 #include <type_traits>
31
32 /* Prints integral parameter x as hex string, with '0' fill */
33 template <typename T>
loghex(T x)34 std::string loghex(T x) {
35 static_assert(std::is_integral<T>::value, "loghex parameter must be integral.");
36 std::stringstream tmp;
37 tmp << std::showbase << std::internal << std::hex << std::setfill('0')
38 << std::setw((sizeof(T) * 2) + 2) << +x;
39 return tmp.str();
40 }
41
42 /* Prints integral array as hex string, with '0' fill */
43 template <typename T, size_t N>
loghex(std::array<T,N> array)44 std::string loghex(std::array<T, N> array) {
45 static_assert(std::is_integral<T>::value, "type stored in array must be integral.");
46 std::stringstream tmp;
47 for (const auto& x : array) {
48 tmp << std::internal << std::hex << std::setfill('0') << std::setw((sizeof(uint8_t) * 2) + 2)
49 << +x;
50 }
51 return tmp.str();
52 }
53
54 /**
55 * Append a field name to a string.
56 *
57 * The field names are added to the string with "|" in between.
58 *
59 * @param p_result a pointer to the result string to add the field name to
60 * @param append if true the field name will be added
61 * @param name the field name to add
62 * @return the result string
63 */
AppendField(std::string * p_result,bool append,const std::string & name)64 inline std::string& AppendField(std::string* p_result, bool append, const std::string& name) {
65 bluetooth::log::assert_that(p_result != nullptr, "assert failed: p_result != nullptr");
66 if (!append) {
67 return *p_result;
68 }
69 if (!p_result->empty()) {
70 *p_result += "|";
71 }
72 *p_result += name;
73 return *p_result;
74 }
75
76 #endif // __cplusplus
77