Add support for the tracing infastructure in coreboot.
[coreboot.git] / src / console / vsprintf.c
1 /*
2  * This file is part of the coreboot project.
3  *
4  * Copyright (C) 2009 coresystems GmbH
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; version 2 of
9  * the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301 USA
20  */
21
22 #include <string.h>
23 #include <smp/spinlock.h>
24 #include <console/vtxprintf.h>
25 #include <trace.h>
26
27 DECLARE_SPIN_LOCK(vsprintf_lock)
28
29 static char *str_buf;
30
31 static void str_tx_byte(unsigned char byte)
32 {
33         *str_buf = byte;
34         str_buf++;
35 }
36
37 static int vsprintf(char *buf, const char *fmt, va_list args)
38 {
39         int i;
40
41         DISABLE_TRACE;
42         spin_lock(&vsprintf_lock);
43
44         str_buf = buf;
45         i = vtxprintf(str_tx_byte, fmt, args);
46         *str_buf = '\0';
47
48         spin_unlock(&vsprintf_lock);
49         ENABLE_TRACE;
50
51         return i;
52 }
53
54 int sprintf(char *buf, const char *fmt, ...)
55 {
56         va_list args;
57         int i;
58
59         va_start(args, fmt);
60         i = vsprintf(buf, fmt, args);
61         va_end(args);
62
63         return i;
64 }