Skip to content

Commit

Permalink
queue to avoid race conditions problems with ExpoFS
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-bottamedi committed Jun 4, 2021
1 parent b95ca9d commit e3f35c5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [3.0.4] - 04-06-2021

- queue management to avoid race conditions problems with ExpoFS
- minor bugfix

## [3.0.3] - 12-02-2021

- removed EncodingType reference on fileAsyncTransport
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# react-native-logs

Performance-aware simple logger for React-Native with custom levels and transports (colored console,
Performance-aware simple logger for React-Native and Expo (managed and bare) with custom levels and transports (colored console,
file writing, etc.). Each level has its severity: a number that represents its importance in
ascending order from the least important to the most important. Eg. _debug:0, info:1, warn:2,
error:3_. By config the logger with a minium severity level, you will see only the logs that have it
Expand All @@ -31,6 +31,12 @@ OR
yarn add react-native-logs
```

OR

```sh
expo install react-native-logs
```

## Quick Start

```javascript
Expand Down Expand Up @@ -207,7 +213,7 @@ Print the logs with a formatted `console.log` output.

### **fileAsyncTransport**

This transport requires the installation of `react-native-fs`([install tutorial here](https://github.com/itinance/react-native-fs)) or `expo-file-system`(beta), and allows you to save the
This transport requires the installation of `react-native-fs`([install tutorial here](https://github.com/itinance/react-native-fs)) or `expo-file-system`, and allows you to save the
logs on the `<filePath>/<fileName>.txt` file.

#### Accepted Options:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-logs",
"version": "3.0.3",
"version": "3.0.4",
"description": "Performance-aware simple logger for React-Native with namespaces, custom levels and custom transports (colored console, file writing, etc.)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
41 changes: 34 additions & 7 deletions src/transports/fileAsyncTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
import { transportFunctionType } from "../index";
import { transportFunctionType } from '../index';

interface EXPOqueueitem {
FS: any;
file: string;
msg: string;
}

let EXPOqueue: Array<EXPOqueueitem> = [];
let EXPOelaborate = false;

const EXPOFSreadwrite = async () => {
EXPOelaborate = true;
const item = EXPOqueue[0];
const prevFile = await item.FS.readAsStringAsync(item.file);
const newMsg = prevFile + item.msg;
await item.FS.writeAsStringAsync(item.file, newMsg);
EXPOelaborate = false;
EXPOqueue.shift();
if (EXPOqueue.length > 0) {
await EXPOFSreadwrite();
}
};

const EXPOcheckqueue = async (FS: any, file: string, msg: string) => {
EXPOqueue.push({ FS, file, msg });
if (!EXPOelaborate) {
await EXPOFSreadwrite();
}
};

const EXPOFSappend = async (FS: any, file: string, msg: string) => {
try {
Expand All @@ -7,9 +36,7 @@ const EXPOFSappend = async (FS: any, file: string, msg: string) => {
await FS.writeAsStringAsync(file, msg);
return true;
} else {
const prevFile = await FS.readAsStringAsync(file);
const newMsg = prevFile + msg;
await FS.writeAsStringAsync(file, newMsg);
await EXPOcheckqueue(FS, file, msg);
return true;
}
} catch (error) {
Expand All @@ -20,7 +47,7 @@ const EXPOFSappend = async (FS: any, file: string, msg: string) => {

const RNFSappend = async (FS: any, file: string, msg: string) => {
try {
await FS.appendFile(file, msg, "utf8");
await FS.appendFile(file, msg, 'utf8');
return true;
} catch (error) {
console.error(error);
Expand All @@ -30,7 +57,7 @@ const RNFSappend = async (FS: any, file: string, msg: string) => {

const fileAsyncTransport: transportFunctionType = (props) => {
let WRITE: (FS: any, file: string, msg: string) => Promise<boolean>;
let fileName: string = "log";
let fileName: string = 'log';
let filePath: string;

if (!props?.options?.FS) {
Expand Down Expand Up @@ -59,7 +86,7 @@ const fileAsyncTransport: transportFunctionType = (props) => {
if (props?.options?.filePath) filePath = props.options.filePath;

let output = `${props?.msg}\n`;
var path = filePath + "/" + fileName;
var path = filePath + '/' + fileName;

WRITE(props.options.FS, path, output);
};
Expand Down

0 comments on commit e3f35c5

Please sign in to comment.