Skip to content

Commit

Permalink
feat: add simple logging interface (js only at the moment) (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
hannojg authored Jun 28, 2024
1 parent 2b68a38 commit 5ffda6c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
16 changes: 16 additions & 0 deletions package/example/Shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ import { StrictMode } from 'react'
import { version } from 'react-native-worklets-core/package.json'
console.log(`Using react-native-worklets-core@${version}`)

// Setup a logger for filament
import { setLogger } from 'react-native-filament'
// A function that can wrap a console log call to add a prefix
const prefix = '[filament-logger]'
const prefixLog = (logFn) => (...messages) => {
const date = new Date()
logFn(prefix, `[${date.toLocaleTimeString()} ${date.getMilliseconds().toString().padStart(3, 0)}]`, ...messages)
}
setLogger({
debug: prefixLog(console.debug),
log: prefixLog(console.log),
info: prefixLog(console.info),
warn: prefixLog(console.warn),
error: prefixLog(console.error),
})

// Run filament tests
import { runTests } from 'react-native-filament-test'
runTests()
Expand Down
2 changes: 1 addition & 1 deletion package/example/Shared/src/AnimationTransitions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DefaultLight } from './components/DefaultLight'
const animationInterpolationTime = 5

function Renderer() {
const { view, scene } = useFilamentContext()
const { view } = useFilamentContext()

const model = useModel(DroneGlb)
const modelAnimator = useAnimator(model)
Expand Down
2 changes: 2 additions & 0 deletions package/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export * from './hooks/useCameraManipulator'
// utilities
export * from './utilities/getAssetFromModel'
export * from './utilities/withCleanupScope'
export * from './utilities/logger/LoggingInterface'
export { setLogger } from './utilities/logger/Logger'

// Bullet 3
export * from './bullet'
Expand Down
21 changes: 21 additions & 0 deletions package/src/utilities/logger/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { LoggerInterface } from './LoggingInterface'

let loggerInstance: LoggerInterface | null = null

export const setLogger = (logger: LoggerInterface) => {
loggerInstance = logger
}

export const getLogger = (): LoggerInterface => {
if (loggerInstance == null) {
const noopLogger: LoggerInterface = {
debug: () => {},
info: () => {},
warn: () => {},
error: () => {},
}
return noopLogger
}

return loggerInstance
}
1 change: 1 addition & 0 deletions package/src/utilities/logger/LoggingInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type LoggerInterface = Pick<typeof console, 'debug' | 'info' | 'warn' | 'error'>

0 comments on commit 5ffda6c

Please sign in to comment.