loc_log.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* Copyright (c) 2011-2012, 2015, 2020-2021 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. #define LOG_NDEBUG 0
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <sys/time.h>
  34. #include "log_util.h"
  35. #include "loc_log.h"
  36. #include "msg_q.h"
  37. #include <loc_pla.h>
  38. #include "LogBuffer.h"
  39. #include <unordered_map>
  40. #include <fstream>
  41. #include <algorithm>
  42. #include <string>
  43. #include <cctype>
  44. #define BUFFER_SIZE 120
  45. #define LOG_TAG_LEVEL_CONF_FILE_PATH "/data/vendor/location/gps.prop"
  46. // Logging Improvements
  47. const char *loc_logger_boolStr[]={"False","True"};
  48. const char VOID_RET[] = "None";
  49. const char FROM_AFW[] = "===>";
  50. const char TO_MODEM[] = "--->";
  51. const char FROM_MODEM[] = "<---";
  52. const char TO_AFW[] = "<===";
  53. const char EXIT_TAG[] = "Exiting";
  54. const char ENTRY_TAG[] = "Entering";
  55. const char EXIT_ERROR_TAG[] = "Exiting with error";
  56. int build_type_prop = BUILD_TYPE_PROP_NA;
  57. const string gEmptyStr = "";
  58. const string gUnknownStr = "UNKNOWN";
  59. /* Logging Mechanism */
  60. loc_logger_s_type loc_logger;
  61. /* tag base logging control map*/
  62. static std::unordered_map<std::string, uint8_t> tag_level_map;
  63. static bool tag_map_inited = false;
  64. /* returns the least signification bit that is set in the mask
  65. Param
  66. mask - bit mask.
  67. clearTheBit - if true, mask gets modified upon return.
  68. returns 0 if mask is 0.
  69. */
  70. uint64_t loc_get_least_bit(uint64_t& mask, bool clearTheBit) {
  71. uint64_t bit = 0;
  72. if (mask > 0) {
  73. uint64_t less1 = mask - 1;
  74. bit = mask & ~(less1);
  75. if (clearTheBit) {
  76. mask &= less1;
  77. }
  78. }
  79. return bit;
  80. }
  81. string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl) {
  82. string out;
  83. while (mask > 0) {
  84. out += loc_get_name_from_tbl(tbl, loc_get_least_bit(mask));
  85. if (mask > 0) {
  86. out += " | ";
  87. }
  88. }
  89. return out;
  90. }
  91. DECLARE_TBL(loc_msg_q_status) =
  92. {
  93. NAME_VAL( eMSG_Q_SUCCESS ),
  94. NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
  95. NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
  96. NAME_VAL( eMSG_Q_INVALID_HANDLE ),
  97. NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
  98. NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
  99. };
  100. /* Find msg_q status name */
  101. const char* loc_get_msg_q_status(int status)
  102. {
  103. return loc_get_name_from_val(loc_msg_q_status_tbl, (int64_t) status);
  104. }
  105. //Target names
  106. DECLARE_TBL(target_name) =
  107. {
  108. NAME_VAL(GNSS_NONE),
  109. NAME_VAL(GNSS_MSM),
  110. NAME_VAL(GNSS_GSS),
  111. NAME_VAL(GNSS_MDM),
  112. NAME_VAL(GNSS_AUTO),
  113. NAME_VAL(GNSS_UNKNOWN)
  114. };
  115. /*===========================================================================
  116. FUNCTION loc_get_target_name
  117. DESCRIPTION
  118. Returns pointer to a string that contains name of the target
  119. XX:XX:XX.000\0
  120. RETURN VALUE
  121. The target name string
  122. ===========================================================================*/
  123. const char *loc_get_target_name(unsigned int target)
  124. {
  125. static char ret[BUFFER_SIZE];
  126. snprintf(ret, sizeof(ret), " %s with%s SSC",
  127. loc_get_name_from_val(target_name_tbl, getTargetGnssType(target)),
  128. ((target & HAS_SSC) == HAS_SSC) ? gEmptyStr.c_str() : "out");
  129. return ret;
  130. }
  131. /*===========================================================================
  132. FUNCTION loc_get_time
  133. DESCRIPTION
  134. Logs a callback event header.
  135. The pointer time_string should point to a buffer of at least 13 bytes:
  136. XX:XX:XX.000\0
  137. RETURN VALUE
  138. The time string
  139. ===========================================================================*/
  140. char *loc_get_time(char *time_string, size_t buf_size)
  141. {
  142. struct timeval now; /* sec and usec */
  143. struct tm now_tm; /* broken-down time */
  144. char hms_string[80]; /* HH:MM:SS */
  145. gettimeofday(&now, NULL);
  146. localtime_r(&now.tv_sec, &now_tm);
  147. strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
  148. snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
  149. return time_string;
  150. }
  151. /*===========================================================================
  152. FUNCTION get_timestamp
  153. DESCRIPTION
  154. Generates a timestamp using the current system time
  155. DEPENDENCIES
  156. N/A
  157. RETURN VALUE
  158. Char pointer to the parameter str
  159. SIDE EFFECTS
  160. N/A
  161. ===========================================================================*/
  162. char * get_timestamp(char *str, unsigned long buf_size)
  163. {
  164. struct timeval tv;
  165. struct timezone tz;
  166. int hh, mm, ss;
  167. gettimeofday(&tv, &tz);
  168. hh = tv.tv_sec/3600%24;
  169. mm = (tv.tv_sec%3600)/60;
  170. ss = tv.tv_sec%60;
  171. snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
  172. return str;
  173. }
  174. /*===========================================================================
  175. FUNCTION log_buffer_insert
  176. DESCRIPTION
  177. Insert a log sentence with specific level to the log buffer.
  178. RETURN VALUE
  179. N/A
  180. ===========================================================================*/
  181. void log_buffer_insert(char *str, unsigned long buf_size __unused, int level)
  182. {
  183. timespec tv = {};
  184. clock_gettime(CLOCK_BOOTTIME, &tv);
  185. uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000;
  186. string ss = str;
  187. loc_util::LogBuffer::getInstance()->append(ss, level, elapsedTime);
  188. }
  189. void log_tag_level_map_init()
  190. {
  191. if (tag_map_inited) {
  192. return;
  193. }
  194. std::string filename = LOG_TAG_LEVEL_CONF_FILE_PATH;
  195. std::ifstream s(filename);
  196. if (!s.is_open()) {
  197. ALOGE("cannot open file:%s", LOG_TAG_LEVEL_CONF_FILE_PATH);
  198. } else {
  199. std::string line;
  200. while (std::getline(s, line)) {
  201. line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
  202. int pos = line.find('=');
  203. if (pos <= 0 || pos >= (line.size() - 1)) {
  204. ALOGE("wrong format in gps.prop");
  205. continue;
  206. }
  207. std::string tag = line.substr(0, pos);
  208. std::string level = line.substr(pos+1, 1);
  209. if (!std::isdigit(*(level.begin()))) {
  210. ALOGE("wrong format in gps.prop");
  211. continue;
  212. }
  213. tag_level_map[tag] = (uint8_t)std::stoul(level);
  214. }
  215. }
  216. tag_map_inited = true;
  217. }
  218. int get_tag_log_level(const char* tag)
  219. {
  220. if (!tag_map_inited) {
  221. return -1;
  222. }
  223. // in case LOG_TAG isn't defined in a source file, use the global log level
  224. if (tag == NULL) {
  225. return loc_logger.DEBUG_LEVEL;
  226. }
  227. int log_level;
  228. auto search = tag_level_map.find(std::string(tag));
  229. if (tag_level_map.end() != search) {
  230. log_level = search->second;
  231. } else {
  232. log_level = loc_logger.DEBUG_LEVEL;
  233. }
  234. return log_level;
  235. }