<commit_before>﻿using System;
using System.IO;

namespace Symbooglix
{
    public class CouldNotCreateDirectoryException : Exception
    {
        public CouldNotCreateDirectoryException(string msg) : base(msg)
        {

        }
    }

    public class ExecutorLogger : IExecutorEventHandler
    {
        public DirectoryInfo Root
        {
            get;
            private set;
        }

        public DirectoryInfo TerminatedExecutionStatesDir
        {
            get;
            private set;
        }

        private ExecutionStateLogger TerminatedStateConstraintsLogger;
        private ExecutionStateLogger TerminatedStateUnsatCoreLogger;
        private ExecutionStateLogger TerminatedStateInfoLogger;

        public ExecutorLogger(string path, bool makeDirectoryInPath)
        {
            this.Root = null;

            if (makeDirectoryInPath)
            {
                // Try to find an available directory to log stuff in
                // FIXME: How do we fix the TOCTOU here?
                for (int counter = 0; counter < 5000; ++counter)
                {
                    string pathToUse = Path.Combine(path, "symbooglix-" + counter);

                    if (Directory.Exists(pathToUse))
                        continue;

                    try
                    {
                        this.Root = Directory.CreateDirectory(pathToUse);
                        break;
                    }
                    catch (Exception e)
                    {
                        throw new CouldNotCreateDirectoryException("Exception throw when creating: " + e.ToString());
                    }
                }

                if (this.Root == null)
                    throw new CouldNotCreateDirectoryException("Available numbers exhausted");

            }
            else
            {
                if (Directory.Exists(path))
                    throw new CouldNotCreateDirectoryException("Directory \"" + path + "\" already exists");

                // Just use the path
                try
                {
                    this.Root = Directory.CreateDirectory(path);
                }
                catch (Exception e)
                {
                    throw new CouldNotCreateDirectoryException("Exception throw when creating: " + e.ToString());
                }
            }

            SetupLoggers();
        }

        private void CreateDirectories()
        {
            TerminatedExecutionStatesDir = Directory.CreateDirectory(Path.Combine(this.Root.FullName, "terminated_states"));
        }

        protected virtual void SetupLoggers()
        {
            CreateDirectories();
            TerminatedStateConstraintsLogger = new ExecutionStateConstraintLogger(this.TerminatedExecutionStatesDir.FullName);
            TerminatedStateUnsatCoreLogger = new ExecutionStateUnSatCoreLogger(this.TerminatedExecutionStatesDir.FullName);
            TerminatedStateInfoLogger = new ExecutionStateInfoLogger(this.TerminatedExecutionStatesDir.FullName);
        }

        public void Connect(Executor e)
        {
            TerminatedStateConstraintsLogger.Connect(e);
            TerminatedStateUnsatCoreLogger.Connect(e);
            TerminatedStateInfoLogger.Connect(e);
        }

        public void Disconnect(Executor e)
        {
            TerminatedStateConstraintsLogger.Disconnect(e);
            TerminatedStateUnsatCoreLogger.Disconnect(e);
            TerminatedStateInfoLogger.Disconnect(e);
        }
    }
}
<commit_msg>Refactor ExecutorLogger a little so that it has a list of IExecutorEventHandlers rather than individual fields. In principle this should make it a little easier to add other loggers.<commit_after>﻿using System;
using System.IO;
using System.Collections.Generic;

namespace Symbooglix
{
    public class CouldNotCreateDirectoryException : Exception
    {
        public CouldNotCreateDirectoryException(string msg) : base(msg)
        {

        }
    }

    public class ExecutorLogger : IExecutorEventHandler
    {
        public DirectoryInfo Root
        {
            get;
            private set;
        }

        public DirectoryInfo TerminatedExecutionStatesDir
        {
            get;
            protected set;
        }

        private List<IExecutorEventHandler> Loggers;

        public ExecutorLogger(string path, bool makeDirectoryInPath)
        {
            this.Root = null;
            this.Loggers = new List<IExecutorEventHandler>();

            if (makeDirectoryInPath)
            {
                // Try to find an available directory to log stuff in
                // FIXME: How do we fix the TOCTOU here?
                for (int counter = 0; counter < 5000; ++counter)
                {
                    string pathToUse = Path.Combine(path, "symbooglix-" + counter);

                    if (Directory.Exists(pathToUse))
                        continue;

                    try
                    {
                        this.Root = Directory.CreateDirectory(pathToUse);
                        break;
                    }
                    catch (Exception e)
                    {
                        throw new CouldNotCreateDirectoryException("Exception throw when creating: " + e.ToString());
                    }
                }

                if (this.Root == null)
                    throw new CouldNotCreateDirectoryException("Available numbers exhausted");

            }
            else
            {
                if (Directory.Exists(path))
                    throw new CouldNotCreateDirectoryException("Directory \"" + path + "\" already exists");

                // Just use the path
                try
                {
                    this.Root = Directory.CreateDirectory(path);
                }
                catch (Exception e)
                {
                    throw new CouldNotCreateDirectoryException("Exception throw when creating: " + e.ToString());
                }
            }

            CreateDirectories();
            SetupLoggers();
        }

        protected virtual void CreateDirectories()
        {
            TerminatedExecutionStatesDir = Directory.CreateDirectory(Path.Combine(this.Root.FullName, "terminated_states"));
        }

        protected virtual void SetupLoggers()
        {
            Loggers.Add(new ExecutionStateConstraintLogger(this.TerminatedExecutionStatesDir.FullName));
            Loggers.Add(new ExecutionStateUnSatCoreLogger(this.TerminatedExecutionStatesDir.FullName));
            Loggers.Add(new ExecutionStateInfoLogger(this.TerminatedExecutionStatesDir.FullName));
        }

        public void Connect(Executor e)
        {
            foreach (var logger in Loggers)
                logger.Connect(e);
        }

        public void Disconnect(Executor e)
        {
            foreach (var logger in Loggers)
                logger.Disconnect(e);
        }
    }
}

{'fraction_non_alphanumeric': 0.06740602045225406, 'fraction_numerical': 0.001440299582313121, 'mean_word_length': 1.5473220836390316, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '371445', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1512, 'n_words': 359}