RN-log日志

作者: alongzjl | 来源:发表于2018-08-24 16:36 被阅读0次

    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);

        })

      })

    }

    相关文章

      网友评论

        本文标题:RN-log日志

        本文链接:https://www.haomeiwen.com/subject/mpciiftx.html