Skip to content

Commit

Permalink
Unify logging between Tauri/Front-End, remove pino (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie authored Mar 28, 2024
1 parent 5ca4449 commit 77e91c4
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 280 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
"lodash": "^4.17.21",
"nanoid": "^5.0.6",
"normalize.css": "^8.0.1",
"pino": "^8.19.0",
"pino-pretty": "^10.3.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-fontawesome": "^1.7.1",
Expand Down
42 changes: 0 additions & 42 deletions src/lib/logger.ts

This file was deleted.

20 changes: 16 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { error } from '@tauri-apps/plugin-log';
import * as logger from '@tauri-apps/plugin-log';

import useToastsStore from '../stores/useToastsStore';

Expand Down Expand Up @@ -28,7 +28,12 @@ export const parseDuration = (duration: number | null): string => {
* Friendly logging for caught errors
* https://kentcdodds.com/blog/get-a-catch-block-error-message-with-typescript
*/
export const logAndNotifyError = (err: unknown, pre?: string): void => {
export const logAndNotifyError = (
err: unknown,
pre?: string,
isWarning = false,
silent = false,
): void => {
let message;
if (err instanceof Error) message = err.message;
else message = String(err);
Expand All @@ -37,6 +42,13 @@ export const logAndNotifyError = (err: unknown, pre?: string): void => {
message = `${pre}: ${message}`;
}

error(message);
useToastsStore.getState().api.add('danger', message);
if (isWarning) {
logger.warn(message);
} else {
logger.error(message);
}

if (silent === false) {
useToastsStore.getState().api.add('danger', message);
}
};
4 changes: 2 additions & 2 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React from 'react';
import * as ReactDOM from 'react-dom/client';
import { RouterProvider } from 'react-router-dom';
import { QueryClientProvider } from '@tanstack/react-query';
import { attachConsole } from '@tauri-apps/plugin-log';
import * as logger from '@tauri-apps/plugin-log';

import router from './views/router';
import { queryClient } from './lib/query';
Expand All @@ -28,7 +28,7 @@ import './styles/main.module.css';
|--------------------------------------------------------------------------
*/

attachConsole();
logger.attachConsole();

const wrap = document.getElementById('wrap');

Expand Down
3 changes: 1 addition & 2 deletions src/stores/PlaylistsAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Playlist, Track } from '../generated/typings';
import logger from '../lib/logger';
import channels from '../lib/ipc-channels';
import router from '../views/router';
import library from '../lib/library';
Expand All @@ -16,7 +15,7 @@ const play = async (playlistID: string): Promise<void> => {
try {
const playlist = await library.getPlaylist(playlistID);
const tracks = await library.getTracks(playlist.tracks);
usePlayerStore.getState().api.start(tracks).catch(logger.warn);
usePlayerStore.getState().api.start(tracks);
} catch (err) {
logAndNotifyError(err);
}
Expand Down
11 changes: 7 additions & 4 deletions src/stores/SettingsAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Config, DefaultView } from '../generated/typings';
import { Theme } from '../types/museeks';
import { themes } from '../lib/themes';
import config from '../lib/config';
import { logAndNotifyError } from '../lib/utils';

import useToastsStore from './useToastsStore';

Expand Down Expand Up @@ -102,10 +103,12 @@ const checkForUpdate = async (
useToastsStore.getState().api.add('success', message);
}
} catch (e) {
if (!options.silentFail)
useToastsStore
.getState()
.api.add('danger', 'An error occurred while checking updates.');
logAndNotifyError(
e,
'An error occurred while checking updates.',
true,
options.silentFail,
);
}
};

Expand Down
5 changes: 2 additions & 3 deletions src/stores/usePlayerStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { persist } from 'zustand/middleware';

import { PlayerStatus } from '../types/museeks';
import { shuffleTracks } from '../lib/utils-player';
import logger from '../lib/logger';
import router from '../views/router';
import player from '../lib/player';
import { Track, Repeat } from '../generated/typings';
Expand Down Expand Up @@ -500,9 +499,9 @@ function createPlayerStore<T extends PlayerState>(store: StateCreator<T>) {
onRehydrateStorage: () => {
return (state, error) => {
if (error || state == null) {
logger.error(
'an error happened during player store hydration',
logAndNotifyError(
error,
'an error happened during player store hydration',
);
} else {
// Let's set the player's src and currentTime with the info we have persisted in store
Expand Down
6 changes: 3 additions & 3 deletions src/views/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
isRouteErrorResponse,
useRouteError,
} from 'react-router-dom';
import * as logger from '@tauri-apps/plugin-log';

import * as ViewMessage from '../elements/ViewMessage/ViewMessage';
import ExternalLink from '../elements/ExternalLink/ExternalLink';
import logger from '../lib/logger';

import RootView from './Root';
import ViewLibrary from './ViewLibrary';
Expand Down Expand Up @@ -92,8 +92,6 @@ export default router;

function GlobalErrorBoundary() {
const error = useRouteError();
logger.error(error);

let errorMessage: string;

if (isRouteErrorResponse(error)) {
Expand All @@ -106,6 +104,8 @@ function GlobalErrorBoundary() {
errorMessage = 'Unknown error';
}

logger.error(errorMessage);

return (
<ViewMessage.Notice>
<p>
Expand Down
Loading

0 comments on commit 77e91c4

Please sign in to comment.