Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Metroxe committed Feb 27, 2024
2 parents fd66c29 + 61848ea commit 785295b
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 29 deletions.
4 changes: 3 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"date-prompt": "^1.0.0",
"inquirer": "^9.2.12",
"inquirer-autocomplete-prompt": "^3.0.1",
"vorpal": "^1.12.0"
"vorpal": "^1.12.0",
"winston": "^3.11.0",
"winston-daily-rotate-file": "^4.7.1"
},
"devDependencies": {
"@types/inquirer": "9.0.7",
Expand Down
9 changes: 8 additions & 1 deletion apps/cli/src/commands/operator-control/operator-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vorpal from "vorpal";
import Logger from "../../utils/Logger.js"
import { getSignerFromPrivateKey, operatorRuntime, listOwnersForOperator, Challenge, PublicNodeBucketInformation } from "@sentry/core";

/**
Expand Down Expand Up @@ -62,7 +63,13 @@ export function bootOperator(cli: Vorpal) {
stopFunction = await operatorRuntime(
signer,
undefined,
(log: string) => this.log(log),
(log: string) => {
if (log.startsWith("Error")) {
Logger.error(log);
return;
}
Logger.log(log)
},
selectedOwners,
(publicNodeData: PublicNodeBucketInformation | undefined, challenge: Challenge, message: string) => {
const errorMessage = `The comparison between public node and challenge failed:\n` +
Expand Down
75 changes: 75 additions & 0 deletions apps/cli/src/utils/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import path from 'path';
import fs from 'fs';
import DailyRotateFile from 'winston-daily-rotate-file';
import winston from 'winston';

let logger: winston.Logger;
const getLogger = () => {
if (!logger) {
const transports = [];

transports.push(
new winston.transports.Console({
format: winston.format.printf((log: { message: string }) => log.message),
})
)

const LOG_PATH = getLogPath();
mkdir(LOG_PATH);
mkdir(path.join(LOG_PATH, 'errors'));

transports.push(
new DailyRotateFile({
dirname: LOG_PATH,
filename: 'combined',
datePattern: 'yyyyMMDD',
extension: '.log',
maxFiles: '3d'
}),
)

transports.push(
new DailyRotateFile({
dirname: path.join(LOG_PATH, 'errors'),
filename: 'error',
datePattern: 'yyyyMMDD',
level: 'error',
extension: '.log',
maxFiles: '14d'
})
)

logger = winston.createLogger({
transports
});
}

return logger;
};

// Return the os specific app data folder
// OS X - '/Users/user/Library/Preferences/sentry-cli/logs'
// Windows - 'C:\Users\user\AppData\Roaming\sentry-cli\logs'
// Linux - '/home/user/.local/share/sentry-cli/logs'
const getLogPath = (): string => {
const root = process.env.APPDATA || (process.platform == 'darwin' ? process.env.HOME + '/Library/Preferences' : process.env.HOME + "/.local/share");
return path.join(root, 'sentry-cli', 'logs')
}

const mkdir = (_path: string): void => {
if (!fs.existsSync(_path)) {
fs.mkdirSync(_path, { recursive: true });
}
}

const log = (...args: any): void => {
getLogger().info({ timestamp: new Date().toLocaleString(), level: 'INFO', message: args, PID: process.pid });
};

const error = (...args: any): void => {
getLogger().error({ timestamp: new Date().toLocaleString(), level: 'ERROR', message: args, PID: process.pid });
};

export default {
log, error
}
2 changes: 2 additions & 0 deletions apps/sentry-client-desktop/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {autoUpdater} from 'electron-updater';

const isWindows = os.platform() === "win32";

log.initialize();

//-------------------------------------------------------------------
// Logging
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PropsWithChildren, useState} from "react";
import {CountryDropdown} from "@/components/blockpass/CountryDropdown";
import log from "electron-log";

interface BlockpassProps {
onClick?: () => void;
Expand All @@ -24,7 +25,7 @@ export function Blockpass({onClick = () => {}, children = "Begin KYC"}: PropsWit
return window.electron.openExternal(`https://verify-with.blockpass.org/?clientId=xai_node_007da`);
}
} else {
console.log("Please select a country");
log.info("Please select a country");
}
}

Expand Down
9 changes: 5 additions & 4 deletions apps/sentry-client-desktop/src/features/home/SentryWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {AssignKeysSentryNotRunning} from "@/components/AssignKeysSentryNotRunnin
import {GrRefresh} from "react-icons/gr";
import {LuListChecks} from "react-icons/lu";
import {useStorage} from "@/features/storage";
import log from "electron-log";

// TODO -> replace with dynamic value later
export const recommendedFundingBalance = ethers.parseEther("0.005");
Expand Down Expand Up @@ -84,10 +85,10 @@ export function SentryWallet() {
}, 2000);
})
.catch(err => {
console.error('Unable to copy to clipboard: ', err);
log.error('Unable to copy to clipboard: ', err);
});
} else {
console.error('Clipboard API not available, unable to copy to clipboard');
log.error('Clipboard API not available, unable to copy to clipboard');
}
}

Expand All @@ -101,10 +102,10 @@ export function SentryWallet() {
}, 2000);
})
.catch(err => {
console.error('Unable to copy to clipboard: ', err);
log.error('Unable to copy to clipboard: ', err);
});
} else {
console.error('Clipboard API not available, unable to copy to clipboard');
log.error('Clipboard API not available, unable to copy to clipboard');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {useBalance} from "@/hooks/useBalance";
import {recommendedFundingBalance} from "@/features/home/SentryWallet";
import {getLatestChallenge} from "@sentry/core";
import {ReactNode, useEffect, useState} from "react";
import log from "electron-log";

export function SentryNodeStatusCard() {
const {publicKey} = useOperator();
Expand All @@ -26,7 +27,7 @@ export function SentryNodeStatusCard() {
: <div>Error retrieving challenge data</div>
);
} catch (error) {
console.error('Error fetching latest challenge:', error);
log.error('Error fetching latest challenge:', error);
setTimeAgoString(<div>Error fetching latest challenge</div>);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {useOperator} from "../../operator";
import {PiCopy} from "react-icons/pi";
import {useState} from "react";
import {BiLoaderAlt} from "react-icons/bi";
import log from "electron-log";

export function ExportSentryDrawer() {
const setDrawerState = useSetAtom(drawerStateAtom);
Expand All @@ -21,10 +22,10 @@ export function ExportSentryDrawer() {
}, 2000);
})
.catch(err => {
console.error('Unable to copy to clipboard: ', err);
log.error('Unable to copy to clipboard: ', err);
});
} else {
console.error('Clipboard API not available, unable to copy to clipboard');
log.error('Clipboard API not available, unable to copy to clipboard');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {useOperator} from "@/features/operator";
import {useState} from "react";
import {accruingStateAtom} from "@/hooks/useAccruingInfo";
import {useAtomValue} from "jotai";
import log from "electron-log";

export function FundsInSentryWalletCard() {
const {isLoading: isOperatorLoading, publicKey: operatorAddress} = useOperator();
Expand All @@ -26,10 +27,10 @@ export function FundsInSentryWalletCard() {
}, 2000);
})
.catch(err => {
console.error('Unable to copy to clipboard: ', err);
log.error('Unable to copy to clipboard: ', err);
});
} else {
console.error('Clipboard API not available, unable to copy to clipboard');
log.error('Clipboard API not available, unable to copy to clipboard');
}
}

Expand Down
5 changes: 3 additions & 2 deletions apps/sentry-client-desktop/src/features/keys/HasKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {ethers} from "ethers";
import {BiLoaderAlt} from "react-icons/bi";
import {useGetWalletBalance} from "@/hooks/useGetWalletBalance";
import {useGetSingleWalletBalance} from "@/hooks/useGetSingleWalletBalance";
import log from "electron-log";

interface HasKeysProps {
combinedOwners: string[],
Expand Down Expand Up @@ -213,10 +214,10 @@ export function HasKeys({combinedOwners, combinedLicensesMap, statusMap, isWalle
}, 1500);
})
.catch(err => {
console.error('Unable to copy to clipboard: ', err);
log.error('Unable to copy to clipboard: ', err);
});
} else {
console.error('Clipboard API not available, unable to copy to clipboard');
log.error('Clipboard API not available, unable to copy to clipboard');
}
}

Expand Down
6 changes: 4 additions & 2 deletions apps/sentry-client-desktop/src/features/keys/StatusPulse.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import log from "electron-log";

interface PulseStyle {
size?: "sm" | "md"
}
Expand All @@ -16,7 +18,7 @@ export function GreenPulse({size="sm"}: PulseStyle) {
pulseH = "1.25rem";
break;
default:
console.log("Invalid size"); // Handle the case where size is none of the specified values
log.info("Invalid size"); // Handle the case where size is none of the specified values
}

const greenPulseStyle = {
Expand Down Expand Up @@ -48,7 +50,7 @@ export function YellowPulse({size="sm"}: PulseStyle) {
pulseH = "1.25rem";
break;
default:
console.log("Invalid size"); // Handle the case where size is none of the specified values
log.info("Invalid size"); // Handle the case where size is none of the specified values
}

const yellowPulseStyle = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useEffect, useState} from 'react';
import {atom, useAtom} from 'jotai';
import log from "electron-log";

const dataAtom = atom<IData | undefined>(undefined);

Expand Down Expand Up @@ -57,7 +58,7 @@ export function useStorage(): IUseStorageResponse {
await window.ipcRenderer.invoke('fs-writeFileSync', filePath, JSON.stringify(newData));
setData(newData);
} catch (error) {
console.error(error);
log.error(error);
} finally {
setLoading(false);
}
Expand All @@ -70,7 +71,7 @@ export function useStorage(): IUseStorageResponse {
await window.ipcRenderer.invoke('fs-unlinkSync', filePath);
setData(undefined);
} catch (error) {
console.error(error);
log.error(error);
} finally {
setLoading(false);
}
Expand Down
23 changes: 14 additions & 9 deletions apps/sentry-client-desktop/src/hooks/useOperatorRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {useOperator} from "@/features/operator";
import {atom, useAtom} from "jotai";
import {useEffect, useState} from "react";
import {useStorage} from "@/features/storage";
import log from "electron-log";

function onAssertionMissMatch(publicNodeData: PublicNodeBucketInformation, challenge: Challenge, message: string) {
const errorMessage = `The comparison between public node and challenge failed:\n` +
Expand Down Expand Up @@ -36,16 +37,20 @@ export function useOperatorRuntime() {
}
}, [signer]);

function writeLog(log: string) {
console.info(log); // for debugging purposes
const _logs = runtimeLogs.concat([]);
if (_logs.length === 1000) {
_logs.shift();
}
function writeLog(message: string) {
if (message.startsWith("Error")) {
log.error(message);
} else {
log.info(message);
}
const _logs = runtimeLogs.concat([]);
if (_logs.length === 1000) {
_logs.shift();
}

_logs.push(log);
setRuntimeLogs(_logs);
}
_logs.push(message);
setRuntimeLogs(_logs);
}

async function startRuntime() {
if (!sentryRunning && stop === undefined) {
Expand Down
3 changes: 2 additions & 1 deletion apps/sentry-client-desktop/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as Sentry from "@sentry/react";
import {HttpClient} from "@sentry/integrations";
import "./index.css";
import {createRoutesFromChildren, matchRoutes, useLocation, useNavigationType} from "react-router-dom";
import log from "electron-log";

const rootElement = document.getElementById('root')!

Expand Down Expand Up @@ -55,5 +56,5 @@ postMessage({payload: 'removeLoading'}, '*')

// Use contextBridge
window.ipcRenderer.on('main-process-message', (_event, message) => {
console.log(message)
log.info(message)
})
Loading

0 comments on commit 785295b

Please sign in to comment.