summaryrefslogtreecommitdiff
path: root/hifi/xaf/host-apf/proxy/xf-trace.c
blob: fb902676234020d45f4a24109e3067339c510045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*******************************************************************************
* Copyright (C) 2018 Cadence Design Systems, Inc.
* 
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to use this Software with Cadence processor cores only and 
* not with any other processors and platforms, subject to
* the following conditions:
* 
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

******************************************************************************/

#include "xf.h"
#include <sys/time.h>

#if XF_TRACE
/*******************************************************************************
 * Local data definitions
 ******************************************************************************/

/* ...tracing lock */
static pthread_mutex_t  xf_trace_mutex;

/*******************************************************************************
 * Tracing facility
 ******************************************************************************/

/* ...timestamp function */
static u32 xf_timenow(void)
{
    struct timeval          tv;

    /* ...get current time value */
    gettimeofday(&tv, NULL);

    /* ...wrap over every 100 seconds */
    return (u32)((tv.tv_sec % 100) * 1000000 + tv.tv_usec);
}

/* ...tracing initialization */
void xf_trace_init(const char *banner)
{
    /* ...initialize tracing lock */
    pthread_mutex_init(&xf_trace_mutex, NULL);

    /* ...output banner */
    xf_trace(banner);
}

/* ...tracing primitive */
int xf_trace(const char *format, ...)
{
    va_list     args;
    static char buf[256];
    char       *b = buf;
    
    /* ...get global tracing lock */
    pthread_mutex_lock(&xf_trace_mutex);

    /* ...output timestamp */
    b += sprintf(b, "[%08u] ", xf_timenow());

    /* ...output format string */
    va_start(args, format);
    b += vsprintf(b, format, args);
    va_end(args);

    /* ...put terminator */
    *b = '\0';

    /* ...output prepared string */
    __xf_puts(buf);
    
    /* ...release tracing lock */
    pthread_mutex_unlock(&xf_trace_mutex);

    return 0;
}

#endif  /* XF_TRACE */