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