Skip to content

Commit 86dd854

Browse files
committed
Add custom log output support.
1 parent d435e6a commit 86dd854

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

stable-diffusion.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ void pretty_progress(int step, int steps, float time) {
108108
}
109109
}
110110
progress += "|";
111-
printf(time > 1.0f ? "\r%s %i/%i - %.2fs/it" : "\r%s %i/%i - %.2fit/s",
111+
//printf(time > 1.0f ? "\r%s %i/%i - %.2fs/it" : "\r%s %i/%i - %.2fit/s",
112+
// progress.c_str(), step, steps,
113+
// time > 1.0f || time == 0 ? time : (1.0f / time));
114+
LOG_DEBUG(time > 1.0f ? "\r%s %i/%i - %.2fs/it" : "\r%s %i/%i - %.2fit/s",
112115
progress.c_str(), step, steps,
113116
time > 1.0f || time == 0 ? time : (1.0f / time));
114117
fflush(stdout); // for linux

util.cpp

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,34 +170,56 @@ void set_sd_log_level(SDLogLevel level) {
170170
log_level = level;
171171
}
172172

173+
174+
void set_sd_log_callback(LOG_CALLBACK lcb) {
175+
log_callback = lcb;
176+
}
177+
178+
173179
void log_printf(SDLogLevel level, const char* file, int line, const char* format, ...) {
174-
if (level < log_level) {
175-
return;
176-
}
177-
va_list args;
178-
va_start(args, format);
179-
180-
if (level == SDLogLevel::DEBUG) {
181-
printf("[DEBUG] %s:%-4d - ", basename(file).c_str(), line);
182-
vprintf(format, args);
183-
printf("\n");
184-
fflush(stdout);
185-
} else if (level == SDLogLevel::INFO) {
186-
printf("[INFO] %s:%-4d - ", basename(file).c_str(), line);
187-
vprintf(format, args);
188-
printf("\n");
189-
fflush(stdout);
190-
} else if (level == SDLogLevel::WARN) {
191-
fprintf(stdout, "[WARN] %s:%-4d - ", basename(file).c_str(), line);
192-
vfprintf(stdout, format, args);
193-
fprintf(stdout, "\n");
194-
fflush(stdout);
180+
181+
182+
LOG_CALLBACK callback_temp = log_callback;
183+
if (callback_temp != NULL) {
184+
185+
va_list args_temp;
186+
va_start(args_temp, format);
187+
callback_temp(level, file, line, format, args_temp);
188+
va_end(args_temp);
189+
195190
} else {
196-
fprintf(stderr, "[ERROR] %s:%-4d - ", basename(file).c_str(), line);
197-
vfprintf(stderr, format, args);
198-
fprintf(stderr, "\n");
199-
fflush(stderr);
191+
if (level < log_level) {
192+
return;
193+
}
194+
va_list args;
195+
va_start(args, format);
196+
197+
198+
if (level == SDLogLevel::DEBUG) {
199+
printf("[DEBUG] %s:%-4d - ", basename(file).c_str(), line);
200+
vprintf(format, args);
201+
printf("\n");
202+
fflush(stdout);
203+
} else if (level == SDLogLevel::INFO) {
204+
printf("[INFO] %s:%-4d - ", basename(file).c_str(), line);
205+
vprintf(format, args);
206+
printf("\n");
207+
fflush(stdout);
208+
} else if (level == SDLogLevel::WARN) {
209+
fprintf(stdout, "[WARN] %s:%-4d - ", basename(file).c_str(), line);
210+
vfprintf(stdout, format, args);
211+
fprintf(stdout, "\n");
212+
fflush(stdout);
213+
} else {
214+
fprintf(stderr, "[ERROR] %s:%-4d - ", basename(file).c_str(), line);
215+
vfprintf(stderr, format, args);
216+
fprintf(stderr, "\n");
217+
fflush(stderr);
218+
}
219+
220+
va_end(args);
200221
}
201222

202-
va_end(args);
223+
224+
203225
}

util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ enum SDLogLevel {
3232
};
3333

3434
void set_sd_log_level(SDLogLevel level);
35+
typedef void (*LOG_CALLBACK)(SDLogLevel level, const char* file, int line, const char* format, va_list args);
36+
static LOG_CALLBACK log_callback = NULL;
37+
void set_sd_log_callback(LOG_CALLBACK);
3538

3639
void log_printf(SDLogLevel level, const char* file, int line, const char* format, ...);
3740

0 commit comments

Comments
 (0)