<commit_before>#include "controller.h"

#include <assert.h>

#include "util/log.h"

bool
controller_init(struct controller *controller, socket_t control_socket) {
    cbuf_init(&controller->queue);

    bool ok = receiver_init(&controller->receiver, control_socket);
    if (!ok) {
        return false;
    }

    ok = sc_mutex_init(&controller->mutex);
    if (!ok) {
        receiver_destroy(&controller->receiver);
        return false;
    }

    ok = sc_cond_init(&controller->msg_cond);
    if (!ok) {
        receiver_destroy(&controller->receiver);
        sc_mutex_destroy(&controller->mutex);
        return false;
    }

    controller->control_socket = control_socket;
    controller->stopped = false;

    return true;
}

void
controller_destroy(struct controller *controller) {
    sc_cond_destroy(&controller->msg_cond);
    sc_mutex_destroy(&controller->mutex);

    struct control_msg msg;
    while (cbuf_take(&controller->queue, &msg)) {
        control_msg_destroy(&msg);
    }

    receiver_destroy(&controller->receiver);
}

bool
controller_push_msg(struct controller *controller,
                      const struct control_msg *msg) {
    control_msg_log(msg);
    sc_mutex_lock(&controller->mutex);
    bool was_empty = cbuf_is_empty(&controller->queue);
    bool res = cbuf_push(&controller->queue, *msg);
    if (was_empty) {
        sc_cond_signal(&controller->msg_cond);
    }
    sc_mutex_unlock(&controller->mutex);
    return res;
}

static bool
process_msg(struct controller *controller,<commit_msg>Attempt to log message only in verbose mode<commit_after>#include "controller.h"

#include <assert.h>

#include "util/log.h"

bool
controller_init(struct controller *controller, socket_t control_socket) {
    cbuf_init(&controller->queue);

    bool ok = receiver_init(&controller->receiver, control_socket);
    if (!ok) {
        return false;
    }

    ok = sc_mutex_init(&controller->mutex);
    if (!ok) {
        receiver_destroy(&controller->receiver);
        return false;
    }

    ok = sc_cond_init(&controller->msg_cond);
    if (!ok) {
        receiver_destroy(&controller->receiver);
        sc_mutex_destroy(&controller->mutex);
        return false;
    }

    controller->control_socket = control_socket;
    controller->stopped = false;

    return true;
}

void
controller_destroy(struct controller *controller) {
    sc_cond_destroy(&controller->msg_cond);
    sc_mutex_destroy(&controller->mutex);

    struct control_msg msg;
    while (cbuf_take(&controller->queue, &msg)) {
        control_msg_destroy(&msg);
    }

    receiver_destroy(&controller->receiver);
}

bool
controller_push_msg(struct controller *controller,
                      const struct control_msg *msg) {
    if (sc_get_log_level() <= SC_LOG_LEVEL_VERBOSE) {
        control_msg_log(msg);
    }

    sc_mutex_lock(&controller->mutex);
    bool was_empty = cbuf_is_empty(&controller->queue);
    bool res = cbuf_push(&controller->queue, *msg);
    if (was_empty) {
        sc_cond_signal(&controller->msg_cond);
    }
    sc_mutex_unlock(&controller->mutex);
    return res;
}

static bool
process_msg(struct controller *controller,
{'fraction_non_alphanumeric': 0.12029123140234252, 'fraction_numerical': 0.0, 'mean_word_length': 3.533715925394548, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 41, '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': '1161111', 'n_tokens_mistral': 964, 'n_tokens_neox': 950, 'n_words': 177}