1 /* 2 * Copyright (c) 2007-2008 by Michael Lotz 3 * Heavily based on the original usb_serial driver which is: 4 * 5 * Copyright (c) 2003 by Siarzhuk Zharski <imker@gmx.li> 6 * Distributed under the terms of the MIT License. 7 */ 8 #include "Tracing.h" 9 #include "Driver.h" 10 11 #include <stdio.h> //sprintf 12 #include <unistd.h> //posix file i/o - create, write, close 13 #include <Drivers.h> 14 #include <directories.h> 15 #include <driver_settings.h> 16 17 18 #if DEBUG 19 bool gLogEnabled = true; 20 #else 21 bool gLogEnabled = false; 22 #endif 23 24 bool gLogToFile = false; 25 bool gLogAppend = false; 26 bool gLogFunctionCalls = false; 27 bool gLogFunctionReturns = false; 28 bool gLogFunctionResults = false; 29 30 static const char *sLogFilePath = kCommonLogDirectory "/" DRIVER_NAME ".log"; 31 static sem_id sLogLock; 32 33 34 void 35 load_settings() 36 { 37 void *settingsHandle; 38 settingsHandle = load_driver_settings(DRIVER_NAME); 39 40 #if !DEBUG 41 gLogEnabled = get_driver_boolean_parameter(settingsHandle, 42 "debug_output", gLogEnabled, true); 43 #endif 44 45 gLogToFile = get_driver_boolean_parameter(settingsHandle, 46 "debug_output_in_file", gLogToFile, true); 47 gLogAppend = !get_driver_boolean_parameter(settingsHandle, 48 "debug_output_file_rewrite", !gLogAppend, true); 49 gLogFunctionCalls = get_driver_boolean_parameter(settingsHandle, 50 "debug_trace_func_calls", gLogFunctionCalls, false); 51 gLogFunctionReturns = get_driver_boolean_parameter(settingsHandle, 52 "debug_trace_func_returns", gLogFunctionReturns, false); 53 gLogFunctionResults = get_driver_boolean_parameter(settingsHandle, 54 "debug_trace_func_results", gLogFunctionResults, false); 55 56 unload_driver_settings(settingsHandle); 57 } 58 59 60 void 61 create_log_file() 62 { 63 if(!gLogToFile) 64 return; 65 66 int flags = O_WRONLY | O_CREAT | (!gLogAppend ? O_TRUNC : 0); 67 close(open(sLogFilePath, flags, 0666)); 68 sLogLock = create_sem(1, DRIVER_NAME"-logging"); 69 } 70 71 72 void 73 usb_serial_trace(bool force, const char *format, ...) 74 { 75 if (!gLogEnabled && !force) 76 return; 77 78 static char buffer[1024]; 79 char *bufferPointer = buffer; 80 if (!gLogToFile) { 81 const char *prefix = "\33[32m"DRIVER_NAME":\33[0m "; 82 strcpy(bufferPointer, prefix); 83 bufferPointer += strlen(prefix); 84 } 85 86 va_list argumentList; 87 va_start(argumentList, format); 88 vsprintf(bufferPointer, format, argumentList); 89 va_end(argumentList); 90 91 if (gLogToFile) { 92 acquire_sem(sLogLock); 93 int fd = open(sLogFilePath, O_WRONLY | O_APPEND); 94 write(fd, buffer, strlen(buffer)); 95 close(fd); 96 release_sem(sLogLock); 97 } else 98 dprintf(buffer); 99 } 100 101 102 void 103 trace_termios(struct termios *tios) 104 { 105 TRACE("struct termios:\n" 106 "\tc_iflag: 0x%08x\n" 107 "\tc_oflag: 0x%08x\n" 108 "\tc_cflag: 0x%08x\n" 109 "\tc_lflag: 0x%08x\n" 110 "\tc_line: 0x%08x\n" 111 // "\tc_ixxxxx: 0x%08x\n" 112 // "\tc_oxxxxx: 0x%08x\n" 113 "\tc_cc[0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x]\n", 114 tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag, 115 tios->c_line, 116 // tios->c_ixxxxx, tios->c_oxxxxx, 117 tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3], 118 tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7], 119 tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]); 120 } 121 122 123 #ifdef __BEOS__ 124 125 126 void 127 trace_ddomain(struct ddomain *dd) 128 { 129 TRACE("struct ddomain:\n" 130 "\tddrover: 0x%08x\n" 131 "\tbg: %d, locked: %d\n", dd->r, dd->bg, dd->locked); 132 } 133 134 135 void 136 trace_str(struct str *str) 137 { 138 TRACE("struct str:\n" 139 "\tbuffer: 0x%08x\n" 140 "\tbufsize: %d\n" 141 "\tcount: %d\n" 142 "\ttail: %d\n" 143 "\tallocated: %d\n", 144 str->buffer, str->bufsize, str->count, str->tail, str->allocated); 145 } 146 147 148 void 149 trace_winsize(struct winsize *ws) 150 { 151 TRACE("struct winsize:\n" 152 "\tws_row: %d\n" 153 "\tws_col: %d\n" 154 "\tws_xpixel: %d\n" 155 "\tws_ypixel: %d\n", 156 ws->ws_row, ws->ws_col, ws->ws_xpixel, ws->ws_ypixel); 157 } 158 159 160 void 161 trace_tty(struct tty *tty) 162 { 163 TRACE("struct tty:\n" 164 "\tnopen: %d, flags: 0x%08x,\n", tty->nopen, tty->flags); 165 166 TRACE("ddomain dd:\n"); 167 trace_ddomain(&tty->dd); 168 TRACE("ddomain ddi:\n"); 169 trace_ddomain(&tty->ddi); 170 171 TRACE("\tpgid: %08x\n", tty->pgid); 172 TRACE("termios t:"); 173 trace_termios(&tty->t); 174 175 TRACE("\tiactivity: %d, ibusy: %d\n", tty->iactivity, tty->ibusy); 176 177 TRACE("str istr:\n"); 178 trace_str(&tty->istr); 179 TRACE("str rstr:\n"); 180 trace_str(&tty->rstr); 181 TRACE("str ostr:\n"); 182 trace_str(&tty->ostr); 183 184 TRACE("winsize wsize:\n"); 185 trace_winsize(&tty->wsize); 186 187 TRACE("\tservice: 0x%08x\n", tty->service); 188 } 189 190 191 #endif /* __BEOS__ */ 192