Skip to content

Commit edb8c0f

Browse files
committed
Write to log when enabled otherwise direct
1 parent c879317 commit edb8c0f

File tree

4 files changed

+106
-32
lines changed

4 files changed

+106
-32
lines changed

common/console.cpp

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
#include <termios.h>
2626
#endif
2727

28-
#define ANSI_COLOR_RED "\x1b[31m"
29-
#define ANSI_COLOR_GREEN "\x1b[32m"
30-
#define ANSI_COLOR_YELLOW "\x1b[33m"
31-
#define ANSI_COLOR_BLUE "\x1b[34m"
32-
#define ANSI_COLOR_MAGENTA "\x1b[35m"
33-
#define ANSI_COLOR_CYAN "\x1b[36m"
34-
#define ANSI_COLOR_RESET "\x1b[0m"
35-
#define ANSI_BOLD "\x1b[1m"
28+
#define ANSI_COLOR_RED LOG_COL_RED
29+
#define ANSI_COLOR_GREEN LOG_COL_GREEN
30+
#define ANSI_COLOR_YELLOW LOG_COL_YELLOW
31+
#define ANSI_COLOR_BLUE LOG_COL_BLUE
32+
#define ANSI_COLOR_MAGENTA LOG_COL_MAGENTA
33+
#define ANSI_COLOR_CYAN LOG_COL_CYAN
34+
#define ANSI_COLOR_RESET LOG_COL_DEFAULT
35+
#define ANSI_BOLD LOG_COL_BOLD
3636

3737
namespace console {
3838

@@ -143,30 +143,58 @@ namespace console {
143143
// Keep track of current display and only emit ANSI code if it changes
144144
void set_display(display_t display) {
145145
if (advanced_display && current_display != display) {
146-
fflush(stdout);
147-
switch(display) {
148-
case reset:
149-
fprintf(out, ANSI_COLOR_RESET);
150-
break;
151-
case prompt:
152-
fprintf(out, ANSI_COLOR_YELLOW);
153-
break;
154-
case user_input:
155-
fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
156-
break;
157-
case error:
158-
fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
159-
break;
160-
case reasoning:
161-
fprintf(out, ANSI_COLOR_BLUE);
162-
break;
163-
}
164146
current_display = display;
165-
fflush(out);
147+
148+
if (display == user_input && common_log_is_active(common_log_main())) {
149+
common_log_flush(common_log_main());
150+
}
151+
152+
if (display == user_input || !common_log_is_active(common_log_main())) {
153+
fflush(stdout);
154+
switch(display) {
155+
case reset:
156+
fprintf(out, ANSI_COLOR_RESET);
157+
break;
158+
case prompt:
159+
fprintf(out, ANSI_COLOR_YELLOW);
160+
break;
161+
case user_input:
162+
fprintf(out, ANSI_BOLD ANSI_COLOR_GREEN);
163+
break;
164+
case error:
165+
fprintf(out, ANSI_BOLD ANSI_COLOR_RED);
166+
break;
167+
case reasoning:
168+
fprintf(out, ANSI_COLOR_BLUE);
169+
break;
170+
}
171+
fflush(out);
172+
}
173+
}
174+
}
175+
176+
display_t get_display() {
177+
return current_display;
178+
}
179+
180+
const char * get_display_color() {
181+
switch(current_display) {
182+
case reset:
183+
return ANSI_COLOR_RESET;
184+
case prompt:
185+
return ANSI_COLOR_YELLOW;
186+
case user_input:
187+
return ANSI_BOLD ANSI_COLOR_GREEN;
188+
case error:
189+
return ANSI_BOLD ANSI_COLOR_RED;
190+
case reasoning:
191+
return ANSI_COLOR_BLUE;
192+
default:
193+
return "";
166194
}
167195
}
168196

169-
void write(const char* format, ...) {
197+
void write_console(const char* format, ...) {
170198
va_list args;
171199
va_start(args, format);
172200
vfprintf(out, format, args);

common/console.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <string>
6+
#include "log.h"
67

78
namespace console {
89
enum display_t {
@@ -16,9 +17,27 @@ namespace console {
1617
void init(bool use_simple_io, bool use_advanced_display);
1718
void cleanup();
1819
void set_display(display_t display);
20+
display_t get_display();
21+
const char * get_display_color();
1922
bool readline(std::string & line, bool multiline_input);
2023

21-
void write(const char* format, ...);
24+
void write_console(const char* format, ...);
25+
26+
template<typename... Args>
27+
void write(const char* format, Args... args) {
28+
if (get_display() == user_input || !common_log_is_active(common_log_main())) {
29+
write_console(format, args...);
30+
31+
} else {
32+
const char * color = get_display_color();
33+
std::string colored_format = std::string(color) + format + LOG_COL_DEFAULT;
34+
common_log_add(common_log_main(), GGML_LOG_LEVEL_CONT, colored_format.c_str(), args...);
35+
}
36+
}
37+
38+
inline void write(const char* data) {
39+
write("%s", data);
40+
}
2241

2342
inline void write(const std::string & data) {
2443
write("%s", data.c_str());

common/log.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ struct common_log {
174174
std::mutex mtx;
175175
std::thread thrd;
176176
std::condition_variable cv;
177+
std::condition_variable cv_flushed;
177178

178179
FILE * file;
179180

@@ -288,6 +289,10 @@ struct common_log {
288289
cur = entries[head];
289290

290291
head = (head + 1) % entries.size();
292+
293+
if (head == tail) {
294+
cv_flushed.notify_all();
295+
}
291296
}
292297

293298
if (cur.is_end) {
@@ -376,6 +381,18 @@ struct common_log {
376381

377382
this->timestamps = timestamps;
378383
}
384+
385+
bool is_active() const {
386+
return running;
387+
}
388+
389+
void flush() {
390+
if (!running) {
391+
return;
392+
}
393+
std::unique_lock<std::mutex> lock(mtx);
394+
cv_flushed.wait(lock, [this]() { return head == tail; });
395+
}
379396
};
380397

381398
//
@@ -409,6 +426,14 @@ void common_log_free(struct common_log * log) {
409426
delete log;
410427
}
411428

429+
bool common_log_is_active(struct common_log * log) {
430+
return log->is_active();
431+
}
432+
433+
void common_log_flush(struct common_log * log) {
434+
log->flush();
435+
}
436+
412437
void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
413438
va_list args;
414439
va_start(args, fmt);

common/log.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ struct common_log;
4242

4343
struct common_log * common_log_init();
4444
struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
45-
void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
46-
void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
47-
void common_log_free (struct common_log * log);
45+
void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
46+
void common_log_resume (struct common_log * log); // resume the worker thread, not thread-safe
47+
void common_log_free (struct common_log * log);
48+
bool common_log_is_active(struct common_log * log); // check if logging is active
49+
void common_log_flush (struct common_log * log); // wait for all pending messages to be processed
4850

4951
LOG_ATTRIBUTE_FORMAT(3, 4)
5052
void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...);

0 commit comments

Comments
 (0)