Skip to content

Commit

Permalink
#164 refactor IPC operations to use their own log_cap_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-courtis committed May 7, 2024
1 parent d30fd2c commit a74ef4a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion inc/ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ struct IpcOperation {
int socket_client;
bool done;
int rc;
bool send_logs; // not for bad or colliding requests
bool send_state; // not for bad requests
struct SList *log_cap_lines;
};

struct IpcRequest {
Expand Down
4 changes: 4 additions & 0 deletions src/ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ void ipc_operation_free(struct IpcOperation *operation) {

ipc_request_free(operation->request);

log_cap_lines_stop(&operation->log_cap_lines);

slist_free_vals(&operation->log_cap_lines, log_cap_line_free);

free(operation);
}

11 changes: 4 additions & 7 deletions src/marshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,7 @@ void marshal_messages(YAML::Emitter &e, struct IpcOperation *operation) {
bool began = false;
LogThreshold threshold = operation->request->log_threshold;

for (struct SList *i = log_cap_lines; i; i = i->nex) {
for (struct SList *i = operation->log_cap_lines; i; i = i->nex) {
struct LogCapLine *cap_line = (struct LogCapLine*)i->val;

if (!cap_line || !cap_line->line) {
Expand Down Expand Up @@ -1048,9 +1048,7 @@ char *marshal_ipc_response(struct IpcOperation *operation) {
}
}

if (operation->send_logs) {
marshal_messages(e, operation);
}
marshal_messages(e, operation);

e << YAML::Key << "RC" << YAML::Value << operation->rc;

Expand All @@ -1070,9 +1068,8 @@ char *marshal_ipc_response(struct IpcOperation *operation) {
log_error("marshalling ipc response: %s", e.what());
}

if (operation->send_logs) {
log_capture_clear();
}
// clear marshalled messages
slist_free_vals(&operation->log_cap_lines, log_cap_line_free);

return yaml;
}
Expand Down
15 changes: 6 additions & 9 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ void notify_ipc_operation(void) {
ipc_send_operation(ipc_operation);

if (ipc_operation->done) {
log_capture_stop();
log_capture_clear();

close(ipc_operation->socket_client);

log_cap_lines_stop(&ipc_operation->log_cap_lines);
ipc_operation_free(ipc_operation);
ipc_operation = NULL;
}
Expand All @@ -70,22 +68,21 @@ void receive_ipc_request(int server_socket) {
return;
}

log_capture_clear();
log_capture_start();
ipc_operation = (struct IpcOperation*)calloc(1, sizeof(struct IpcOperation));
log_cap_lines_start(&ipc_operation->log_cap_lines);

struct IpcRequest *ipc_request = ipc_receive_request(server_socket);
if (!ipc_request) {
log_error("\nFailed to read IPC request");
log_capture_stop();
log_capture_clear();
log_cap_lines_stop(&ipc_operation->log_cap_lines);
ipc_operation_free(ipc_operation);
ipc_operation = NULL;
return;
}

ipc_operation = (struct IpcOperation*)calloc(1, sizeof(struct IpcOperation));
ipc_operation->request = ipc_request;
ipc_operation->socket_client = ipc_request->socket_client;
ipc_operation->done = true;
ipc_operation->send_logs = true;
ipc_operation->send_state = true;

if (ipc_request->bad) {
Expand Down
13 changes: 6 additions & 7 deletions tst/tst-marshalling.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

#include "marshalling.h"

void lcl(enum LogThreshold threshold, char *line) {
void lcl(enum LogThreshold threshold, char *line, struct SList **log_cap_lines) {
struct LogCapLine *lcl = calloc(1, sizeof(struct LogCapLine));

lcl->threshold = threshold;
lcl->line = strdup(line);

slist_append(&log_cap_lines, lcl);
slist_append(log_cap_lines, lcl);
}

int before_all(void **state) {
Expand Down Expand Up @@ -189,7 +189,6 @@ void marshal_ipc_response__map(void **state) {
ipc_operation->request = ipc_request;
ipc_operation->done = true;
ipc_operation->rc = 1;
ipc_operation->send_logs = true;
ipc_operation->send_state = true;

cfg = cfg_all();
Expand All @@ -198,10 +197,10 @@ void marshal_ipc_response__map(void **state) {
lid->closed = true;
lid->device_path = "/path/to/lid";

lcl(DEBUG, "dbg");
lcl(INFO, "inf");
lcl(WARNING, "war");
lcl(ERROR, "err");
lcl(DEBUG, "dbg", &ipc_operation->log_cap_lines);
lcl(INFO, "inf", &ipc_operation->log_cap_lines);
lcl(WARNING, "war", &ipc_operation->log_cap_lines);
lcl(ERROR, "err", &ipc_operation->log_cap_lines);

struct Mode mode1 = {
.width = 10,
Expand Down

0 comments on commit a74ef4a

Please sign in to comment.