1232950Stheraven/*
2232950Stheraven * Auvia BeOS Driver for Via VT82xx Southbridge audio
3232950Stheraven *
4232950Stheraven * Copyright (c) 2003, Jerome Duval (jerome.duval@free.fr)
5232950Stheraven *
6232950Stheraven * Original code : BeOS Driver for Intel ICH AC'97 Link interface
7232950Stheraven * Copyright (c) 2002, Marcus Overhagen <marcus@overhagen.de>
8232950Stheraven *
9232950Stheraven * All rights reserved.
10232950Stheraven * Redistribution and use in source and binary forms, with or without modification,
11232950Stheraven * are permitted provided that the following conditions are met:
12232950Stheraven *
13232950Stheraven * - Redistributions of source code must retain the above copyright notice,
14232950Stheraven *   this list of conditions and the following disclaimer.
15232950Stheraven * - Redistributions in binary form must reproduce the above copyright notice,
16232950Stheraven *   this list of conditions and the following disclaimer in the documentation
17232950Stheraven *   and/or other materials provided with the distribution.
18232950Stheraven *
19232950Stheraven * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20232950Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21232950Stheraven * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22232950Stheraven * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23232950Stheraven * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24232950Stheraven * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25232950Stheraven * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26232950Stheraven * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27227825Stheraven * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28227825Stheraven * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29227825Stheraven *
30227825Stheraven */
31227825Stheraven
32227825Stheraven
33227825Stheraven#include <KernelExport.h>
34227825Stheraven
35227825Stheraven#include <fcntl.h>
36227825Stheraven#include <stdio.h>
37227825Stheraven#include <string.h>
38227825Stheraven#include <unistd.h>
39227825Stheraven
40227825Stheraven#include <directories.h>
41227825Stheraven#include <OS.h>
42227825Stheraven
43227825Stheraven#include "debug.h"
44253159Stheraven#include "auvia.h"
45253159Stheraven
46227825Stheraven
47227972Stheraven#if DEBUG > 0
48253159Stheravenstatic const char *logfile = kSystemLogDirectory "/auvia.log";
49227825Stheravenstatic sem_id loglock;
50253159Stheraven#endif
51253159Stheraven
52253159Stheraven
53253159Stheravenvoid debug_printf(const char *text,...);
54253159Stheravenvoid log_printf(const char *text,...);
55253159Stheravenvoid log_create(void);
56253159Stheraven
57253159Stheraven
58253159Stheravenvoid debug_printf(const char *text,...)
59253159Stheraven{
60253159Stheraven	char buf[1024];
61253159Stheraven	va_list ap;
62253159Stheraven
63253159Stheraven	va_start(ap,text);
64253159Stheraven	vsprintf(buf,text,ap);
65253159Stheraven	va_end(ap);
66253159Stheraven
67253159Stheraven	dprintf(DRIVER_NAME ": %s",buf);
68253159Stheraven}
69253159Stheraven
70227972Stheraven
71227972Stheravenvoid log_create()
72253159Stheraven{
73278724Sdim#if DEBUG > 0
74278724Sdim	int fd = open(logfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
75253159Stheraven	const char *text = DRIVER_NAME ", " VERSION "\n";
76278724Sdim	loglock = create_sem(1,"logfile sem");
77278724Sdim	write(fd,text,strlen(text));
78278724Sdim	close(fd);
79253159Stheraven#endif
80278724Sdim}
81253159Stheraven
82278724Sdim
83253159Stheravenvoid log_printf(const char *text,...)
84253159Stheraven{
85253159Stheraven#if DEBUG > 0
86278724Sdim	int fd;
87253159Stheraven	char buf[1024];
88278724Sdim	va_list ap;
89278724Sdim
90278724Sdim	va_start(ap,text);
91278724Sdim	vsprintf(buf,text,ap);
92278724Sdim	va_end(ap);
93278724Sdim
94278724Sdim	dprintf(DRIVER_NAME ": %s",buf);
95278724Sdim
96278724Sdim	acquire_sem(loglock);
97278724Sdim	fd = open(logfile, O_WRONLY | O_APPEND);
98278724Sdim	write(fd,buf,strlen(buf));
99278724Sdim	close(fd);
100278724Sdim	release_sem(loglock);
101278724Sdim
102278724Sdim	#if DEBUG > 1
103278724Sdim		snooze(150000);
104315965Sdim	#endif
105278724Sdim#endif
106278724Sdim}
107278724Sdim