Skip to content

Commit

Permalink
View audit log in app
Browse files Browse the repository at this point in the history
  • Loading branch information
lucyb committed Jul 11, 2023
1 parent 780ba99 commit a800bae
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
11 changes: 10 additions & 1 deletion sacro-app/src/main-menu.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { Menu, dialog, app, BrowserWindow } = require("electron");
const { Menu, dialog, app, shell, BrowserWindow } = require("electron");
const log = require("electron-log");
const fs = require("fs");
const os = require("node:os");
Expand Down Expand Up @@ -76,6 +76,15 @@ const mainMenu = (serverUrl) => {
{
role: "help",
submenu: [
{
label: "View Audit Log",
click: async () => {
const auditFile = `${app.getPath("appData")}/SACRO/audit.log`;
const tempFile = `${app.getPath("temp")}/sacro-audit.log`;
fs.copyFileSync(auditFile, tempFile);
shell.openPath(tempFile);
},
},
{
role: "about",
},
Expand Down
19 changes: 16 additions & 3 deletions sacro/logging.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import os
import sys
from pathlib import Path

import structlog


def get_appdir():
home = Path.home()

if sys.platform == "win32":
return home / "AppData/Roaming"
if sys.platform == "linux":
return home / ".config"
if sys.platform == "darwin":
return home / "Library/Application Support"

return "."


def get_log_filename():
filename = Path(os.getenv("APPDATA", ".")) / "SACRO" / "error.log"
filename = Path(get_appdir()) / "SACRO" / "error.log"
filename.parent.mkdir(parents=True, exist_ok=True)
return str(filename)


def get_audit_filename():
filename = Path(os.getenv("APPDATA", ".")) / "SACRO" / "audit.log"
filename = Path(get_appdir()) / "SACRO" / "audit.log"
filename.parent.mkdir(parents=True, exist_ok=True)
return str(filename)

Expand Down
51 changes: 51 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import os
import sys

from sacro import logging


def test_get_appdir_on_unknown_os(monkeypatch):
monkeypatch.setattr(sys, "platform", "unknown")
assert "." == logging.get_appdir()


def test_get_log_filename_on_win32(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
assert os.path.join("AppData/Roaming") in logging.get_log_filename()
assert "SACRO" in logging.get_log_filename()
assert "error.log" in logging.get_log_filename()


def test_get_log_filename_on_linux(monkeypatch):
monkeypatch.setattr(sys, "platform", "linux")
assert ".config" in logging.get_log_filename()
assert "SACRO" in logging.get_log_filename()
assert "error.log" in logging.get_log_filename()


def test_get_log_filename_on_macos(monkeypatch):
monkeypatch.setattr(sys, "platform", "darwin")
assert os.path.join("Library/Application Support") in logging.get_log_filename()
assert "SACRO" in logging.get_log_filename()
assert "error.log" in logging.get_log_filename()


def test_get_audit_filename_on_win32(monkeypatch):
monkeypatch.setattr(sys, "platform", "win32")
assert os.path.join("AppData/Roaming") in logging.get_audit_filename()
assert "SACRO" in logging.get_audit_filename()
assert "audit.log" in logging.get_audit_filename()


def test_get_audit_filename_on_linux(monkeypatch):
monkeypatch.setattr(sys, "platform", "linux")
assert ".config" in logging.get_audit_filename()
assert "SACRO" in logging.get_audit_filename()
assert "audit.log" in logging.get_audit_filename()


def test_get_audit_filename_on_macos(monkeypatch):
monkeypatch.setattr(sys, "platform", "darwin")
assert os.path.join("Library/Application Support") in logging.get_audit_filename()
assert "SACRO" in logging.get_audit_filename()
assert "audit.log" in logging.get_audit_filename()

0 comments on commit a800bae

Please sign in to comment.