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