Skip to content

Commit

Permalink
feat: remove logger complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
muuvmuuv committed Jan 21, 2025
1 parent 78459a6 commit c1909aa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 61 deletions.
10 changes: 5 additions & 5 deletions src/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function applySettings(settings: WorkspaceConfiguration): void {
return // no settings, nothing to do
}

// log.debug("NEW", JSON.stringify(settings, undefined, 2))
// log("NEW", JSON.stringify(settings, undefined, 2))

const workspaceSettings = workspace.getConfiguration()

Expand All @@ -26,8 +26,8 @@ export function applySettings(settings: WorkspaceConfiguration): void {

const configString = settings[k] as string

workspaceSettings.update(k, configString, true).then(undefined, (error: string) => {
log.error(error)
workspaceSettings.update(k, configString, true).then(undefined, (error) => {
log(error)
window.showErrorMessage(
`You tried to apply \`${k}: ${configString}\` but this is not a valid VS Code settings key/value pair. Please make sure all settings that you give to Sundial are valid inside VS Code settings!`,
)
Expand All @@ -41,7 +41,7 @@ export enum TimeName {
}

export function changeThemeTo(newTheme: string): void {
log.debug("Changing theme to", newTheme)
log("Changing theme to", newTheme)
const { workbench } = getConfig()
if (newTheme !== workbench.colorTheme) {
workbench.update("colorTheme", newTheme, true)
Expand All @@ -61,7 +61,7 @@ export function changeToNight(): void {
}

export function toggleTheme(time?: TimeName): void {
log.debug("Toggle theme to:", time || "toggle")
log("Toggle theme to", time || "toggle")
const config = getConfig()
switch (time) {
case TimeName.Day: {
Expand Down
28 changes: 2 additions & 26 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import { window } from "vscode"

enum LogLevel {
Silent = 0,
Info = 1,
Error = 2,
Debug = 3,
}

type AllowedTypes = string | number | boolean | Date

export const outputChannel = window.createOutputChannel("Sundial")

class Logger {
info(...messages: AllowedTypes[]) {
this.log(messages, LogLevel.Info)
}

error(...messages: AllowedTypes[]) {
this.log(messages, LogLevel.Error)
}

debug(...messages: AllowedTypes[]) {
this.log(messages, LogLevel.Debug)
}

private log(messages: AllowedTypes[], level: LogLevel = LogLevel.Silent) {
const logMessage = `[${LogLevel[level].toUpperCase()}] ${messages.join(" ")}`
outputChannel.appendLine(logMessage)
}
export function log(...messages: AllowedTypes[]) {
outputChannel.appendLine(messages.join(" "))
}

export const log = new Logger()
10 changes: 5 additions & 5 deletions src/sensors/autolocale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function getAutoLocale(): Promise<Tides> {
let latitude = context.globalState.get<number>("userLatitude")
let longitude = context.globalState.get<number>("userLongitude")

log.debug("Auto locale timeout", timeout)
log("Auto locale timeout", timeout)

const connected = await isOnline()

Expand All @@ -41,12 +41,12 @@ export async function getAutoLocale(): Promise<Tides> {
context.globalState.update("userLongitude", longitude)
} catch (error) {
if (error instanceof Error) {
log.error(error.message)
log(error.message)
}
window.showErrorMessage("Fetching your location went wrong, please open an issue")
}
} else {
log.info("Not connected, reusing existing geolocation")
log("Not connected, reusing existing geolocation")
}

if (!(latitude && longitude)) {
Expand All @@ -57,8 +57,8 @@ export async function getAutoLocale(): Promise<Tides> {
)
}

log.debug("Auto locale latitude", latitude)
log.debug("Auto locale longitude", longitude)
log("Auto locale latitude", latitude)
log("Auto locale longitude", longitude)

const tides = getTimes(now, latitude, longitude)

Expand Down
4 changes: 2 additions & 2 deletions src/sensors/latlong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export function getLatLong(): Tides {
throw window.showErrorMessage("Sundial needs both, latitude and longitude")
}

log.debug("Config latitude", config.sundial.latitude)
log.debug("Config longitude", config.sundial.longitude)
log("Config latitude", config.sundial.latitude)
log("Config longitude", config.sundial.longitude)

const tides = getTimes(
new Date(),
Expand Down
47 changes: 24 additions & 23 deletions src/sundial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class Sundial {
* Enable the automation and checks.
*/
enableExtension(): void {
log.info("Enabling Sundial")
log("Enabling Sundial")
Sundial.extensionContext.globalState.update(STATE_ENABLED, true)
this.automator()
this.check()
Expand All @@ -62,7 +62,7 @@ export class Sundial {
* Disable the extension automation and checks.
*/
disableExtension(): void {
log.info("Disabling Sundial")
log("Disabling Sundial")
Sundial.extensionContext.globalState.update(STATE_ENABLED, false)
this.killAutomator()
}
Expand All @@ -77,13 +77,13 @@ export class Sundial {
}
const { sundial } = getConfig()
if (sundial.interval === 0) {
log.info("Automator offline")
log("Automator offline")
return
}
log.info("Automator online")
log("Automator online")
const interval = 1000 * 60 * sundial.interval
this.checkInterval = setInterval(() => {
log.info("Run autocheck")
log("Run autocheck")
this.check()
}, interval)
}
Expand All @@ -103,19 +103,19 @@ export class Sundial {
return // disabled or already running
}

log.info("Check initialized")
log("Check initialized")

this.isRunning = true
this.killAutomator()

const currentTimeName = await this.getCurrentTime()
log.debug(`Current time is ${currentTimeName}`)
log(`Current time is ${currentTimeName}`)

if (currentTimeName === TimeName.Day) {
log.info("Will apply your day theme! 🌕")
log("Will apply your day theme! 🌕")
changeToDay()
} else {
log.info("Will apply your night theme! 🌑")
log("Will apply your night theme! 🌑")
changeToNight()
}

Expand Down Expand Up @@ -171,12 +171,12 @@ export class Sundial {
this.evaluateTides(tides)

if (nowIsAfterSunrise && nowIsBeforeSunset) {
log.debug("It is", TimeName.Day)
log("It is", TimeName.Day)
return TimeName.Day
}

if (nowIsBeforeSunrise || nowIsAfterSunset) {
log.debug("It is", TimeName.Night)
log("It is", TimeName.Night)
return TimeName.Night
}

Expand All @@ -190,16 +190,17 @@ export class Sundial {
const { sundial } = getConfig()

if (sundial.latitude && sundial.longitude) {
log.info("Will use your latitude and longitude")
log("Will use your latitude and longitude")
return getLatLong()
}

if (sundial.autoLocale) {
log.info("Will now try to detect your location")
log("Will now try to detect your location")
return await getAutoLocale()
}

log.info("Will use your time settings")
log("Will use your time settings")

return {
sunrise: parse(sundial.sunrise, "HH:mm", new Date()),
sunset: parse(sundial.sunset, "HH:mm", new Date()),
Expand All @@ -215,11 +216,11 @@ export class Sundial {
let { sunrise, sunset } = tides
if (sundial.dayVariable) {
sunrise = addMinutes(sunrise, sundial.dayVariable)
log.debug(`Adjusted ${sundial.dayVariable} minutes from day`)
log(`Adjusted ${sundial.dayVariable} minutes from day`)
}
if (sundial.nightVariable) {
sunset = addMinutes(sunset, sundial.nightVariable)
log.debug(`Adjusted ${sundial.nightVariable} minutes from night`)
log(`Adjusted ${sundial.nightVariable} minutes from night`)
}

const now = Date.now()
Expand All @@ -229,13 +230,13 @@ export class Sundial {
const nowIsBeforeSunset = isBefore(now, sunset)
const nowIsAfterSunset = isAfter(now, sunset)

log.debug("Now:", now)
log.debug("Sunrise:", sunrise)
log.debug("Sunset:", sunset)
log.debug("nowIsBeforeSunrise:", nowIsBeforeSunrise)
log.debug("nowIsAfterSunrise:", nowIsAfterSunrise)
log.debug("nowIsBeforeSunset:", nowIsBeforeSunset)
log.debug("nowIsAfterSunset:", nowIsAfterSunset)
log("Now:", now)
log("Sunrise:", sunrise)
log("Sunset:", sunset)
log("nowIsBeforeSunrise:", nowIsBeforeSunrise)
log("nowIsAfterSunrise:", nowIsAfterSunrise)
log("nowIsBeforeSunset:", nowIsBeforeSunset)
log("nowIsAfterSunset:", nowIsAfterSunset)

return {
nowIsBeforeSunrise,
Expand Down

0 comments on commit c1909aa

Please sign in to comment.