1 /* Copyright (c) 2011-2012, 2015, 2020 The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation, nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef LOC_LOG_H
31 #define LOC_LOG_H
32 
33 #include <ctype.h>
34 #include <stdlib.h>
35 #include <unordered_map>
36 #include <string>
37 #include "loc_target.h"
38 #include "loc_misc_utils.h"
39 
40 using std::string;
41 using std::unordered_map;
42 
43 typedef unordered_map<int64_t, string> NameValTbl;
44 
45 #define NAME_VAL(x) {x, "" #x ""}
46 #define DECLARE_TBL(T) static const NameValTbl T##_tbl
47 
48 extern const string gEmptyStr;
49 extern const string gUnknownStr;
50 
51 #define CHECK_MASK(type, value, mask_var, mask) \
52    (((mask_var) & (mask)) ? (type) (value) : (type) (-1))
53 
54 #define LOC_TABLE_SIZE(table) (sizeof(table)/sizeof((table)[0]))
55 
56 #define FIELDVAL_DEC(field) \
57         loc_put_tag_val(#field, to_string(field))
58 #define FIELDVAL_DEC_ARR(field) \
59         loc_put_tag_val(#field, \
60                         loc_parenthesize(loc_prim_arr_to_string(field, \
61                                                                 sizeof(field)/sizeof(field[0]))))
62 #define FIELDVAL_HEX(field) \
63         loc_put_tag_val(#field, to_string_hex(field))
64 #define FIELDVAL_HEX_ARR(field) \
65         loc_put_tag_val(#field, \
66                         loc_parenthesize(loc_prim_arr_to_string(field, \
67                                                                 sizeof(field)/sizeof(field[0]), \
68                                                                 false)))
69 #define FIELDVAL_ENUM(field, tbl) \
70         loc_put_tag_val(#field, \
71                         loc_get_name_from_tbl(tbl, field, gUnknownStr))
72 #define FIELDVAL_MASK(field, tbl) \
73         loc_put_tag_val(#field, \
74                         to_string_hex((uint64_t)field) + " " + \
75                                 loc_parenthesize(loc_get_bit_defs(field, tbl)))
76 
77 /* get from a table of strings with index */
78 /* tbl - map of <int, string> entries
79    key - key to the matching entry
80    defalt - default pointer in case of incorrect parameters
81  */
82 inline static const string& loc_get_name_from_tbl(const NameValTbl& tbl, int64_t key,
83                                     const string& defalt = gEmptyStr) {
84     auto item = tbl.find(key);
85     if (item != tbl.end()) {
86         return item->second;
87     } else {
88         return defalt;
89     }
90 }
91 
92 /* puts to string formatted "TAG: VAL" with option ending string, default to newline */
93 inline string loc_put_tag_val(const string& tag, const string& val, const string& eol = "\n") {
94     return tag + ": " + val + eol;
95 }
96 
loc_parenthesize(const string & str)97 inline string loc_parenthesize(const string& str) {
98     return "(" + str + ")";
99 }
100 
101 /* Get names from value */
loc_get_name_from_val(const NameValTbl & table,int64_t value)102 inline const char* loc_get_name_from_val(const NameValTbl& table, int64_t value) {
103     return loc_get_name_from_tbl(table, value, gUnknownStr).c_str();
104 }
105 
log_succ_fail_string(int is_succ)106 inline const char* log_succ_fail_string(int is_succ) {
107     return is_succ? "successful" : "failed";
108 }
109 
110 /* prints mask into a string with bit definitions from tbl */
111 /* mask - bit mask, to be expanded into " BIT_NAMEx | BIT_NAMEy ... "
112    tbl - a table with defs for each bit, defined as <bit, name> entries
113          {{bit0, "BIT0_NAME"}, {bit1, "BIT1_NAME"}, .... {bitn, "BITn_NAME"} }
114    entries - number of strings in the table
115  */
116 string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl);
117 uint64_t loc_get_least_bit(uint64_t& mask, bool clearThebit = true);
118 const char* loc_get_msg_q_status(int status);
119 const char* loc_get_target_name(unsigned int target);
120 char *loc_get_time(char *time_string, size_t buf_size);
121 
122 #endif /* LOC_LOG_H */
123