Skip to content

Commit 5e98400

Browse files
committed
logging.txt file is created that logs all the errors with timestamp
1 parent 44fb655 commit 5e98400

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Recorded data
22
*.csv
33

4+
/logs/
5+
logging.txt
6+
47
# Byte-compiled / optimized / DLL files
58
__pycache__/
69
*.py[cod]

app.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import queue
99
import yaml
1010
from pathlib import Path
11+
import os
1112

1213
console_queue = queue.Queue()
1314
app = Flask(__name__)
@@ -20,6 +21,22 @@
2021
stream_active = False
2122
running_apps = {} # Dictionary to track running apps
2223

24+
@app.route('/log_error', methods=['POST'])
25+
def log_error():
26+
try:
27+
error_data = request.get_json()
28+
if not error_data or 'error' not in error_data or 'log_error' in str(error_data):
29+
return jsonify({'status': 'error', 'message': 'Invalid data'}), 400
30+
31+
os.makedirs('logs', exist_ok=True)
32+
33+
with open('logs/logging.txt', 'a') as f:
34+
f.write(error_data['error'])
35+
36+
return jsonify({'status': 'success'})
37+
except Exception as e:
38+
return jsonify({'status': 'error', 'message': 'Logging failed'}), 500
39+
2340
def run_async(coro):
2441
def wrapper(*args, **kwargs):
2542
loop = asyncio.new_event_loop()

static/script.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
let isLogging = false;
2+
3+
function logError(error) {
4+
if (isLogging) return; // Prevent recursion
5+
6+
isLogging = true;
7+
try {
8+
const errorMessage = `[${getTimestamp()}] ${error.stack || error.message || error}\n`;
9+
10+
// Fallback to console if fetch fails
11+
fetch('/log_error', {
12+
method: 'POST',
13+
headers: { 'Content-Type': 'application/json' },
14+
body: JSON.stringify({ error: errorMessage })
15+
}).catch(() => {
16+
console.error('Failed to log error:', errorMessage);
17+
});
18+
19+
console.error(errorMessage);
20+
} finally {
21+
isLogging = false;
22+
}
23+
}
24+
125
async function loadApps() {
226
try {
327
const appGrid = document.getElementById('app-grid');
@@ -21,7 +45,7 @@ async function loadApps() {
2145

2246
return config.apps;
2347
} catch (error) {
24-
console.error('Error loading apps config:', error);
48+
logError('Error loading apps config:', error);
2549

2650
// Show error state to user
2751
const appGrid = document.getElementById('app-grid');
@@ -50,7 +74,7 @@ async function initializeApplication() {
5074
setupCategoryFilter(apps);
5175
startAppStatusChecker();
5276
} catch (error) {
53-
console.error('Application initialization failed:', error);
77+
logError('Application initialization failed:', error);
5478
}
5579
}
5680

@@ -133,7 +157,7 @@ async function handleAppClick(app, card) {
133157
card.innerHTML = originalContent;
134158
updateAppStatus(app.script); // Update status after launch
135159
} catch (error) {
136-
console.error('Error launching app:', error);
160+
logError('Error launching app:', error);
137161
showAlert(`Failed to launch ${app.title}: ${error.message}`);
138162
card.innerHTML = originalContent;
139163
}
@@ -165,7 +189,7 @@ async function updateAppStatus(appName) {
165189
}
166190
}
167191
} catch (error) {
168-
console.error('Error checking app status:', error);
192+
logError('Error checking app status:', error);
169193
}
170194
}
171195

@@ -432,7 +456,7 @@ function scanBleDevices() {
432456
})
433457
.catch(error => {
434458
isScanning = false;
435-
console.error('BLE scan error:', error);
459+
logError('BLE scan error:', error);
436460
bleDevicesList.innerHTML = `
437461
<div class="text-center py-4 text-red-500">
438462
Error scanning for devices. Please try again.
@@ -532,7 +556,7 @@ connectBtn.addEventListener('click', async () => {
532556
throw new Error(data.message || 'Connection failed');
533557
}
534558
} catch (error) {
535-
console.error('Connection error:', error);
559+
logError('Connection error:', error);
536560
showStatus(`Connection failed: ${error.message}`, 'fa-times-circle', 'text-red-500');
537561
// Return to connect state
538562
connectingBtn.classList.add('hidden');
@@ -573,7 +597,7 @@ async function pollConnectionStatus() {
573597
});
574598
}
575599
} catch (error) {
576-
console.error('Connection polling error:', error);
600+
logError('Connection polling error:', error);
577601
showStatus(`Connection failed: Try again`, 'fa-times-circle', 'text-red-500');
578602
// Return to connect state
579603
connectingBtn.classList.add('hidden');
@@ -686,7 +710,7 @@ disconnectBtn.addEventListener('click', async () => {
686710
}
687711
}
688712
} catch (error) {
689-
console.error('Disconnection error:', error);
713+
logError('Disconnection error:', error);
690714
// Return to disconnect state if disconnection failed
691715
disconnectingBtn.classList.add('hidden');
692716
disconnectBtn.classList.remove('hidden');
@@ -707,7 +731,7 @@ function startConsoleUpdates() {
707731
};
708732

709733
eventSource.onerror = function() {
710-
console.error('EventSource failed');
734+
logError('EventSource failed');
711735
if (eventSource) {
712736
eventSource.close();
713737
eventSource = null;
@@ -754,7 +778,7 @@ function toggleRecording() {
754778
}
755779
})
756780
.catch(error => {
757-
console.error('Error stopping recording:', error);
781+
logError('Error stopping recording:', error);
758782
});
759783
} else {
760784
// Start recording - send the filename (or null for default)
@@ -780,7 +804,7 @@ function toggleRecording() {
780804
}
781805
})
782806
.catch(error => {
783-
console.error('Error starting recording:', error);
807+
logError('Error starting recording:', error);
784808
showAlert('Failed to start recording: ' + error.message);
785809
});
786810
}
@@ -858,7 +882,7 @@ function checkStreamStatus() {
858882
}
859883
})
860884
.catch(error => {
861-
console.error('Error fetching stream status:', error);
885+
logError('Error fetching stream status:', error);
862886
});
863887
}
864888

@@ -903,6 +927,9 @@ checkStreamStatus();
903927
// Initialize the app when DOM is loaded
904928
document.addEventListener('DOMContentLoaded', () => {
905929
initializeApplication();
930+
window.onerror = function(message, source, lineno, colno, error) {
931+
logError(error || message);
932+
return true; };
906933

907934
document.getElementById('github-btn').addEventListener('click', () => {
908935
window.open('https://github.com/upsidedownlabs/Chords-Python', '_blank');

0 commit comments

Comments
 (0)