Code Monkey home page Code Monkey logo

shakelogkit's Introduction

shakelogkit-logo

ShakeLogKit

ShakeLogKit is a Swift package that enables logging and viewing logs in your iOS application when the device is shaken or activated by a button. This package leverages OSLog for logging and provides a simple ViewModifier to display the logs in a SwiftUI full screen page.

Features

  • Detect shake gestures and trigger custom actions.
  • Log messages using OSLog.
  • Display logs in a SwiftUI sheet when the device is shaken or activated by a button.
  • Customize logging settings including time intervals, subsystem filters, and shake gesture usage.

Requirements

  • iOS 15.0+
  • Swift 5.9+

shakelogkit-demo

Installation

Swift Package Manager

  1. In Xcode, open your project and navigate to File > Swift Packages > Add Package Dependency.
  2. Enter the repository URL for ShakeLogKit.
  3. Follow the prompts to add the package to your project.

Alternatively, add the following line to your Package.swift:

dependencies: [
    .package(url: "https://github.com/GioPalusa/shakeLogKit/", branch: "main")
]

Usage

Basic Setup

  1. Import ShakeLogKit:

    import ShakeLogKit
  2. Enable Shake Logging:

    In your SwiftUI view, apply the enableShakeLogging modifier:

    struct ContentView: View {
        @State private var showingLogs = false
        
        var body: some View {
            Text("Hello, World!")
                .enableShakeLogging(showingLogs: $showingLogs)
        }
    }

    This will use the default settings which logs messages and displays them in a sheet when the device is shaken.

Advanced Setup

  1. Define ShakeLogSettings:

    Create an instance of ShakeLogSettings to customize the logging behavior:

    import SwiftUI
    import ShakeLogKit
    
    @State private var shouldShowLogs: Bool? = nil
    
    let settings = ShakeLogSettings(
        timeInterval: -3600,            // Fetch logs from the last hour
        useShake: true,                 // Enable shake gesture to show logs
        subsystem: "com.example.app",   // Filter logs by subsystem
        shouldShowLogs: $shouldShowLogs // Binding to control log display
    )
  2. Apply Settings:

    Pass the settings and the isEnabled binding to the enableShakeLogging modifier:

    struct ContentView: View {
        @State private var shouldShowLogs: Bool? = nil
        @State private var isEnabled = true
        @State private var showingLogs = false
    
        var body: some View {
            Text("Hello, World!")
                .enableShakeLogging(
                    ShakeLogSettings(
                        timeInterval: -3600,
                        useShake: true,
                        subsystem: "com.example.app",
                        shouldShowLogs: $shouldShowLogs
                    ),
                    isEnabled: $isEnabled,
                    showingLogs: $showingLogs
                )
        }
    }

Logging Messages

You can use the ShakeLogFileManager to log messages, but this is not required if you are already using OSLog in your project:

import ShakeLogKit

ShakeLogFileManager.shared.log("This is a test log message.")

Viewing Logs

When the device is shaken, the logs will be displayed in a SwiftUI sheet if logging is enabled. The logs can be filtered by type and searched using the provided interface.

Example

Here is a complete example of how to set up and use ShakeLogKit in an iOS application:

import SwiftUI
import ShakeLogKit

@main
struct MyApp: App {
    @State private var shouldShowLogs: Bool? = nil
    @State private var isEnabled = true
    @State private var showingLogs = false

    var body: some Scene {
        WindowGroup {
            ContentView()
                .enableShakeLogging(
                    ShakeLogSettings(
                        timeInterval: -3600,
                        useShake: true,
                        subsystem: "com.example.app",
                        shouldShowLogs: $shouldShowLogs
                    ),
                    isEnabled: $isEnabled,
                    showingLogs: $showingLogs
                )
        }
    }
}

struct ContentView: View {
    @State private var shouldShowLogs: Bool? = nil
    @State private var isEnabled = true
    @State private var showingLogs = false

    var body: some View {
        VStack {
            Text("Main Content")
                .padding()

            Button("Log a Message") {
                ShakeLogFileManager.shared.log("This is a test log message.")
            }
        }
        .enableShakeLogging(
            ShakeLogSettings(
                timeInterval: -3600,
                useShake: true,
                subsystem: "com.example.app",
                shouldShowLogs: $shouldShowLogs
            ),
            isEnabled: $isEnabled,
            showingLogs: $showingLogs
        )
    }
}

License

ShakeLogKit is released under the MIT license.

shakelogkit's People

Contributors

giopalusa avatar

Stargazers

 avatar Jack Nutting avatar Iggy Drougge avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.