cleanLog method

void cleanLog()

清除当前平台存储目录下所有 .log / .dmp 文件。

Implementation

void cleanLog() async {
  // final dir = await getLocalSupportDir();
  final String? dir;
  if (Platform.isWindows) {
    dir = await getLocalSupportDir();
  } else if (Platform.isAndroid) {
    final esdTemp = await getExternalStorageDirectory();
    if (esdTemp != null) {
      dir = esdTemp.path;
    } else {
      final tdTemp = await getTemporaryDirectory();
      dir = tdTemp.path;
    }
  } else {
    dir = await getLocalDocumentDir();
  }
  if (dir == null) {
    return;
  }
  await for (final file in Directory(dir).list(followLinks: false)) {
    // 只清常规文件:目录 / 链接跳过——空 `*.log` 目录会被 delete() 静默
    // 删除,非空目录则抛异常(本函数 void async,错误无人接)。
    if (file is! File) {
      continue;
    }
    if (path.extension(file.path) == '.log' ||
        path.extension(file.path) == '.dmp') {
      await file.delete();
    }
  }
}