2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009, 2010 Daniel Borkmann.
5 * Subject to the GPL, version 2.
18 static char buffer
[1024];
19 static size_t buffer_use
= 0;
21 static struct spinlock buffer_lock
;
22 static size_t lcount
= 0;
24 size_t tprintf_get_free_count(void)
28 spinlock_lock(&buffer_lock
);
29 ret
= get_tty_size() - 5 - lcount
;
30 spinlock_unlock(&buffer_lock
);
36 * We want to print our stuff terminal aligned. Since we're printing packets
37 * we're in slowpath anyways. If stdin/stdout are connected to a terminal
38 * then default size = 1024; else size = 4096.
40 void tprintf_flush(void)
43 size_t flush_len
= get_tty_size() - 5;
45 while (buffer_use
-- > 0) {
46 if (lcount
== flush_len
) {
49 while (buffer_use
> 0 && (*ptr
== ' ' ||
50 *ptr
== ',' || *ptr
== '\n')) {
57 flush_len
= get_tty_size() - 5;
61 /* Collect in stream buffer. */
70 bug_on(buffer_use
> 0);
73 void tprintf_init(void)
75 spinlock_init(&buffer_lock
);
76 memset(buffer
, 0, sizeof(buffer
));
79 void tprintf_cleanup(void)
81 spinlock_lock(&buffer_lock
);
83 spinlock_unlock(&buffer_lock
);
84 spinlock_destroy(&buffer_lock
);
87 void tprintf(char *msg
, ...)
93 spinlock_lock(&buffer_lock
);
94 avail
= sizeof(buffer
) - buffer_use
;
97 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
100 panic("vsnprintf screwed up in tprintf!\n");
101 if (ret
> sizeof(buffer
))
102 panic("No mem in tprintf left!\n");
104 buffer
[buffer_use
] = 0;
107 avail
= sizeof(buffer
) - buffer_use
;
109 ret
= vsnprintf(buffer
+ buffer_use
, avail
, msg
, vl
);
112 panic("vsnprintf screwed up in tprintf!\n");
116 spinlock_unlock(&buffer_lock
);