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