summaryrefslogtreecommitdiffstats
path: root/runtime/user/print.c
blob: 45c76f5a4a0097ff9434003518df11ebd707f481 (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
#ifndef _PRINT_C_ /* -*- linux-c -*- */
#define _PRINT_C_

#include <linux/config.h>
#include <stdio.h>

/** @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.
 *
 * The reason to do this is to allow multiple small prints to be combined then
 * timestamped and sent together to stpd. It could flush automatically on newlines,
 * but what about stack traces which span many lines?  So try this and see how it works for us.
 * @{
 */

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

static int _stp_pbuf_len[NR_CPUS];

#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;

	fwrite (buf, len, 1, stdout);
	_stp_pbuf_len[cpu] = 0;
}

#define _stp_printf(args...) _stp_sprintf(_stp_stdout,args)
#define _stp_vprintf(fmt,args) _stp_vsprintf(_stp_stdout,fmt,args)
#define _stp_print_cstr(str) _stp_string_cat_cstr(_stp_stdout,str)
#define _stp_print_string(str) _stp_string_cat_string(_stp_stdout,str)
/** Write a String or C string into the print buffer.
 * This macro selects the proper function to call.
 * @param str A String or C string (char *)
 * @sa _stp_print_cstr _stp_print_string
 */

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

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