Skip to content

Commit 1b87cd8

Browse files
committed
log : option to disable the log prefix
1 parent ff3b380 commit 1b87cd8

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ env:
2424
GGML_NLOOP: 3
2525
GGML_N_THREADS: 1
2626
LLAMA_LOG_COLORS: 1
27+
LLAMA_LOG_PREFIX: 1
2728
LLAMA_LOG_TIMESTAMPS: 1
2829

2930
jobs:

.github/workflows/server.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ on:
2222

2323
env:
2424
LLAMA_LOG_COLORS: 1
25+
LLAMA_LOG_PREFIX: 1
2526
LLAMA_LOG_TIMESTAMPS: 1
2627
LLAMA_LOG_VERBOSITY: 10
2728

common/arg.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,13 @@ gpt_params_context gpt_params_parser_init(gpt_params & params, llama_example ex,
19681968
gpt_log_set_verbosity_thold(value);
19691969
}
19701970
).set_env("LLAMA_LOG_VERBOSITY"));
1971+
add_opt(llama_arg(
1972+
{"--log-prefix"},
1973+
"Enable prefx in log messages",
1974+
[](gpt_params &) {
1975+
gpt_log_set_prefix(gpt_log_main(), true);
1976+
}
1977+
).set_env("LLAMA_LOG_PREFIX"));
19711978
add_opt(llama_arg(
19721979
{"--log-timestamps"},
19731980
"Enable timestamps in log messages",

common/log.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ static std::vector<const char *> g_col = {
5757
struct gpt_log_entry {
5858
enum ggml_log_level level;
5959

60+
bool prefix;
61+
6062
int64_t timestamp;
6163

6264
std::vector<char> msg;
@@ -80,7 +82,7 @@ struct gpt_log_entry {
8082
}
8183
}
8284

83-
if (level != GGML_LOG_LEVEL_NONE) {
85+
if (level != GGML_LOG_LEVEL_NONE && prefix) {
8486
if (timestamp) {
8587
// [M.s.ms.us]
8688
fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
@@ -118,6 +120,7 @@ struct gpt_log {
118120

119121
gpt_log(size_t capacity) {
120122
file = nullptr;
123+
prefix = false;
121124
timestamps = false;
122125
running = false;
123126
t_start = t_us();
@@ -148,6 +151,7 @@ struct gpt_log {
148151

149152
FILE * file;
150153

154+
bool prefix;
151155
bool timestamps;
152156
bool running;
153157

@@ -205,6 +209,7 @@ struct gpt_log {
205209
}
206210

207211
entry.level = level;
212+
entry.prefix = prefix;
208213
entry.timestamp = 0;
209214
if (timestamps) {
210215
entry.timestamp = t_us() - t_start;
@@ -333,6 +338,12 @@ struct gpt_log {
333338
resume();
334339
}
335340

341+
void set_prefix(bool prefix) {
342+
std::lock_guard<std::mutex> lock(mtx);
343+
344+
this->prefix = prefix;
345+
}
346+
336347
void set_timestamps(bool timestamps) {
337348
std::lock_guard<std::mutex> lock(mtx);
338349

@@ -381,6 +392,10 @@ void gpt_log_set_colors(struct gpt_log * log, bool colors) {
381392
log->set_colors(colors);
382393
}
383394

395+
void gpt_log_set_prefix(struct gpt_log * log, bool prefix) {
396+
log->set_prefix(prefix);
397+
}
398+
384399
void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps) {
385400
log->set_timestamps(timestamps);
386401
}

common/log.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,29 @@ void gpt_log_free (struct gpt_log * log);
3232
LOG_ATTRIBUTE_FORMAT(3, 4)
3333
void gpt_log_add(struct gpt_log * log, enum ggml_log_level level, const char * fmt, ...);
3434

35+
// defaults: file = NULL, colors = false, prefix = false, timestamps = false
36+
//
37+
// regular log output:
38+
//
39+
// ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
40+
// llm_load_tensors: ggml ctx size = 0.27 MiB
41+
// llm_load_tensors: offloading 32 repeating layers to GPU
42+
// llm_load_tensors: offloading non-repeating layers to GPU
43+
//
44+
// with prefix = true, timestamps = true, the log output will look like this:
45+
//
46+
// 0.00.035.060 D ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
47+
// 0.00.035.064 I llm_load_tensors: ggml ctx size = 0.27 MiB
48+
// 0.00.090.578 I llm_load_tensors: offloading 32 repeating layers to GPU
49+
// 0.00.090.579 I llm_load_tensors: offloading non-repeating layers to GPU
50+
//
51+
// I - info, W - warning, E - error, D - debug
52+
//
53+
3554
void gpt_log_set_file (struct gpt_log * log, const char * file); // not thread-safe
3655
void gpt_log_set_colors (struct gpt_log * log, bool colors); // not thread-safe
37-
void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps);
56+
void gpt_log_set_prefix (struct gpt_log * log, bool prefix); // whether to output prefix to each log
57+
void gpt_log_set_timestamps(struct gpt_log * log, bool timestamps); // whether to output timestamps in the prefix
3858

3959
// helper macros for logging
4060
// use these to avoid computing log arguments if the verbosity is lower than the threshold

tests/test-log.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ int main() {
2525

2626
if (rand () % 10 < 5) {
2727
gpt_log_set_timestamps(gpt_log_main(), rand() % 2);
28+
gpt_log_set_prefix (gpt_log_main(), rand() % 2);
2829
}
2930
}
3031
});

0 commit comments

Comments
 (0)