BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / usb_davicom / Settings.cpp
blob3e46e946e12815f8700e886926a1610fca220e87
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 */
8 #include "Settings.h"
10 #include <malloc.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include <driver_settings.h>
15 #include <lock.h>
17 #include "Driver.h"
20 mutex gLogLock;
21 static char *gLogFilePath = NULL;
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;
32 static
33 void create_log()
35 if (gLogFilePath == NULL)
36 return;
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);
43 mutex_init(&gLogLock, DRIVER_NAME"-logging");
47 void
48 load_settings()
50 void *handle = load_driver_settings(DRIVER_NAME);
51 if (handle == 0)
52 return;
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);
71 unload_driver_settings(handle);
73 create_log();
77 void
78 release_settings()
80 if (gLogFilePath != NULL) {
81 mutex_destroy(&gLogLock);
82 free(gLogFilePath);
87 void usb_davicom_trace(bool force, const char* func, const char *fmt, ...)
89 if (!(force || gTraceOn)) {
90 return;
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);
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);
111 if (func != NULL) {
112 sprintf(buf_ptr, "%s::", func);
113 buf_ptr += strlen(buf_ptr);
116 va_start(arg_list, fmt);
117 vsprintf(buf_ptr, fmt, arg_list);
118 va_end(arg_list);
120 if (gLogFilePath == NULL) {
121 dprintf(buffer);
122 return;
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);
131 mutex_unlock(&gLogLock);