Skip to content

Commit

Permalink
Add logging to file (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipecrs authored Mar 27, 2024
1 parent d54237d commit 5e95fa9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 37 deletions.
1 change: 0 additions & 1 deletion electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export default defineConfig({
plugins: [externalizeDepsPlugin()],
build: {
outDir: "dist/main",
minify: true,
},
},
});
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"main": "dist/main/index.js",
"scripts": {
"clean": "rimraf dist out",
"start": "electron-vite preview",
"start": "electron-vite preview --sourcemap",
"dev": "electron-vite dev",
"package": "electron-vite build && electron-forge package",
"make": "electron-vite build && electron-forge make",
Expand All @@ -27,6 +27,7 @@
"dependencies": {
"clipboard-event": "^1.6.0",
"electron-clipboard-ex": "^1.3.3",
"electron-log": "^5.1.2",
"electron-squirrel-startup": "^1.0.0",
"electron-store": "^8.2.0",
"follow-redirects": "^1.15.6",
Expand Down
7 changes: 4 additions & 3 deletions src/main/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from "node:fs/promises";
import path from "node:path";
import fswin from "fswin";
import log from "electron-log";

import { hostName, isReceivingFileNameSuffix } from "./global.js";
import {
Expand Down Expand Up @@ -140,15 +141,15 @@ export const cleanFiles = async (syncFolder: string) => {
/^((0|[1-9][0-9]*)-[0-9a-zA-Z-]+\.txt)|(receiving-[0-9a-zA-Z-]+\.txt)|([0-9a-zA-Z-]+\.is-reading\.txt)$/
);
if (match) {
console.log(`Deleting file used by previous versions: ${filePath}`);
log.info(`Deleting file used by previous versions: ${filePath}`);
await deleteFileOrFolderRecursively(filePath);
}
continue;
}

const fileStat = await fs.lstat(filePath);
if (fileStat.ctime.getTime() <= currentTimeMinus5Min) {
console.log(`Deleting: ${filePath}`);
log.info(`Deleting: ${filePath}`);
await deleteFileOrFolderRecursively(filePath);
continue;
}
Expand All @@ -159,7 +160,7 @@ export const cleanFiles = async (syncFolder: string) => {
parsedFile.from === "others" &&
fileStat.ctime.getTime() <= currentTimeMinus1Min
) {
console.log(`Unsyncing: ${filePath}`);
log.info(`Unsyncing: ${filePath}`);
unsyncFileOrFolderRecursively(filePath);
}
}
Expand Down
64 changes: 34 additions & 30 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import fs from "node:fs/promises";
import { watch, StatWatcher } from "node:fs";
import path from "node:path";
import type { ClipboardEventListener } from "clipboard-event";
import {
Menu,
Notification,
Tray,
app,
clipboard,
dialog,
Menu,
nativeImage,
Notification,
shell,
Tray,
} from "electron";
import clipboardEx from "electron-clipboard-ex";
import log from "electron-log";
import Store from "electron-store";
import cron from "node-cron";
import { StatWatcher, watch } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
import { gt as semverGreaterThan } from "semver";
import type { ClipboardEventListener } from "clipboard-event";

import {
cleanFiles,
parseClipboardFileName,
getNextFileNumber as getNextFileNumber,
isThereMoreThanOneClipboardFile,
isIsReceivingFile,
ClipboardType,
ClipboardText,
isClipboardTextEquals,
isClipboardTextEmpty,
ClipboardType,
ParsedClipboardFileName,
cleanFiles,
getNextFileNumber,
isClipboardTextEmpty,
isClipboardTextEquals,
isIsReceivingFile,
isThereMoreThanOneClipboardFile,
parseClipboardFileName,
} from "./clipboard.js";
import { hostName, hostNameIsReceivingFileName } from "./global.js";
import {
Expand All @@ -41,18 +42,19 @@ import {

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if ((await import("electron-squirrel-startup")).default) {
console.error("Squirrel event handled. Quitting...");
app.exit();
}

const gotTheLock = app.requestSingleInstanceLock();

if (!gotTheLock) {
console.error("Another instance is already running. Quitting...");
app.exit();
}

process.on("uncaughtException", function (error) {
console.error(`Uncaught exception: ${error}`);
});
log.errorHandler.startCatching({ showDialog: false });
log.eventLogger.startLogging();

type ConfigType = {
folder?: string;
Expand Down Expand Up @@ -110,7 +112,7 @@ const writeClipboardToFile = async () => {
(file) => isIsReceivingFile(file) && file !== hostNameIsReceivingFileName
).length === 0
) {
console.error(
log.info(
"No other computer is receiving clipboards. Skipping clipboard send..."
);
return;
Expand Down Expand Up @@ -159,7 +161,7 @@ const writeClipboardToFile = async () => {
clipboardType = "files";
}
} catch (error) {
console.error("Error reading current clipboard");
log.error(`Error reading current clipboard:\n${error}`);
return;
}

Expand Down Expand Up @@ -243,7 +245,7 @@ const writeClipboardToFile = async () => {
}
}
}
console.log(`Clipboard written to ${destinationPath}`);
log.info(`Clipboard written to ${destinationPath}`);
lastTimeWritten = currentTime;
lastFileNumberWritten = fileNumber;

Expand Down Expand Up @@ -284,14 +286,13 @@ const readClipboardFromFile = async (parsedFile: ParsedClipboardFileName) => {
}
newFilesCount = parsedFile.filesCount;
if (!newFilesCount) {
console.error(
`Could not read the number of files in ${file}. Skipping...`
);
// This should not happen, but just in case
log.warn(`Could not read the number of files in ${file}. Skipping...`);
return;
}
const filesCountInFolder = await getTotalNumberOfFiles([file]);
if (newFilesCount !== filesCountInFolder) {
console.error(
log.info(
`Not all files are yet present in _files folder. Current: ${filesCountInFolder}, expected: ${newFilesCount}. Skipping...`
);
return;
Expand All @@ -301,7 +302,7 @@ const readClipboardFromFile = async (parsedFile: ParsedClipboardFileName) => {
);
}
} catch (error) {
console.error(`Error reading clipboard from file ${file}: ${error}`);
log.error(`Error reading clipboard ${file}:\n${error}`);
return;
}

Expand Down Expand Up @@ -338,7 +339,7 @@ const readClipboardFromFile = async (parsedFile: ParsedClipboardFileName) => {
currentClipboardType = "files";
}
} catch (error) {
console.error(`Error reading current clipboard: ${error}`);
log.error(`Error reading current clipboard: ${error}`);
return;
}

Expand Down Expand Up @@ -370,6 +371,9 @@ const readClipboardFromFile = async (parsedFile: ParsedClipboardFileName) => {
lastFileNumberWritten &&
currentFileNumber < lastFileNumberWritten
) {
log.info(
`Skipping reading clipboard from ${file} as a newer clipboard was already sent`
);
return;
}

Expand All @@ -383,7 +387,7 @@ const readClipboardFromFile = async (parsedFile: ParsedClipboardFileName) => {
clipboardEx.writeFilePaths(newFilePaths);
lastClipboardFilePathsRead = newFilePaths;
}
console.log(`Clipboard was read from ${file}`);
log.info(`Clipboard was read from ${file}`);
lastTimeRead = currentTime;

setIconFor5Seconds("clipboard_received");
Expand Down Expand Up @@ -563,7 +567,7 @@ const cleanup = async () => {
};

const reload = async () => {
console.log("Reloading configuration...");
log.info("Reloading configuration...");
await cleanup();
await initialize();
};
Expand Down Expand Up @@ -622,7 +626,7 @@ const isUpdateAvailable = async () => {
path: "/felipecrs/clipboard-sync/releases/latest",
});
} catch (error) {
console.error(`Could not get latest version from GitHub: ${error}`);
log.error(`Could not get latest version from GitHub:\n${error}`);
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { RequestOptions } from "node:https";
import { promisify } from "node:util";
import { createHash } from "node:crypto";
import followRedirects from "follow-redirects";
import log from "electron-log";

export const isArrayEquals = (arr1?: any[], arr2?: any[]) => {
if (arr1 && arr2 && arr1.length == arr2.length) {
Expand Down Expand Up @@ -42,7 +43,7 @@ export const iterateThroughFilesRecursively = async (
}
}
} catch (err) {
console.error(err);
log.error(`Error while iterating through ${fileOrFolder}:\n${err}`);
}
}
return results;
Expand Down Expand Up @@ -88,7 +89,7 @@ export const deleteFileOrFolderRecursively = async (fileOrFolder: string) => {
await fs.unlink(fileOrFolder);
}
} catch (err) {
console.error(err);
log.error(`Error deleting ${fileOrFolder}:\n${err}`);
}
};

Expand Down

0 comments on commit 5e95fa9

Please sign in to comment.