@@ -106,9 +106,8 @@ struct SDCliParams {
106106 }
107107 }
108108 if (mode_found == -1 ) {
109- fprintf (stderr,
110- " error: invalid mode %s, must be one of [%s]\n " ,
111- mode_c_str, SD_ALL_MODES_STR);
109+ LOG_ERROR (" error: invalid mode %s, must be one of [%s]\n " ,
110+ mode_c_str, SD_ALL_MODES_STR);
112111 exit (1 );
113112 }
114113 mode = (SDMode)mode_found;
@@ -128,8 +127,7 @@ struct SDCliParams {
128127 }
129128 }
130129 if (preview_found == -1 ) {
131- fprintf (stderr, " error: preview method %s\n " ,
132- preview);
130+ LOG_ERROR (" error: preview method %s" , preview);
133131 return -1 ;
134132 }
135133 preview_method = (preview_t )preview_found;
@@ -161,7 +159,7 @@ struct SDCliParams {
161159
162160 bool process_and_check () {
163161 if (output_path.length () == 0 ) {
164- fprintf (stderr, " error: the following arguments are required: output_path\n " );
162+ LOG_ERROR ( " error: the following arguments are required: output_path" );
165163 return false ;
166164 }
167165
@@ -219,18 +217,6 @@ void parse_args(int argc, const char** argv, SDCliParams& cli_params, SDContextP
219217 }
220218}
221219
222- static std::string sd_basename (const std::string& path) {
223- size_t pos = path.find_last_of (' /' );
224- if (pos != std::string::npos) {
225- return path.substr (pos + 1 );
226- }
227- pos = path.find_last_of (' \\ ' );
228- if (pos != std::string::npos) {
229- return path.substr (pos + 1 );
230- }
231- return path;
232- }
233-
234220std::string get_image_params (const SDCliParams& cli_params, const SDContextParams& ctx_params, const SDGenerationParams& gen_params, int64_t seed) {
235221 std::string parameter_string = gen_params.prompt_with_lora + " \n " ;
236222 if (gen_params.negative_prompt .size () != 0 ) {
@@ -288,47 +274,9 @@ std::string get_image_params(const SDCliParams& cli_params, const SDContextParam
288274 return parameter_string;
289275}
290276
291- /* Enables Printing the log level tag in color using ANSI escape codes */
292277void sd_log_cb (enum sd_log_level_t level, const char * log, void * data) {
293278 SDCliParams* cli_params = (SDCliParams*)data;
294- int tag_color;
295- const char * level_str;
296- FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
297-
298- if (!log || (!cli_params->verbose && level <= SD_LOG_DEBUG)) {
299- return ;
300- }
301-
302- switch (level) {
303- case SD_LOG_DEBUG:
304- tag_color = 37 ;
305- level_str = " DEBUG" ;
306- break ;
307- case SD_LOG_INFO:
308- tag_color = 34 ;
309- level_str = " INFO" ;
310- break ;
311- case SD_LOG_WARN:
312- tag_color = 35 ;
313- level_str = " WARN" ;
314- break ;
315- case SD_LOG_ERROR:
316- tag_color = 31 ;
317- level_str = " ERROR" ;
318- break ;
319- default : /* Potential future-proofing */
320- tag_color = 33 ;
321- level_str = " ?????" ;
322- break ;
323- }
324-
325- if (cli_params->color == true ) {
326- fprintf (out_stream, " \033 [%d;1m[%-5s]\033 [0m " , tag_color, level_str);
327- } else {
328- fprintf (out_stream, " [%-5s] " , level_str);
329- }
330- fputs (log, out_stream);
331- fflush (out_stream);
279+ log_print (level, log, cli_params->verbose , cli_params->color );
332280}
333281
334282bool load_images_from_dir (const std::string dir,
@@ -338,7 +286,7 @@ bool load_images_from_dir(const std::string dir,
338286 int max_image_num = 0 ,
339287 bool verbose = false ) {
340288 if (!fs::exists (dir) || !fs::is_directory (dir)) {
341- fprintf (stderr, " '%s' is not a valid directory\n " , dir.c_str ());
289+ LOG_ERROR ( " '%s' is not a valid directory\n " , dir.c_str ());
342290 return false ;
343291 }
344292
@@ -360,14 +308,12 @@ bool load_images_from_dir(const std::string dir,
360308 std::transform (ext.begin (), ext.end (), ext.begin (), ::tolower);
361309
362310 if (ext == " .jpg" || ext == " .jpeg" || ext == " .png" || ext == " .bmp" ) {
363- if (verbose) {
364- printf (" load image %zu from '%s'\n " , images.size (), path.c_str ());
365- }
311+ LOG_DEBUG (" load image %zu from '%s'" , images.size (), path.c_str ());
366312 int width = 0 ;
367313 int height = 0 ;
368314 uint8_t * image_buffer = load_image_from_file (path.c_str (), width, height, expected_width, expected_height);
369315 if (image_buffer == nullptr ) {
370- fprintf (stderr, " load image from '%s' failed\n " , path.c_str ());
316+ LOG_ERROR ( " load image from '%s' failed" , path.c_str ());
371317 return false ;
372318 }
373319
@@ -429,6 +375,8 @@ int main(int argc, const char* argv[]) {
429375 cli_params.preview_fps /= 4 ;
430376
431377 sd_set_log_callback (sd_log_cb, (void *)&cli_params);
378+ log_verbose = cli_params.verbose ;
379+ log_color = cli_params.color ;
432380 sd_set_preview_callback (step_callback,
433381 cli_params.preview_method ,
434382 cli_params.preview_interval ,
@@ -437,10 +385,10 @@ int main(int argc, const char* argv[]) {
437385 (void *)&cli_params);
438386
439387 if (cli_params.verbose ) {
440- printf (" %s" , sd_get_system_info ());
441- printf (" %s\n " , cli_params.to_string ().c_str ());
442- printf (" %s\n " , ctx_params.to_string ().c_str ());
443- printf (" %s\n " , gen_params.to_string ().c_str ());
388+ LOG_INFO (" %s" , sd_get_system_info ());
389+ LOG_INFO (" %s" , cli_params.to_string ().c_str ());
390+ LOG_INFO (" %s" , ctx_params.to_string ().c_str ());
391+ LOG_INFO (" %s" , gen_params.to_string ().c_str ());
444392 }
445393
446394 if (cli_params.mode == CONVERT) {
@@ -450,17 +398,16 @@ int main(int argc, const char* argv[]) {
450398 ctx_params.wtype ,
451399 ctx_params.tensor_type_rules .c_str ());
452400 if (!success) {
453- fprintf (stderr,
454- " convert '%s'/'%s' to '%s' failed\n " ,
455- ctx_params.model_path .c_str (),
456- ctx_params.vae_path .c_str (),
457- cli_params.output_path .c_str ());
401+ LOG_ERROR (" convert '%s'/'%s' to '%s' failed" ,
402+ ctx_params.model_path .c_str (),
403+ ctx_params.vae_path .c_str (),
404+ cli_params.output_path .c_str ());
458405 return 1 ;
459406 } else {
460- printf (" convert '%s'/'%s' to '%s' success\n " ,
461- ctx_params.model_path .c_str (),
462- ctx_params.vae_path .c_str (),
463- cli_params.output_path .c_str ());
407+ LOG_INFO (" convert '%s'/'%s' to '%s' success" ,
408+ ctx_params.model_path .c_str (),
409+ ctx_params.vae_path .c_str (),
410+ cli_params.output_path .c_str ());
464411 return 0 ;
465412 }
466413 }
@@ -503,7 +450,7 @@ int main(int argc, const char* argv[]) {
503450 int height = 0 ;
504451 init_image.data = load_image_from_file (gen_params.init_image_path .c_str (), width, height, gen_params.width , gen_params.height );
505452 if (init_image.data == nullptr ) {
506- fprintf (stderr, " load image from '%s' failed\n " , gen_params.init_image_path .c_str ());
453+ LOG_ERROR ( " load image from '%s' failed" , gen_params.init_image_path .c_str ());
507454 release_all_resources ();
508455 return 1 ;
509456 }
@@ -516,7 +463,7 @@ int main(int argc, const char* argv[]) {
516463 int height = 0 ;
517464 end_image.data = load_image_from_file (gen_params.end_image_path .c_str (), width, height, gen_params.width , gen_params.height );
518465 if (end_image.data == nullptr ) {
519- fprintf (stderr, " load image from '%s' failed\n " , gen_params.end_image_path .c_str ());
466+ LOG_ERROR ( " load image from '%s' failed" , gen_params.end_image_path .c_str ());
520467 release_all_resources ();
521468 return 1 ;
522469 }
@@ -528,15 +475,15 @@ int main(int argc, const char* argv[]) {
528475 int height = 0 ;
529476 mask_image.data = load_image_from_file (gen_params.mask_image_path .c_str (), width, height, gen_params.width , gen_params.height , 1 );
530477 if (mask_image.data == nullptr ) {
531- fprintf (stderr, " load image from '%s' failed\n " , gen_params.mask_image_path .c_str ());
478+ LOG_ERROR ( " load image from '%s' failed" , gen_params.mask_image_path .c_str ());
532479 release_all_resources ();
533480 return 1 ;
534481 }
535482 } else {
536483 mask_image.data = (uint8_t *)malloc (gen_params.width * gen_params.height );
537484 memset (mask_image.data , 255 , gen_params.width * gen_params.height );
538485 if (mask_image.data == nullptr ) {
539- fprintf (stderr, " malloc mask image failed\n " );
486+ LOG_ERROR ( " malloc mask image failed" );
540487 release_all_resources ();
541488 return 1 ;
542489 }
@@ -547,7 +494,7 @@ int main(int argc, const char* argv[]) {
547494 int height = 0 ;
548495 control_image.data = load_image_from_file (gen_params.control_image_path .c_str (), width, height, gen_params.width , gen_params.height );
549496 if (control_image.data == nullptr ) {
550- fprintf (stderr, " load image from '%s' failed\n " , gen_params.control_image_path .c_str ());
497+ LOG_ERROR ( " load image from '%s' failed" , gen_params.control_image_path .c_str ());
551498 release_all_resources ();
552499 return 1 ;
553500 }
@@ -568,7 +515,7 @@ int main(int argc, const char* argv[]) {
568515 int height = 0 ;
569516 uint8_t * image_buffer = load_image_from_file (path.c_str (), width, height);
570517 if (image_buffer == nullptr ) {
571- fprintf (stderr, " load image from '%s' failed\n " , path.c_str ());
518+ LOG_ERROR ( " load image from '%s' failed" , path.c_str ());
572519 release_all_resources ();
573520 return 1 ;
574521 }
@@ -616,7 +563,7 @@ int main(int argc, const char* argv[]) {
616563 num_results = 1 ;
617564 results = (sd_image_t *)calloc (num_results, sizeof (sd_image_t ));
618565 if (results == nullptr ) {
619- printf (" failed to allocate results array\n " );
566+ LOG_INFO (" failed to allocate results array" );
620567 release_all_resources ();
621568 return 1 ;
622569 }
@@ -627,7 +574,7 @@ int main(int argc, const char* argv[]) {
627574 sd_ctx_t * sd_ctx = new_sd_ctx (&sd_ctx_params);
628575
629576 if (sd_ctx == nullptr ) {
630- printf (" new_sd_ctx_t failed\n " );
577+ LOG_INFO (" new_sd_ctx_t failed" );
631578 release_all_resources ();
632579 return 1 ;
633580 }
@@ -704,7 +651,7 @@ int main(int argc, const char* argv[]) {
704651 }
705652
706653 if (results == nullptr ) {
707- printf (" generate failed\n " );
654+ LOG_ERROR (" generate failed" );
708655 free_sd_ctx (sd_ctx);
709656 return 1 ;
710657 }
@@ -721,7 +668,7 @@ int main(int argc, const char* argv[]) {
721668 gen_params.upscale_tile_size );
722669
723670 if (upscaler_ctx == nullptr ) {
724- printf (" new_upscaler_ctx failed\n " );
671+ LOG_ERROR (" new_upscaler_ctx failed" );
725672 } else {
726673 for (int i = 0 ; i < num_results; i++) {
727674 if (results[i].data == nullptr ) {
@@ -731,7 +678,7 @@ int main(int argc, const char* argv[]) {
731678 for (int u = 0 ; u < gen_params.upscale_repeats ; ++u) {
732679 sd_image_t upscaled_image = upscale (upscaler_ctx, current_image, upscale_factor);
733680 if (upscaled_image.data == nullptr ) {
734- printf (" upscale failed\n " );
681+ LOG_ERROR (" upscale failed" );
735682 break ;
736683 }
737684 free (current_image.data );
@@ -749,8 +696,8 @@ int main(int argc, const char* argv[]) {
749696 std::error_code ec;
750697 fs::create_directories (out_dir, ec); // OK if already exists
751698 if (ec) {
752- fprintf (stderr, " failed to create directory '%s': %s\n " ,
753- out_dir.string ().c_str (), ec.message ().c_str ());
699+ LOG_ERROR ( " failed to create directory '%s': %s" ,
700+ out_dir.string ().c_str (), ec.message ().c_str ());
754701 return 1 ;
755702 }
756703 }
@@ -780,7 +727,7 @@ int main(int argc, const char* argv[]) {
780727 vid_output_path = base_path + " .avi" ;
781728 }
782729 create_mjpg_avi_from_sd_images (vid_output_path.c_str (), results, num_results, gen_params.fps );
783- printf (" save result MJPG AVI video to '%s'\n " , vid_output_path.c_str ());
730+ LOG_INFO (" save result MJPG AVI video to '%s'\n " , vid_output_path.c_str ());
784731 } else {
785732 // appending ".png" to absent or unknown extension
786733 if (!is_jpg && file_ext_lower != " .png" ) {
@@ -796,11 +743,11 @@ int main(int argc, const char* argv[]) {
796743 if (is_jpg) {
797744 write_ok = stbi_write_jpg (final_image_path.c_str (), results[i].width , results[i].height , results[i].channel ,
798745 results[i].data , 90 , get_image_params (cli_params, ctx_params, gen_params, gen_params.seed + i).c_str ());
799- printf (" save result JPEG image to '%s' (%s)\n " , final_image_path.c_str (), write_ok == 0 ? " failure" : " success" );
746+ LOG_INFO (" save result JPEG image to '%s' (%s)" , final_image_path.c_str (), write_ok == 0 ? " failure" : " success" );
800747 } else {
801748 write_ok = stbi_write_png (final_image_path.c_str (), results[i].width , results[i].height , results[i].channel ,
802749 results[i].data , 0 , get_image_params (cli_params, ctx_params, gen_params, gen_params.seed + i).c_str ());
803- printf (" save result PNG image to '%s' (%s)\n " , final_image_path.c_str (), write_ok == 0 ? " failure" : " success" );
750+ LOG_INFO (" save result PNG image to '%s' (%s)" , final_image_path.c_str (), write_ok == 0 ? " failure" : " success" );
804751 }
805752 }
806753 }
0 commit comments