xref: /haiku/src/add-ons/kernel/drivers/audio/usb/Settings.cpp (revision 4b7e219688450694efc9d1890f83f816758c16d3)
1 /*
2  *	Driver for USB Audio Device Class devices.
3  *	Copyright (c) 2009-13 S.Zharski <imker@gmx.li>
4  *	Distributed under the terms of the MIT license.
5  *
6  */
7 
8 
9 #include "Settings.h"
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 #include <driver_settings.h>
16 #include <lock.h>
17 
18 #include "Driver.h"
19 
20 
21 uint32 gTraceMask = ERR;
22 bool gTruncateLogFile = false;
23 bool gAddTimeStamp = true;
24 static char* gLogFilePath = NULL;
25 mutex gLogLock;
26 
27 static
28 void create_log()
29 {
30 	if (gLogFilePath == NULL)
31 		return;
32 
33 	int flags = O_WRONLY | O_CREAT | ((gTruncateLogFile) ? O_TRUNC : 0);
34 	int fd = open(gLogFilePath, flags, 0666);
35 	if (fd >= 0)
36 		close(fd);
37 
38 	mutex_init(&gLogLock, DRIVER_NAME"-logging");
39 }
40 
41 
42 void load_settings()
43 {
44 	void* handle = load_driver_settings(DRIVER_NAME);
45 	if (handle == 0)
46 		return;
47 
48 	gTraceMask = strtoul(get_driver_parameter(handle, "trace", "1", "0"), 0, 0);
49 	gTruncateLogFile = get_driver_boolean_parameter(handle,	"truncate_logfile",
50 						gTruncateLogFile, true);
51 	gAddTimeStamp = get_driver_boolean_parameter(handle, "add_timestamp",
52 						gAddTimeStamp, true);
53 	const char* logFilePath = get_driver_parameter(handle, "logfile",
54 						NULL, "/var/log/"DRIVER_NAME".log");
55 	if (logFilePath != NULL)
56 		gLogFilePath = strdup(logFilePath);
57 
58 	unload_driver_settings(handle);
59 
60 	create_log();
61 }
62 
63 
64 void release_settings()
65 {
66 	if (gLogFilePath != NULL) {
67 		mutex_destroy(&gLogLock);
68 		free(gLogFilePath);
69 	}
70 }
71 
72 
73 void usb_audio_trace(uint32 bits, const char* func, const char* fmt, ...)
74 {
75 	if ((gTraceMask & bits) == 0)
76 		return;
77 
78 	va_list arg_list;
79 	static const char* prefix = DRIVER_NAME":";
80 	static char buffer[1024];
81 	char* buf_ptr = buffer;
82 	if (gLogFilePath == NULL) {
83 		strlcpy(buffer, prefix, sizeof(buffer));
84 		buf_ptr += strlen(prefix);
85 	}
86 
87 	if (gAddTimeStamp) {
88 		bigtime_t time = system_time();
89 		uint32 msec = time / 1000;
90 		uint32 sec = msec / 1000;
91 		sprintf(buf_ptr, "%02ld.%02ld.%03ld:",
92 				sec / 60, sec % 60, msec % 1000);
93 		buf_ptr += strlen(buf_ptr);
94 	}
95 
96 	if (func	!= NULL) {
97 		sprintf(buf_ptr, "%s::", func);
98 		buf_ptr += strlen(buf_ptr);
99 	}
100 
101 	va_start(arg_list, fmt);
102 	vsprintf(buf_ptr, fmt, arg_list);
103 	va_end(arg_list);
104 
105 	if (gLogFilePath == NULL) {
106 		dprintf(buffer);
107 		return;
108 	}
109 
110 	mutex_lock(&gLogLock);
111 	int fd = open(gLogFilePath, O_WRONLY | O_APPEND);
112 	if (fd >= 0) {
113 		write(fd, buffer, strlen(buffer));
114 		close(fd);
115 	}
116 	mutex_unlock(&gLogLock);
117 }
118 
119