Skip to content

Commit

Permalink
Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Oct 14, 2022
1 parent fe58d4f commit 76aa227
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ fn main() {
utils::update_tray,
utils::random_values,
utils::logger,
utils::write_logs
])
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
println!("{}, {argv:?}, {cwd}", app.package_info().name);
Expand Down
15 changes: 14 additions & 1 deletion core/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use std::env;
use std::{env, fs};
use std::io::Write;
use tauri::{GlobalShortcutManager, Manager};

#[tauri::command]
Expand Down Expand Up @@ -54,3 +55,15 @@ pub fn logger(message: String, time: String, kind: &str) {
),
}
}

#[tauri::command]
pub fn write_logs(name: String, message: String) {
let mut file = fs::OpenOptions::new()
.write(true)
.append(true)
.create(true)
.open(name)
.unwrap();

write!(file, "{}", message).unwrap();
}
2 changes: 1 addition & 1 deletion core/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"allowlist": {
"all": true,
"fs": {
"scope": ["$CONFIG/Levminer/*"]
"scope": ["$CACHE/com.levminer.authme/*"]
}
},
"bundle": {
Expand Down
7 changes: 7 additions & 0 deletions interface/layout/app.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import { Route, router } from "tinro"
import { onMount } from "svelte"
import { state } from "../stores/state"
import logger from "interface/utils/logger"
import UpdateAlert from "../components/updateAlert.svelte"
import BuildNumber from "../components/buildNumber.svelte"
Expand All @@ -46,10 +47,16 @@
import Edit from "../windows/edit/edit.svelte"
onMount(() => {
// Listen for router events
router.subscribe((data) => {
console.log("Path changed:", data)
document.querySelector(".top").scrollIntoView()
})
// Listen for errors
window.addEventListener("unhandledrejection", (error) => {
logger.error(`Unknown runtime error occurred: ${error.reason}`)
})
})
</script>
27 changes: 23 additions & 4 deletions interface/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { invoke } from "@tauri-apps/api"
import { invoke, path, fs } from "@tauri-apps/api"
import { dev } from "../../build.json"

let fileName: string

const getTime = () => {
return new Date().toLocaleString("hu")
Expand All @@ -8,24 +11,40 @@ export const log = (message: string) => {
const time = getTime()

console.log(`[AUTHME LOG] (${time}) ${`${message}`}`)

invoke("logger", { message, time, kind: "log" })
writeToFile(`[AUTHME LOG] (${time}) ${`${message}`}`)
}

export const warn = (message: string) => {
const time = getTime()

console.log(`[AUTHME WARN] (${time}) ${message}`)

invoke("logger", { message, time, kind: "warn" })
writeToFile(`[AUTHME WARN] (${time}) ${message}`)
}

export const error = (message: string) => {
const time = getTime()

console.log(`[AUTHME ERROR] (${time}) ${message}`)

invoke("logger", { message, time, kind: "error" })
writeToFile(`[AUTHME ERROR] (${time}) ${message}`)
}

const writeToFile = async (message: string) => {
if (dev === true) {
return
}

const time = new Date().toISOString().replace("T", "-").replaceAll(":", "-").substring(0, 19)
const folderPath = await path.join(await path.cacheDir(), "com.levminer.authme", "logs")
await fs.createDir(folderPath, { recursive: true })

if (fileName === undefined) {
fileName = `authme-${time}.log`
}

invoke("write_logs", { name: `${folderPath}/${fileName}`, message: `${message}\n` })
}

export default { log, warn, error }
3 changes: 1 addition & 2 deletions interface/windows/settings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ export const clearData = async () => {
}

export const showLogs = async () => {
const folderPath = await path.join(await path.configDir(), "Levminer", "Authme 4")

const folderPath = await path.join(await path.cacheDir(), "com.levminer.authme", "logs")
open(folderPath)
}

Expand Down

0 comments on commit 76aa227

Please sign in to comment.