mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-01-02 18:53:36 +00:00
feat: supports correct UTF-8 printing on windows (#1101)
This commit is contained in:
parent
9fa7f415df
commit
ebe9d26a72
@ -106,9 +106,8 @@ struct SDCliParams {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (mode_found == -1) {
|
if (mode_found == -1) {
|
||||||
fprintf(stderr,
|
LOG_ERROR("error: invalid mode %s, must be one of [%s]\n",
|
||||||
"error: invalid mode %s, must be one of [%s]\n",
|
mode_c_str, SD_ALL_MODES_STR);
|
||||||
mode_c_str, SD_ALL_MODES_STR);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
mode = (SDMode)mode_found;
|
mode = (SDMode)mode_found;
|
||||||
@ -128,8 +127,7 @@ struct SDCliParams {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (preview_found == -1) {
|
if (preview_found == -1) {
|
||||||
fprintf(stderr, "error: preview method %s\n",
|
LOG_ERROR("error: preview method %s", preview);
|
||||||
preview);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
preview_method = (preview_t)preview_found;
|
preview_method = (preview_t)preview_found;
|
||||||
@ -161,7 +159,7 @@ struct SDCliParams {
|
|||||||
|
|
||||||
bool process_and_check() {
|
bool process_and_check() {
|
||||||
if (output_path.length() == 0) {
|
if (output_path.length() == 0) {
|
||||||
fprintf(stderr, "error: the following arguments are required: output_path\n");
|
LOG_ERROR("error: the following arguments are required: output_path");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,18 +217,6 @@ void parse_args(int argc, const char** argv, SDCliParams& cli_params, SDContextP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string sd_basename(const std::string& path) {
|
|
||||||
size_t pos = path.find_last_of('/');
|
|
||||||
if (pos != std::string::npos) {
|
|
||||||
return path.substr(pos + 1);
|
|
||||||
}
|
|
||||||
pos = path.find_last_of('\\');
|
|
||||||
if (pos != std::string::npos) {
|
|
||||||
return path.substr(pos + 1);
|
|
||||||
}
|
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string get_image_params(const SDCliParams& cli_params, const SDContextParams& ctx_params, const SDGenerationParams& gen_params, int64_t seed) {
|
std::string get_image_params(const SDCliParams& cli_params, const SDContextParams& ctx_params, const SDGenerationParams& gen_params, int64_t seed) {
|
||||||
std::string parameter_string = gen_params.prompt_with_lora + "\n";
|
std::string parameter_string = gen_params.prompt_with_lora + "\n";
|
||||||
if (gen_params.negative_prompt.size() != 0) {
|
if (gen_params.negative_prompt.size() != 0) {
|
||||||
@ -288,47 +274,9 @@ std::string get_image_params(const SDCliParams& cli_params, const SDContextParam
|
|||||||
return parameter_string;
|
return parameter_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enables Printing the log level tag in color using ANSI escape codes */
|
|
||||||
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
||||||
SDCliParams* cli_params = (SDCliParams*)data;
|
SDCliParams* cli_params = (SDCliParams*)data;
|
||||||
int tag_color;
|
log_print(level, log, cli_params->verbose, cli_params->color);
|
||||||
const char* level_str;
|
|
||||||
FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
|
|
||||||
|
|
||||||
if (!log || (!cli_params->verbose && level <= SD_LOG_DEBUG)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (level) {
|
|
||||||
case SD_LOG_DEBUG:
|
|
||||||
tag_color = 37;
|
|
||||||
level_str = "DEBUG";
|
|
||||||
break;
|
|
||||||
case SD_LOG_INFO:
|
|
||||||
tag_color = 34;
|
|
||||||
level_str = "INFO";
|
|
||||||
break;
|
|
||||||
case SD_LOG_WARN:
|
|
||||||
tag_color = 35;
|
|
||||||
level_str = "WARN";
|
|
||||||
break;
|
|
||||||
case SD_LOG_ERROR:
|
|
||||||
tag_color = 31;
|
|
||||||
level_str = "ERROR";
|
|
||||||
break;
|
|
||||||
default: /* Potential future-proofing */
|
|
||||||
tag_color = 33;
|
|
||||||
level_str = "?????";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cli_params->color == true) {
|
|
||||||
fprintf(out_stream, "\033[%d;1m[%-5s]\033[0m ", tag_color, level_str);
|
|
||||||
} else {
|
|
||||||
fprintf(out_stream, "[%-5s] ", level_str);
|
|
||||||
}
|
|
||||||
fputs(log, out_stream);
|
|
||||||
fflush(out_stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool load_images_from_dir(const std::string dir,
|
bool load_images_from_dir(const std::string dir,
|
||||||
@ -338,7 +286,7 @@ bool load_images_from_dir(const std::string dir,
|
|||||||
int max_image_num = 0,
|
int max_image_num = 0,
|
||||||
bool verbose = false) {
|
bool verbose = false) {
|
||||||
if (!fs::exists(dir) || !fs::is_directory(dir)) {
|
if (!fs::exists(dir) || !fs::is_directory(dir)) {
|
||||||
fprintf(stderr, "'%s' is not a valid directory\n", dir.c_str());
|
LOG_ERROR("'%s' is not a valid directory\n", dir.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,14 +308,12 @@ bool load_images_from_dir(const std::string dir,
|
|||||||
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
||||||
|
|
||||||
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp") {
|
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp") {
|
||||||
if (verbose) {
|
LOG_DEBUG("load image %zu from '%s'", images.size(), path.c_str());
|
||||||
printf("load image %zu from '%s'\n", images.size(), path.c_str());
|
|
||||||
}
|
|
||||||
int width = 0;
|
int width = 0;
|
||||||
int height = 0;
|
int height = 0;
|
||||||
uint8_t* image_buffer = load_image_from_file(path.c_str(), width, height, expected_width, expected_height);
|
uint8_t* image_buffer = load_image_from_file(path.c_str(), width, height, expected_width, expected_height);
|
||||||
if (image_buffer == nullptr) {
|
if (image_buffer == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", path.c_str());
|
LOG_ERROR("load image from '%s' failed", path.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -429,6 +375,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
cli_params.preview_fps /= 4;
|
cli_params.preview_fps /= 4;
|
||||||
|
|
||||||
sd_set_log_callback(sd_log_cb, (void*)&cli_params);
|
sd_set_log_callback(sd_log_cb, (void*)&cli_params);
|
||||||
|
log_verbose = cli_params.verbose;
|
||||||
|
log_color = cli_params.color;
|
||||||
sd_set_preview_callback(step_callback,
|
sd_set_preview_callback(step_callback,
|
||||||
cli_params.preview_method,
|
cli_params.preview_method,
|
||||||
cli_params.preview_interval,
|
cli_params.preview_interval,
|
||||||
@ -437,10 +385,10 @@ int main(int argc, const char* argv[]) {
|
|||||||
(void*)&cli_params);
|
(void*)&cli_params);
|
||||||
|
|
||||||
if (cli_params.verbose) {
|
if (cli_params.verbose) {
|
||||||
printf("%s", sd_get_system_info());
|
LOG_INFO("%s", sd_get_system_info());
|
||||||
printf("%s\n", cli_params.to_string().c_str());
|
LOG_INFO("%s", cli_params.to_string().c_str());
|
||||||
printf("%s\n", ctx_params.to_string().c_str());
|
LOG_INFO("%s", ctx_params.to_string().c_str());
|
||||||
printf("%s\n", gen_params.to_string().c_str());
|
LOG_INFO("%s", gen_params.to_string().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cli_params.mode == CONVERT) {
|
if (cli_params.mode == CONVERT) {
|
||||||
@ -450,17 +398,16 @@ int main(int argc, const char* argv[]) {
|
|||||||
ctx_params.wtype,
|
ctx_params.wtype,
|
||||||
ctx_params.tensor_type_rules.c_str());
|
ctx_params.tensor_type_rules.c_str());
|
||||||
if (!success) {
|
if (!success) {
|
||||||
fprintf(stderr,
|
LOG_ERROR("convert '%s'/'%s' to '%s' failed",
|
||||||
"convert '%s'/'%s' to '%s' failed\n",
|
ctx_params.model_path.c_str(),
|
||||||
ctx_params.model_path.c_str(),
|
ctx_params.vae_path.c_str(),
|
||||||
ctx_params.vae_path.c_str(),
|
cli_params.output_path.c_str());
|
||||||
cli_params.output_path.c_str());
|
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
printf("convert '%s'/'%s' to '%s' success\n",
|
LOG_INFO("convert '%s'/'%s' to '%s' success",
|
||||||
ctx_params.model_path.c_str(),
|
ctx_params.model_path.c_str(),
|
||||||
ctx_params.vae_path.c_str(),
|
ctx_params.vae_path.c_str(),
|
||||||
cli_params.output_path.c_str());
|
cli_params.output_path.c_str());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -503,7 +450,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
init_image.data = load_image_from_file(gen_params.init_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
init_image.data = load_image_from_file(gen_params.init_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
||||||
if (init_image.data == nullptr) {
|
if (init_image.data == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", gen_params.init_image_path.c_str());
|
LOG_ERROR("load image from '%s' failed", gen_params.init_image_path.c_str());
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -516,7 +463,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
end_image.data = load_image_from_file(gen_params.end_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
end_image.data = load_image_from_file(gen_params.end_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
||||||
if (end_image.data == nullptr) {
|
if (end_image.data == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", gen_params.end_image_path.c_str());
|
LOG_ERROR("load image from '%s' failed", gen_params.end_image_path.c_str());
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -528,7 +475,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
mask_image.data = load_image_from_file(gen_params.mask_image_path.c_str(), width, height, gen_params.width, gen_params.height, 1);
|
mask_image.data = load_image_from_file(gen_params.mask_image_path.c_str(), width, height, gen_params.width, gen_params.height, 1);
|
||||||
if (mask_image.data == nullptr) {
|
if (mask_image.data == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", gen_params.mask_image_path.c_str());
|
LOG_ERROR("load image from '%s' failed", gen_params.mask_image_path.c_str());
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -536,7 +483,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
mask_image.data = (uint8_t*)malloc(gen_params.width * gen_params.height);
|
mask_image.data = (uint8_t*)malloc(gen_params.width * gen_params.height);
|
||||||
memset(mask_image.data, 255, gen_params.width * gen_params.height);
|
memset(mask_image.data, 255, gen_params.width * gen_params.height);
|
||||||
if (mask_image.data == nullptr) {
|
if (mask_image.data == nullptr) {
|
||||||
fprintf(stderr, "malloc mask image failed\n");
|
LOG_ERROR("malloc mask image failed");
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -547,7 +494,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
control_image.data = load_image_from_file(gen_params.control_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
control_image.data = load_image_from_file(gen_params.control_image_path.c_str(), width, height, gen_params.width, gen_params.height);
|
||||||
if (control_image.data == nullptr) {
|
if (control_image.data == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", gen_params.control_image_path.c_str());
|
LOG_ERROR("load image from '%s' failed", gen_params.control_image_path.c_str());
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -568,7 +515,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
int height = 0;
|
int height = 0;
|
||||||
uint8_t* image_buffer = load_image_from_file(path.c_str(), width, height);
|
uint8_t* image_buffer = load_image_from_file(path.c_str(), width, height);
|
||||||
if (image_buffer == nullptr) {
|
if (image_buffer == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", path.c_str());
|
LOG_ERROR("load image from '%s' failed", path.c_str());
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -616,7 +563,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
num_results = 1;
|
num_results = 1;
|
||||||
results = (sd_image_t*)calloc(num_results, sizeof(sd_image_t));
|
results = (sd_image_t*)calloc(num_results, sizeof(sd_image_t));
|
||||||
if (results == nullptr) {
|
if (results == nullptr) {
|
||||||
printf("failed to allocate results array\n");
|
LOG_INFO("failed to allocate results array");
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -627,7 +574,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
|
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
|
||||||
|
|
||||||
if (sd_ctx == nullptr) {
|
if (sd_ctx == nullptr) {
|
||||||
printf("new_sd_ctx_t failed\n");
|
LOG_INFO("new_sd_ctx_t failed");
|
||||||
release_all_resources();
|
release_all_resources();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -704,7 +651,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (results == nullptr) {
|
if (results == nullptr) {
|
||||||
printf("generate failed\n");
|
LOG_ERROR("generate failed");
|
||||||
free_sd_ctx(sd_ctx);
|
free_sd_ctx(sd_ctx);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -721,7 +668,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
gen_params.upscale_tile_size);
|
gen_params.upscale_tile_size);
|
||||||
|
|
||||||
if (upscaler_ctx == nullptr) {
|
if (upscaler_ctx == nullptr) {
|
||||||
printf("new_upscaler_ctx failed\n");
|
LOG_ERROR("new_upscaler_ctx failed");
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; i < num_results; i++) {
|
for (int i = 0; i < num_results; i++) {
|
||||||
if (results[i].data == nullptr) {
|
if (results[i].data == nullptr) {
|
||||||
@ -731,7 +678,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
for (int u = 0; u < gen_params.upscale_repeats; ++u) {
|
for (int u = 0; u < gen_params.upscale_repeats; ++u) {
|
||||||
sd_image_t upscaled_image = upscale(upscaler_ctx, current_image, upscale_factor);
|
sd_image_t upscaled_image = upscale(upscaler_ctx, current_image, upscale_factor);
|
||||||
if (upscaled_image.data == nullptr) {
|
if (upscaled_image.data == nullptr) {
|
||||||
printf("upscale failed\n");
|
LOG_ERROR("upscale failed");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
free(current_image.data);
|
free(current_image.data);
|
||||||
@ -749,8 +696,8 @@ int main(int argc, const char* argv[]) {
|
|||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
fs::create_directories(out_dir, ec); // OK if already exists
|
fs::create_directories(out_dir, ec); // OK if already exists
|
||||||
if (ec) {
|
if (ec) {
|
||||||
fprintf(stderr, "failed to create directory '%s': %s\n",
|
LOG_ERROR("failed to create directory '%s': %s",
|
||||||
out_dir.string().c_str(), ec.message().c_str());
|
out_dir.string().c_str(), ec.message().c_str());
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -780,7 +727,7 @@ int main(int argc, const char* argv[]) {
|
|||||||
vid_output_path = base_path + ".avi";
|
vid_output_path = base_path + ".avi";
|
||||||
}
|
}
|
||||||
create_mjpg_avi_from_sd_images(vid_output_path.c_str(), results, num_results, gen_params.fps);
|
create_mjpg_avi_from_sd_images(vid_output_path.c_str(), results, num_results, gen_params.fps);
|
||||||
printf("save result MJPG AVI video to '%s'\n", vid_output_path.c_str());
|
LOG_INFO("save result MJPG AVI video to '%s'\n", vid_output_path.c_str());
|
||||||
} else {
|
} else {
|
||||||
// appending ".png" to absent or unknown extension
|
// appending ".png" to absent or unknown extension
|
||||||
if (!is_jpg && file_ext_lower != ".png") {
|
if (!is_jpg && file_ext_lower != ".png") {
|
||||||
@ -796,11 +743,11 @@ int main(int argc, const char* argv[]) {
|
|||||||
if (is_jpg) {
|
if (is_jpg) {
|
||||||
write_ok = stbi_write_jpg(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
|
write_ok = stbi_write_jpg(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
|
||||||
results[i].data, 90, get_image_params(cli_params, ctx_params, gen_params, gen_params.seed + i).c_str());
|
results[i].data, 90, get_image_params(cli_params, ctx_params, gen_params, gen_params.seed + i).c_str());
|
||||||
printf("save result JPEG image to '%s' (%s)\n", final_image_path.c_str(), write_ok == 0 ? "failure" : "success");
|
LOG_INFO("save result JPEG image to '%s' (%s)", final_image_path.c_str(), write_ok == 0 ? "failure" : "success");
|
||||||
} else {
|
} else {
|
||||||
write_ok = stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
|
write_ok = stbi_write_png(final_image_path.c_str(), results[i].width, results[i].height, results[i].channel,
|
||||||
results[i].data, 0, get_image_params(cli_params, ctx_params, gen_params, gen_params.seed + i).c_str());
|
results[i].data, 0, get_image_params(cli_params, ctx_params, gen_params, gen_params.seed + i).c_str());
|
||||||
printf("save result PNG image to '%s' (%s)\n", final_image_path.c_str(), write_ok == 0 ? "failure" : "success");
|
LOG_INFO("save result PNG image to '%s' (%s)", final_image_path.c_str(), write_ok == 0 ? "failure" : "success");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -86,6 +86,114 @@ static std::string argv_to_utf8(int index, const char** argv) {
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static void print_utf8(FILE* stream, const char* utf8) {
|
||||||
|
if (!utf8)
|
||||||
|
return;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
HANDLE h = (stream == stderr)
|
||||||
|
? GetStdHandle(STD_ERROR_HANDLE)
|
||||||
|
: GetStdHandle(STD_OUTPUT_HANDLE);
|
||||||
|
|
||||||
|
int wlen = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
|
||||||
|
if (wlen <= 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
wchar_t* wbuf = (wchar_t*)malloc(wlen * sizeof(wchar_t));
|
||||||
|
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wbuf, wlen);
|
||||||
|
|
||||||
|
DWORD written;
|
||||||
|
WriteConsoleW(h, wbuf, wlen - 1, &written, NULL);
|
||||||
|
|
||||||
|
free(wbuf);
|
||||||
|
#else
|
||||||
|
fputs(utf8, stream);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string sd_basename(const std::string& path) {
|
||||||
|
size_t pos = path.find_last_of('/');
|
||||||
|
if (pos != std::string::npos) {
|
||||||
|
return path.substr(pos + 1);
|
||||||
|
}
|
||||||
|
pos = path.find_last_of('\\');
|
||||||
|
if (pos != std::string::npos) {
|
||||||
|
return path.substr(pos + 1);
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void log_print(enum sd_log_level_t level, const char* log, bool verbose, bool color) {
|
||||||
|
int tag_color;
|
||||||
|
const char* level_str;
|
||||||
|
FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
|
||||||
|
|
||||||
|
if (!log || (!verbose && level <= SD_LOG_DEBUG)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (level) {
|
||||||
|
case SD_LOG_DEBUG:
|
||||||
|
tag_color = 37;
|
||||||
|
level_str = "DEBUG";
|
||||||
|
break;
|
||||||
|
case SD_LOG_INFO:
|
||||||
|
tag_color = 34;
|
||||||
|
level_str = "INFO";
|
||||||
|
break;
|
||||||
|
case SD_LOG_WARN:
|
||||||
|
tag_color = 35;
|
||||||
|
level_str = "WARN";
|
||||||
|
break;
|
||||||
|
case SD_LOG_ERROR:
|
||||||
|
tag_color = 31;
|
||||||
|
level_str = "ERROR";
|
||||||
|
break;
|
||||||
|
default: /* Potential future-proofing */
|
||||||
|
tag_color = 33;
|
||||||
|
level_str = "?????";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (color) {
|
||||||
|
fprintf(out_stream, "\033[%d;1m[%-5s]\033[0m ", tag_color, level_str);
|
||||||
|
} else {
|
||||||
|
fprintf(out_stream, "[%-5s] ", level_str);
|
||||||
|
}
|
||||||
|
print_utf8(out_stream, log);
|
||||||
|
fflush(out_stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LOG_BUFFER_SIZE 4096
|
||||||
|
|
||||||
|
static bool log_verbose = false;
|
||||||
|
static bool log_color = false;
|
||||||
|
|
||||||
|
static void log_printf(sd_log_level_t level, const char* file, int line, const char* format, ...) {
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
static char log_buffer[LOG_BUFFER_SIZE + 1];
|
||||||
|
int written = snprintf(log_buffer, LOG_BUFFER_SIZE, "%s:%-4d - ", sd_basename(file).c_str(), line);
|
||||||
|
|
||||||
|
if (written >= 0 && written < LOG_BUFFER_SIZE) {
|
||||||
|
vsnprintf(log_buffer + written, LOG_BUFFER_SIZE - written, format, args);
|
||||||
|
}
|
||||||
|
size_t len = strlen(log_buffer);
|
||||||
|
if (log_buffer[len - 1] != '\n') {
|
||||||
|
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
|
||||||
|
}
|
||||||
|
|
||||||
|
log_print(level, log_buffer, log_verbose, log_color);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define LOG_DEBUG(format, ...) log_printf(SD_LOG_DEBUG, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||||
|
#define LOG_INFO(format, ...) log_printf(SD_LOG_INFO, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||||
|
#define LOG_WARN(format, ...) log_printf(SD_LOG_WARN, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||||
|
#define LOG_ERROR(format, ...) log_printf(SD_LOG_ERROR, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||||
|
|
||||||
struct StringOption {
|
struct StringOption {
|
||||||
std::string short_name;
|
std::string short_name;
|
||||||
std::string long_name;
|
std::string long_name;
|
||||||
@ -295,11 +403,11 @@ static bool parse_options(int argc, const char** argv, const std::vector<ArgOpti
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (invalid_arg) {
|
if (invalid_arg) {
|
||||||
fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
|
LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!found_arg) {
|
if (!found_arg) {
|
||||||
fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
|
LOG_ERROR("error: unknown argument: %s", arg.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -514,8 +622,8 @@ struct SDContextParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
wtype = str_to_sd_type(arg);
|
wtype = str_to_sd_type(arg);
|
||||||
if (wtype == SD_TYPE_COUNT) {
|
if (wtype == SD_TYPE_COUNT) {
|
||||||
fprintf(stderr, "error: invalid weight format %s\n",
|
LOG_ERROR("error: invalid weight format %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -528,8 +636,8 @@ struct SDContextParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
rng_type = str_to_rng_type(arg);
|
rng_type = str_to_rng_type(arg);
|
||||||
if (rng_type == RNG_TYPE_COUNT) {
|
if (rng_type == RNG_TYPE_COUNT) {
|
||||||
fprintf(stderr, "error: invalid rng type %s\n",
|
LOG_ERROR("error: invalid rng type %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -542,8 +650,8 @@ struct SDContextParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
sampler_rng_type = str_to_rng_type(arg);
|
sampler_rng_type = str_to_rng_type(arg);
|
||||||
if (sampler_rng_type == RNG_TYPE_COUNT) {
|
if (sampler_rng_type == RNG_TYPE_COUNT) {
|
||||||
fprintf(stderr, "error: invalid sampler rng type %s\n",
|
LOG_ERROR("error: invalid sampler rng type %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -556,8 +664,8 @@ struct SDContextParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
prediction = str_to_prediction(arg);
|
prediction = str_to_prediction(arg);
|
||||||
if (prediction == PREDICTION_COUNT) {
|
if (prediction == PREDICTION_COUNT) {
|
||||||
fprintf(stderr, "error: invalid prediction type %s\n",
|
LOG_ERROR("error: invalid prediction type %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -570,8 +678,8 @@ struct SDContextParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
lora_apply_mode = str_to_lora_apply_mode(arg);
|
lora_apply_mode = str_to_lora_apply_mode(arg);
|
||||||
if (lora_apply_mode == LORA_APPLY_MODE_COUNT) {
|
if (lora_apply_mode == LORA_APPLY_MODE_COUNT) {
|
||||||
fprintf(stderr, "error: invalid lora apply model %s\n",
|
LOG_ERROR("error: invalid lora apply model %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -695,13 +803,13 @@ struct SDContextParams {
|
|||||||
|
|
||||||
bool process_and_check(SDMode mode) {
|
bool process_and_check(SDMode mode) {
|
||||||
if (mode != UPSCALE && model_path.length() == 0 && diffusion_model_path.length() == 0) {
|
if (mode != UPSCALE && model_path.length() == 0 && diffusion_model_path.length() == 0) {
|
||||||
fprintf(stderr, "error: the following arguments are required: model_path/diffusion_model\n");
|
LOG_ERROR("error: the following arguments are required: model_path/diffusion_model\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mode == UPSCALE) {
|
if (mode == UPSCALE) {
|
||||||
if (esrgan_path.length() == 0) {
|
if (esrgan_path.length() == 0) {
|
||||||
fprintf(stderr, "error: upscale mode needs an upscaler model (--upscale-model)\n");
|
LOG_ERROR("error: upscale mode needs an upscaler model (--upscale-model)\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1118,8 +1226,8 @@ struct SDGenerationParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
sample_params.sample_method = str_to_sample_method(arg);
|
sample_params.sample_method = str_to_sample_method(arg);
|
||||||
if (sample_params.sample_method == SAMPLE_METHOD_COUNT) {
|
if (sample_params.sample_method == SAMPLE_METHOD_COUNT) {
|
||||||
fprintf(stderr, "error: invalid sample method %s\n",
|
LOG_ERROR("error: invalid sample method %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -1132,8 +1240,8 @@ struct SDGenerationParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
high_noise_sample_params.sample_method = str_to_sample_method(arg);
|
high_noise_sample_params.sample_method = str_to_sample_method(arg);
|
||||||
if (high_noise_sample_params.sample_method == SAMPLE_METHOD_COUNT) {
|
if (high_noise_sample_params.sample_method == SAMPLE_METHOD_COUNT) {
|
||||||
fprintf(stderr, "error: invalid high noise sample method %s\n",
|
LOG_ERROR("error: invalid high noise sample method %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -1146,8 +1254,8 @@ struct SDGenerationParams {
|
|||||||
const char* arg = argv[index];
|
const char* arg = argv[index];
|
||||||
sample_params.scheduler = str_to_scheduler(arg);
|
sample_params.scheduler = str_to_scheduler(arg);
|
||||||
if (sample_params.scheduler == SCHEDULER_COUNT) {
|
if (sample_params.scheduler == SCHEDULER_COUNT) {
|
||||||
fprintf(stderr, "error: invalid scheduler %s\n",
|
LOG_ERROR("error: invalid scheduler %s",
|
||||||
arg);
|
arg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -1228,17 +1336,17 @@ struct SDGenerationParams {
|
|||||||
try {
|
try {
|
||||||
custom_sigmas.push_back(std::stof(item));
|
custom_sigmas.push_back(std::stof(item));
|
||||||
} catch (const std::invalid_argument& e) {
|
} catch (const std::invalid_argument& e) {
|
||||||
fprintf(stderr, "error: invalid float value '%s' in --sigmas\n", item.c_str());
|
LOG_ERROR("error: invalid float value '%s' in --sigmas", item.c_str());
|
||||||
return -1;
|
return -1;
|
||||||
} catch (const std::out_of_range& e) {
|
} catch (const std::out_of_range& e) {
|
||||||
fprintf(stderr, "error: float value '%s' out of range in --sigmas\n", item.c_str());
|
LOG_ERROR("error: float value '%s' out of range in --sigmas", item.c_str());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (custom_sigmas.empty() && !sigmas_str.empty()) {
|
if (custom_sigmas.empty() && !sigmas_str.empty()) {
|
||||||
fprintf(stderr, "error: could not parse any sigma values from '%s'\n", argv[index]);
|
LOG_ERROR("error: could not parse any sigma values from '%s'", argv[index]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -1334,7 +1442,7 @@ struct SDGenerationParams {
|
|||||||
try {
|
try {
|
||||||
j = json::parse(json_str);
|
j = json::parse(json_str);
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
fprintf(stderr, "json parse failed %s\n", json_str.c_str());
|
LOG_ERROR("json parse failed %s", json_str.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1443,7 +1551,7 @@ struct SDGenerationParams {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
printf("can not found lora %s\n", final_path.lexically_normal().string().c_str());
|
LOG_WARN("can not found lora %s", final_path.lexically_normal().string().c_str());
|
||||||
tmp = m.suffix().str();
|
tmp = m.suffix().str();
|
||||||
prompt = std::regex_replace(prompt, re, "", std::regex_constants::format_first_only);
|
prompt = std::regex_replace(prompt, re, "", std::regex_constants::format_first_only);
|
||||||
continue;
|
continue;
|
||||||
@ -1482,17 +1590,17 @@ struct SDGenerationParams {
|
|||||||
bool process_and_check(SDMode mode, const std::string& lora_model_dir) {
|
bool process_and_check(SDMode mode, const std::string& lora_model_dir) {
|
||||||
prompt_with_lora = prompt;
|
prompt_with_lora = prompt;
|
||||||
if (width <= 0) {
|
if (width <= 0) {
|
||||||
fprintf(stderr, "error: the width must be greater than 0\n");
|
LOG_ERROR("error: the width must be greater than 0\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (height <= 0) {
|
if (height <= 0) {
|
||||||
fprintf(stderr, "error: the height must be greater than 0\n");
|
LOG_ERROR("error: the height must be greater than 0\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sample_params.sample_steps <= 0) {
|
if (sample_params.sample_steps <= 0) {
|
||||||
fprintf(stderr, "error: the sample_steps must be greater than 0\n");
|
LOG_ERROR("error: the sample_steps must be greater than 0\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1501,7 +1609,7 @@ struct SDGenerationParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (strength < 0.f || strength > 1.f) {
|
if (strength < 0.f || strength > 1.f) {
|
||||||
fprintf(stderr, "error: can only work with strength in [0.0, 1.0]\n");
|
LOG_ERROR("error: can only work with strength in [0.0, 1.0]\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1523,31 +1631,31 @@ struct SDGenerationParams {
|
|||||||
};
|
};
|
||||||
trim(token);
|
trim(token);
|
||||||
if (token.empty()) {
|
if (token.empty()) {
|
||||||
fprintf(stderr, "error: invalid easycache option '%s'\n", easycache_option.c_str());
|
LOG_ERROR("error: invalid easycache option '%s'", easycache_option.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (idx >= 3) {
|
if (idx >= 3) {
|
||||||
fprintf(stderr, "error: easycache expects exactly 3 comma-separated values (threshold,start,end)\n");
|
LOG_ERROR("error: easycache expects exactly 3 comma-separated values (threshold,start,end)\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
values[idx] = std::stof(token);
|
values[idx] = std::stof(token);
|
||||||
} catch (const std::exception&) {
|
} catch (const std::exception&) {
|
||||||
fprintf(stderr, "error: invalid easycache value '%s'\n", token.c_str());
|
LOG_ERROR("error: invalid easycache value '%s'", token.c_str());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
if (idx != 3) {
|
if (idx != 3) {
|
||||||
fprintf(stderr, "error: easycache expects exactly 3 comma-separated values (threshold,start,end)\n");
|
LOG_ERROR("error: easycache expects exactly 3 comma-separated values (threshold,start,end)\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (values[0] < 0.0f) {
|
if (values[0] < 0.0f) {
|
||||||
fprintf(stderr, "error: easycache threshold must be non-negative\n");
|
LOG_ERROR("error: easycache threshold must be non-negative\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (values[1] < 0.0f || values[1] >= 1.0f || values[2] <= 0.0f || values[2] > 1.0f || values[1] >= values[2]) {
|
if (values[1] < 0.0f || values[1] >= 1.0f || values[2] <= 0.0f || values[2] > 1.0f || values[1] >= values[2]) {
|
||||||
fprintf(stderr, "error: easycache start/end percents must satisfy 0.0 <= start < end <= 1.0\n");
|
LOG_ERROR("error: easycache start/end percents must satisfy 0.0 <= start < end <= 1.0\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
easycache_params.enabled = true;
|
easycache_params.enabled = true;
|
||||||
@ -1587,7 +1695,7 @@ struct SDGenerationParams {
|
|||||||
|
|
||||||
if (mode == UPSCALE) {
|
if (mode == UPSCALE) {
|
||||||
if (init_image_path.length() == 0) {
|
if (init_image_path.length() == 0) {
|
||||||
fprintf(stderr, "error: upscale mode needs an init image (--init-img)\n");
|
LOG_ERROR("error: upscale mode needs an init image (--init-img)\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1702,13 +1810,13 @@ uint8_t* load_image_common(bool from_memory,
|
|||||||
image_buffer = (uint8_t*)stbi_load(image_path_or_bytes, &width, &height, &c, expected_channel);
|
image_buffer = (uint8_t*)stbi_load(image_path_or_bytes, &width, &height, &c, expected_channel);
|
||||||
}
|
}
|
||||||
if (image_buffer == nullptr) {
|
if (image_buffer == nullptr) {
|
||||||
fprintf(stderr, "load image from '%s' failed\n", image_path);
|
LOG_ERROR("load image from '%s' failed", image_path);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if (c < expected_channel) {
|
if (c < expected_channel) {
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"the number of channels for the input image must be >= %d,"
|
"the number of channels for the input image must be >= %d,"
|
||||||
"but got %d channels, image_path = %s\n",
|
"but got %d channels, image_path = %s",
|
||||||
expected_channel,
|
expected_channel,
|
||||||
c,
|
c,
|
||||||
image_path);
|
image_path);
|
||||||
@ -1716,12 +1824,12 @@ uint8_t* load_image_common(bool from_memory,
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if (width <= 0) {
|
if (width <= 0) {
|
||||||
fprintf(stderr, "error: the width of image must be greater than 0, image_path = %s\n", image_path);
|
LOG_ERROR("error: the width of image must be greater than 0, image_path = %s", image_path);
|
||||||
free(image_buffer);
|
free(image_buffer);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if (height <= 0) {
|
if (height <= 0) {
|
||||||
fprintf(stderr, "error: the height of image must be greater than 0, image_path = %s\n", image_path);
|
LOG_ERROR("error: the height of image must be greater than 0, image_path = %s", image_path);
|
||||||
free(image_buffer);
|
free(image_buffer);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -1743,10 +1851,10 @@ uint8_t* load_image_common(bool from_memory,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (crop_x != 0 || crop_y != 0) {
|
if (crop_x != 0 || crop_y != 0) {
|
||||||
printf("crop input image from %dx%d to %dx%d, image_path = %s\n", width, height, crop_w, crop_h, image_path);
|
LOG_INFO("crop input image from %dx%d to %dx%d, image_path = %s", width, height, crop_w, crop_h, image_path);
|
||||||
uint8_t* cropped_image_buffer = (uint8_t*)malloc(crop_w * crop_h * expected_channel);
|
uint8_t* cropped_image_buffer = (uint8_t*)malloc(crop_w * crop_h * expected_channel);
|
||||||
if (cropped_image_buffer == nullptr) {
|
if (cropped_image_buffer == nullptr) {
|
||||||
fprintf(stderr, "error: allocate memory for crop\n");
|
LOG_ERROR("error: allocate memory for crop\n");
|
||||||
free(image_buffer);
|
free(image_buffer);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@ -1762,13 +1870,13 @@ uint8_t* load_image_common(bool from_memory,
|
|||||||
image_buffer = cropped_image_buffer;
|
image_buffer = cropped_image_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("resize input image from %dx%d to %dx%d\n", width, height, expected_width, expected_height);
|
LOG_INFO("resize input image from %dx%d to %dx%d", width, height, expected_width, expected_height);
|
||||||
int resized_height = expected_height;
|
int resized_height = expected_height;
|
||||||
int resized_width = expected_width;
|
int resized_width = expected_width;
|
||||||
|
|
||||||
uint8_t* resized_image_buffer = (uint8_t*)malloc(resized_height * resized_width * expected_channel);
|
uint8_t* resized_image_buffer = (uint8_t*)malloc(resized_height * resized_width * expected_channel);
|
||||||
if (resized_image_buffer == nullptr) {
|
if (resized_image_buffer == nullptr) {
|
||||||
fprintf(stderr, "error: allocate memory for resize input image\n");
|
LOG_ERROR("error: allocate memory for resize input image\n");
|
||||||
free(image_buffer);
|
free(image_buffer);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -151,12 +151,12 @@ struct SDSvrParams {
|
|||||||
|
|
||||||
bool process_and_check() {
|
bool process_and_check() {
|
||||||
if (listen_ip.empty()) {
|
if (listen_ip.empty()) {
|
||||||
fprintf(stderr, "error: the following arguments are required: listen_ip\n");
|
LOG_ERROR("error: the following arguments are required: listen_ip");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen_port < 0 || listen_port > 65535) {
|
if (listen_port < 0 || listen_port > 65535) {
|
||||||
fprintf(stderr, "error: listen_port should be in the range [0, 65535]\n");
|
LOG_ERROR("error: listen_port should be in the range [0, 65535]");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@ -256,47 +256,9 @@ std::vector<uint8_t> write_image_to_vector(
|
|||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Enables Printing the log level tag in color using ANSI escape codes */
|
|
||||||
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
||||||
SDSvrParams* svr_params = (SDSvrParams*)data;
|
SDSvrParams* svr_params = (SDSvrParams*)data;
|
||||||
int tag_color;
|
log_print(level, log, svr_params->verbose, svr_params->color);
|
||||||
const char* level_str;
|
|
||||||
FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
|
|
||||||
|
|
||||||
if (!log || (!svr_params->verbose && level <= SD_LOG_DEBUG)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (level) {
|
|
||||||
case SD_LOG_DEBUG:
|
|
||||||
tag_color = 37;
|
|
||||||
level_str = "DEBUG";
|
|
||||||
break;
|
|
||||||
case SD_LOG_INFO:
|
|
||||||
tag_color = 34;
|
|
||||||
level_str = "INFO";
|
|
||||||
break;
|
|
||||||
case SD_LOG_WARN:
|
|
||||||
tag_color = 35;
|
|
||||||
level_str = "WARN";
|
|
||||||
break;
|
|
||||||
case SD_LOG_ERROR:
|
|
||||||
tag_color = 31;
|
|
||||||
level_str = "ERROR";
|
|
||||||
break;
|
|
||||||
default: /* Potential future-proofing */
|
|
||||||
tag_color = 33;
|
|
||||||
level_str = "?????";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (svr_params->color == true) {
|
|
||||||
fprintf(out_stream, "\033[%d;1m[%-5s]\033[0m ", tag_color, level_str);
|
|
||||||
} else {
|
|
||||||
fprintf(out_stream, "[%-5s] ", level_str);
|
|
||||||
}
|
|
||||||
fputs(log, out_stream);
|
|
||||||
fflush(out_stream);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char** argv) {
|
int main(int argc, const char** argv) {
|
||||||
@ -306,19 +268,21 @@ int main(int argc, const char** argv) {
|
|||||||
parse_args(argc, argv, svr_params, ctx_params, default_gen_params);
|
parse_args(argc, argv, svr_params, ctx_params, default_gen_params);
|
||||||
|
|
||||||
sd_set_log_callback(sd_log_cb, (void*)&svr_params);
|
sd_set_log_callback(sd_log_cb, (void*)&svr_params);
|
||||||
|
log_verbose = svr_params.verbose;
|
||||||
|
log_color = svr_params.color;
|
||||||
|
|
||||||
if (svr_params.verbose) {
|
if (svr_params.verbose) {
|
||||||
printf("%s", sd_get_system_info());
|
LOG_INFO("%s", sd_get_system_info());
|
||||||
printf("%s\n", svr_params.to_string().c_str());
|
LOG_INFO("%s", svr_params.to_string().c_str());
|
||||||
printf("%s\n", ctx_params.to_string().c_str());
|
LOG_INFO("%s", ctx_params.to_string().c_str());
|
||||||
printf("%s\n", default_gen_params.to_string().c_str());
|
LOG_INFO("%s", default_gen_params.to_string().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t(false, false, false);
|
sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t(false, false, false);
|
||||||
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
|
sd_ctx_t* sd_ctx = new_sd_ctx(&sd_ctx_params);
|
||||||
|
|
||||||
if (sd_ctx == nullptr) {
|
if (sd_ctx == nullptr) {
|
||||||
printf("new_sd_ctx_t failed\n");
|
LOG_ERROR("new_sd_ctx_t failed");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,9 +395,7 @@ int main(int argc, const char** argv) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (svr_params.verbose) {
|
LOG_DEBUG("%s\n", gen_params.to_string().c_str());
|
||||||
printf("%s\n", gen_params.to_string().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
sd_image_t init_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
sd_image_t init_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
||||||
sd_image_t control_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
sd_image_t control_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
||||||
@ -490,7 +452,7 @@ int main(int argc, const char** argv) {
|
|||||||
results[i].channel,
|
results[i].channel,
|
||||||
output_compression);
|
output_compression);
|
||||||
if (image_bytes.empty()) {
|
if (image_bytes.empty()) {
|
||||||
printf("write image to mem failed\n");
|
LOG_ERROR("write image to mem failed");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -611,9 +573,7 @@ int main(int argc, const char** argv) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (svr_params.verbose) {
|
LOG_DEBUG("%s\n", gen_params.to_string().c_str());
|
||||||
printf("%s\n", gen_params.to_string().c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
sd_image_t init_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
sd_image_t init_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
||||||
sd_image_t control_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
sd_image_t control_image = {(uint32_t)gen_params.width, (uint32_t)gen_params.height, 3, nullptr};
|
||||||
@ -735,7 +695,7 @@ int main(int argc, const char** argv) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
printf("listening on: %s:%d\n", svr_params.listen_ip.c_str(), svr_params.listen_port);
|
LOG_INFO("listening on: %s:%d\n", svr_params.listen_ip.c_str(), svr_params.listen_port);
|
||||||
svr.listen(svr_params.listen_ip, svr_params.listen_port);
|
svr.listen(svr_params.listen_ip, svr_params.listen_port);
|
||||||
|
|
||||||
// cleanup
|
// cleanup
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user