Skip to content

Commit 83095ca

Browse files
committed
server: fix CI Build
1 parent a1e3f04 commit 83095ca

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

examples/server/main.cpp

+32-8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#include <vector>
88

99
// #include "preprocessing.hpp"
10-
#include "b64.cpp"
1110
#include "flux.hpp"
12-
#include "json.hpp"
1311
#include "stable-diffusion.h"
1412

1513
#define STB_IMAGE_IMPLEMENTATION
@@ -24,7 +22,9 @@
2422
#define STB_IMAGE_RESIZE_STATIC
2523
#include "stb_image_resize.h"
2624

25+
#include "b64.cpp"
2726
#include "httplib.h"
27+
#include "json.hpp"
2828

2929
const char* rng_type_to_str[] = {
3030
"std_default",
@@ -565,6 +565,22 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
565565
fflush(out_stream);
566566
}
567567

568+
void* server_log_params = NULL;
569+
570+
// enable logging in the server
571+
#define LOG_BUFFER_SIZE 1024
572+
void sd_log(enum sd_log_level_t level, const char* format, ...) {
573+
va_list args;
574+
va_start(args, format);
575+
576+
char log[LOG_BUFFER_SIZE];
577+
vsnprintf(log, 1024, format, args);
578+
strncat(log, "\n", LOG_BUFFER_SIZE - strlen(log));
579+
580+
sd_log_cb(level, log, server_log_params);
581+
va_end(args);
582+
}
583+
568584
static void log_server_request(const httplib::Request& req, const httplib::Response& res) {
569585
printf("request: %s %s (%s)\n", req.method.c_str(), req.path.c_str(), req.body.c_str());
570586
}
@@ -613,7 +629,8 @@ void parseJsonPrompt(std::string json_str, SDParams* params) {
613629
try {
614630
std::string sample_method = payload["sample_method"];
615631
// TODO map to enum value
616-
LOG_WARN("sample_method is not supported yet\n");
632+
// LOG_WARN("sample_method is not supported yet\n");
633+
sd_log(sd_log_level_t::SD_LOG_WARN, "sample_method is not supported yet\n");
617634
} catch (...) {
618635
}
619636
try {
@@ -635,7 +652,8 @@ void parseJsonPrompt(std::string json_str, SDParams* params) {
635652
try {
636653
std::string control_cond = payload["control_cond"];
637654
// TODO map to enum value
638-
LOG_WARN("control_cond is not supported yet\n");
655+
// LOG_WARN("control_cond is not supported yet\n");
656+
sd_log(sd_log_level_t::SD_LOG_WARN, "control_cond is not supported yet\n");
639657
} catch (...) {
640658
}
641659
try {
@@ -665,6 +683,8 @@ int main(int argc, const char* argv[]) {
665683

666684
sd_set_log_callback(sd_log_cb, (void*)&params);
667685

686+
server_log_params = (void*)&params;
687+
668688
if (params.verbose) {
669689
print_params(params);
670690
printf("%s", sd_get_system_info());
@@ -701,7 +721,8 @@ int main(int argc, const char* argv[]) {
701721
int n_prompts = 0;
702722

703723
const auto txt2imgRequest = [&sd_ctx, &params, &n_prompts](const httplib::Request& req, httplib::Response& res) {
704-
LOG_INFO("raw body is: %s\n", req.body.c_str());
724+
// LOG_DEBUG("raw body is: %s\n", req.body.c_str());
725+
sd_log(sd_log_level_t::SD_LOG_DEBUG, "raw body is: %s\n", req.body.c_str());
705726
// parse req.body as json using jsoncpp
706727
using json = nlohmann::json;
707728

@@ -710,7 +731,8 @@ int main(int argc, const char* argv[]) {
710731
parseJsonPrompt(json_str, &params);
711732
} catch (json::parse_error& e) {
712733
// assume the request is just a prompt
713-
LOG_WARN("Failed to parse json: %s\n Assuming it's just a prompt...\n", e.what());
734+
// LOG_WARN("Failed to parse json: %s\n Assuming it's just a prompt...\n", e.what());
735+
sd_log(sd_log_level_t::SD_LOG_WARN, "Failed to parse json: %s\n Assuming it's just a prompt...\n", e.what());
714736
std::string prompt = req.body;
715737
if (!prompt.empty()) {
716738
params.prompt = prompt;
@@ -719,9 +741,11 @@ int main(int argc, const char* argv[]) {
719741
}
720742
} catch (...) {
721743
// Handle any other type of exception
722-
LOG_ERROR("An unexpected error occurred\n");
744+
// LOG_ERROR("An unexpected error occurred\n");
745+
sd_log(sd_log_level_t::SD_LOG_ERROR, "An unexpected error occurred\n");
723746
}
724-
LOG_INFO("prompt is: %s\n", params.prompt.c_str());
747+
// LOG_DEBUG("prompt is: %s\n", params.prompt.c_str());
748+
sd_log(sd_log_level_t::SD_LOG_DEBUG, "prompt is: %s\n", params.prompt.c_str());
725749

726750
{
727751
sd_image_t* results;

0 commit comments

Comments
 (0)