clear method

Future<void> clear()

清空日志

Implementation

Future<void> clear() async {
  _memoryStorage.clear();

  // 关闭旧 sink 后再删除文件,避免文件被占用。
  await _fileSink?.flush();
  await _fileSink?.close();
  _fileSink = null;

  final dir = _storageDir;
  if (dir != null && await dir.exists()) {
    final logFiles = await _getLogFiles();
    for (final file in logFiles) {
      await file.delete();
    }
  }
  _currentFileSize = 0;

  // 重建当前日志文件引用,使后续 store 可继续写入。
  if (dir != null) {
    final fileName = _getLogFileName(DateTime.now());
    _currentLogFile = File(path.join(dir.path, fileName));
  }
}