Skip to content

Commit

Permalink
add: log view
Browse files Browse the repository at this point in the history
  • Loading branch information
Friedjof committed May 23, 2024
1 parent 478f6f1 commit 9f24e90
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 4 deletions.
4 changes: 4 additions & 0 deletions data-template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<script src="/script.js"></script>
</head>
<body>
<!-- Logs Button -->
<div class="log-button">
<button id="log-button" onclick=showLogs()>Logs &#128221;</button>
</div>
<!-- Clouds -->
<div id="clouds">
<span class="cloud" style="font-size:200px;">&#127781;</span>
Expand Down
71 changes: 69 additions & 2 deletions data-template/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function showTimer(id, timer) {
row.appendChild(enabled);

var popup_button = document.createElement('td');
popup_button.innerHTML = '<button onclick="showPopup(' + id + ')" class="input_buttons">&#128197;</button>';
popup_button.innerHTML = '<button onclick="showWeekdayConfig(' + id + ')" class="input_buttons">&#128197;</button>';
popup_button.id = 'popup-' + id;
row.appendChild(popup_button);

Expand All @@ -68,7 +68,7 @@ function showTimer(id, timer) {
document.getElementById('timers').getElementsByTagName('tbody')[0].appendChild(row);
}

function showPopup(id) {
function showPopup() {
var overlay = document.createElement('div');
overlay.className = 'overlay';
overlay.onclick = function() {
Expand All @@ -89,6 +89,73 @@ function showPopup(id) {

modal.appendChild(closeButton);

return modal;
}

function showLogs() {
var modal = showPopup();

var text = document.createElement('h2');
text.className = 'schedule-header';
text.textContent = 'Logs 📜';
modal.appendChild(text);

var body = document.createElement('div');
body.className = 'log-body';
modal.appendChild(body);

// Show IFrame of /logging
var iframe = document.createElement('iframe');
iframe.src = url + '/logging';
iframe.className = 'logging-iframe';
body.appendChild(iframe);

// Refresh button
var button_div = document.createElement('div');
button_div.className = 'refresh-div';
modal.appendChild(button_div);

var refresh_button = document.createElement('button');
refresh_button.innerHTML = 'Refresh';
refresh_button.className = 'refresh-button';
refresh_button.onclick = function() {
iframe.src = url + '/logging';
}
button_div.appendChild(refresh_button);

var link_button = document.createElement('button');
link_button.innerHTML = 'Open in new tab';
link_button.className = 'link-button';
link_button.onclick = function() {
window.open(url + '/logging', '_blank');
}
button_div.appendChild(link_button);

var clear_button = document.createElement('button');
clear_button.innerHTML = 'Clear logs';
clear_button.className = 'clear-button';
clear_button.onclick = function() {
fetch(url + '/reset_logs')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.catch((error) => {
console.error('Error:', error);
});

iframe.src = url + '/logging';
}
button_div.appendChild(clear_button);

document.body.appendChild(modal);
}

function showWeekdayConfig(id) {
var modal = showPopup();

var text = document.createElement('h2');
text.className = 'schedule-header';
text.textContent = '⏰ ' + timers[id].time + ' - ' + timers[id].name;
Expand Down
31 changes: 31 additions & 0 deletions data-template/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,35 @@ input[type="number"] {
font-style: italic;
display: flex;
justify-content: center;
}

.logging-iframe {
border-radius: 15px;
overflow: hidden;
border: 2px solid black;
box-shadow: #333 0px 0px 10px;
width: 100%;
height: 100%;
}


.log-button {
position: fixed;
top: 10px;
left: 10px;
border: none;
}

.log-body {
width: 80%;
height: 68%;
margin: 20px auto;
border-color: #007bff;
border-radius: 10px;
border-width: 2px;
}

.refresh-div {
display: flex;
justify-content: center;
}
34 changes: 34 additions & 0 deletions lib/LoggingManager/LoggingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,40 @@ bool LoggingManager::is_initialized()
return this->initialized;
}

bool LoggingManager::reset_logs()
{
// check if file exists
if (!LittleFS.exists(this->filename))
{
File file = LittleFS.open(this->filename, "w");
file.close();

this->file_line_counter = 0;

this->log(LOG_LEVEL_INFO, "No log file found, new log file was created");

return true;
}

// delete file
if (LittleFS.remove(this->filename))
{
// create file
File file = LittleFS.open(this->filename, "w");
file.close();

this->file_line_counter = 0;

this->log(LOG_LEVEL_INFO, "Log file was reset");

return true;
}

this->log(LOG_LEVEL_ERROR, "Log file could not be reset");

return false;
}

String LoggingManager::log_string(log_level_t level)
{
String log_message = "(";
Expand Down
1 change: 1 addition & 0 deletions lib/LoggingManager/LoggingManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class LoggingManager

void begin();
bool is_initialized();
bool reset_logs();

void log(log_level_t level, const char *message);
void log(log_level_t level, String message);
Expand Down
13 changes: 11 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void setup()
int line_counter = loggingManager.count_log_lines() + 1;
int file_line_counter = loggingManager.get_file_line_counter() + 1;

loggingManager.start_seq(LOG_LEVEL_INFO_FILE, "GET /log_lines [");
loggingManager.start_seq(LOG_LEVEL_INFO, "GET /log_lines [");
loggingManager.append_seq(line_counter);
loggingManager.append_seq(", ");
loggingManager.append_seq(file_line_counter);
Expand Down Expand Up @@ -242,7 +242,6 @@ void setup()
server.on("/sleep", HTTP_GET, [](AsyncWebServerRequest *request)
{
loggingManager.log(LOG_LEVEL_INFO, "GET /sleep");
loggingManager.log(LOG_LEVEL_INFO_FILE, "Schlafmodus aktiviert");

goToSleep();

Expand Down Expand Up @@ -311,6 +310,14 @@ void setup()

request->send(200); });

// reset the logs (delete and create new file)
server.on("/reset_logs", HTTP_GET, [](AsyncWebServerRequest *request)
{
loggingManager.log(LOG_LEVEL_INFO, "GET /reset_logs");
loggingManager.reset_logs();

request->send(200); });

// get autosleep remaining time
server.on("/autosleep", HTTP_GET, [](AsyncWebServerRequest *request)
{
Expand Down Expand Up @@ -385,6 +392,8 @@ void goToSleep()
// Set next alert
alertManager.set_next_alert();

loggingManager.log(LOG_LEVEL_INFO_FILE, "Go to sleep");

// go to sleep
#if defined(ESP32DEV)
esp_deep_sleep_start();
Expand Down

0 comments on commit 9f24e90

Please sign in to comment.