7
7
#include < vector>
8
8
9
9
// #include "preprocessing.hpp"
10
- #include " b64.cpp"
11
10
#include " flux.hpp"
12
- #include " json.hpp"
13
11
#include " stable-diffusion.h"
14
12
15
13
#define STB_IMAGE_IMPLEMENTATION
24
22
#define STB_IMAGE_RESIZE_STATIC
25
23
#include " stb_image_resize.h"
26
24
25
+ #include " b64.cpp"
27
26
#include " httplib.h"
27
+ #include " json.hpp"
28
28
29
29
const char * rng_type_to_str[] = {
30
30
" std_default" ,
@@ -565,6 +565,22 @@ void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
565
565
fflush (out_stream);
566
566
}
567
567
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
+
568
584
static void log_server_request (const httplib::Request& req, const httplib::Response& res) {
569
585
printf (" request: %s %s (%s)\n " , req.method .c_str (), req.path .c_str (), req.body .c_str ());
570
586
}
@@ -613,7 +629,8 @@ void parseJsonPrompt(std::string json_str, SDParams* params) {
613
629
try {
614
630
std::string sample_method = payload[" sample_method" ];
615
631
// 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 " );
617
634
} catch (...) {
618
635
}
619
636
try {
@@ -635,7 +652,8 @@ void parseJsonPrompt(std::string json_str, SDParams* params) {
635
652
try {
636
653
std::string control_cond = payload[" control_cond" ];
637
654
// 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 " );
639
657
} catch (...) {
640
658
}
641
659
try {
@@ -665,6 +683,8 @@ int main(int argc, const char* argv[]) {
665
683
666
684
sd_set_log_callback (sd_log_cb, (void *)¶ms);
667
685
686
+ server_log_params = (void *)¶ms;
687
+
668
688
if (params.verbose ) {
669
689
print_params (params);
670
690
printf (" %s" , sd_get_system_info ());
@@ -701,7 +721,8 @@ int main(int argc, const char* argv[]) {
701
721
int n_prompts = 0 ;
702
722
703
723
const auto txt2imgRequest = [&sd_ctx, ¶ms, &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 ());
705
726
// parse req.body as json using jsoncpp
706
727
using json = nlohmann::json;
707
728
@@ -710,7 +731,8 @@ int main(int argc, const char* argv[]) {
710
731
parseJsonPrompt (json_str, ¶ms);
711
732
} catch (json::parse_error& e) {
712
733
// 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 ());
714
736
std::string prompt = req.body ;
715
737
if (!prompt.empty ()) {
716
738
params.prompt = prompt;
@@ -719,9 +741,11 @@ int main(int argc, const char* argv[]) {
719
741
}
720
742
} catch (...) {
721
743
// 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 " );
723
746
}
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 ());
725
749
726
750
{
727
751
sd_image_t * results;
0 commit comments