public static void saveExistingErrors(
        final File markFile,
        final AtomicBuffer errorBuffer,
        final PrintStream logger,
        final String errorFilePrefix)
    {
        try
        {
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            final int observations = printErrorLog(errorBuffer, new PrintStream(baos, false, "US-ASCII"));
            if (observations > 0)
            {
                final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSZ");
                final File errorLogFile = new File(
                    markFile.getParentFile(), errorFilePrefix + '-' + dateFormat.format(new Date()) + "-error.log");

                if (null != logger)
                {
                    logger.println("WARNING: existing errors saved to: " + errorLogFile);
                }

                try (FileOutputStream out = new FileOutputStream(errorLogFile))
                {
                    baos.writeTo(out);
                }
            }
        }
        catch (final Exception ex)
        {
            LangUtil.rethrowUnchecked(ex);
        }
    }
@Test
    void saveExistingErrorsCreatesErrorFileInTheSameDirectoryAsTheCorrespondingMarkFile()
    {
        final File markFile = tempDir.resolve("mark.dat").toFile();
        final DistinctErrorLog errorLog =
            new DistinctErrorLog(new UnsafeBuffer(allocateDirect(10 * 1024)), SystemEpochClock.INSTANCE);
        assertTrue(errorLog.record(new Exception("Just to test")));
        final PrintStream logger = mock(PrintStream.class);
        final String errorFilePrefix = "my-file-";

        CommonContext.saveExistingErrors(markFile, errorLog.buffer(), logger, errorFilePrefix);

        final File[] files = tempDir.toFile().listFiles(
            (dir, name) -> name.endsWith("-error.log") && name.startsWith(errorFilePrefix));
        assertNotNull(files);
        assertEquals(1, files.length);

        verify(logger).println(and(startsWith("WARNING: existing errors saved to: "), endsWith("-error.log")));
        verifyNoMoreInteractions(logger);
    }