blob: b750932da8102172dafebff5dfad874bef36528f [file] [log] [blame]
Kevin Tangae2fb7c2020-02-01 22:55:15 -08001/* Copyright (c) 2011-2012, 2015, 2020 The Linux Foundation. All rights reserved.
Ajay Dudanif77c85b2012-07-09 15:27:30 -07002 *
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.
Dante Russo85f672f2013-06-05 09:11:09 -070012 * * Neither the name of The Linux Foundation, nor the names of its
Ajay Dudanif77c85b2012-07-09 15:27:30 -070013 * 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
Ajay Dudanif77c85b2012-07-09 15:27:30 -070033#include <ctype.h>
Valeri Atamaniouk78bb1322015-10-21 18:53:58 +030034#include <stdlib.h>
Kevin Tangae2fb7c2020-02-01 22:55:15 -080035#include <unordered_map>
36#include <string>
Dante Russo85f672f2013-06-05 09:11:09 -070037#include "loc_target.h"
Kevin Tangae2fb7c2020-02-01 22:55:15 -080038#include "loc_misc_utils.h"
Ajay Dudanif77c85b2012-07-09 15:27:30 -070039
Kevin Tangae2fb7c2020-02-01 22:55:15 -080040using std::string;
41using std::unordered_map;
Ajay Dudanif77c85b2012-07-09 15:27:30 -070042
Kevin Tangae2fb7c2020-02-01 22:55:15 -080043typedef unordered_map<int64_t, string> NameValTbl;
44
45#define NAME_VAL(x) {x, "" #x ""}
46#define DECLARE_TBL(T) static const NameValTbl T##_tbl
Ajay Dudanif77c85b2012-07-09 15:27:30 -070047
Saurabh Srivastava63fec832020-05-02 01:17:46 +053048extern const string gEmptyStr;
49extern const string gUnknownStr;
Ajay Dudanif77c85b2012-07-09 15:27:30 -070050
51#define CHECK_MASK(type, value, mask_var, mask) \
Valeri Atamaniouk78bb1322015-10-21 18:53:58 +030052 (((mask_var) & (mask)) ? (type) (value) : (type) (-1))
53
54#define LOC_TABLE_SIZE(table) (sizeof(table)/sizeof((table)[0]))
Ajay Dudanif77c85b2012-07-09 15:27:30 -070055
Kevin Tangae2fb7c2020-02-01 22:55:15 -080056#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, \
Saurabh Srivastava63fec832020-05-02 01:17:46 +053071 loc_get_name_from_tbl(tbl, field, gUnknownStr))
Kevin Tangae2fb7c2020-02-01 22:55:15 -080072#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 */
Saurabh Srivastava63fec832020-05-02 01:17:46 +053082inline static const string& loc_get_name_from_tbl(const NameValTbl& tbl, int64_t key,
83 const string& defalt = gEmptyStr) {
Kevin Tangae2fb7c2020-02-01 22:55:15 -080084 auto item = tbl.find(key);
85 if (item != tbl.end()) {
86 return item->second;
87 } else {
Saurabh Srivastava63fec832020-05-02 01:17:46 +053088 return defalt;
Kevin Tangae2fb7c2020-02-01 22:55:15 -080089 }
90}
91
92/* puts to string formatted "TAG: VAL" with option ending string, default to newline */
93inline string loc_put_tag_val(const string& tag, const string& val, const string& eol = "\n") {
94 return tag + ": " + val + eol;
95}
96
97inline string loc_parenthesize(const string& str) {
98 return "(" + str + ")";
99}
100
Ajay Dudanif77c85b2012-07-09 15:27:30 -0700101/* Get names from value */
Kevin Tangae2fb7c2020-02-01 22:55:15 -0800102inline const char* loc_get_name_from_val(const NameValTbl& table, int64_t value) {
Saurabh Srivastava63fec832020-05-02 01:17:46 +0530103 return loc_get_name_from_tbl(table, value, gUnknownStr).c_str();
Kevin Tangae2fb7c2020-02-01 22:55:15 -0800104}
105
106inline 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 */
116string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl);
117uint64_t loc_get_least_bit(uint64_t& mask, bool clearThebit = true);
Ajay Dudanif77c85b2012-07-09 15:27:30 -0700118const char* loc_get_msg_q_status(int status);
Tushar Janefalkare5e62722013-07-30 13:34:25 -0700119const char* loc_get_target_name(unsigned int target);
Kevin Tangae2fb7c2020-02-01 22:55:15 -0800120char *loc_get_time(char *time_string, size_t buf_size);
Ajay Dudanif77c85b2012-07-09 15:27:30 -0700121
122#endif /* LOC_LOG_H */