loc_log.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef LOC_LOG_H
  30. #define LOC_LOG_H
  31. #include <ctype.h>
  32. #include <stdlib.h>
  33. #include <string>
  34. #include <loc_pla.h>
  35. #include "loc_target.h"
  36. #include "loc_misc_utils.h"
  37. #ifdef NO_UNORDERED_SET_OR_MAP
  38. #include <map>
  39. using std::map;
  40. #define unordered_map map
  41. #else
  42. #include <unordered_map>
  43. using std::unordered_map;
  44. #endif
  45. using std::string;
  46. typedef unordered_map<int64_t, string> NameValTbl;
  47. #define NAME_VAL(x) {x, "" #x ""}
  48. #define DECLARE_TBL(T) static const NameValTbl T##_tbl
  49. extern const string gEmptyStr;
  50. extern const string gUnknownStr;
  51. #define CHECK_MASK(type, value, mask_var, mask) \
  52. (((mask_var) & (mask)) ? (type) (value) : (type) (-1))
  53. #define LOC_TABLE_SIZE(table) (sizeof(table)/sizeof((table)[0]))
  54. #define FIELDVAL_DEC(field) \
  55. loc_put_tag_val(#field, to_string(field))
  56. #define FIELDVAL_DEC_ARR(field) \
  57. loc_put_tag_val(#field, \
  58. loc_parenthesize(loc_prim_arr_to_string(field, \
  59. sizeof(field)/sizeof(field[0]))))
  60. #define FIELDVAL_HEX(field) \
  61. loc_put_tag_val(#field, to_string_hex(field))
  62. #define FIELDVAL_HEX_ARR(field) \
  63. loc_put_tag_val(#field, \
  64. loc_parenthesize(loc_prim_arr_to_string(field, \
  65. sizeof(field)/sizeof(field[0]), \
  66. false)))
  67. #define FIELDVAL_ENUM(field, tbl) \
  68. loc_put_tag_val(#field, \
  69. loc_get_name_from_tbl(tbl, field, gUnknownStr))
  70. #define FIELDVAL_MASK(field, tbl) \
  71. loc_put_tag_val(#field, \
  72. to_string_hex((uint64_t)field) + " " + \
  73. loc_parenthesize(loc_get_bit_defs(field, tbl)))
  74. /* get from a table of strings with index */
  75. /* tbl - map of <int, string> entries
  76. key - key to the matching entry
  77. defalt - default pointer in case of incorrect parameters
  78. */
  79. inline static const string& loc_get_name_from_tbl(const NameValTbl& tbl, int64_t key,
  80. const string& defalt = gEmptyStr) {
  81. auto item = tbl.find(key);
  82. if (item != tbl.end()) {
  83. return item->second;
  84. } else {
  85. return defalt;
  86. }
  87. }
  88. /* puts to string formatted "TAG: VAL" with option ending string, default to newline */
  89. inline string loc_put_tag_val(const string& tag, const string& val, const string& eol = "\n") {
  90. return tag + ": " + val + eol;
  91. }
  92. inline string loc_parenthesize(const string& str) {
  93. return "(" + str + ")";
  94. }
  95. /* Get names from value */
  96. inline const char* loc_get_name_from_val(const NameValTbl& table, int64_t value) {
  97. return loc_get_name_from_tbl(table, value, gUnknownStr).c_str();
  98. }
  99. inline const char* log_succ_fail_string(int is_succ) {
  100. return is_succ? "successful" : "failed";
  101. }
  102. /* prints mask into a string with bit definitions from tbl */
  103. /* mask - bit mask, to be expanded into " BIT_NAMEx | BIT_NAMEy ... "
  104. tbl - a table with defs for each bit, defined as <bit, name> entries
  105. {{bit0, "BIT0_NAME"}, {bit1, "BIT1_NAME"}, .... {bitn, "BITn_NAME"} }
  106. entries - number of strings in the table
  107. */
  108. string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl);
  109. uint64_t loc_get_least_bit(uint64_t& mask, bool clearThebit = true);
  110. const char* loc_get_msg_q_status(int status);
  111. const char* loc_get_target_name(unsigned int target);
  112. char *loc_get_time(char *time_string, size_t buf_size);
  113. #endif /* LOC_LOG_H */