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