var RNFS = require('react-native-fs');
const path = 'file:///sdcard/along/logs';
export function writeLog(data, index) {
if (!index) {
index = 0;
}
const date = new Date();
const dateStr = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
const filePath = `${path}/h5shell-rn/${dateStr}_${index}.txt`;
makeDir(path).then(() => {
return makeDir(path + '/h5shell-rn');
}).then(() => {
return RNFS.exists(filePath);
}).then((fileExists) => {
const time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
const input = `${time} ${data}`;
if (!fileExists) {
RNFS.writeFile(filePath, input, 'utf8');
} else {
RNFS.stat(filePath).then(stats => {
if (stats.size > 5 * 1024 * 1024) {
writeLog(data, index++);
} else {
RNFS.appendFile(filePath, `\r\n${input}`, 'utf8');
}
})
}
})
}
export function makeDir(dir) {
return new Promise((resolve, reject) => {
RNFS.exists(dir).then(exists => {
if(!exists) {
return RNFS.mkdir(dir);
}
}).then(()=>{
resolve();
}).catch(err => {
reject(err);
})
})
}