Skip to content

Commit

Permalink
feat: customizable pulling interval
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillzyusko committed Oct 18, 2024
1 parent 4653bc2 commit 68a811a
Show file tree
Hide file tree
Showing 30 changed files with 102 additions and 44 deletions.
2 changes: 1 addition & 1 deletion AppLogsPod/AppLogs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension DateFormatter {
}

@available(iOS 15.0, *)
extension OSLogEntryLog.Level: CustomStringConvertible {
extension OSLogEntryLog.Level: @retroactive CustomStringConvertible {
public var description: String {
switch self {
case .undefined:
Expand Down
20 changes: 20 additions & 0 deletions AppLogsPod/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) 2024 Kiryl Ziusko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import AppLogs from 'react-native-app-logs';

// ...

AppLogs.configure({ interval: 5 });

AppLogs.registerHandler({
filter: '[AppName]',
handler: ({ filter, logs }) => {
Expand All @@ -36,12 +38,12 @@ On iOS each process has its own logs and they live only within the process (and
To intercept logs from `NotificationServiceExtension` you need to:

- give common app group for both `NotificationServiceExtension` and the main app;
- specify `appGroupName` in `AppLogs.configureAppGroupName` method:
- specify `appGroupName` in `AppLogs.configure` method:

```ts
import AppLogs from 'react-native-app-logs';

AppLogs.configureAppGroupName('group.applogs.example');
AppLogs.configure({ appGroupName: 'group.applogs.example', interval: 5 });
```

- add new Pod to your `NotificationServiceExtension`:
Expand Down
8 changes: 5 additions & 3 deletions android/src/main/java/com/applogs/AppLogsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.applogs

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap

class AppLogsModule internal constructor(context: ReactApplicationContext) :
AppLogsSpec(context) {
Expand All @@ -11,14 +11,16 @@ class AppLogsModule internal constructor(context: ReactApplicationContext) :
return NAME
}

@ReactMethod
override fun addFilterCondition(filter: String?) {
}

@ReactMethod
override fun removeFilterCondition(filter: String?) {
}

override fun configureAppGroupName(appGroupName: String?) {
}
@ReactMethod
override fun configure(params: ReadableMap) {}

companion object {
const val NAME = "AppLogs"
Expand Down
7 changes: 5 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
} from 'react-native';
import AppLogs, { type NativeLog } from 'react-native-app-logs';

AppLogs.configure({
appGroupName: 'group.applogs.example',
interval: 5,
});

AppLogs.registerHandler({
filter: '[AppName]',
handler: ({ logs }) => {
Expand All @@ -21,8 +26,6 @@ export default function App() {
const [history, setHistory] = useState<NativeLog[]>([]);

useEffect(() => {
AppLogs.configureAppGroupName('group.applogs.example');

const listener = AppLogs.registerHandler({
filter: '[AppName]',
handler: ({ logs }) => {
Expand Down
26 changes: 18 additions & 8 deletions ios/AppLogs.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@ - (instancetype)init
[self sendEvent:@"newLogAvailable" body:@{ @"filter": filter, @"logs": filteredLogs }];
}
}];
// Set up a timer to check for new logs periodically
logCheckTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:@selector(checkForNewLogs)
userInfo:nil
repeats:YES];

lastLogCheckTime = [NSDate dateWithTimeIntervalSince1970:0]; // Start from the epoch time

return self;
Expand Down Expand Up @@ -99,11 +94,26 @@ - (void)sendEvent:(NSString *)name body:(id)body
resolve(filter);
}

RCT_EXPORT_METHOD(configureAppGroupName:(NSString *)appGroupName
RCT_EXPORT_METHOD(configure:(NSDictionary *)params
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)
{
[logStoreHelper setAppGroupName: appGroupName];
NSString *appGroupName = params[@"appGroupName"];
if (appGroupName != nil) {
[logStoreHelper setAppGroupName: params[@"appGroupName"]];
}
NSNumber *interval = params[@"interval"];

if ([interval compare:@-1] != NSOrderedSame) {
// Set up a timer to check for new logs periodically
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:[interval doubleValue]
target:self
selector:@selector(checkForNewLogs)
userInfo:nil
repeats:YES];
});
}

resolve(appGroupName);
}
Expand Down
2 changes: 1 addition & 1 deletion ios/OSLogStoreHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension DateFormatter {
}

@available(iOS 15.0, *)
extension OSLogEntryLog.Level: CustomStringConvertible {
extension OSLogEntryLog.Level: @retroactive CustomStringConvertible {
public var description: String {
switch self {
case .undefined:
Expand Down
2 changes: 1 addition & 1 deletion jest/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const NOOP = () => {};
const AppLogsEvents = {
registerHandler: () => ({ remove: NOOP }),
configureAppGroupName: NOOP,
configure: NOOP,
};

export default AppLogsEvents;
2 changes: 1 addition & 1 deletion lib/commonjs/NativeAppLogs.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/bindings.ios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/bindings.ios.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/bindings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/commonjs/bindings.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/NativeAppLogs.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/bindings.ios.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/bindings.ios.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/bindings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/module/bindings.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion lib/typescript/src/NativeAppLogs.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import type { TurboModule } from 'react-native';
import type { Int32 } from 'react-native/Libraries/Types/CodegenTypes';
type ConfigureParams = Readonly<{
appGroupName: string | undefined;
interval: Int32;
}>;
export interface Spec extends TurboModule {
addFilterCondition(filter: string): void;
removeFilterCondition(filter: string): void;
configureAppGroupName(appGroupName: string): void;
configure(params: ConfigureParams): void;
}
declare const _default: Spec;
export default _default;
Expand Down
2 changes: 1 addition & 1 deletion lib/typescript/src/NativeAppLogs.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/typescript/src/bindings.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NativeLog } from './types';
import type { ConfigureParams, NativeLog } from './types';
declare const AppLogsEvents: {
registerHandler: (_fn: {
handler: (params: {
Expand All @@ -9,7 +9,7 @@ declare const AppLogsEvents: {
}) => {
remove: () => void;
};
configureAppGroupName: (_appGroupName: string) => void;
configure: (_params: ConfigureParams) => void;
};
export default AppLogsEvents;
//# sourceMappingURL=bindings.d.ts.map
2 changes: 1 addition & 1 deletion lib/typescript/src/bindings.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/typescript/src/bindings.ios.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { NativeLog } from './types';
import type { ConfigureParams, NativeLog } from './types';
declare const AppLogsEvents: {
registerHandler: ({ handler, filter, }: {
handler: (params: {
Expand All @@ -9,7 +9,7 @@ declare const AppLogsEvents: {
}) => {
remove: () => void;
};
configureAppGroupName: (appGroupName: string) => void;
configure: (params: ConfigureParams) => void;
};
export default AppLogsEvents;
//# sourceMappingURL=bindings.ios.d.ts.map
2 changes: 1 addition & 1 deletion lib/typescript/src/bindings.ios.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions lib/typescript/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ export type NativeLog = {
tid: number;
level: string;
};
export type ConfigureParams = {
/** common `appGroupName` shared between targets (iOS only) */
appGroupName?: string;
/** interval (in seconds) to fetch logs (iOS only). Specify `-1` if you don't want to run periodic checks */
interval: number;
};
//# sourceMappingURL=types.d.ts.map
Loading

0 comments on commit 68a811a

Please sign in to comment.