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