Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
VITE_BASEURL=https://api.example.com
VITE_BEARERTOKEN=YOUR_BEARER_TOKEN
VITE_BEARERTOKEN=YOUR_BEARER_TOKEN
VITE_LOG_FILE_NAME = vigad.log
VITE_LOGLEVEL = error
4 changes: 4 additions & 0 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ async function createWindow() {

// Get all screens/windows from the main process to the renderer process
ipcMain.handle('get-screens', getScreen);

// Electron wants this to be called initially before calling
// app.getPath('logs')
app.setAppLogsPath();
}

// This method will be called when Electron has finished
Expand Down
67 changes: 67 additions & 0 deletions electron/preload/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { app } from 'electron';
import log from 'electron-log';
import path from 'node:path';
import fs from 'fs';

function domReady(
condition: DocumentReadyState[] = ['complete', 'interactive']
) {
Expand Down Expand Up @@ -110,4 +115,66 @@ contextBridge.exposeInMainWorld('electronAPI', {
});
});
},
saveLog: (
message: string,
level: ElectronLogLevel = ElectronLogLevel.INFO,
logFileName: string
) => {
//github.com/finos/SymphonyElectron/blob/0431a9f5add13cd16c19006775f1e907f3c3b2ce/src/common/logger.ts#L66
const logDirectoryPath = path.join(app.getPath('exe'), 'logs');

// Create the log directory if it doesn't exist
if (!fs.existsSync(logDirectoryPath)) {
fs.mkdirSync(logDirectoryPath);
}

const logFilePath = path.join(logDirectoryPath, logFileName);

try {
// Save log message using electron-log
log.transports.file.resolvePath = () => logFilePath;
log[level](message);

return { success: true, message: message };
} catch (error) {
console.error(error);
return { success: false, message: 'Error saving log file.' };
}

// const logDirecotryName = 'logs';

// console.log(process.env.NODE_ENV);

// log.transports.file.format = '[{h}:{i}:{s}:{ms}] [{level}] {text}';
// log.transports.file.maxSize = 5 * 1024 * 1024; // 5 MB

// if (process.env.NODE_ENV === 'development') {
// // Set the desired log file name
// log.transports.file.resolvePath = () =>
// path.join(logDirecotryName, logFileName);

// // Save log message using electron-log
// log[level](message);
// } else {
// // TODO: cant save log file in the root directory of the installed application yet
// // Set the desired log file name in the root directory of the installed application
// const logFilePath = path.join(app.getPath('exe'), logFileName);
// log.transports.file.resolvePath = () => logFilePath;

// // Save log message using electron-log
// log[level](message);
// }
},
});

/**
* Electron log levels enum declaration
*/
export enum ElectronLogLevel {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just updating the values for these enums to numbers (as in .env) may be a good solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However you need to make sure that eg. loglevel INFO also includes everything WARN & ERROR (this only makes sense)

Copy link
Contributor

@tonoizer tonoizer Jun 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we still need to discuss what we really want out of this

INFO = 'info',
WARN = 'warn',
ERROR = 'error',
VERBOSE = 'verbose',
DEBUG = 'debug',
SILLY = 'silly',
}
28 changes: 28 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@
"@vueuse/components": "^10.1.2",
"@vueuse/core": "^10.1.2",
"clipboardy": "^3.0.0",
"electron-log": "^4.4.8",
"randexp": "^0.5.3",
"roboto-fontface": "^0.10.0",
"tesseract.js": "^3.0.3",
"tplant": "^3.1.0",
"tslog": "^4.8.2",
"vue-router": "^4.2.2",
"vue3-drag-resize": "^2.0.5",
"vuetify": "^3.3.2"
Expand Down
6 changes: 6 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,24 @@ import BottomNavigation from '@/components/Navigation/BottomNavigation.vue';
import NotificantionProvider from '@/components/Notifications/NotificationProvider/NotificationProvider.vue';
import { NotificationAnchorPosition } from '@/components/Notifications/NotificationAnchorPosition';
import useStreamHandler from '@/composables/useStreamHandler/useStreamHandler';
import useLogger from '@/composables/useLogger/useLogger';

// Force the application to navigate to the default route
const router = useRouter();

// Get the default preview video stream function
const { setDefaultPreviewVideoStream } = useStreamHandler();

// Get the addLog function from the useLogger composable
const { addLog } = useLogger();

onMounted(async () => {
// set the default preview video stream
await setDefaultPreviewVideoStream();
// navigate to the default route
router.push('/');
// add a log entry
addLog('Application started');
});
</script>

Expand Down
11 changes: 11 additions & 0 deletions src/composables/useLogger/electron-log-level.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Electron log levels enum
*/
export enum ElectronLogLevel {
INFO = 'info',
WARN = 'warn',
ERROR = 'error',
VERBOSE = 'verbose',
DEBUG = 'debug',
SILLY = 'silly',
}
87 changes: 87 additions & 0 deletions src/composables/useLogger/useLogger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { ref } from 'vue';
import { Logger } from 'tslog';
import { ElectronLogLevel } from './electron-log-level';

/**
* Create a logger instance
* @type {Logger}
*/
const logger = new Logger();

/**
* Reference to the electron logger in the main process
* @type {any}
*/
const electronLogger = (window as any).electronAPI;

/**
* Logger composable
*/
export default function useLogger() {
const logMessages = ref<string[]>([]);

/**
* Function to add log messages
* @param {string} message - The log message
* @param {ElectronLogLevel} [logLevel=ElectronLogLevel.INFO] - The log level
*/
const addLog = (
message: string,
logLevel = ElectronLogLevel.INFO,
fileName = import.meta.env.VITE_LOG_FILE_NAME
) => {
logger.debug(message);
logMessages.value.push(message);
electronLogger.saveLog(message, logLevel, fileName);
};

/**
* Function to add warning log messages
* @param {string} message - The log message
*/
const addWarnLog = (message: string) => {
addLog(message, ElectronLogLevel.WARN);
};

/**
* Function to add error log messages
* @param {string} message - The log message
*/
const addErrorLog = (message: string) => {
addLog(message, ElectronLogLevel.ERROR);
};

/**
* Function to add verbose log messages
* @param {string} message - The log message
*/
const addVerboseLog = (message: string) => {
addLog(message, ElectronLogLevel.VERBOSE);
};

/**
* Function to add debug log messages
* @param {string} message - The log message
*/
const addDebugLog = (message: string) => {
addLog(message, ElectronLogLevel.DEBUG);
};

/**
* Function to add silly log messages
* @param {string} message - The log message
*/
const addSillyLog = (message: string) => {
addLog(message, ElectronLogLevel.SILLY);
};

return {
log: logMessages,
addLog,
addErrorLog,
addWarnLog,
addVerboseLog,
addDebugLog,
addSillyLog,
};
}
13 changes: 13 additions & 0 deletions src/composables/useNotificationSystem/useNotificationSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ref } from 'vue';
import useTokenGenerator from '@/composables/useTokenGenerator/useTokenGenerator';
import useLogger from '@/composables/useLogger/useLogger';

/**
* notifications list
Expand All @@ -11,6 +12,7 @@ const notifications = ref<Notification[]>([]);
*/
export default function useNotificationSystem() {
const { generateValidToken } = useTokenGenerator();
const { addLog, addWarnLog, addErrorLog } = useLogger();

/**
* Create a notification
Expand All @@ -22,6 +24,17 @@ export default function useNotificationSystem() {
options
);

const type = _options.type;

// Add log message to the log file
if (type === 'error') {
addErrorLog(_options.title);
} else if (type === 'warning') {
addWarnLog(_options.title);
} else {
addLog(_options.title);
}

notifications.value.push(
...[
{
Expand Down