Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Sep 28, 2022
1 parent 6333827 commit 77a8665
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ fn main() {
utils::get_args,
utils::update_tray,
utils::random_values,
utils::logger,
])
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
println!("{}, {argv:?}, {cwd}", app.package_info().name);
Expand Down
22 changes: 22 additions & 0 deletions core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,25 @@ pub fn random_values(length: usize) -> String {

rand_string.into()
}

#[tauri::command]
pub fn logger(message: String, time: String, kind: &str) {
match kind {
"log" => println!(
"\x1b[32m[AUTHME LOG] \x1b[34m({}) \x1b[37m{}",
time, message
),
"warn" => println!(
"\x1b[33m[AUTHME WARN] \x1b[34m({}) \x1b[37m{}",
time, message
),
"error" => println!(
"\x1b[31m[AUTHME ERROR] \x1b[34m({}) \x1b[37m{}",
time, message
),
&_ => println!(
"\x1b[31m[AUTHME LOG] \x1b[34m({}) \x1b[37m{}",
time, message
),
}
}
5 changes: 3 additions & 2 deletions interface/utils/encryption.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { invoke, dialog } from "@tauri-apps/api"
import { getSettings, setSettings } from "interface/stores/settings"
import logger from "./logger"

const settings = getSettings()

Expand Down Expand Up @@ -112,7 +113,7 @@ export const createWebAuthnLogin = async () => {
} catch (error) {
dialog.message(`Failed to register your authenticator! This feature might not be supported on your machine. \n\n${error}`, { type: "error" })

console.log(error)
logger.error(`Failed to register hardware key: ${error}`)

return "error"
}
Expand All @@ -139,7 +140,7 @@ export const verifyWebAuthnLogin = async () => {
} catch (error) {
dialog.message(`Failed to login with your authenticator. Please try again! \n\n${error}`, { type: "error" })

console.log(error)
logger.error(`Failed to login with hardware key: ${error}`)

return "error"
}
Expand Down
31 changes: 31 additions & 0 deletions interface/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { invoke } from "@tauri-apps/api"

const getTime = () => {
return new Date().toLocaleString("hu")
}

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

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

invoke("logger", { message, time, kind: "log" })
}

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

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

invoke("logger", { message, time, kind: "warn" })
}

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

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

invoke("logger", { message, time, kind: "error" })
}

export default { log, warn, error }
11 changes: 4 additions & 7 deletions interface/utils/update.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { updater, event, dialog } from "@tauri-apps/api"
import { updater, dialog } from "@tauri-apps/api"
import { relaunch } from "@tauri-apps/api/process"
import { getState, setState } from "interface/stores/state"
import { dev } from "../../build.json"
import logger from "./logger"

const state = getState()
let releaseNotes: string
Expand All @@ -16,13 +17,13 @@ export const checkForUpdate = async () => {
if (shouldUpdate) {
releaseNotes = manifest.body

console.log(manifest)
logger.log(`Latest update: ${JSON.stringify(manifest)}`)

state.updateAvailable = true
setState(state)
}
} catch (error) {
console.log(error)
logger.error(`Failed to check for update: ${error}`)
}
}
}
Expand All @@ -37,7 +38,3 @@ export const installUpdate = async () => {
export const showReleaseNotes = () => {
dialog.message(releaseNotes)
}

event.listen("tauri://update-status", (res) => {
console.log("New status: ", res)
})
3 changes: 2 additions & 1 deletion interface/windows/codes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dialog, fs } from "@tauri-apps/api"
import { getSettings, setSettings } from "../../stores/settings"
import { getState, setState } from "../../stores/state"
import { decryptData, encryptData } from "interface/utils/encryption"
import logger from "interface/utils/logger"

const settings = getSettings()
const state = getState()
Expand Down Expand Up @@ -159,7 +160,7 @@ export const generateCodeElements = (data: LibImportFile) => {
try {
refreshCodes(secrets)
} catch (error) {
console.error("Error refreshing codes")
logger.error("Error refreshing codes")
}
}, 500)

Expand Down

0 comments on commit 77a8665

Please sign in to comment.