chore: better logging

log some things only in dev mode
This commit is contained in:
2026-01-04 21:02:14 +01:00
parent 9843565c7b
commit c8920df9b3
5 changed files with 40 additions and 31 deletions
+15 -21
View File
@@ -7,6 +7,7 @@ import {
type ShellCommandResponse,
type TreeElement
} from './types';
import { dev } from '$app/environment';
export async function get_screenshot(ip: string): Promise<Blob | null> {
const options = { method: 'PATCH' };
@@ -62,7 +63,7 @@ export async function get_file_data(
echo
done
`;
const raw_response = await run_shell_command(ip, command, true);
const raw_response = await run_shell_command(ip, command);
if (!raw_response.ok || !raw_response.json) return null;
const json_response = raw_response.json as ShellCommandResponse;
if (json_response.exitCode === 0 && json_response.stdout.trim() === '') return [];
@@ -93,7 +94,7 @@ export async function get_file_data(
export async function get_file_tree_data(ip: string, path: string): Promise<TreeElement[] | null> {
const command = `cd ".${path}" && tree -Js`;
const raw_response = await run_shell_command(ip, command, true);
const raw_response = await run_shell_command(ip, command);
if (!raw_response.ok || !raw_response.json) return null;
const json_response = raw_response.json as ShellCommandResponse;
@@ -166,7 +167,6 @@ export async function get_thumbnail_blob(ip: string, path_to_file: string): Prom
ip,
`/file/preview${path_to_file}`,
{ method: 'GET' },
true,
[415]
);
if (!raw_response.ok || !raw_response.blob) return null;
@@ -188,11 +188,10 @@ async function request_display(
ip: string,
api_route: string,
options: { method: string; headers?: Record<string, string>; body?: string },
log_in_debug: boolean = false,
supress_error_handling_http_codes: number[] = []
): Promise<RequestResponse> {
const url = `http://${ip}:1323/api${api_route}`;
return await request(url, options, log_in_debug, supress_error_handling_http_codes);
return await request(url, options, supress_error_handling_http_codes);
}
async function request_control(
@@ -206,15 +205,12 @@ async function request_control(
async function request(
url: string,
options: { method: string; headers?: Record<string, string>; body?: string },
log_in_debug: boolean = false,
supress_error_handling_http_codes: number[] = []
): Promise<RequestResponse> {
try {
const cache_buster = `${url.includes('?') ? '&' : '?'}=${Date.now()}`;
if (log_in_debug) {
console.debug(url + cache_buster, options.body);
} else {
console.log(url + cache_buster, options.body);
if (dev) {
console.debug('Sending request: ', url + cache_buster, 'with', options.body ?? 'none');
}
const response = await fetch(url + cache_buster, options);
if (response.ok || supress_error_handling_http_codes.includes(response.status)) {
@@ -227,10 +223,8 @@ async function request(
const json: Record<string, unknown> = await response.json();
request_response = { ok: response.ok, http_code: response.status, json: json };
}
if (log_in_debug) {
if (dev) {
console.debug(request_response);
} else {
console.log(request_response);
}
return request_response;
}
@@ -250,7 +244,9 @@ async function request(
);
} catch (error: unknown) {
if (error instanceof TypeError && /fetch|NetworkError/i.test(error.message)) {
console.log('Request failed - Is the targeted device online?');
if (dev) {
console.warn('Request failed - Is the targeted device online?');
}
} else {
console.error(url, error);
notifications.push('error', `Fataler Fehler bei API-Anfrage`, `${url}\nFehler: ${error}`);
@@ -271,7 +267,9 @@ function handle_shell_error(
shell_response.stderr &&
/bash: line \d+: cd: .+: No such file or directory/.test(shell_response.stderr)
) {
console.log('current file_path does not exist on display:', ip);
if (dev) {
console.debug('current file_path does not exist on display:', ip);
}
return true;
}
console.error(shell_response);
@@ -286,11 +284,7 @@ function handle_shell_error(
return false;
}
async function run_shell_command(
ip: string,
command: string,
log_in_debug: boolean = false
): Promise<RequestResponse> {
async function run_shell_command(ip: string, command: string): Promise<RequestResponse> {
const options = {
method: 'PATCH',
headers: { 'content-type': 'application/json' },
@@ -298,5 +292,5 @@ async function run_shell_command(
command: command
})
};
return await request_display(ip, '/shellCommand', options, log_in_debug);
return await request_display(ip, '/shellCommand', options);
}
@@ -1,3 +1,4 @@
import { dev } from '$app/environment';
import { db } from './files_display.db';
import { get_display_by_id } from './stores/displays';
import { remove_all_files_without_display, remove_file_from_display } from './stores/files';
@@ -86,7 +87,9 @@ async function start_task_loop() {
break;
}
console.log('AKTUELL IN TASKS', tasks.length);
if (dev) {
console.debug('AKTUELL IN TASKS', tasks.length);
}
tasks.shift(); // Remove current_task from tasks
}
}
@@ -164,6 +167,6 @@ async function show_error(
`Datei: "${task.file_name}", Display-IP: ${task.display_ip}\nFehler: ${error}`
);
if (type === 'download') return;
await remove_file_from_display(task.display_id, task.file_primary_key);
await remove_file_from_display(task.display_id, task.file_primary_key);
await remove_all_files_without_display();
}
@@ -5,6 +5,7 @@ import { get_uuid, image_content_hash } from '../utils';
import { get_screenshot } from '../api_handler';
import { delete_and_deselect_unique_files_from_display } from './files';
import { db } from '../files_display.db';
import { dev } from '$app/environment';
export async function is_display_name_taken(name: string): Promise<boolean> {
const exists = await db.displays.where('name').equals(name).first();
@@ -23,11 +24,15 @@ export async function add_display(
let group_id: string;
if (group) {
group_id = group.id;
console.log('DISPLAYGROUP WURDE NICHT ERSTELLT');
if (dev) {
console.debug('DISPLAYGROUP WURDE NICHT ERSTELLT');
}
} else {
group_id = get_uuid();
await db.display_groups.put({ id: group_id, position: 0 });
console.log('DISPLAYGROUP WURDE ERSTELLT');
if (dev) {
console.info('DISPLAYGROUP WURDE ERSTELLT');
}
}
const element_count_in_group = (await db.displays.where('group_id').equals(group_id).toArray())
.length;
+1 -1
View File
@@ -29,7 +29,7 @@ export async function change_file_path(new_path: string) {
for (const display of displays) {
const changed_paths = await get_changed_directory_paths(display, new_path);
if (!changed_paths) continue;
console.log('Update file system from', display.name, ':', changed_paths);
console.debug('Update file system from', display.name, ':', changed_paths);
for (const path of changed_paths) {
await update_folder_elements_recursively(display, path);
}
+12 -5
View File
@@ -29,7 +29,11 @@
import PopUp from '$lib/components/PopUp.svelte';
import { get_file_primary_key, type Inode, type PopupContent } from '$lib/ts/types';
import TextInput from '$lib/components/TextInput.svelte';
import { first_letter_is_valid, get_accepted_file_type_string, is_valid_name } from '$lib/ts/utils';
import {
first_letter_is_valid,
get_accepted_file_type_string,
is_valid_name
} from '$lib/ts/utils';
import { delete_files, rename_file } from '$lib/ts/api_handler';
import HighlightedText from '$lib/components/HighlightedText.svelte';
import { liveQuery, type Observable } from 'dexie';
@@ -88,7 +92,7 @@
popup_close_function();
await run_for_selected_files_on_selected_displays(async (ip: string, file_names: string[]) => {
if (file_names.length !== 1) {
console.log('EEEERRRRROOOOOOR', file_names);
console.error(file_names);
return; // Error
}
await rename_file(ip, $current_file_path, file_names[0], new_file_name);
@@ -165,7 +169,8 @@
const trimmed_input = input.trim();
if (trimmed_input.length === 0 || trimmed_input.length > 50)
return [false, 'Ungültige Länge'];
if (!first_letter_is_valid(trimmed_input)) return [false, `Name darf nicht mit ${trimmed_input[0]} beginnen`];
if (!first_letter_is_valid(trimmed_input))
return [false, `Name darf nicht mit ${trimmed_input[0]} beginnen`];
if (!is_valid_name(trimmed_input)) return [false, 'Name enthält ungültige Zeichen'];
if (($current_folder_elements ?? []).some((e) => e.name === trimmed_input))
return [false, 'Name bereits verwendet'];
@@ -191,7 +196,8 @@
const trimmed_input = input.trim() + extension;
if (trimmed_input.length === 0 || trimmed_input.length > 50)
return [false, 'Ungültige Länge'];
if (!first_letter_is_valid(trimmed_input)) return [false, `Name darf nicht mit ${trimmed_input[0]} beginnen`];
if (!first_letter_is_valid(trimmed_input))
return [false, `Name darf nicht mit ${trimmed_input[0]} beginnen`];
if (!is_valid_name(trimmed_input)) return [false, 'Name enthält ungültige Zeichen'];
if (
($current_folder_elements ?? []).some(
@@ -250,7 +256,8 @@
bind:this={file_input}
multiple
accept={get_accepted_file_type_string()}
onchange={(e) => add_upload((e.target as HTMLInputElement).files!, $selected_display_ids, $current_file_path)}
onchange={(e) =>
add_upload((e.target as HTMLInputElement).files!, $selected_display_ids, $current_file_path)}
/>
<div class="bg-stone-800 h-full rounded-2xl grid grid-rows-[2.5rem_1fr] min-h-0">