• This repository has been archived on 18/Dec/2022
  • Stars
    star
    196
  • Rank 198,553 (Top 4 %)
  • Language
    Kotlin
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

โœ๏ธ Method call logging for Kotlin Multiplatform

โœ๏ธ Cabret โœ๏ธ

jCenter PRs Welcome jCenter

Introduction ๐Ÿ™‹โ€โ™‚๏ธ ๐Ÿ™‹โ€

This is an Kotlin Library that enables Annotation-triggered method call logging for Kotlin Multiplatform. Inspired by Hugo, Hunter-Debug and the blog posts by bnorm .

Simply add @DebugLog to your methods and it will automatically log all arguments that are passed to the function the return value and the time the function needed to excecute.

When the following function gets called:

@DebugLog
fun exampleFun(
    first: String,
    last: String,
    age: Int = 31,
    isLoggedIn: Boolean = false
): String = "$first $last"

fun main(){
  exampleFun("Jens","Klingenberg")
}

It will automatically log:

Example -> exampleFun( first= Jens, last= Klingenberg, age= 31, isLoggedIn= false)
Example <- exampleFun() [2.63ms] =  Jens Klingenberg

Show some โค๏ธ and star the repo to support the project

GitHub stars GitHub forks GitHub watchers Twitter Follow

Setup

You can take a look at DemoProject as an example

1) Gradle Plugin

Add the dependency to your buildscript

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath "de.jensklingenberg.cabret:cabret-gradle:1.0.3"
    }
}

2) Apply the plugin

Kotlin DSL:

plugins {
     id("de.jensklingenberg.cabret")
}

configure<de.jensklingenberg.gradle.CabretGradleExtension> {
    enabled = true
    version = "1.0.4"
}

Groovy DSL:

plugins {
    id 'de.jensklingenberg.cabret'
}

cabret {
    enabled = true
    version = 1.0.4
}

The plugin will only be active when enabled is set to true

3) Log Library

To be able to use the DebugLog annotation, you also need add the dependecies on cabret-log.

Multiplatform (Common, JS, Native)

You can add dependency to the right to the common source set:

commonMain {
    dependencies {
        implementation "de.jensklingenberg.cabret:cabret-log:1.0.4"
    }
}

Platform-specific

You can also add platform-specific dependecies

sourceSets {
    jvmMain {
            dependencies {
                 implementation "de.jensklingenberg.cabret:cabret-log-jvm:1.0.4"
            }
   }
}

Here's a list of all available targets:

def cabretVersion = "1.0.4"

implementation "de.jensklingenberg.cabret:cabret-log-jvm:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-js:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-android:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-iosx64:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-iosarm64:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-linux:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-macos:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-watchosarm32:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-watchosarm64:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-tvosarm32:$cabretVersion"
implementation "de.jensklingenberg.cabret:cabret-log-tvosarm64:$cabretVersion"

4) Enable IR

Cabret is using a Kotlin Compiler Plugin that is using the IR Backend. For Native targets it's already enabled, but you need to activate it in your build.gradle for Kotlin JVM/JS

Kotlin/JVM
tasks.withType<KotlinCompile>().configureEach {
  kotlinOptions {
    useIR = true
  }
}
Kotlin/JS
target {
  js(IR) {
  }
}

Logging

Tag

@DebugLog( tag = "MyTag")

You can add a tag to the DebugLog annotation under which you can find your logged data. When you don't add a custom tag, Cabret will use the file name for top level function and the class name for class functions as the tag.

LogLevel

@DebugLog(logLevel = Cabret.LogLevel.ERROR)

You can set a LogLevel to the DebugLog Annotation. You can choose between VERBOSE, DEBUG, INFO, WARN or ERROR. By default DEBUG is selected.

Custom Logger

By default Cabret will log the data with printLn() or on Android with android.util.Log and the selected LogLevel. E.g. LogLevel.ERROR will be logged with Log.e(), LogLevel.INFO will be logged with Log.i(), etc.

You can add your own Logger. All you need to do is to add your object of Cabret.Logger to Cabret.addLogger() somewhere at the beginning of your programm.

Cabret.addLogger(object : Cabret.Logger {
            override fun log(data: LogData) {
                //Add your logger here
                println(data.tag + " " + data.msg)
            }
})

๐Ÿ‘ท Project Structure

  • cabret-compiler-plugin - This module contains the Kotlin Compiler Plugin for JVM/JS targets
  • cabret-compiler-native-plugin - This module contains the Kotlin Compiler Plugin for native targets
  • cabret-compiler-runtime - This module contains the shared Kotlin Compiler Plugin Logic for JVM/JS/Native compiler
  • cabret-gradle - This module contains the gradle plugin which triggers the two compiler plugins
  • cabret-log - A Kotlin Multiplatform project with the DebugLog annotation and the default loggers
  • DemoProject - A Kotlin Multiplatform project that is using the debuglog compiler plugin

โœ๏ธ Feedback

Feel free to send feedback on Twitter or file an issue. Feature requests are always welcome.

๐Ÿ“œ License


This project is licensed under Apache License, Version 2.0

Copyright 2020 Jens Klingenberg

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

Jetpack-Compose-Playground

Community-driven collection of Jetpack Compose example code and tutorials ๐Ÿš€ https://foso.github.io/compose
Kotlin
3,132
star
2

Ktorfit

HTTP client generator / KSP plugin for Kotlin Multiplatform (Android, iOS, Js, Jvm, Native) using KSP and Ktor clients inspired by Retrofit https://foso.github.io/Ktorfit
Kotlin
1,545
star
3

MpApt

(Deprecated) ๐Ÿ”ง Kotlin Native/JS/JVM Annotation Processor library for Kotlin compiler plugins
Kotlin
238
star
4

KotlinCompilerPluginExample

This is an example project that shows how to create a Kotlin Compiler Plugin. The plugin will print "Hello from" and the name of the file that is being compiled, as a compiler warning to the terminal log.
Kotlin
107
star
5

HtmlToComposeWebConverter

IntelliJ Idea Plugin that can convert HTML to Compose HTML code. https://plugins.jetbrains.com/plugin/18261-html-to-compose-web-converter
Kotlin
96
star
6

Showdown

Showdown is a selfhosted open source web app/server, you can use for remote planning pokerยฎ with scrum teams. Try at http://showdown.fly.dev/#/
Kotlin
48
star
7

KotlinReactNativeMpp

A ReactNative App written with Kotlin JS
Kotlin
35
star
8

Sheasy

This an Android App that helps you share/manage your files on your Android Device through a WebInterface in the Browser - Built with Ktor and Kotlin-React
Kotlin
35
star
9

Folders2kt

An interpreter/transpiler, written in Kotlin, for the esoteric programming language Folders, a language with no code and just folders
Kotlin
27
star
10

ExoPlayer-with-MediaControls

This is a small project i created to learn how to use the Exoplayer from Google. It plays an video from an URL and you can control the playback. It can now play HLS,Dash and mp4 streams
Java
20
star
11

gtvmonkey-scripts

This a template project that helps you write Greasemonkey/Tampermonkey/ViolentMonkey scripts with KotlinJs
Kotlin
18
star
12

compose-snake-web

This is a Compose HTML port of CompoSnake
Kotlin
12
star
13

JKAndroidWebserver

This is an example project that uses NanoHTTPd to run a Webserver on Android
Java
9
star
14

BitriseArtifactDownloader

This is a flutter app which uses the Bitrise Api(https://api-docs.bitrise.io/) to show the bitrise projects and builds and lets you download your artifacts.
Dart
9
star
15

JKManageSpaceActivity

This is an example project for my post about ManageSpaceActivity on Android
Kotlin
8
star
16

C-Crit_Generator

A generator for your critical secret keys
Kotlin
6
star
17

Android-Deeplink-Starter

IntelliJ/Android Studio plugin that adds an alternative UI to start Android deeplinks
Kotlin
3
star
18

compose-html-hello-world

Kotlin
3
star
19

ReverseMe

The app is part of my post about reserve engineering android apps
Java
3
star
20

ComposeReact

Just for fun experiment with a React like API for Jetpack Compose
Kotlin
2
star
21

MealApp

Kotlin Multiplatform project using Jetpack Compose and SwiftUI
Kotlin
2
star
22

JKLiveDataExample

This is an example project for my article about LiveData http://jensklingenberg.de/learn-how-to-use-livedata/
Java
2
star
23

KmdcExample

Example project with Compose HTML and Kmdc
Kotlin
2
star
24

JKWebsocket

Example Project that shows how to use NanoHTTP/NanoWSD to create a Websocket Server. After you connect to it, it will post "Hello World" plus the systemdate to the client every second.
Kotlin
2
star
25

KSP-Example

Kotlin
1
star
26

BasicKmm

Kotlin
1
star
27

dukat-idea

Intellij Plugin that adds a menu entry to start Dukat to generate KotlinJs Wrappers
Kotlin
1
star
28

ExtensionGenerator

Work in Progress
Kotlin
1
star
29

BananiaMapConverter

Tiled map files and converter for the game Banania
Kotlin
1
star
30

Foso

1
star
31

compose-web-nes-css

Compose Wrapper for Nes Css (WIP)
Kotlin
1
star
32

kt_dart_builder

A fluent Kotlin API for generating valid Dart source code
Kotlin
1
star
33

Experimental

Nothing to see here
Kotlin
1
star
34

kotlin-react-Hello-World

A simple Kotlin React Project with gradle, that shows Hello World and a reactstrap button
Kotlin
1
star
35

kotlinforeverything

WIP
1
star
36

compose

This is only used as an url shortener for https://github.com/Foso/Jetpack-Compose-Playground/
HTML
1
star
37

JK_Launcher

This is a very simple launcher app.
Java
1
star