<commit_before>#include "./framework.h"
#include <sys/stat.h>
#include <string>
#include <memory>
#include <csignal>
#include <sys/types.h>
#include <unistd.h>
#include "backtrace_formatter.h"

namespace lms {
namespace internal {

Framework::Framework(const std::string &mainConfigFilePath)
    : m_executionManager(*this), logger("lms.Framework"),
      mainConfigFilePath(mainConfigFilePath), m_running(false), is_debug(false) {

    // start();
}

void Framework::addSearchPath(const std::string &path) {
    m_loader.addSearchPath(path);
}

void Framework::start() {
    bool firstRun = true;
    m_running = true;

    while (m_running) {
        // config monitor stuff
        if (firstRun || configMonitor.hasChangedFiles()) {
            logger.info() << "Reload configs";
            configMonitor.unwatchAll();
            RuntimeInfo runtime;
            for(const auto &f: flags) {
                logger.info("flag") << f;
            }
            XmlParser parser(runtime, flags);
            parser.parseFile(mainConfigFilePath);

            for (auto error : parser.errors()) {
                logger.error("XML") << error;
            }

            try {
                logger.time("updateSystem");
                if(! updateSystem(runtime)) {
                    m_running = false;
                }
                logger.timeEnd("updateSystem");
            } catch(std::exception const &ex) {
                logger.error() << lms::typeName(ex) << ": " <<  ex.what();
                m_running = false;
            }

            if(isDebug()) {
                printOverview();
            }

            for (auto file : parser.files()) {
                configMonitor.watch(file);
            }
        }

        if(!cycle()) {
            m_running = false;
        }
        firstRun = false;
    }

    logger.info() << "Stopped";
}

bool Framework::updateSystem(const RuntimeInfo &info) {
    if(isDebug()) {
        logger.debug() << "updateSystem()";
    }

    // Update or load services
    for (const ServiceInfo &serviceInfo : info.services) {
        if(isDebug()) {
            logger.debug() << "Loading service " << serviceInfo.name;
        }
        auto it = services.find(serviceInfo.name);
        if (it == services.end()) {
            // Not loaded yet
            std::shared_ptr<Service> service(m_loader.loadService(serviceInfo));

            service->initBase(serviceInfo);

            try {
                if (!service->init()) {
                    logger.error() << "Service " << serviceInfo.name
                                   << " failed to init()";
                    return false;
                }
            } catch (std::exception const &ex) {
                logger.error() << serviceInfo.name << " throws "
                               << lms::typeName(ex) << " : " << ex.what();
                return false;
            }

            services[serviceInfo.name] = service;
        } else {
            // already loaded
            it->second->initBase(serviceInfo);
            it->second->configsChanged();
        }<commit_msg>Configure clock from configs<commit_after>#include "./framework.h"
#include <sys/stat.h>
#include <string>
#include <memory>
#include <csignal>
#include <sys/types.h>
#include <unistd.h>
#include "backtrace_formatter.h"

namespace lms {
namespace internal {

Framework::Framework(const std::string &mainConfigFilePath)
    : m_executionManager(*this), logger("lms.Framework"),
      mainConfigFilePath(mainConfigFilePath), m_running(false), is_debug(false) {

    // start();
}

void Framework::addSearchPath(const std::string &path) {
    m_loader.addSearchPath(path);
}

void Framework::start() {
    bool firstRun = true;
    m_running = true;

    while (m_running) {
        // config monitor stuff
        if (firstRun || configMonitor.hasChangedFiles()) {
            logger.info() << "Reload configs";
            configMonitor.unwatchAll();
            RuntimeInfo runtime;
            for(const auto &f: flags) {
                logger.info("flag") << f;
            }
            XmlParser parser(runtime, flags);
            parser.parseFile(mainConfigFilePath);

            for (auto error : parser.errors()) {
                logger.error("XML") << error;
            }

            try {
                logger.time("updateSystem");
                if(! updateSystem(runtime)) {
                    m_running = false;
                }
                logger.timeEnd("updateSystem");
            } catch(std::exception const &ex) {
                logger.error() << lms::typeName(ex) << ": " <<  ex.what();
                m_running = false;
            }

            if(isDebug()) {
                printOverview();
            }

            for (auto file : parser.files()) {
                configMonitor.watch(file);
            }
        }

        if(!cycle()) {
            m_running = false;
        }
        firstRun = false;
    }

    logger.info() << "Stopped";
}

bool Framework::updateSystem(const RuntimeInfo &info) {
    if(isDebug()) {
        logger.debug() << "updateSystem()";
    }

    // Update clock
    m_clock.cycleTime(info.clock.cycle);
    m_clock.enabledSleep(info.clock.sleep);
    m_clock.enabledSlowWarning(info.clock.slowWarnings);
    m_clock.enabledCompensate(info.clock.sleepCompensate);

    // Update or load services
    for (const ServiceInfo &serviceInfo : info.services) {
        if(isDebug()) {
            logger.debug() << "Loading service " << serviceInfo.name;
        }
        auto it = services.find(serviceInfo.name);
        if (it == services.end()) {
            // Not loaded yet
            std::shared_ptr<Service> service(m_loader.loadService(serviceInfo));

            service->initBase(serviceInfo);

            try {
                if (!service->init()) {
                    logger.error() << "Service " << serviceInfo.name
                                   << " failed to init()";
                    return false;
                }
            } catch (std::exception const &ex) {
                logger.error() << serviceInfo.name << " throws "
                               << lms::typeName(ex) << " : " << ex.what();
                return false;
            }

            services[serviceInfo.name] = service;
        } else {
            // already loaded
            it->second->initBase(serviceInfo);
            it->second->configsChanged();
        }
{'fraction_non_alphanumeric': 0.1333745938418691, 'fraction_numerical': 0.0, 'mean_word_length': 1.7114093959731544, 'pattern_counts': {'":': 2, '<': 89, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2198171', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1701, 'n_words': 333}