Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / logger.c
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // Defining _POSIX_C_SOURCE macro with 199309L (or greater) as value
22 // causes header files to expose definitions
23 // corresponding to the POSIX.1b, Real-time extensions
24 // (IEEE Std 1003.1b-1993) specification
25 //
26 // For this specific file, see use of clock_gettime,
27 // Refer to http://pubs.opengroup.org/stage7tc1/functions/clock_gettime.html
28 // and to http://man7.org/linux/man-pages/man2/clock_gettime.2.html
29 #ifndef _POSIX_C_SOURCE
30 #define _POSIX_C_SOURCE 200809L
31 #endif
32
33 // Platform check can be extended to check and/or define more, or could be
34 // moved into a config.h
35 #if !defined(__ARDUINO__) && !defined(ARDUINO)
36 #define HAVE_UNISTD_H 1
37 #endif
38
39 // Pull in _POSIX_TIMERS feature test macro to check for
40 // clock_gettime() support.
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43
44 // if we have unistd.h, we're a Unix system
45 #include <time.h>
46 #include <sys/time.h>
47 #endif
48
49 #include "logger.h"
50 #include "string.h"
51 #include "oic_logger.h"
52 #include "oic_console_logger.h"
53
54 #ifndef __TIZEN__
55 static oic_log_ctx_t *logCtx = 0;
56
57 static oic_log_level LEVEL_XTABLE[] =
58 { OIC_LOG_DEBUG, OIC_LOG_INFO, OIC_LOG_WARNING, OIC_LOG_ERROR, OIC_LOG_FATAL };
59
60 #endif
61
62 static const uint16_t LINE_BUFFER_SIZE = (16 * 2) + 16 +
63         1; // Show 16 bytes, 2 chars/byte, spaces between bytes, null termination
64
65 // Convert LogLevel to platform-specific severity level.  Store in PROGMEM on Arduino
66 #ifdef __ANDROID__
67 #ifdef ADB_SHELL
68 static const char *LEVEL[] =
69 {   "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
70
71 #else
72 static android_LogPriority LEVEL[] =
73 {   ANDROID_LOG_DEBUG, ANDROID_LOG_INFO, ANDROID_LOG_WARN, ANDROID_LOG_ERROR, ANDROID_LOG_FATAL};
74 #endif
75 #elif defined __linux__
76 static const char *LEVEL[] __attribute__ ((unused)) =
77 {   "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
78 #elif defined ARDUINO
79 #include <stdarg.h>
80 #include "Arduino.h"
81
82 PROGMEM const char level0[] = "DEBUG";
83 PROGMEM const char level1[] = "INFO";
84 PROGMEM const char level2[] = "WARNING";
85 PROGMEM const char level3[] = "ERROR";
86 PROGMEM const char level4[] = "FATAL";
87
88 PROGMEM const char *const LEVEL[] =
89 {   level0, level1, level2, level3, level4};
90
91 static void OICLogString(LogLevel level, PROGMEM const char *tag, PROGMEM const char *logStr);
92 #ifdef ARDUINO_ARCH_AVR
93 //Mega2560 and other 8-bit AVR microcontrollers
94 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_word(addr));}
95 #elif defined ARDUINO_ARCH_SAM
96 //Arduino Due and other 32-bit ARM micro-controllers
97 #define GET_PROGMEM_BUFFER(buffer, addr) { strcpy_P(buffer, (char*)pgm_read_dword(addr));}
98 #else
99 #define GET_PROGMEM_BUFFER(buffer, addr) { buffer[0] = '\0';}
100 #endif
101 #endif // __ANDROID__
102
103 #ifndef ARDUINO
104 #ifndef __TIZEN__
105 void OICLogConfig(oic_log_ctx_t *ctx)
106 {
107     logCtx = ctx;
108 }
109
110 void OICLogInit()
111 {
112
113 }
114
115 void OICLogShutdown()
116 {
117 #ifdef __linux__
118     if (logCtx && logCtx->destroy)
119     {
120         logCtx->destroy(logCtx);
121     }
122 #endif
123 }
124
125 /**
126  * Output a log string with the specified priority level.
127  * Only defined for Linux and Android
128  *
129  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
130  * @param tag    - Module name
131  * @param logStr - log string
132  */
133 void OICLog(LogLevel level, const char *tag, const char *logStr)
134 {
135     if (!logStr || !tag)
136     {
137         return;
138     }
139
140 #ifdef __ANDROID__
141
142 #ifdef ADB_SHELL
143     printf("%s: %s: %s\n", LEVEL[level], tag, logStr);
144 #else
145     __android_log_write(LEVEL[level], tag, logStr);
146 #endif
147
148 #elif defined __linux__
149     if (logCtx && logCtx->write_level)
150     {
151         logCtx->write_level(logCtx, LEVEL_XTABLE[level], logStr);
152
153     }
154     else
155     {
156         int min = 0;
157         int sec = 0;
158         int ms = 0;
159 #ifdef _POSIX_TIMERS
160         struct timespec when = {};
161         clockid_t clk = CLOCK_REALTIME;
162 #ifdef CLOCK_REALTIME_COARSE
163         clk = CLOCK_REALTIME_COARSE;
164 #endif
165         if (!clock_gettime(clk, &when))
166         {
167             min = (when.tv_sec / 60) % 60;
168             sec = when.tv_sec % 60;
169             ms = when.tv_nsec / 1000000;
170         }
171 #else
172         struct timeval now;
173         if (!gettimeofday(&now, NULL))
174         {
175             min = (now.tv_sec / 60) % 60;
176             sec = now.tv_sec % 60;
177             ms = now.tv_usec * 1000;
178         }
179 #endif
180         printf("%02d:%02d.%03d %s: %s: %s\n", min, sec, ms, LEVEL[level], tag, logStr);
181     }
182 #endif
183 }
184
185 /**
186  * Output a variable argument list log string with the specified priority level.
187  * Only defined for Linux and Android
188  *
189  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
190  * @param tag    - Module name
191  * @param format - variadic log string
192  */
193 void OICLogv(LogLevel level, const char *tag, const char *format, ...)
194 {
195     if (!format || !tag)
196     {
197         return;
198     }
199     char buffer[MAX_LOG_V_BUFFER_SIZE] = {};
200     va_list args;
201     va_start(args, format);
202     vsnprintf(buffer, sizeof buffer - 1, format, args);
203     va_end(args);
204     OICLog(level, tag, buffer);
205 }
206
207 /**
208  * Output the contents of the specified buffer (in hex) with the specified priority level.
209  *
210  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
211  * @param tag        - Module name
212  * @param buffer     - pointer to buffer of bytes
213  * @param bufferSize - max number of byte in buffer
214  */
215 void OICLogBuffer(LogLevel level, const char *tag, const uint8_t *buffer, uint16_t bufferSize)
216 {
217     if (!buffer || !tag || (bufferSize == 0))
218     {
219         return;
220     }
221
222     // I've got no idea why static initialization doesn't work here.  It seems that the compiler
223     // seems to think that this is a variable-sized object
224     char lineBuffer[LINE_BUFFER_SIZE];
225     memset(lineBuffer, 0, sizeof lineBuffer);
226     int lineIndex = 0;
227     int i;
228     for (i = 0; i < bufferSize; i++)
229     {
230         // Format the buffer data into a line
231         snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
232         lineIndex++;
233         // Output 16 values per line
234         if (((i + 1) % 16) == 0)
235         {
236             OICLog(level, tag, lineBuffer);
237             memset(lineBuffer, 0, sizeof lineBuffer);
238             lineIndex = 0;
239         }
240     }
241     // Output last values in the line, if any
242     if (bufferSize % 16)
243     {
244         OICLog(level, tag, lineBuffer);
245     }
246 }
247 #endif //__TIZEN__
248 #endif //ARDUINO
249 #ifdef ARDUINO
250 /**
251  * Initialize the serial logger for Arduino
252  * Only defined for Arduino
253  */
254 void OICLogInit()
255 {
256     Serial.begin(115200);
257 }
258
259 /**
260  * Output a log string with the specified priority level.
261  * Only defined for Arduino.  Only uses PROGMEM strings
262  * for the tag parameter
263  *
264  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
265  * @param tag    - Module name
266  * @param logStr - log string
267  */
268  void OICLogString(LogLevel level, PROGMEM const char * tag,
269          const char * logStr)
270  {
271      if (!logStr || !tag)
272      {
273          return;
274      }
275
276      char buffer[LINE_BUFFER_SIZE] = {};
277      strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
278      Serial.print(buffer);
279
280      char c = NULL;
281      Serial.print(F(": "));
282      while ((c = pgm_read_byte(tag)))
283      {
284          Serial.write(c);
285          tag++;
286      }
287      Serial.print(F(": "));
288
289      Serial.println(logStr);
290  }
291
292 /**
293  * Output the contents of the specified buffer (in hex) with the specified
294  * priority level.
295  *
296  * @param level      - DEBUG, INFO, WARNING, ERROR, FATAL
297  * @param tag        - Module name
298  * @param buffer     - pointer to buffer of bytes
299  * @param bufferSize - max number of byte in buffer
300  */
301  void OICLogBuffer(LogLevel level, PROGMEM const char * tag,
302          const uint8_t * buffer, uint16_t bufferSize)
303  {
304      if (!buffer || !tag || (bufferSize == 0))
305      {
306          return;
307      }
308
309      char lineBuffer[LINE_BUFFER_SIZE] = {0};
310      uint8_t lineIndex = 0;
311      for (uint8_t i = 0; i < bufferSize; i++)
312      {
313         // Format the buffer data into a line
314         snprintf(&lineBuffer[lineIndex*3], sizeof(lineBuffer)-lineIndex*3, "%02X ", buffer[i]);
315         lineIndex++;
316          // Output 16 values per line
317          if (((i+1)%16) == 0)
318          {
319              OICLogString(level, tag, lineBuffer);
320              memset(lineBuffer, 0, sizeof lineBuffer);
321              lineIndex = 0;
322          }
323      }
324      // Output last values in the line, if any
325      if (bufferSize % 16)
326      {
327          OICLogString(level, tag, lineBuffer);
328      }
329  }
330
331 /**
332  * Output a log string with the specified priority level.
333  * Only defined for Arduino.  Uses PROGMEM strings
334  *
335  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
336  * @param tag    - Module name
337  * @param logStr - log string
338  */
339 void OICLog(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
340               PROGMEM const char *logStr)
341 {
342     if (!logStr || !tag)
343     {
344         return;
345     }
346     char buffer[LINE_BUFFER_SIZE] = {0};
347     GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
348     Serial.print(buffer);
349     char c;
350     Serial.print(F(": "));
351     while ((c = pgm_read_byte(tag)))
352     {
353         Serial.write(c);
354         tag++;
355     }
356     Serial.print(F(": "));
357     Serial.print(lineNum);
358     Serial.print(F(": "));
359     while ((c = pgm_read_byte(logStr)))
360     {
361         Serial.write(c);
362         logStr++;
363     }
364     Serial.println();
365 }
366
367 /**
368  * Output a variable argument list log string with the specified priority level.
369  * Only defined for Arduino as depicted below.
370  *
371  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
372  * @param tag    - Module name
373  * @param format - variadic log string
374  */
375 void OICLogv(LogLevel level, PROGMEM const char *tag, const int16_t lineNum,
376                 PROGMEM const char *format, ...)
377 {
378     char buffer[LINE_BUFFER_SIZE];
379     va_list ap;
380     va_start(ap, format);
381     GET_PROGMEM_BUFFER(buffer, &(LEVEL[level]));
382     Serial.print(buffer);
383
384     char c;
385     Serial.print(F(": "));
386     while ((c = pgm_read_byte(tag))) {
387      Serial.write(c);
388      tag++;
389      }
390     Serial.print(F(": "));
391     Serial.print(lineNum);
392     Serial.print(F(": "));
393
394 #ifdef __AVR__
395     vsnprintf_P(buffer, sizeof(buffer), format, ap);
396 #else
397     vsnprintf(buffer, sizeof(buffer), format, ap);
398 #endif
399     for (char *p = &buffer[0]; *p; p++)
400     {
401         // emulate cooked mode for newlines
402         if (*p == '\n')
403         {
404             Serial.write('\r');
405         }
406         Serial.write(*p);
407     }
408     Serial.println();
409     va_end(ap);
410 }
411 /**
412  * Output a variable argument list log string with the specified priority level.
413  * Only defined for Arduino as depicted below.
414  *
415  * @param level  - DEBUG, INFO, WARNING, ERROR, FATAL
416  * @param tag    - Module name
417  * @param format - variadic log string
418  */
419 void OICLogv(LogLevel level, const char *tag, const __FlashStringHelper *format, ...)
420 {
421     char buffer[LINE_BUFFER_SIZE];
422     va_list ap;
423     va_start(ap, format);
424     // strcpy_P(buffer, (char*)pgm_read_word(&(LEVEL[level])));
425     // Serial.print(buffer);
426
427     Serial.print(LEVEL[level]);
428     // char c;
429     Serial.print(F(": "));
430
431     /*while ((c = pgm_read_byte(tag))) {
432      Serial.write(c);
433      tag++;
434      }*/
435     Serial.print(tag);
436     Serial.print(F(": "));
437
438 #ifdef __AVR__
439     vsnprintf_P(buffer, sizeof(buffer), (const char *)format, ap); // progmem for AVR
440 #else
441     vsnprintf(buffer, sizeof(buffer), (const char *)format, ap); // for the rest of the world
442 #endif
443     for (char *p = &buffer[0]; *p; p++)
444     {
445         // emulate cooked mode for newlines
446         if (*p == '\n')
447         {
448             // Serial.write('\r');
449             Serial.print('\r');
450         }
451         //Serial.write(*p);
452         Serial.print(p);
453     }
454     Serial.println();
455     va_end(ap);
456 }
457
458 #endif //ARDUINO
459