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 = kSystemLogDirectory "/" DRIVER_NAME ".log";
31 static sem_id sLogLock;
32
33
34 void
load_settings()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
create_log_file()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
pc_serial_trace(bool force,const char * format,...)73 pc_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 = "\033[32m" DRIVER_NAME ":\033[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("%s", buffer);
99 }
100
101
102 void
trace_termios(struct termios * tios)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