Michael Bestas | 58e7cab | 2023-05-12 04:05:32 +0300 | [diff] [blame] | 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 | #define LOG_NDEBUG 0 |
| 31 | |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <unistd.h> |
| 35 | #include <sys/time.h> |
| 36 | #include "log_util.h" |
| 37 | #include "loc_log.h" |
| 38 | #include "msg_q.h" |
| 39 | #include <loc_pla.h> |
| 40 | #include "LogBuffer.h" |
| 41 | |
| 42 | #define BUFFER_SIZE 120 |
| 43 | |
| 44 | // Logging Improvements |
| 45 | const char *loc_logger_boolStr[]={"False","True"}; |
| 46 | const char VOID_RET[] = "None"; |
| 47 | const char FROM_AFW[] = "===>"; |
| 48 | const char TO_MODEM[] = "--->"; |
| 49 | const char FROM_MODEM[] = "<---"; |
| 50 | const char TO_AFW[] = "<==="; |
| 51 | const char EXIT_TAG[] = "Exiting"; |
| 52 | const char ENTRY_TAG[] = "Entering"; |
| 53 | const char EXIT_ERROR_TAG[] = "Exiting with error"; |
| 54 | |
| 55 | const string gEmptyStr = ""; |
| 56 | const string gUnknownStr = "UNKNOWN"; |
| 57 | /* Logging Mechanism */ |
| 58 | loc_logger_s_type loc_logger; |
| 59 | |
| 60 | /* returns the least signification bit that is set in the mask |
| 61 | Param |
| 62 | mask - bit mask. |
| 63 | clearTheBit - if true, mask gets modified upon return. |
| 64 | returns 0 if mask is 0. |
| 65 | */ |
| 66 | uint64_t loc_get_least_bit(uint64_t& mask, bool clearTheBit) { |
| 67 | uint64_t bit = 0; |
| 68 | |
| 69 | if (mask > 0) { |
| 70 | uint64_t less1 = mask - 1; |
| 71 | bit = mask & ~(less1); |
| 72 | if (clearTheBit) { |
| 73 | mask &= less1; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return bit; |
| 78 | } |
| 79 | |
| 80 | string loc_get_bit_defs(uint64_t mask, const NameValTbl& tbl) { |
| 81 | string out; |
| 82 | while (mask > 0) { |
| 83 | out += loc_get_name_from_tbl(tbl, loc_get_least_bit(mask)); |
| 84 | if (mask > 0) { |
| 85 | out += " | "; |
| 86 | } |
| 87 | } |
| 88 | return out; |
| 89 | } |
| 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 | |
| 101 | /* Find msg_q status name */ |
| 102 | const char* loc_get_msg_q_status(int status) |
| 103 | { |
| 104 | return loc_get_name_from_val(loc_msg_q_status_tbl, (int64_t) status); |
| 105 | } |
| 106 | |
| 107 | //Target names |
| 108 | DECLARE_TBL(target_name) = |
| 109 | { |
| 110 | NAME_VAL(GNSS_NONE), |
| 111 | NAME_VAL(GNSS_MSM), |
| 112 | NAME_VAL(GNSS_GSS), |
| 113 | NAME_VAL(GNSS_MDM), |
| 114 | NAME_VAL(GNSS_AUTO), |
| 115 | NAME_VAL(GNSS_UNKNOWN) |
| 116 | }; |
| 117 | |
| 118 | /*=========================================================================== |
| 119 | |
| 120 | FUNCTION loc_get_target_name |
| 121 | |
| 122 | DESCRIPTION |
| 123 | Returns pointer to a string that contains name of the target |
| 124 | |
| 125 | XX:XX:XX.000\0 |
| 126 | |
| 127 | RETURN VALUE |
| 128 | The target name string |
| 129 | |
| 130 | ===========================================================================*/ |
| 131 | const char *loc_get_target_name(unsigned int target) |
| 132 | { |
| 133 | int64_t index = 0; |
| 134 | static char ret[BUFFER_SIZE]; |
| 135 | |
| 136 | snprintf(ret, sizeof(ret), " %s with%s SSC", |
| 137 | loc_get_name_from_val(target_name_tbl, getTargetGnssType(target)), |
| 138 | ((target & HAS_SSC) == HAS_SSC) ? gEmptyStr.c_str() : "out"); |
| 139 | |
| 140 | return ret; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | /*=========================================================================== |
| 145 | |
| 146 | FUNCTION loc_get_time |
| 147 | |
| 148 | DESCRIPTION |
| 149 | Logs a callback event header. |
| 150 | The pointer time_string should point to a buffer of at least 13 bytes: |
| 151 | |
| 152 | XX:XX:XX.000\0 |
| 153 | |
| 154 | RETURN VALUE |
| 155 | The time string |
| 156 | |
| 157 | ===========================================================================*/ |
| 158 | char *loc_get_time(char *time_string, size_t buf_size) |
| 159 | { |
| 160 | struct timeval now; /* sec and usec */ |
| 161 | struct tm now_tm; /* broken-down time */ |
| 162 | char hms_string[80]; /* HH:MM:SS */ |
| 163 | |
| 164 | gettimeofday(&now, NULL); |
| 165 | localtime_r(&now.tv_sec, &now_tm); |
| 166 | |
| 167 | strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm); |
| 168 | snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000)); |
| 169 | |
| 170 | return time_string; |
| 171 | } |
| 172 | |
| 173 | /*=========================================================================== |
| 174 | FUNCTION get_timestamp |
| 175 | |
| 176 | DESCRIPTION |
| 177 | Generates a timestamp using the current system time |
| 178 | |
| 179 | DEPENDENCIES |
| 180 | N/A |
| 181 | |
| 182 | RETURN VALUE |
| 183 | Char pointer to the parameter str |
| 184 | |
| 185 | SIDE EFFECTS |
| 186 | N/A |
| 187 | ===========================================================================*/ |
| 188 | char * get_timestamp(char *str, unsigned long buf_size) |
| 189 | { |
| 190 | struct timeval tv; |
| 191 | struct timezone tz; |
| 192 | int hh, mm, ss; |
| 193 | gettimeofday(&tv, &tz); |
| 194 | hh = tv.tv_sec/3600%24; |
| 195 | mm = (tv.tv_sec%3600)/60; |
| 196 | ss = tv.tv_sec%60; |
| 197 | snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec); |
| 198 | return str; |
| 199 | } |
| 200 | |
| 201 | /*=========================================================================== |
| 202 | |
| 203 | FUNCTION log_buffer_insert |
| 204 | |
| 205 | DESCRIPTION |
| 206 | Insert a log sentence with specific level to the log buffer. |
| 207 | |
| 208 | RETURN VALUE |
| 209 | N/A |
| 210 | |
| 211 | ===========================================================================*/ |
Michael Bestas | be7fc1a | 2021-01-06 19:23:51 +0200 | [diff] [blame] | 212 | void log_buffer_insert(char *str, unsigned long buf_size __unused, int level) |
Michael Bestas | 58e7cab | 2023-05-12 04:05:32 +0300 | [diff] [blame] | 213 | { |
| 214 | timespec tv; |
| 215 | clock_gettime(CLOCK_BOOTTIME, &tv); |
| 216 | uint64_t elapsedTime = (uint64_t)tv.tv_sec + (uint64_t)tv.tv_nsec/1000000000; |
| 217 | string ss = str; |
| 218 | loc_util::LogBuffer::getInstance()->append(ss, level, elapsedTime); |
| 219 | } |