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();
if (loaded.empty()) {
cerr << "Image " << loaded << " empty" << endl;
cerr << "{ \"error\" : \"" << image_path << " empty\" }" << endl;
} else {
cout << "Loaded image " << image_path << endl;
cout << " dim " << loaded.cols << "x" << loaded.rows << endl;
cout << "target " << w << "x" << h << endl;
cout << "{ \"loaded\" : \"" << image_path << "\", ";
cout << "\"original\" : { \"w\" : " << loaded.cols << ", \"h\" : " << loaded.rows << " }, ";
cout << "\"resized\" : { \"w\" : " << w << ", \"h\" : " << h << " } }" << endl;
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)));
flip(image, image, 0);

View File

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

View File

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