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