Skip to content

Commit

Permalink
logfile enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
Haneef Mohammed committed Nov 13, 2021
1 parent 9567660 commit d8ce2bf
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"version": "0.4.8-pre4",
"activationEvents": [
"onDebugResolve:cortex-debug"
"onDebugResolve:cortex-debug",
"onStartupFinished"
],
"categories": [
"Debuggers"
Expand Down Expand Up @@ -328,8 +329,11 @@
"description": "If true, display variables in their natural format as specified by ARM. Hex otherwise."
},
"cortex-debug.dbgServerLogfile": {
"type": "string",
"default": "",
"type": [
"string",
"null"
],
"default": null,
"description": "Logs the contents of the gdb-server terminal. Use ${PID} in the file name to make it unique per VSCode session"
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/frontend/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export class CortexDebugExtension {
}
}
}
if (e.affectsConfiguration(`cortex-debug.${CortexDebugKeys.SERVER_LOG_FILE_NAME}`)) {
const config = vscode.workspace.getConfiguration('cortex-debug');
const fName = config.get(CortexDebugKeys.SERVER_LOG_FILE_NAME, '');
this.gdbServerConsole.createLogFile(fName);
}
}

private getSVDFile(device: string): string {
Expand Down
48 changes: 34 additions & 14 deletions src/frontend/server_console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export class GDBServerConsole {
public createLogFile(logFileName: string) {
this.logFName = logFileName;
const showErr = !!this.logFName;

if (this.logFd >= 0) {
try {
fs.closeSync(this.logFd);
}
finally {
this.logFd = -1;
}
}

try {
if (this.logFName) {
const dir = path.dirname(this.logFName);
Expand Down Expand Up @@ -89,12 +99,14 @@ export class GDBServerConsole {
protected debugMsg(msg: string) {
if (true) {
try {
msg = 'SERVER CONSOLE DEBUG: ' + msg;
const date = new Date();
msg = `[${date.toISOString()}] SERVER CONSOLE DEBUG: ` + msg;
console.log(msg);
if (this.ptyTerm) {
msg += msg.endsWith('\n') ? '' : '\n';
magentaWrite(msg, this.ptyTerm);
}
this.logData(msg, true);
}
finally {}
}
Expand Down Expand Up @@ -140,25 +152,33 @@ export class GDBServerConsole {
});
socket.on('data', (data) => {
this.ptyTerm.write(data);
try {
if (this.logFd >= 0) {
if (!this.ptyTerm.isReady) {
// Maybe we should do our own buffering rather than the pty doing it. This can
// help if the user kills the terminal. But we would have lost previous data anyways
fs.writeFileSync(this.logFd, '******* Terminal not yet ready, buffering ******');
}
fs.writeFileSync(this.logFd, data.toString());
}
}
catch (e) {
this.logFd = -1;
}
this.logData(data);
});
socket.on('error', (e) => {
this.debugMsg(`GDBServerConsole: onBackendConnect: gdb-server program client error ${e}`);
});
}

private logData(data: Buffer | string, suppressNotReadMsg = false) {
try {
if (this.logFd >= 0) {
if (!this.ptyTerm.isReady && !suppressNotReadMsg) {
// Maybe we should do our own buffering rather than the pty doing it. This can
// help if the user kills the terminal. But we would have lost previous data anyways
const date = new Date();
const msg = `\n[${date.toISOString()}] SERVER CONSOLE DEBUG: ******* Terminal not yet ready, buffering... ******\n`;
console.log(msg);
fs.writeFileSync(this.logFd, msg);
}
fs.writeFileSync(this.logFd, data.toString());
fs.fdatasyncSync(this.logFd);
}
}
catch (e) {
this.logFd = -1;
}
}

public sendToBackend(data: string | Buffer) {
if (this.toBackend) {
this.toBackend.write(data.toString());
Expand Down

0 comments on commit d8ce2bf

Please sign in to comment.