BPicture: Fix archive constructor.
[haiku.git] / src / add-ons / kernel / drivers / network / usb_asix / Settings.cpp
blob5ac346e77136e6371bc6086453981fb85517392b
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.
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.
14 #include "Settings.h"
16 #include <malloc.h>
17 #include <stdio.h>
18 #include <string.h>
20 #include <driver_settings.h>
21 #include <lock.h>
24 bool gTraceOn = false;
25 bool gTruncateLogFile = false;
26 bool gAddTimeStamp = true;
27 bool gTraceFlow = false;
28 static char *gLogFilePath = NULL;
29 mutex gLogLock;
31 static
32 void create_log()
34 if (gLogFilePath == NULL)
35 return;
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);
42 mutex_init(&gLogLock, DRIVER_NAME"-logging");
46 void load_settings()
48 void *handle = load_driver_settings(DRIVER_NAME);
49 if (handle == 0)
50 return;
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);
65 unload_driver_settings(handle);
67 create_log();
71 void release_settings()
73 if (gLogFilePath != NULL) {
74 mutex_destroy(&gLogLock);
75 free(gLogFilePath);
80 void usb_asix_trace(bool force, const char* func, const char *fmt, ...)
82 if (!(force || gTraceOn)) {
83 return;
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);
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);
104 if (func != NULL) {
105 sprintf(buf_ptr, "%s::", func);
106 buf_ptr += strlen(buf_ptr);
109 va_start(arg_list, fmt);
110 vsprintf(buf_ptr, fmt, arg_list);
111 va_end(arg_list);
113 if (gLogFilePath == NULL) {
114 dprintf(buffer);
115 return;
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);
124 mutex_unlock(&gLogLock);