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 #include "USB3.h" 11 12 #include <stdio.h> //sprintf 13 #include <unistd.h> //posix file i/o - create, write, close 14 #include <Drivers.h> 15 #include <directories.h> 16 #include <driver_settings.h> 17 18 19 #if DEBUG 20 bool gLogEnabled = true; 21 #else 22 bool gLogEnabled = false; 23 #endif 24 25 bool gLogToFile = false; 26 bool gLogAppend = false; 27 bool gLogFunctionCalls = true; 28 bool gLogFunctionReturns = true; 29 bool gLogFunctionResults = true; 30 31 static const char *sLogFilePath = kCommonLogDirectory "/" DRIVER_NAME ".log"; 32 static sem_id sLogLock; 33 34 35 void 36 load_settings() 37 { 38 void *settingsHandle; 39 settingsHandle = load_driver_settings(DRIVER_NAME); 40 41 #if !DEBUG 42 gLogEnabled = get_driver_boolean_parameter(settingsHandle, 43 "debug_output", gLogEnabled, true); 44 #endif 45 46 gLogToFile = get_driver_boolean_parameter(settingsHandle, 47 "debug_output_in_file", gLogToFile, true); 48 gLogAppend = !get_driver_boolean_parameter(settingsHandle, 49 "debug_output_file_rewrite", !gLogAppend, true); 50 gLogFunctionCalls = get_driver_boolean_parameter(settingsHandle, 51 "debug_trace_func_calls", gLogFunctionCalls, false); 52 gLogFunctionReturns = get_driver_boolean_parameter(settingsHandle, 53 "debug_trace_func_returns", gLogFunctionReturns, false); 54 gLogFunctionResults = get_driver_boolean_parameter(settingsHandle, 55 "debug_trace_func_results", gLogFunctionResults, false); 56 57 unload_driver_settings(settingsHandle); 58 } 59 60 61 void 62 create_log_file() 63 { 64 if(!gLogToFile) 65 return; 66 67 int flags = O_WRONLY | O_CREAT | (!gLogAppend ? O_TRUNC : 0); 68 close(open(sLogFilePath, flags, 0666)); 69 sLogLock = create_sem(1, DRIVER_NAME"-logging"); 70 } 71 72 73 void 74 usb_serial_trace(bool force, const char *format, ...) 75 { 76 if (!gLogEnabled && !force) 77 return; 78 79 static char buffer[1024]; 80 char *bufferPointer = buffer; 81 if (!gLogToFile) { 82 const char *prefix = "\33[32m"DRIVER_NAME":\33[0m "; 83 strcpy(bufferPointer, prefix); 84 bufferPointer += strlen(prefix); 85 } 86 87 va_list argumentList; 88 va_start(argumentList, format); 89 vsprintf(bufferPointer, format, argumentList); 90 va_end(argumentList); 91 92 if (gLogToFile) { 93 acquire_sem(sLogLock); 94 int fd = open(sLogFilePath, O_WRONLY | O_APPEND); 95 write(fd, buffer, strlen(buffer)); 96 close(fd); 97 release_sem(sLogLock); 98 } else 99 dprintf(buffer); 100 } 101 102 103 void 104 trace_termios(struct termios *tios) 105 { 106 TRACE("struct termios:\n" 107 "\tc_iflag: 0x%08x\n" 108 "\tc_oflag: 0x%08x\n" 109 "\tc_cflag: 0x%08x\n" 110 "\tc_lflag: 0x%08x\n" 111 "\tc_line: 0x%08x\n" 112 "\tc_ispeed: 0x%08x\n" 113 "\tc_ospeed: 0x%08x\n" 114 "\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", 115 tios->c_iflag, tios->c_oflag, tios->c_cflag, tios->c_lflag, 116 tios->c_line, 117 tios->c_ispeed, tios->c_ospeed, 118 tios->c_cc[0], tios->c_cc[1], tios->c_cc[2], tios->c_cc[3], 119 tios->c_cc[4], tios->c_cc[5], tios->c_cc[6], tios->c_cc[7], 120 tios->c_cc[8], tios->c_cc[9], tios->c_cc[10]); 121 } 122