summaryrefslogtreecommitdiffstats
path: root/runtime/print.c
blob: 8b0d267dc9f424b564233b6579e81ea4985e8a3c (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef _PRINT_C_ /* -*- linux-c -*- */
#define _PRINT_C_

#include <linux/config.h>

#include "io.c"

/** @file print.c
 * @addtogroup print Print Buffer
 * Print Buffer Functions.
 * The print buffer is for collecting output to send to the user daemon.
 * This is a per-cpu static buffer.  The buffer is sent when
 * _stp_print_flush() is called.
 * @{
 */

/** Size of buffer, not including terminating NULL */
#define STP_PRINT_BUF_LEN 8000

static int _stp_pbuf_len[NR_CPUS];

#ifdef STP_NETLINK_ONLY
#define STP_PRINT_BUF_START 0
static char _stp_pbuf[NR_CPUS][STP_PRINT_BUF_LEN + 1];

void _stp_print_flush (void)
{
	int cpu = smp_processor_id();
	char *buf = &_stp_pbuf[cpu][0];
	int len = _stp_pbuf_len[cpu];

	if (len == 0)
		return;

	if ( app.logging == 0) {
		_stp_pbuf_len[cpu] = 0;
		return;
	}
	
	/* enforce newline at end  */
	if (buf[len - 1] != '\n') {
		buf[len++] = '\n';
		buf[len] = '\0';
	}
	
	send_reply (STP_REALTIME_DATA, buf, len + 1, stpd_pid);
	_stp_pbuf_len[cpu] = 0;
}

#else /* ! STP_NETLINK_ONLY */
/* size of timestamp, in bytes, including space */
#define TIMESTAMP_SIZE 19
#define STP_PRINT_BUF_START (TIMESTAMP_SIZE + 1)
static char _stp_pbuf[NR_CPUS][STP_PRINT_BUF_LEN + STP_PRINT_BUF_START + 1];

void _stp_print_flush (void)
{
	int cpu = smp_processor_id();
	char *buf = &_stp_pbuf[cpu][0];
	char *ptr = buf + STP_PRINT_BUF_START;
	struct timeval tv;

	if (_stp_pbuf_len[cpu] == 0)
		return;
	
	/* enforce newline at end  */
	if (ptr[_stp_pbuf_len[cpu]-1] != '\n') {
		ptr[_stp_pbuf_len[cpu]++] = '\n';
		ptr[_stp_pbuf_len[cpu]] = '\0';
	}
	
	do_gettimeofday(&tv);
	scnprintf (buf, TIMESTAMP_SIZE+1, "[%li.%06li] ", tv.tv_sec, tv.tv_usec);
	buf[TIMESTAMP_SIZE] = ' ';
	relayapp_write(buf, _stp_pbuf_len[cpu] + TIMESTAMP_SIZE + 2);
	_stp_pbuf_len[cpu] = 0;
}
#endif /* STP_NETLINK_ONLY */

/** Sprint into the scratch buffer.
 * Like printf, except output goes into a global scratch buffer
 * which will contain the null-terminated output.
 * Safe because overflowing the buffer is not allowed.
 * Size is limited by length of scratch buffer, STP_BUF_LEN.
 *
 * @param fmt A printf-style format string followed by a 
 * variable number of args.
 * @sa _stp_pbuf_clear
 */

void _stp_printf (const char *fmt, ...)
{
	int num;
	va_list args;
	int cpu = smp_processor_id();
	char *buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
	va_start(args, fmt);
	num = vscnprintf(buf, STP_PRINT_BUF_LEN - _stp_pbuf_len[cpu], fmt, args);
	va_end(args);
	if (num > 0)
		_stp_pbuf_len[cpu] += num;
}

void _stp_vprintf (const char *fmt, va_list args)
{
	int num;
	int cpu = smp_processor_id();
	char *buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
	num = vscnprintf(buf, STP_PRINT_BUF_LEN -_stp_pbuf_len[cpu], fmt, args);
	if (num > 0)
		_stp_pbuf_len[cpu] += num;
}

/** Write a string into the scratch buffer.
 * Copies a string into a global scratch buffer.
 * Safe because overflowing the buffer is not allowed.
 * Size is limited by length of scratch buffer, STP_BUF_LEN.
 * This is more efficient than using _stp_sprint().
 *
 * @param str A string.
 */

void _stp_print_cstr (const char *str)
{
	int cpu = smp_processor_id();
	char *buf = &_stp_pbuf[cpu][STP_PRINT_BUF_START] + _stp_pbuf_len[cpu];
	int num = strlen (str);
	if (num > STP_PRINT_BUF_LEN - _stp_pbuf_len[cpu])
		num = STP_PRINT_BUF_LEN - _stp_pbuf_len[cpu];
	strncpy (buf, str, num+1);
	_stp_pbuf_len[cpu] += num;
}

/** Clear the scratch buffer.
 * This function should be called before anything is written to 
 * the scratch buffer.  Output will accumulate in the buffer
 * until this function is called again.  
 * @returns A pointer to the buffer.
 */

char *_stp_print_clear (void)
{
	int cpu = smp_processor_id();
	_stp_pbuf_len[cpu] = 0;
	return &_stp_pbuf[cpu][STP_PRINT_BUF_START];
}

#include "string.c"

void _stp_print_string (String str)
{
	if (str->len)
		_stp_print_cstr (str->buf);
}

#define _stp_print(str)							\
	({								\
	  if (__builtin_types_compatible_p (typeof (str), char[])) {	\
		  char *x = (char *)str;				\
		  _stp_print_cstr(x);					\
	  } else {							\
		  String x = (String)str;				\
		  _stp_print_string(x);					\
	  }								\
  })

/** @} */
#endif /* _PRINT_C_ */