1 /*
2 * SiS 7018, Trident 4D Wave DX/NX, Acer Lab M5451 Sound Driver.
3 * Copyright (c) 2002, 2008-2011 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 <lock.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16
17 bool gTraceOn = false;
18 bool gTruncateLogFile = false;
19 bool gAddTimeStamp = true;
20 static char *gLogFilePath = NULL;
21 mutex gLogLock;
22
23
24 static
create_log()25 void create_log()
26 {
27 if (gLogFilePath == NULL)
28 return;
29
30 int flags = O_WRONLY | O_CREAT | ((gTruncateLogFile) ? O_TRUNC : 0);
31 int fd = open(gLogFilePath, flags, 0666);
32 if (fd >= 0)
33 close(fd);
34
35 mutex_init(&gLogLock, DRIVER_NAME"-logging");
36 }
37
38
load_settings()39 void load_settings()
40 {
41 void *handle = load_driver_settings(DRIVER_NAME);
42 if (handle == 0)
43 return;
44
45 gTraceOn = get_driver_boolean_parameter(handle, "trace", gTraceOn, true);
46 gTruncateLogFile = get_driver_boolean_parameter(handle, "truncate_logfile",
47 gTruncateLogFile, true);
48 gAddTimeStamp = get_driver_boolean_parameter(handle, "add_timestamp",
49 gAddTimeStamp, true);
50 const char * logFilePath = get_driver_parameter(handle, "logfile",
51 NULL, "/var/log/" DRIVER_NAME ".log");
52 if (logFilePath != NULL) {
53 gLogFilePath = strdup(logFilePath);
54 }
55
56 unload_driver_settings(handle);
57
58 create_log();
59 }
60
61
release_settings()62 void release_settings()
63 {
64 if (gLogFilePath != NULL) {
65 mutex_destroy(&gLogLock);
66 free(gLogFilePath);
67 }
68 }
69
70
SiS7018_trace(bool force,const char * func,const char * fmt,...)71 void SiS7018_trace(bool force, const char* func, const char *fmt, ...)
72 {
73 if (!(force || gTraceOn)) {
74 return;
75 }
76
77 va_list arg_list;
78 static const char *prefix = DRIVER_NAME":";
79 static char buffer[1024];
80 char *buf_ptr = buffer;
81 if (gLogFilePath == NULL) {
82 strlcpy(buffer, prefix, sizeof(buffer));
83 buf_ptr += strlen(prefix);
84 }
85
86 if (gAddTimeStamp) {
87 bigtime_t time = system_time();
88 uint32 msec = time / 1000;
89 uint32 sec = msec / 1000;
90 sprintf(buf_ptr, "%02" B_PRIu32 ".%02" B_PRIu32 ".%03" B_PRIu32 ":",
91 sec / 60, sec % 60, msec % 1000);
92 buf_ptr += strlen(buf_ptr);
93 }
94
95 if (func != NULL) {
96 sprintf(buf_ptr, "%s::", func);
97 buf_ptr += strlen(buf_ptr);
98 }
99
100 va_start(arg_list, fmt);
101 vsprintf(buf_ptr, fmt, arg_list);
102 va_end(arg_list);
103
104 if (gLogFilePath == NULL) {
105 dprintf("%s", buffer);
106 return;
107 }
108
109 mutex_lock(&gLogLock);
110 int fd = open(gLogFilePath, O_WRONLY | O_APPEND);
111 if (fd >= 0) {
112 write(fd, buffer, strlen(buffer));
113 close(fd);
114 }
115 mutex_unlock(&gLogLock);
116 }
117
118