Home | History | Annotate | Download | only in utils
      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_NDEBUG 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_AUTO),
    113     NAME_VAL(GNSS_UNKNOWN)
    114 };
    115 
    116 static const size_t target_name_num = LOC_TABLE_SIZE(target_name);
    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     int index = 0;
    134     static char ret[BUFFER_SIZE];
    135 
    136     index =  getTargetGnssType(target);
    137     if( index < 0 || (unsigned)index >= target_name_num )
    138         index = target_name_num - 1;
    139 
    140     if( (target & HAS_SSC) == HAS_SSC ) {
    141         snprintf(ret, sizeof(ret), " %s with SSC",
    142            loc_get_name_from_val(target_name, target_name_num, (long)index) );
    143     }
    144     else {
    145        snprintf(ret, sizeof(ret), " %s  without SSC",
    146            loc_get_name_from_val(target_name, target_name_num, (long)index) );
    147     }
    148     return ret;
    149 }
    150 
    151 
    152 /*===========================================================================
    153 
    154 FUNCTION loc_get_time
    155 
    156 DESCRIPTION
    157    Logs a callback event header.
    158    The pointer time_string should point to a buffer of at least 13 bytes:
    159 
    160    XX:XX:XX.000\0
    161 
    162 RETURN VALUE
    163    The time string
    164 
    165 ===========================================================================*/
    166 char *loc_get_time(char *time_string, size_t buf_size)
    167 {
    168    struct timeval now;     /* sec and usec     */
    169    struct tm now_tm;       /* broken-down time */
    170    char hms_string[80];    /* HH:MM:SS         */
    171 
    172    gettimeofday(&now, NULL);
    173    localtime_r(&now.tv_sec, &now_tm);
    174 
    175    strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
    176    snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
    177 
    178    return time_string;
    179 }
    180 
    181 
    182 /*===========================================================================
    183 FUNCTION loc_logger_init
    184 
    185 DESCRIPTION
    186    Initializes the state of DEBUG_LEVEL and TIMESTAMP
    187 
    188 DEPENDENCIES
    189    N/A
    190 
    191 RETURN VALUE
    192    None
    193 
    194 SIDE EFFECTS
    195    N/A
    196 ===========================================================================*/
    197 void loc_logger_init(unsigned long debug, unsigned long timestamp)
    198 {
    199    loc_logger.DEBUG_LEVEL = debug;
    200 #ifdef TARGET_BUILD_VARIANT_USER
    201    // force user builds to 2 or less
    202    if (loc_logger.DEBUG_LEVEL > 2) {
    203        loc_logger.DEBUG_LEVEL = 2;
    204    }
    205 #endif
    206    loc_logger.TIMESTAMP   = timestamp;
    207 }
    208 
    209 
    210 /*===========================================================================
    211 FUNCTION get_timestamp
    212 
    213 DESCRIPTION
    214    Generates a timestamp using the current system time
    215 
    216 DEPENDENCIES
    217    N/A
    218 
    219 RETURN VALUE
    220    Char pointer to the parameter str
    221 
    222 SIDE EFFECTS
    223    N/A
    224 ===========================================================================*/
    225 char * get_timestamp(char *str, unsigned long buf_size)
    226 {
    227   struct timeval tv;
    228   struct timezone tz;
    229   int hh, mm, ss;
    230   gettimeofday(&tv, &tz);
    231   hh = tv.tv_sec/3600%24;
    232   mm = (tv.tv_sec%3600)/60;
    233   ss = tv.tv_sec%60;
    234   snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
    235   return str;
    236 }
    237 
    238