Skip to content

Commit

Permalink
Merge pull request #15 from purusharths/patch-1
Browse files Browse the repository at this point in the history
add macro to enable logging for server side
  • Loading branch information
linusseelinger committed May 22, 2023
2 parents 030cc10 + d2dea30 commit 07ac377
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/umbridge.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef UMBRIDGE
#define UMBRIDGE

// #define LOGGING

// Increase timeout to allow for long-running models.
// This should be (to be on the safe side) significantly greater than the maximum time your model may take
#define CPPHTTPLIB_READ_TIMEOUT_SECOND 60*60
Expand Down Expand Up @@ -399,6 +401,13 @@ namespace umbridge {
res.status = 400;
}

// log request

void log_request(const httplib::Request& req, const httplib::Response& res) {
std::cout << "Incoming request from: " << req.remote_addr << " | Type: " << req.method << " " << req.path << " -> " << res.status << std::endl;

}

// Get model from name
Model& get_model_from_name(std::vector<Model*>& models, std::string name) {
for (auto& model : models) {
Expand Down Expand Up @@ -655,6 +664,19 @@ namespace umbridge {
});

std::cout << "Listening on port " << port << "..." << std::endl;

#ifdef LOGGING
svr.set_logger([](const httplib::Request& req, const httplib::Response& res) {
if (res.status >= 500) {
std::cerr << "[ERROR] ";
} else if (res.status >= 400) {
std::cerr << "[WARNING] ";
} else {
std::cout << "[INFO] ";
}
log_request(req, res);
});
#endif
svr.listen(host.c_str(), port);
std::cout << "Quit" << std::endl;
}
Expand Down

0 comments on commit 07ac377

Please sign in to comment.