<commit_before>package scribe.writer.file

import java.io._
import java.nio.charset.Charset
import java.nio.file.{Files, Path}
import java.util.concurrent.atomic.AtomicLong
import java.util.zip.{GZIPInputStream, GZIPOutputStream}

import scala.annotation.tailrec
import scala.util.Try

object LogFile {
  private[file] var map = Map.empty[String, LogFile]

  def apply(path: Path,
            append: Boolean = true,
            autoFlush: Boolean = true,
            charset: Charset = Charset.defaultCharset(),
            mode: LogFileMode = LogFileMode.IO): LogFile = synchronized {
    val key = s"${mode.key}.${path.normalize().toFile.getCanonicalPath}"
    map.get(key) match {
      case Some(lf) if append != lf.append || autoFlush != lf.autoFlush || charset != lf.charset => {
        lf.dispose()

        val nlf = new LogFile(key, path, append, autoFlush, charset, mode)
        map += key -> nlf
        nlf
      }
      case Some(lf) => lf
      case None => {
        val lf = new LogFile(key, path, append, autoFlush, charset, mode)
        map += key -> lf
        lf
      }
    }
  }
}

class LogFile(val key: String,
              val path: Path,
              val append: Boolean,
              val autoFlush: Boolean,
              val charset: Charset,
              val mode: LogFileMode) {
  private lazy val sizeCounter = new AtomicLong(0L)
  private lazy val writer = {
    Files.createDirectories(path.getParent)
    mode.createWriter(this)
  }

  def size: Long = sizeCounter.get()

  final def write(output: String): Unit = {
    writer.write(output)
    if (autoFlush) {
      writer.flush()
    }
    sizeCounter.addAndGet(output.length)
  }

  def rename(fileName: String): LogFile = rename(path.getParent.resolve(fileName))

  def rename(newPath: Path): LogFile = {
    dispose()
    Files.move(path, newPath)
    LogFile(newPath, append, autoFlush, charset, mode)<commit_msg>Updated rename functionality to check existence of both original and destination before renaming (Applies to #67)<commit_after>package scribe.writer.file

import java.io._
import java.nio.charset.Charset
import java.nio.file.{Files, Path}
import java.util.concurrent.atomic.AtomicLong
import java.util.zip.{GZIPInputStream, GZIPOutputStream}

import scala.annotation.tailrec
import scala.util.Try

object LogFile {
  private[file] var map = Map.empty[String, LogFile]

  def apply(path: Path,
            append: Boolean = true,
            autoFlush: Boolean = true,
            charset: Charset = Charset.defaultCharset(),
            mode: LogFileMode = LogFileMode.IO): LogFile = synchronized {
    val key = s"${mode.key}.${path.normalize().toFile.getCanonicalPath}"
    map.get(key) match {
      case Some(lf) if append != lf.append || autoFlush != lf.autoFlush || charset != lf.charset => {
        lf.dispose()

        val nlf = new LogFile(key, path, append, autoFlush, charset, mode)
        map += key -> nlf
        nlf
      }
      case Some(lf) => lf
      case None => {
        val lf = new LogFile(key, path, append, autoFlush, charset, mode)
        map += key -> lf
        lf
      }
    }
  }
}

class LogFile(val key: String,
              val path: Path,
              val append: Boolean,
              val autoFlush: Boolean,
              val charset: Charset,
              val mode: LogFileMode) {
  private lazy val sizeCounter = new AtomicLong(0L)
  private lazy val writer = {
    Files.createDirectories(path.getParent)
    mode.createWriter(this)
  }

  def size: Long = sizeCounter.get()

  final def write(output: String): Unit = {
    writer.write(output)
    if (autoFlush) {
      writer.flush()
    }
    sizeCounter.addAndGet(output.length)
  }

  def rename(fileName: String): LogFile = rename(path.getParent.resolve(fileName))

  def rename(newPath: Path): LogFile = {
    if (Files.exists(newPath)) {
      Files.delete(newPath)
    }
    if (Files.exists(path)) {
      dispose()
      Files.move(path, newPath)
    }
    LogFile(newPath, append, autoFlush, charset, mode)
{'fraction_non_alphanumeric': 0.11361944926817166, 'fraction_numerical': 0.0009923096005953857, 'mean_word_length': 2.6129032258064515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2365692', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1256, 'n_words': 319}