Converted all stdout and stderr messages to JSON for parsing from child process

This commit is contained in:
Matt McWilliams 2024-05-14 20:17:06 -04:00
parent e902e1c3da
commit 8d6e1a8f67
3 changed files with 33 additions and 34 deletions

View File

@ -18,14 +18,12 @@ Mat Image::loadImage (string& image_path, uint64_t& x, uint64_t& y, uint64_t& w,
Mat image = getBlank(); Mat image = getBlank();
if (loaded.empty()) { if (loaded.empty()) {
cerr << "Image " << loaded << " empty" << endl; cerr << "{ \"error\" : \"" << image_path << " empty\" }" << endl;
} else { } else {
cout << "Loaded image " << image_path << endl; cout << "{ \"loaded\" : \"" << image_path << "\", ";
cout << " dim " << loaded.cols << "x" << loaded.rows << endl; cout << "\"original\" : { \"w\" : " << loaded.cols << ", \"h\" : " << loaded.rows << " }, ";
cout << "target " << w << "x" << h << endl; cout << "\"resized\" : { \"w\" : " << w << ", \"h\" : " << h << " } }" << endl;
resize(loaded, resized, Size(h, w)); resize(loaded, resized, Size(h, w));
//cout << " image " << image.cols << "x" << image.rows << endl;
//cout << "resized " << resized.cols << "x" << resized.rows << endl;
resized.copyTo(image(Rect(x, y, resized.cols, resized.rows))); resized.copyTo(image(Rect(x, y, resized.cols, resized.rows)));
flip(image, image, 0); flip(image, image, 0);

View File

@ -83,9 +83,9 @@ void postAction () {
const char* msg = outgoingMessage.c_str(); const char* msg = outgoingMessage.c_str();
int bytesSent = send(clientSocket, msg, strlen(msg), 0); int bytesSent = send(clientSocket, msg, strlen(msg), 0);
if (bytesSent < 0) { if (bytesSent < 0) {
cerr << "Error sending ACK to client" << endl; cerr << "{ \"error\" : \"Error sending ACK to client\" }" << endl;
} else { } else {
cout << "Sent = " << outgoingMessage << endl; cout << "{ \"sent\" : " << outgoingMessage << " }" << endl;
} }
} }
@ -114,7 +114,8 @@ void actionLoad () {
image.ptr()); // The actual image data itself image.ptr()); // The actual image data itself
auto localCurrentTime = steady_clock::now(); auto localCurrentTime = steady_clock::now();
auto localElapsedTime = duration_cast<milliseconds>(localCurrentTime - localStartTime).count(); auto localElapsedTime = duration_cast<milliseconds>(localCurrentTime - localStartTime).count();
cout << "actionLoad() [" << localElapsedTime << " ms]" << endl; cout << "{ \"load_time\" : " << localElapsedTime << " }" << endl;
completed = true;
} }
void loadBlank () { void loadBlank () {
@ -163,7 +164,7 @@ void display () {
auto currentTime = steady_clock::now(); auto currentTime = steady_clock::now();
auto elapsedTime = duration_cast<milliseconds>(currentTime - startTime).count(); auto elapsedTime = duration_cast<milliseconds>(currentTime - startTime).count();
if (elapsedTime > exposureTime) { if (elapsedTime > exposureTime) {
cout << "Exposed = " << elapsedTime << " ms" << endl; cout << "{ \"exposed\" : " << elapsedTime << ", \"exposure\" : " << exposureTime << " }" << endl;
displaying = false; displaying = false;
timing = false; timing = false;
completed = true; completed = true;
@ -219,7 +220,7 @@ void handleTCPConnection(int clientSocket) {
while (true) { while (true) {
int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0); int bytesRead = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
if (bytesRead <= 0) { if (bytesRead <= 0) {
cerr << "Client disconnected or errored out" << endl; cerr << "{ \"error\" : \"Client disconnected or errored out\" }" << endl;
break; break;
} }
@ -245,11 +246,11 @@ void runTCPServer (int serverSocket) {
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen); clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket < 0) { if (clientSocket < 0) {
cerr << "Error accepting client connection" << endl; cerr << "{ \"error\" : \"Error accepting client connection\" }" << endl;
continue; continue;
} }
cout << "New client connected" << endl; cout << "{ \"client\" : \"CONNECTED\" }" << endl;
handleTCPConnection(clientSocket); handleTCPConnection(clientSocket);
} }
@ -258,21 +259,21 @@ void runTCPServer (int serverSocket) {
uint8_t handleArgs (int argc, char** argv) { uint8_t handleArgs (int argc, char** argv) {
if (argc > 1) { if (argc > 1) {
screenWidth = atoi(argv[1]); screenWidth = atoi(argv[1]);
cout << "Set width: " << screenWidth << endl; cout << "{ \"screen_width\" : " << screenWidth << " }" << endl;
} else { } else {
cerr << "Please provide screen width as first argument" << endl; cerr << "{ \"error\" : \"Please provide screen width as first argument\" }" << endl;
return 1; return 1;
} }
if (argc > 2) { if (argc > 2) {
screenHeight = atoi(argv[2]); screenHeight = atoi(argv[2]);
cout << "Set height: " << screenHeight << endl; cout << "{ \"screen_height\" : " << screenHeight << " } " << endl;
} else { } else {
cerr << "Please provide screen height as second argument" << endl; cerr << "{ \"error\" : \"Please provide screen height as second argument\" }" << endl;
return 2; return 2;
} }
if (argc > 3) { if (argc > 3) {
PORT = (uint16_t) atoi(argv[3]); PORT = (uint16_t) atoi(argv[3]);
cout << "Set port: " << PORT << endl; cout << "{ \"port\" : " << PORT << " }" << endl;
} }
return 0; return 0;
} }
@ -298,7 +299,7 @@ int main (int argc, char** argv) {
int serverSocket = socket(AF_INET, SOCK_STREAM, 0); int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0) { if (serverSocket < 0) {
cerr << "Error creating socket" << endl; cerr << "{ \"error\" : \"Error creating socket\" }" << endl;
return 1; return 1;
} }
@ -309,17 +310,17 @@ int main (int argc, char** argv) {
int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr)); int bindResult = ::bind(serverSocket, reinterpret_cast<struct sockaddr*>(&serverAddr), sizeof(serverAddr));
if (bindResult < 0) { if (bindResult < 0) {
cerr << "Error binding socket" << endl; cerr << "{ \"error\" : \"Error binding socket\" }" << endl;
close(serverSocket); close(serverSocket);
return 1; return 1;
} }
if (listen(serverSocket, 5) < 0) { if (listen(serverSocket, 5) < 0) {
cerr << "Error listening on socket" << endl; cerr << "{ \"error\" : \"Error listening on socket\" }" << endl;
return 1; return 1;
} }
cout << "TCP server listening on port " << PORT << endl; cout << "{ \"listening\" : true, \"port\" : " << PORT << " }" << endl;
thread serverThread(runTCPServer, serverSocket); thread serverThread(runTCPServer, serverSocket);

View File

@ -18,16 +18,16 @@ void State::setAction (json& msgData) {
action = static_cast<Action>(msgData["action"]); action = static_cast<Action>(msgData["action"]);
switch (action) { switch (action) {
case NONE: case NONE:
cout << "Action = NONE" << endl; cout << "{ \"action\" : \"NONE\" }" << endl;
break; break;
case LOAD: case LOAD:
cout << "Action = LOAD" << endl; cout << "{ \"action\" : \"LOAD\" }" << endl;
break; break;
case DISPLAY: case DISPLAY:
cout << "Action = DISPLAY" << endl; cout << "{ \"action\" : \"DISPLAY\" }" << endl;
break; break;
case STOP: case STOP:
cout << "Action = STOP" << endl; cout << "{ \"action\" : \"STOP\" }" << endl;
break; break;
} }
} }
@ -36,15 +36,15 @@ void State::setImage (json& msgData) {
image = msgData["image"]; image = msgData["image"];
if (!imageExists(image)) { if (!imageExists(image)) {
error(); error();
cerr << "Image " << msgData["image"] << " does not exist" << endl; cerr << "{ \"error\" : \"Image " << msgData["image"] << " does not exist\" }" << endl;
return; return;
} }
cout << "Image = " << image << endl; cout << "{ \"image\" : \"" << image << "\" }" << endl;
} }
void State::setMode (json& msgData) { void State::setMode (json& msgData) {
mode = static_cast<Mode>(msgData["mode"]); mode = static_cast<Mode>(msgData["mode"]);
cout << "Mode = " << mode << endl; cout << "{ \"mode\" : \"" << mode << "\" }"<< endl;
} }
void State::setPosition (json& msgData) { void State::setPosition (json& msgData) {
@ -52,7 +52,7 @@ void State::setPosition (json& msgData) {
y = msgData["position"]["y"]; y = msgData["position"]["y"];
w = msgData["position"]["w"]; w = msgData["position"]["w"];
h = msgData["position"]["h"]; h = msgData["position"]["h"];
cout << "Position[x] = " << x << " [y] = " << y << " [w] = " << w << " [h] = " << h << endl; cout << "{ \"position\" : { \"x\" : " << x << ", \"y\" : " << y << ", \"w\" : " << w << ", \"h\" : " << h << " }" << endl;
} }
void State::setExposure (json& msgData) { void State::setExposure (json& msgData) {
@ -60,20 +60,20 @@ void State::setExposure (json& msgData) {
setNoExposure(); setNoExposure();
} else if (msgData["exposure"].size() == 1) { } else if (msgData["exposure"].size() == 1) {
exposure = { msgData["exposure"][0] }; exposure = { msgData["exposure"][0] };
cout << "Exposure = " << msgData["exposure"][0] << " ms" << endl; cout << "{ \"exposure\" : " << msgData["exposure"][0] << " }" << endl;
} /*else if (msgData["exposure"].size() == 3) { } /*else if (msgData["exposure"].size() == 3) {
exposure = { msgData["exposure"][0], msgData["exposure"][1], msgData["exposure"][2] }; exposure = { msgData["exposure"][0], msgData["exposure"][1], msgData["exposure"][2] };
cout << "Exposure[0] = " << msgData["exposure"][0] << " ms, [1] = " << msgData["exposure"][1] << " ms, [2] = " << msgData["exposure"][2] << " ms" << endl; cout << "Exposure[0] = " << msgData["exposure"][0] << " ms, [1] = " << msgData["exposure"][1] << " ms, [2] = " << msgData["exposure"][2] << " ms" << endl;
}*/ else { }*/ else {
error(); error();
cerr << "Exposure array is incorrect length, " << msgData["exposure"].size() << endl; cerr << "{ \"error\" : \"Exposure array is incorrect length, " << msgData["exposure"].size() << "\" }" << endl;
return; return;
} }
} }
void State::setNoExposure () { void State::setNoExposure () {
exposure = {}; exposure = {};
cout << "No exposure, display indefinitely" << endl; cout << "{ \"exposure\" : null }" << endl;
} }
void State::receiveMessage (string msgString) { void State::receiveMessage (string msgString) {
@ -84,7 +84,7 @@ void State::receiveMessage (string msgString) {
} else { } else {
action = NONE; action = NONE;
error(); error();
cerr << "Received invalid message: " << msgString << endl; cerr << "{ \"error\" : Received invalid message: " << msgString << "\" }" << endl;
return; return;
} }