1/* Copyright (c) 2011 Code Aurora Forum. 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 Code Aurora Forum, Inc. 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
38#include "log_util.h"
39
40// Logging Improvements
41const char *loc_logger_boolStr[]={"False","True"};
42const char VOID_RET[]   = "None";
43const char FROM_AFW[]   = "===>";
44const char TO_MODEM[]   = "--->";
45const char FROM_MODEM[] = "<---";
46const char TO_AFW[]     = "<===";
47const char EXIT_TAG[]   = "Exiting";
48const char ENTRY_TAG[]  = "Entering";
49
50/* Logging Mechanism */
51loc_logger_s_type loc_logger;
52
53/* Get names from value */
54const char* loc_get_name_from_mask(loc_name_val_s_type table[], int table_size, long mask)
55{
56   int i;
57   for (i = 0; i < table_size; i++)
58   {
59      if (table[i].val & (long) mask)
60      {
61         return table[i].name;
62      }
63   }
64   return UNKNOWN_STR;
65}
66
67/* Get names from value */
68const char* loc_get_name_from_val(loc_name_val_s_type table[], int table_size, long value)
69{
70   int i;
71   for (i = 0; i < table_size; i++)
72   {
73      if (table[i].val == (long) value)
74      {
75         return table[i].name;
76      }
77   }
78   return UNKNOWN_STR;
79}
80
81static loc_name_val_s_type loc_msg_q_status[] =
82{
83    NAME_VAL( eMSG_Q_SUCCESS ),
84    NAME_VAL( eMSG_Q_FAILURE_GENERAL ),
85    NAME_VAL( eMSG_Q_INVALID_PARAMETER ),
86    NAME_VAL( eMSG_Q_INVALID_HANDLE ),
87    NAME_VAL( eMSG_Q_UNAVAILABLE_RESOURCE ),
88    NAME_VAL( eMSG_Q_INSUFFICIENT_BUFFER )
89};
90static int loc_msg_q_status_num = sizeof(loc_msg_q_status) / sizeof(loc_name_val_s_type);
91
92/* Find msg_q status name */
93const char* loc_get_msg_q_status(int status)
94{
95   return loc_get_name_from_val(loc_msg_q_status, loc_msg_q_status_num, (long) status);
96}
97
98const char* log_succ_fail_string(int is_succ)
99{
100   return is_succ? "successful" : "failed";
101}
102
103
104/*===========================================================================
105
106FUNCTION loc_get_time
107
108DESCRIPTION
109   Logs a callback event header.
110   The pointer time_string should point to a buffer of at least 13 bytes:
111
112   XX:XX:XX.000\0
113
114RETURN VALUE
115   The time string
116
117===========================================================================*/
118char *loc_get_time(char *time_string, unsigned long buf_size)
119{
120   struct timeval now;     /* sec and usec     */
121   struct tm now_tm;       /* broken-down time */
122   char hms_string[80];    /* HH:MM:SS         */
123
124   gettimeofday(&now, NULL);
125   localtime_r(&now.tv_sec, &now_tm);
126
127   strftime(hms_string, sizeof hms_string, "%H:%M:%S", &now_tm);
128   snprintf(time_string, buf_size, "%s.%03d", hms_string, (int) (now.tv_usec / 1000));
129
130   return time_string;
131}
132
133
134/*===========================================================================
135FUNCTION loc_logger_init
136
137DESCRIPTION
138   Initializes the state of DEBUG_LEVEL and TIMESTAMP
139
140DEPENDENCIES
141   N/A
142
143RETURN VALUE
144   None
145
146SIDE EFFECTS
147   N/A
148===========================================================================*/
149void loc_logger_init(unsigned long debug, unsigned long timestamp)
150{
151   loc_logger.DEBUG_LEVEL = debug;
152   loc_logger.TIMESTAMP   = timestamp;
153}
154
155
156/*===========================================================================
157FUNCTION get_timestamp
158
159DESCRIPTION
160   Generates a timestamp using the current system time
161
162DEPENDENCIES
163   N/A
164
165RETURN VALUE
166   Char pointer to the parameter str
167
168SIDE EFFECTS
169   N/A
170===========================================================================*/
171char * get_timestamp(char *str, unsigned long buf_size)
172{
173  struct timeval tv;
174  struct timezone tz;
175  int hh, mm, ss;
176  gettimeofday(&tv, &tz);
177  hh = tv.tv_sec/3600%24;
178  mm = (tv.tv_sec%3600)/60;
179  ss = tv.tv_sec%60;
180  snprintf(str, buf_size, "%02d:%02d:%02d.%06ld", hh, mm, ss, tv.tv_usec);
181  return str;
182}
183
184