Skip to content

Commit 84ec8a5

Browse files
authored
Name colors (#11573)
It's more descriptive, use #define's so we can use compile-time concatenations. Signed-off-by: Eric Curtin <[email protected]>
1 parent bfcce4d commit 84ec8a5

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

common/log.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ void common_log_set_verbosity_thold(int verbosity) {
1414
common_log_verbosity_thold = verbosity;
1515
}
1616

17-
#define LOG_COL_DEFAULT "\033[0m"
18-
#define LOG_COL_BOLD "\033[1m"
19-
#define LOG_COL_RED "\033[31m"
20-
#define LOG_COL_GREEN "\033[32m"
21-
#define LOG_COL_YELLOW "\033[33m"
22-
#define LOG_COL_BLUE "\033[34m"
23-
#define LOG_COL_MAGENTA "\033[35m"
24-
#define LOG_COL_CYAN "\033[36m"
25-
#define LOG_COL_WHITE "\033[37m"
26-
2717
static int64_t t_us() {
2818
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
2919
}

common/log.h

+10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
#include "ggml.h" // for ggml_log_level
44

5+
#define LOG_COL_DEFAULT "\033[0m"
6+
#define LOG_COL_BOLD "\033[1m"
7+
#define LOG_COL_RED "\033[31m"
8+
#define LOG_COL_GREEN "\033[32m"
9+
#define LOG_COL_YELLOW "\033[33m"
10+
#define LOG_COL_BLUE "\033[34m"
11+
#define LOG_COL_MAGENTA "\033[35m"
12+
#define LOG_COL_CYAN "\033[36m"
13+
#define LOG_COL_WHITE "\033[37m"
14+
515
#ifndef __GNUC__
616
# define LOG_ATTRIBUTE_FORMAT(...)
717
#elif defined(__MINGW32__)

examples/run/run.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@
2424
#include <string>
2525
#include <vector>
2626

27+
#include "chat-template.hpp"
2728
#include "common.h"
2829
#include "json.hpp"
2930
#include "linenoise.cpp/linenoise.h"
3031
#include "llama-cpp.h"
31-
#include "chat-template.hpp"
32+
#include "log.h"
3233

3334
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(_WIN32)
3435
[[noreturn]] static void sigint_handler(int) {
35-
printf("\n\033[0m");
36+
printf("\n" LOG_COL_DEFAULT);
3637
exit(0); // not ideal, but it's the only way to guarantee exit in all cases
3738
}
3839
#endif
@@ -890,7 +891,7 @@ static int check_context_size(const llama_context_ptr & ctx, const llama_batch &
890891
const int n_ctx = llama_n_ctx(ctx.get());
891892
const int n_ctx_used = llama_get_kv_cache_used_cells(ctx.get());
892893
if (n_ctx_used + batch.n_tokens > n_ctx) {
893-
printf("\033[0m\n");
894+
printf(LOG_COL_DEFAULT "\n");
894895
printe("context size exceeded\n");
895896
return 1;
896897
}
@@ -953,7 +954,7 @@ static int generate(LlamaData & llama_data, const std::string & prompt, std::str
953954
batch = llama_batch_get_one(&new_token_id, 1);
954955
}
955956

956-
printf("\033[0m");
957+
printf(LOG_COL_DEFAULT);
957958
return 0;
958959
}
959960

@@ -962,7 +963,7 @@ static int read_user_input(std::string & user_input) {
962963
#ifdef WIN32
963964
printf(
964965
"\r%*s"
965-
"\r\033[0m%s",
966+
"\r" LOG_COL_DEFAULT "%s",
966967
get_terminal_width(), " ", prompt_prefix);
967968

968969
std::getline(std::cin, user_input);
@@ -999,7 +1000,7 @@ static int generate_response(LlamaData & llama_data, const std::string & prompt,
9991000
const bool stdout_a_terminal) {
10001001
// Set response color
10011002
if (stdout_a_terminal) {
1002-
printf("\033[33m");
1003+
printf(LOG_COL_YELLOW);
10031004
}
10041005

10051006
if (generate(llama_data, prompt, response)) {
@@ -1008,7 +1009,7 @@ static int generate_response(LlamaData & llama_data, const std::string & prompt,
10081009
}
10091010

10101011
// End response with color reset and newline
1011-
printf("\n%s", stdout_a_terminal ? "\033[0m" : "");
1012+
printf("\n%s", stdout_a_terminal ? LOG_COL_DEFAULT : "");
10121013
return 0;
10131014
}
10141015

0 commit comments

Comments
 (0)