Commit c9f743b6 authored by finn.wundram's avatar finn.wundram
Browse files

Merge branch 'simpleLogging' into 'main'

Simple logging

See merge request !55
parents 8f4a2524 e0a86fbe
Pipeline #272740 passed with stages
in 4 minutes and 10 seconds
......@@ -7,6 +7,8 @@
#include <stdint.h>
#include "PreProcessingStrings.hpp"
#include "LoggingFacility.hpp"
#include "SimpleFileLogger.hpp"
std::vector<int> coordinateInput();
std::vector<uint8_t> colorInput();
......@@ -29,8 +31,8 @@ int main(int argc, char* argv[]){
}
}
}
Client c(ip_address, port);
Logger log = std::make_shared<SimpleFileLogger>("./client_log.txt");
Client c(ip_address, port, log);
std::string input;
std::vector< std::vector<int> > coordinates;
......
......@@ -4,7 +4,9 @@ project(DisplayProject VERSION 0.0.1)
set(CLIENT_DIR "../../CClient")
set(SHAREDLIB_DIR "../../sharedlib")
add_executable(client.out main.cpp "${CLIENT_DIR}/Client.cpp" "${SHAREDLIB_DIR}/protocol.cpp" "${SHAREDLIB_DIR}/base64.cpp")
file(GLOB SHAREDSRC ${SHAREDLIB_DIR}/*.cpp)
add_executable(client.out main.cpp "${CLIENT_DIR}/Client.cpp" ${SHAREDSRC})
target_include_directories(client.out PUBLIC ${CLIENT_DIR} ${SHAREDLIB_DIR})
......
......@@ -29,6 +29,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <string>
#include "Client.hpp"
#include "LoggingFacility.hpp"
#include "SimpleFileLogger.hpp"
bool isNumeric(std::string str) {
for (int i = 0; i < str.length(); i++)
......@@ -94,7 +96,8 @@ bool cellState(int posX, int posY, std::vector<std::vector<bool>> field)
//Survival: Each live cell with either two or three live neighbors will remain alive for the next generation.
}
int main(int argc, char* argv[]){
Client c = Client("../res/server.toml");
Logger log = std::make_shared<SimpleFileLogger>("./gol_log.txt");
Client c = Client("../res/server.toml", log);
std::string text = "test";
std::vector<char> sendtext;
std::vector<int> screen = std::vector<int>(2);
......
cmake_minimum_required(VERSION 3.12)
project(DisplayProject VERSION 0.0.1)
add_executable(client.out main.cpp Client.cpp ../sharedlib/protocol.cpp ../sharedlib/base64.cpp)
set(CLIENT_DIR ".")
set(SHAREDLIB_DIR "../sharedlib")
file(GLOB SHAREDSRC ${SHAREDLIB_DIR}/*.cpp)
add_executable(client.out main.cpp "${CLIENT_DIR}/Client.cpp" ${SHAREDSRC})
target_include_directories(client.out PUBLIC ${CLIENT_DIR} ${SHAREDLIB_DIR})
target_compile_options(client.out PUBLIC -Wall -Wextra -Wpedantic -O0 -g -std=c++17)
......@@ -36,13 +36,11 @@ std::string Client::printText(std::vector<char> text, int x, int y, unsigned cha
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
printf(sendString.c_str());
printf("\n");
printf("Client: Message sent. Waiting for response\n");
log->info("Client: Message sent. Waiting for response\n");
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
......@@ -59,11 +57,10 @@ std::ostringstream Client::setPixel(int x, int y, uint8_t red,uint8_t green,uint
std::string sendString = dcs_protocol::addHeader("setPixel", "Text", content);
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
printf(sendString.c_str());
printf("\n");
printf("Client: Message sent. Waiting for response\n");
log->info(sendString + "\n");
log->info("Client: Message sent. Waiting for response\n");
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
std::ostringstream temp;
temp << sendString;
......@@ -75,7 +72,7 @@ std::vector<uint8_t> Client::get_bytes( std::string file_name )
std::ifstream infile(file_name, std::ios::in | std::ios::binary);
if (!infile) {
perror("file could not be opened\n");
log->error("file could not be opened\n");
std::vector<uint8_t> empty;
return empty;
}
......@@ -122,7 +119,7 @@ void Client::updateScreen(bool scrolling)
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n");
log->info("Client: Received server response\n");
}
void Client::clearScreen()
......@@ -134,7 +131,7 @@ void Client::clearScreen()
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n");
log->info("Client: Received server response\n");
}
void Client::stopServer(){
......@@ -145,7 +142,7 @@ void Client::stopServer(){
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n");
log->info("Client: Received server response\n");
}
std::vector<int> Client::getScreenSize()
......@@ -155,12 +152,11 @@ std::vector<int> Client::getScreenSize()
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
printf(sendString.data());
printf("\n");
printf("Client: Message sent. Waiting for response\n");
log->info(sendString + "\n");
log->info("Client: Message sent. Waiting for response\n");
std::vector<uint8_t> response = receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
std::string content (response.begin(), response.end());
content = dcs_protocol::getContent(content);
......@@ -186,12 +182,11 @@ Client::ScreenPixel Client::getPixel(int x, int y)
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
printf(sendString.data());
printf("\n");
printf("Client: Message sent. Waiting for response\n");
log->info(sendString + "\n");
log->info("Client: Message sent. Waiting for response\n");
std::vector<uint8_t> response = receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
std::string rcvd_content (response.begin(), response.end());
rcvd_content = dcs_protocol::getContent(rcvd_content);
......@@ -212,8 +207,9 @@ Client::ScreenPixel Client::getPixel(int x, int y)
}
Client::Client(std::string file_name)
Client::Client(std::string file_name, Logger log_)
{
log = log_;
server_response_buffer = std::vector<char>(256);
//Laden von Daten aus den Toml Dateien
......@@ -229,8 +225,9 @@ Client::Client(std::string file_name)
server_address_length = sizeof(server_address);
}
Client::Client(std::string ip_address, int port){
Client::Client(std::string ip_address, int port, Logger log_){
log = log_;
server_response_buffer = std::vector<char>(256);
server_input_socket = socket(AF_INET, SOCK_DGRAM, 0);
......@@ -239,7 +236,7 @@ Client::Client(std::string ip_address, int port){
server_address.sin_port = htons(port);
if(inet_pton(AF_INET, ip_address.c_str(), &server_address.sin_addr) <= 0){
fprintf(stderr, "Invalid address: %s\n", ip_address.c_str());
log->error("Invalid address: " + ip_address);
std::exit(EXIT_FAILURE);
}
server_address_length = sizeof(server_address);
......@@ -278,7 +275,7 @@ std::string Client::drawLine(int xStart, int yStart, int xEnd, int yEnd, uint8_t
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
......@@ -289,7 +286,7 @@ std::string Client::drawPolygon(std::vector<std::vector<int>> points, uint8_t r,
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
......@@ -307,7 +304,7 @@ std::string Client::drawTriangle(int xA, int yA, int xB, int yB, int xC, int yC,
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
......@@ -325,7 +322,7 @@ std::string Client::drawTetragon(int xA, int yA, int xB, int yB, int xC, int yC,
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
......@@ -342,13 +339,13 @@ std::string Client::drawCircle(int x, int y, int rad, uint8_t r, uint8_t g, uint
send_message(std::vector<uint8_t>(sendString.begin(), sendString.end()));
receive_message();
printf("Client: Received server response\n"); //Auf Rueckmeldung warten
log->info("Client: Received server response\n"); //Auf Rueckmeldung warten
return sendString;
}
int Client::send_message(std::vector<uint8_t> msg){
printf("Message size: %d\n", msg.size());
log->info("Message size: " + std::to_string(msg.size()));
return sendto(server_input_socket, msg.data(), msg.size(), 0, (sockaddr *)&server_address, sizeof(server_address));
}
......
......@@ -25,6 +25,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include <time.h>
#include "LoggingFacility.hpp"
typedef int SOCKET;
class Client{
......@@ -39,6 +41,8 @@ class Client{
std::vector<uint8_t> get_bytes( std::string file_name );
std::string drawGenericShape(std::string shape, std::vector<std::vector<int>> points, uint8_t r, uint8_t g, uint8_t b);
Logger log;
public:
struct ScreenPixel{
......@@ -46,8 +50,8 @@ class Client{
uint8_t g;
uint8_t b;
};
Client(std::string file_name);
Client(std::string ip_address, int port);
Client(std::string file_name, Logger log_);
Client(std::string ip_address, int port, Logger log_);
std::string printText(std::vector<char> text, int x, int y, unsigned char red, unsigned char green,unsigned char blue);
std::ostringstream setPixel(int x, int y, uint8_t red,uint8_t green,uint8_t blue);
......
......@@ -29,6 +29,9 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "Client.hpp"
bool unittest(std::string serverpath);
#include "LoggingFacility.hpp"
#include "SimpleFileLogger.hpp"
#include <iostream>
using std::cout;
using std::cerr;
......@@ -60,7 +63,8 @@ int main(int argc, char* argv[]){
/*
Client c = Client("./res/server.toml");
Logger log = std::make_shared<SimpleFileLogger>("client_log.txt");
Client c = Client("./res/server.toml", log);
//std::vector<int> screen = c.getScreenSize();
//std::cout << screen[0] << std::endl;
......@@ -116,7 +120,8 @@ bool equalFiles(ifstream& in1, ifstream& in2)
bool unittest(std::string serverpath)
{
Client c = Client("./res/server.toml");
Logger log = std::make_shared<SimpleFileLogger>("client_log.txt");
Client c = Client("./res/server.toml", log);
c.setPixel(0,0,0,125,255);
Client::ScreenPixel px = c.getPixel(0,0);
......
......@@ -10,10 +10,10 @@ add_definitions(-DMAGICKCORE_HDRI_ENABLE=0)
set(SHAREDLIB ../sharedlib)
set(IMAGEPREPROCESSORS ./ImagePreprocessors)
file(GLOB IMAGEPREPROCESSOR_SOURCES ${IMAGEPREPROCESSORS}/*.cpp)
file(GLOB IMAGEPREPROCESSOR_SRC ${IMAGEPREPROCESSORS}/*.cpp)
file(GLOB SHAREDSRC ${SHAREDLIB}/*.cpp)
add_executable(server.out main.cpp framebuffer.cpp Font.cpp Bitmap.cpp framebuffer_utils.cpp wetelco_adapter.cpp wetelco_sp.cpp RaspberryClient.cpp SocketHandler.cpp FramebufferController.cpp RaspyAdapter.cpp PPM.cpp ${IMAGEPREPROCESSOR_SOURCES} ${SHAREDSRC} Shapemaker.cpp checkImageValidity.cpp PreprocessingParser.cpp)
add_executable(server.out main.cpp framebuffer.cpp Font.cpp Bitmap.cpp framebuffer_utils.cpp wetelco_adapter.cpp wetelco_sp.cpp RaspberryClient.cpp SocketHandler.cpp FramebufferController.cpp RaspyAdapter.cpp PPM.cpp ${IMAGEPREPROCESSOR_SRC} ${SHAREDSRC} Shapemaker.cpp checkImageValidity.cpp PreprocessingParser.cpp)
target_compile_options(server.out PUBLIC -Wall -Wextra -Wpedantic -O0 -std=c++17)
......
......@@ -70,7 +70,6 @@ std::vector<uint8_t> FramebufferController::handle_msg(std::vector<uint8_t> msg)
//Erste Zeile der Nachricht muss immer die Funktion sein
if (std::getline(f, line)) {
std::cout << line << std::endl;
if(line==FUNCTIONPTXT){
response = printText(msg);
......@@ -245,7 +244,6 @@ std::vector<uint8_t> FramebufferController::printText(std::vector<uint8_t> msg)
std::vector<uint8_t> FramebufferController::sendDisplaySize(int sizeX, int sizeY)
{
std::cout << "sendDisplaySize" << std::endl;
std::string content = dcs_protocol::setNumericAttribute("Width", sizeX);
content += dcs_protocol::setNumericAttribute("Height", sizeY);
......@@ -290,10 +288,8 @@ std::vector<uint8_t> FramebufferController::sendPixel(std::vector<uint8_t> msg)
}
void FramebufferController::update_display(bool scrolling){
printf("Updating screen\n");
buffer->create_ppm();
this->display->update(this->buffer, scrolling);
printf("Done\n");
}
void FramebufferController::stop(){
......
......@@ -18,7 +18,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdio.h>
#include <unistd.h>
SocketHandler::SocketHandler(int port, int buffersize){
#include <string>
SocketHandler::SocketHandler(int port, int buffersize, Logger log_){
log = log_;
this->server_input_socket = socket(AF_INET, SOCK_DGRAM, 0);
this->server_address.sin_family = AF_INET;
......@@ -31,7 +34,7 @@ SocketHandler::SocketHandler(int port, int buffersize){
if( bind(this->server_input_socket, (sockaddr*) &(this->server_address), this->server_address_length) < 0){
perror("SockerHandler: Could not bind to socket.\n");
log->error("SockerHandler: Could not bind to socket.\n");
}
this->message_buffer = std::vector<uint8_t>(buffersize);
......@@ -40,7 +43,7 @@ SocketHandler::SocketHandler(int port, int buffersize){
std::vector<uint8_t> SocketHandler::receive(){
if(this->message_buffer.size() == 0){ //Wenn Nachrichtenbuffer Länge von 0 hat wird sofort eine leere
perror("SockerHandler: Input-message buffer has a length of zero\n"); //Nachricht empfangen, dies ist nicht gewollt
log->error("SockerHandler: Input-message buffer has a length of zero\n"); //Nachricht empfangen, dies ist nicht gewollt
}
int input_message_status = recvfrom(this->server_input_socket, this->message_buffer.data(), this->message_buffer.size(), 0, (sockaddr*) &(this->client_address), (socklen_t*) &(this->client_address_length));
......@@ -48,13 +51,15 @@ std::vector<uint8_t> SocketHandler::receive(){
sleep(1);
if (input_message_status < 0){
perror("SockerHandler: Failed to receive message\n"); //Fehler augetreten
log->error("SockerHandler: Failed to receive message\n"); //Fehler augetreten
} else if (input_message_status == 0){
perror("SockerHandler: Received empty message\n"); //Leer Nachricht
log->error("SockerHandler: Received empty message\n"); //Leer Nachricht
} else if(message_buffer.size() < input_message_status){
perror("SockerHandler: Buffer too small\n"); //Nachricht größer als der Buffer
log->error("SockerHandler: Buffer too small\n"); //Nachricht größer als der Buffer
} else {
return std::vector<uint8_t>(this->message_buffer.begin(), this->message_buffer.begin() + input_message_status);
std::vector<uint8_t> msg(this->message_buffer.begin(), this->message_buffer.begin() + input_message_status);
log->info("SocketHandler: Received:\n" + std::string(msg.begin(), msg.end()) + "\n");
return msg;
}
std::vector<uint8_t> empty;
......@@ -62,7 +67,7 @@ std::vector<uint8_t> SocketHandler::receive(){
}
int SocketHandler::respond(std::vector<uint8_t> response){
printf("SockerHandler: Sending response\n");
log->info("SocketHandler: Sending response:\n" + std::string(response.begin(), response.end()) + "\n");
return sendto(
this->server_input_socket,
response.data(),
......
......@@ -24,6 +24,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <netinet/in.h>
#include "LoggingFacility.hpp"
class SocketHandler: public MessageInOut{
private:
int server_input_socket;
......@@ -35,9 +37,10 @@ class SocketHandler: public MessageInOut{
struct sockaddr_in client_address;
int client_address_length;
Logger log;
public:
SocketHandler(int port, int buffersize);
SocketHandler(int port, int buffersize, Logger log_);
//MessageInOut
std::vector<uint8_t> receive() override;
......
......@@ -18,10 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <stdlib.h>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "./lib/toml/cpptoml.h"
#include "./toml.hpp"
......@@ -34,45 +31,17 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include "SocketHandler.hpp"
#include "RaspyAdapter.hpp"
#include "./ImagePreprocessors/ImagePreprocessor.h"
#include "./ImagePreprocessors/ImageProcessingPipeline.h"
#include "./ImagePreprocessors/MagickConvertToPPM.h"
#include "./ImagePreprocessors/MagickResize.h"
#include "./ImagePreprocessors/MagickExtent.h"
#define MESSAGE_LOG "./RcvdMsgLog.txt"
#define DCS_LOGGING_ENABLED 1
//Forward declarations
int print_to_message_log_file(const void* buf, size_t len);
int extract_option_flags(char* arg);
#include "LoggingFacility.hpp"
#include "SimpleFileLogger.hpp"
//Function definitions
int main(int argc, char* argv[]){
int option_flags = 0;
if(argc > 1){
for(int i = 1; i < argc; i++){
if(argv[i][0] == '-'){
option_flags |= extract_option_flags(argv[i]); //Jeweils jeden Teilstring nach validen Optionen überprüfen;
}
}
}
//Parsing of config files
auto serverConfig = toml::parse_file("./res/server.toml");
auto framebufferConfig = toml::parse_file("./res/framebuffer.toml");
auto displayConfig = toml::parse_file("./res/display.toml");
auto raspberryConfig = toml::parse_file("./res/raspberry.toml");
//Parsing of config files
auto serverConfig = toml::parse_file("../res/server.toml");
auto framebufferConfig = toml::parse_file("../res/framebuffer.toml");
auto displayConfig = toml::parse_file("../res/display.toml");
auto raspberryConfig = toml::parse_file("../res/raspberry.toml");
//Extract actual values
......@@ -93,8 +62,10 @@ int main(int argc, char* argv[]){
std::string raspIP = raspberryConfig["IP"].value_or("127.0.0.1");
Logger coms_log = std::make_shared<SimpleFileLogger>("./msg_log.txt");
//Konstruktion der einzelnen Instanzen
std::shared_ptr<SocketHandler> receiver = std::make_shared<SocketHandler>(server_input_port, buffersize);
std::shared_ptr<SocketHandler> receiver = std::make_shared<SocketHandler>(server_input_port, buffersize, coms_log);
std::shared_ptr<RaspberryClient> client = std::make_shared<RaspberryClient>(raspPort, raspBufferSize, raspIP);
......@@ -104,8 +75,8 @@ int main(int argc, char* argv[]){
std::shared_ptr<Framebuffer> buffer = make_shared<Framebuffer>(framebufferX, framebufferY);
Font font(
"" + fontPath,
"" + fontToml);
"." + fontPath,
"." + fontToml);
buffer->setFont(font);
std::cout << font.width() << std::endl;
......@@ -118,38 +89,3 @@ int main(int argc, char* argv[]){
return 0;
}
int print_to_message_log_file(const void* buf, size_t len){
int log_flags = O_RDWR | O_APPEND | O_CREAT; /*| O_TRUNC*/
int log_mode = S_IRWXU;
int log_file = open(MESSAGE_LOG, log_flags, log_mode);
if(log_file < 0){
perror("Server: Could not open testLog file\n");
return -1;
} else {
write(log_file, buf,len);
//tee(log_file, STDOUT_FILENO, input_message.size(), 0); //not working?
close(log_file);
}
return 0;
}
//Commandline arguments sind cStrings, also Nullterminiert
int extract_option_flags(char* arg){
char* c = arg;
int result_flags = 0;
for(int i = 1; c[i] != 0; i++){
if(c[i] == 'l'){
result_flags |= DCS_LOGGING_ENABLED;
}
}
return result_flags;
}
#ifndef DCS_LOGGER
#define DCS_LOGGER
#include <memory>
#include <string_view>
class LoggingFacility{
public:
virtual ~LoggingFacility() = default;
virtual void info(std::string_view entry) = 0;
virtual void warning(std::string_view entry) = 0;
virtual void error(std::string_view entry) = 0;
};
using Logger = std::shared_ptr<LoggingFacility>;
#endif
#include "SimpleFileLogger.hpp"
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string>
SimpleFileLogger::SimpleFileLogger(std::string_view logFile_){
logFile = logFile_;
int log_flags = O_RDWR | O_APPEND | O_CREAT | O_TRUNC;
int log_mode = S_IRWXU;
int log_file = open(logFile.data(), log_flags, log_mode);
if(log_file < 0){
perror("Server: Could not open testLog file\n");
std::exit(EXIT_FAILURE);
} else {
close(log_file);
}
}
void SimpleFileLogger::info(std::string_view entry){
std::string output = "[INFO] " + std::string(entry.begin(), entry.end()) + "\n";
print_to_message_log_file(output.data(), output.size());
}
void SimpleFileLogger::warning(std::string_view entry){
std::string output = "[WARNING] " + std::string(entry.begin(), entry.end()) + "\n";
print_to_message_log_file(output.data(), output.size());
}
void SimpleFileLogger::error(std::string_view entry){
std::string output = "[ERROR] " + std::string(entry.begin(), entry.end()) + "\n";
print_to_message_log_file(output.data(), output.size());
}
int SimpleFileLogger::print_to_message_log_file(const void* buf, size_t len){
int log_flags = O_RDWR | O_APPEND | O_CREAT;
int log_mode = S_IRWXU;
int log_file = open(logFile.data(), log_flags, log_mode);
if(log_file < 0){
perror("SimpleFileLogger: Could not open log file\n");
return