1 /* Copyright (c) 2011-2012, 2015, 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 #define LOG_NDDEBUG 0 31 32 #include <stdio.h> 33 #include <stdlib.h> 34 #include <sys/time.h> 35 #include "loc_log.h" 36 #include "msg_q.h" 37 #include <platform_lib_includes.h> 38 39 #define BUFFER_SIZE 120 40 41 // Logging Improvements 42 const char *loc_logger_boolStr[]={"False","True"}; 43 const char VOID_RET[] = "None"; 44 const char FROM_AFW[] = "===>"; 45 const char TO_MODEM[] = "--->"; 46 const char FROM_MODEM[] = "<---"; 47 const char TO_AFW[] = "<==="; 48 const char EXIT_TAG[] = "Exiting"; 49 const char ENTRY_TAG[] = "Entering"; 50 const char EXIT_ERROR_TAG[] = "Exiting with error"; 51 52 /* Logging Mechanism */ 53 loc_logger_s_type loc_logger; 54 55 /* Get names from value */ 56 const char* loc_get_name_from_mask(const loc_name_val_s_type table[], size_t table_size, long mask) 57 { 58 size_t i; 59 for (i = 0; i < table_size; i++) 60 { 61 if (table[i].val & (long) mask) 62 { 63 return table[i].name; 64 } 65 } 66 return UNKNOWN_STR; 67 } 68 69 /* Get names from value */ 70 const char* loc_get_name_from_val(const loc_name_val_s_type table[], size_t table_size, long value) 71 { 72 size_t i; 73 for (i = 0; i < table_size; i++) 74 { 75 if (table[i].val == (long) value) 76 { 77 return table[i].name; 78 } 79 } 80 return UNKNOWN_STR; 81 } 82 83 static const loc_name_val_s_type loc_msg_q_status[] = 84 { 85 NAME_VAL( eMSG_Q_SUCCESS ), 86 NAME_VAL( eMSG_Q_FAILURE_GENERAL ), 87 NAME_VAL( eMSG_Q_INVALID_PARAMETER ), 88 NAME_VAL( eMSG_Q_INVALID_HANDLE ), 89 NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ), 90 NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER ) 91 }; 92 static const size_t loc_msg_q_status_num = LOC_TABLE_SIZE(loc_msg_q_status); 93 94 /* Find msg_q status name */ 95 const char* loc_get_msg_q_status(int status) 96 { 97 return loc_get_name_from_val(loc_msg_q_status, loc_msg_q_status_num, (long) status); 98 } 99 100 const char* log_succ_fail_string(int is_succ) 101 { 102 return is_succ? "successful" : "failed"; 103 } 104 105 //Target names 106 static const loc_name_val_s_type 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_QCA1530), 113 NAME_VAL(GNSS_AUTO), 114 NAME_VAL(GNSS_UNKNOWN) 115 }; 116 117 static const size_t target_name_num = LOC_TABLE_SIZE(target_name); 118 119 /*=========================================================================== 120 121 FUNCTION loc_get_target_name 122 123 DESCRIPTION 124 Returns pointer to a string that contains name of the target 125 126 XX:XX:XX.000\0 127 128 RETURN VALUE 129 The target name string 130 131 ===========================================================================*/ 132 const char *loc_get_target_name(unsigned int target) 133 { 134 int index = 0; 135 static char ret[BUFFER_SIZE]; 136 137 index = getTargetGnssType(target); 138 if( index < 0 || (unsigned)index >= target_name_num ) 139 index = target_name_num - 1; 140 141 if( (target & HAS_SSC) == HAS_SSC ) { 142 snprintf(ret, sizeof(ret), " %s with SSC", 143 loc_get_name_from_val(target_name, target_name_num, (long)index) ); 144 } 145 else { 146 snprintf(ret, sizeof(ret), " %s without SSC", 147 loc_get_name_from_val(target_name, target_name_num, (long)index) ); 148 } 149 return ret; 150 } 151 152 153 /*=========================================================================== 154 155 FUNCTION loc_get_time 156 157 DESCRIPTION 158 Logs a callback event header. 159 The pointer time_string should point to a buffer of at least 13 bytes: 160 161 XX:XX:XX.000\0 162 163 RETURN VALUE 164 The time string 165 166 ===========================================================================*/ 167 char *loc_get_time(char *time_string, size_t buf_size) 168 { 169 struct timeval now; /* sec and usec */ 170 struct tm now_tm; /* broken-down time */ 171 char hms_string[80]; /* HH:MM:SS */ 172 173 gettimeofday(&now, NULL); 174 localtime_r(&now.tv_sec, &now_tm); 175 176 strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm); 177 snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000)); 178 179 return time_string; 180 } 181 182 183 /*=========================================================================== 184 FUNCTION loc_logger_init 185 186 DESCRIPTION 187 Initializes the state of DEBUG_LEVEL and TIMESTAMP 188 189 DEPENDENCIES 190 N/A 191 192 RETURN VALUE 193 None 194 195 SIDE EFFECTS 196 N/A 197 ===========================================================================*/ 198 void loc_logger_init(unsigned long debug, unsigned long timestamp) 199 { 200 loc_logger.DEBUG_LEVEL = debug; 201 #ifdef TARGET_BUILD_VARIANT_USER 202 // force user builds to 2 or less 203 if (loc_logger.DEBUG_LEVEL > 2) { 204 loc_logger.DEBUG_LEVEL = 2; 205 } 206 #endif 207 loc_logger.TIMESTAMP = timestamp; 208 } 209 210 211 /*=========================================================================== 212 FUNCTION get_timestamp 213 214 DESCRIPTION 215 Generates a timestamp using the current system time 216 217 DEPENDENCIES 218 N/A 219 220 RETURN VALUE 221 Char pointer to the parameter str 222 223 SIDE EFFECTS 224 N/A 225 ===========================================================================*/ 226 char * get_timestamp(char *str, unsigned long buf_size) 227 { 228 struct timeval tv; 229 struct timezone tz; 230 int hh, mm, ss; 231 gettimeofday(&tv, &tz); 232 hh = tv.tv_sec/3600%24; 233 mm = (tv.tv_sec%3600)/60; 234 ss = tv.tv_sec%60; 235 snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec); 236 return str; 237 } 238 239