1 /*
2 * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li>
3 * Distributed under the terms of the MIT License.
4 *
5 */
6
7 #include <OS.h>
8 #include <KernelExport.h>
9
10 #include <USB.h>
11 //#include <CAM.h>
12 #include <stdio.h> //sprintf
13 #include <string.h> //strcpy
14 #include <unistd.h> //posix file i/o - create, write, close
15 #include <directories.h>
16 #include <driver_settings.h>
17
18 #include "usb_vision.h"
19 #include "tracing.h"
20
21 #if DEBUG
22 bool b_log = true;
23 #else
24 bool b_log = false;
25 #endif
26 bool b_log_file = false;
27 bool b_log_append = false;
28 bool b_log_funcalls = false;
29 bool b_log_funcret = false;
30 bool b_log_funcres = false;
31
32 bool b_log_settings_loaded = false;
33
34 static const char *private_log_path
35 = kSystemLogDirectory "/" DRIVER_NAME ".log";
36 static sem_id loglock;
37
load_setting(void)38 void load_setting(void){
39 if(!b_log_settings_loaded){
40 void *settingshandle;
41 settingshandle = load_driver_settings(DRIVER_NAME);
42 #if !DEBUG
43 b_log = get_driver_boolean_parameter(settingshandle, "debug_output", b_log, true);
44 #endif
45 b_log_file = get_driver_boolean_parameter(settingshandle, "debug_output_in_file", b_log_file, true);
46 b_log_append = ! get_driver_boolean_parameter(settingshandle, "debug_output_file_rewrite", !b_log_append, true);
47 b_log_funcalls = get_driver_boolean_parameter(settingshandle, "debug_trace_func_calls", b_log_funcalls, false);
48 b_log_funcret = get_driver_boolean_parameter(settingshandle, "debug_trace_func_returns", b_log_funcret, false);
49 b_log_funcres = get_driver_boolean_parameter(settingshandle, "debug_trace_func_results", b_log_funcres, false);
50 unload_driver_settings(settingshandle);
51 b_log_settings_loaded = true;
52 }
53 }
54
create_log(void)55 void create_log(void){
56 int flags = O_WRONLY | O_CREAT | ((!b_log_append) ? O_TRUNC : 0);
57 if(!b_log_file)
58 return;
59 close(open(private_log_path, flags, 0666));
60 loglock = create_sem(1, DRIVER_NAME"-logging");
61 }
62
usbvision_trace(bool b_force,char * fmt,...)63 void usbvision_trace(bool b_force, char *fmt, ...){
64 if(!(b_force || b_log))
65 return;
66 {
67 va_list arg_list;
68 static char *prefix = "\33[32m"DRIVER_NAME":\33[0m";
69 static char buf[1024];
70 char *buf_ptr = buf;
71 if(!b_log_file){
72 strcpy(buf, prefix);
73 buf_ptr += strlen(prefix);
74 }
75
76 /* {
77 bigtime_t time = system_time();
78 uint32 msec = time / 1000;
79 uint32 sec = msec / 1000;
80 sprintf(buf_ptr, "%02d.%02d.%03d:", sec / 60, sec % 60, msec % 1000);
81 buf_ptr += strlen(buf_ptr);
82 }
83 */
84 va_start(arg_list, fmt);
85 vsprintf(buf_ptr, fmt, arg_list);
86 va_end(arg_list);
87
88 if(b_log_file){
89 int fd;
90 acquire_sem(loglock);
91 fd = open(private_log_path, O_WRONLY | O_APPEND);
92 write(fd, buf, strlen(buf));
93 close(fd);
94 release_sem(loglock);
95 }
96 else
97 dprintf("%s", buf);
98 }
99 }
100
trace_reginfo(xet_nt100x_reg * ri)101 void trace_reginfo(xet_nt100x_reg *ri)
102 {
103 int i;
104 TRACE("struct set_nt100x_reg\n"
105 " reg:%02x\n"
106 " data_length:%d\n",
107 ri->reg, ri->data_length);
108 for(i = 0; i < ri->data_length; i++)
109 TRACE("%02x ", ri->data[i]);
110 TRACE("\n");
111 }
112