Home | History | Annotate | Download | only in proxy
      1 /*******************************************************************************
      2 * Copyright (C) 2018 Cadence Design Systems, Inc.
      3 *
      4 * Permission is hereby granted, free of charge, to any person obtaining
      5 * a copy of this software and associated documentation files (the
      6 * "Software"), to use this Software with Cadence processor cores only and
      7 * not with any other processors and platforms, subject to
      8 * the following conditions:
      9 *
     10 * The above copyright notice and this permission notice shall be included
     11 * in all copies or substantial portions of the Software.
     12 *
     13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     16 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
     17 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     18 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 ******************************************************************************/
     22 
     23 #include "xf.h"
     24 #include <sys/time.h>
     25 
     26 #if XF_TRACE
     27 /*******************************************************************************
     28  * Local data definitions
     29  ******************************************************************************/
     30 
     31 /* ...tracing lock */
     32 static pthread_mutex_t  xf_trace_mutex;
     33 
     34 /*******************************************************************************
     35  * Tracing facility
     36  ******************************************************************************/
     37 
     38 /* ...timestamp function */
     39 static u32 xf_timenow(void)
     40 {
     41     struct timeval          tv;
     42 
     43     /* ...get current time value */
     44     gettimeofday(&tv, NULL);
     45 
     46     /* ...wrap over every 100 seconds */
     47     return (u32)((tv.tv_sec % 100) * 1000000 + tv.tv_usec);
     48 }
     49 
     50 /* ...tracing initialization */
     51 void xf_trace_init(const char *banner)
     52 {
     53     /* ...initialize tracing lock */
     54     pthread_mutex_init(&xf_trace_mutex, NULL);
     55 
     56     /* ...output banner */
     57     xf_trace(banner);
     58 }
     59 
     60 /* ...tracing primitive */
     61 int xf_trace(const char *format, ...)
     62 {
     63     va_list     args;
     64     static char buf[256];
     65     char       *b = buf;
     66 
     67     /* ...get global tracing lock */
     68     pthread_mutex_lock(&xf_trace_mutex);
     69 
     70     /* ...output timestamp */
     71     b += sprintf(b, "[%08u] ", xf_timenow());
     72 
     73     /* ...output format string */
     74     va_start(args, format);
     75     b += vsprintf(b, format, args);
     76     va_end(args);
     77 
     78     /* ...put terminator */
     79     *b = '\0';
     80 
     81     /* ...output prepared string */
     82     __xf_puts(buf);
     83 
     84     /* ...release tracing lock */
     85     pthread_mutex_unlock(&xf_trace_mutex);
     86 
     87     return 0;
     88 }
     89 
     90 #endif  /* XF_TRACE */
     91