Skip to content

Commit 98b0d26

Browse files
committed
Add new console::write routine
1 parent 3087ff7 commit 98b0d26

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

common/console.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ namespace console {
165165
}
166166
}
167167

168+
FILE* get_output_handle() {
169+
return out;
170+
}
171+
168172
static char32_t getchar32() {
169173
#if defined(_WIN32)
170174
HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);

common/console.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#pragma once
44

55
#include <string>
6+
#include <cstdio>
67

78
namespace console {
89
enum display_t {
@@ -17,4 +18,21 @@ namespace console {
1718
void cleanup();
1819
void set_display(display_t display);
1920
bool readline(std::string & line, bool multiline_input);
21+
22+
FILE* get_output_handle();
23+
24+
template<typename... Args>
25+
void write(const char* format, Args... args) {
26+
FILE* out = get_output_handle();
27+
fprintf(out, format, args...);
28+
fflush(out);
29+
}
30+
31+
inline void write(const char* str) {
32+
write("%s", str);
33+
}
34+
35+
inline void write(const std::string & data) {
36+
write(data.c_str());
37+
}
2038
}

tools/main/main.cpp

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ static bool need_insert_eot = false;
4444
static void print_usage(int argc, char ** argv) {
4545
(void) argc;
4646

47-
LOG("\nexample usage:\n");
48-
LOG("\n text generation: %s -m your_model.gguf -p \"I believe the meaning of life is\" -n 128 -no-cnv\n", argv[0]);
49-
LOG("\n chat (conversation): %s -m your_model.gguf -sys \"You are a helpful assistant\"\n", argv[0]);
50-
LOG("\n");
47+
console::write("\nexample usage:\n");
48+
console::write("\n text generation: %s -m your_model.gguf -p \"I believe the meaning of life is\" -n 128 -no-cnv\n", argv[0]);
49+
console::write("\n chat (conversation): %s -m your_model.gguf -sys \"You are a helpful assistant\"\n", argv[0]);
50+
console::write("\n");
5151
}
5252

5353
static bool file_exists(const std::string & path) {
@@ -70,11 +70,11 @@ static void sigint_handler(int signo) {
7070
need_insert_eot = true;
7171
} else {
7272
console::cleanup();
73-
LOG("\n");
73+
console::write("\n");
7474
common_perf_print(*g_ctx, *g_smpl);
7575

7676
// make sure all logs are flushed
77-
LOG("Interrupted by user\n");
77+
console::write("Interrupted by user\n");
7878
common_log_pause(common_log_main());
7979

8080
_exit(130);
@@ -673,7 +673,7 @@ int main(int argc, char ** argv) {
673673

674674
if (chat_add_and_format.get_partial_formatter()) {
675675
for (const auto & msg : chat_msgs) {
676-
LOG("%s\n", msg.content.c_str());
676+
console::write(msg.content + "\n");
677677
}
678678
}
679679

@@ -833,8 +833,7 @@ int main(int argc, char ** argv) {
833833
} else {
834834
console::set_display(console::reset);
835835
}
836-
fprintf(stdout, "%s", out.formatted.c_str());
837-
fflush(stdout);
836+
console::write(out.formatted);
838837
}
839838
console::set_display(console::reset);
840839
}
@@ -870,7 +869,7 @@ int main(int argc, char ** argv) {
870869
const std::string token_str = common_token_to_piece(ctx, id, params.special);
871870

872871
if (!chat_add_and_format.get_partial_formatter()) {
873-
LOG("%s", token_str.c_str());
872+
console::write(token_str);
874873
}
875874

876875
// Record Displayed Tokens To Log
@@ -954,7 +953,7 @@ int main(int argc, char ** argv) {
954953
chat_add_and_format("assistant", assistant_ss.str());
955954
}
956955
is_interacting = true;
957-
LOG("\n");
956+
console::write("\n");
958957
}
959958
}
960959

@@ -968,8 +967,12 @@ int main(int argc, char ** argv) {
968967
if ((n_past > 0 || waiting_for_first_input) && is_interacting) {
969968
LOG_DBG("waiting for user input\n");
970969

970+
// color user input only
971+
console::set_display(console::user_input);
972+
display = params.display_prompt;
973+
971974
if (params.conversation_mode) {
972-
LOG("\n> ");
975+
console::write("\n> ");
973976
}
974977

975978
if (params.input_prefix_bos) {
@@ -980,13 +983,9 @@ int main(int argc, char ** argv) {
980983
std::string buffer;
981984
if (!params.input_prefix.empty() && !params.conversation_mode) {
982985
LOG_DBG("appending input prefix: '%s'\n", params.input_prefix.c_str());
983-
LOG("%s", params.input_prefix.c_str());
986+
console::write(params.input_prefix);
984987
}
985988

986-
// color user input only
987-
console::set_display(console::user_input);
988-
display = params.display_prompt;
989-
990989
std::string line;
991990
bool another_line = true;
992991
do {
@@ -999,7 +998,7 @@ int main(int argc, char ** argv) {
999998
display = true;
1000999

10011000
if (buffer.empty()) { // Ctrl+D on empty line exits
1002-
LOG("EOF by user\n");
1001+
console::write("EOF by user\n");
10031002
break;
10041003
}
10051004

@@ -1017,7 +1016,7 @@ int main(int argc, char ** argv) {
10171016
// append input suffix if any
10181017
if (!params.input_suffix.empty() && !params.conversation_mode) {
10191018
LOG_DBG("appending input suffix: '%s'\n", params.input_suffix.c_str());
1020-
LOG("%s", params.input_suffix.c_str());
1019+
console::write(params.input_suffix);
10211020
}
10221021

10231022
LOG_DBG("buffer: '%s'\n", buffer.c_str());
@@ -1091,7 +1090,7 @@ int main(int argc, char ** argv) {
10911090

10921091
// end of generation
10931092
if (!embd.empty() && llama_vocab_is_eog(vocab, embd.back()) && !(params.interactive)) {
1094-
LOG(" [end of text]\n");
1093+
console::write(" [end of text]\n");
10951094
break;
10961095
}
10971096

@@ -1104,11 +1103,11 @@ int main(int argc, char ** argv) {
11041103
}
11051104

11061105
if (!path_session.empty() && params.prompt_cache_all && !params.prompt_cache_ro) {
1107-
LOG("\n%s: saving final output to session file '%s'\n", __func__, path_session.c_str());
1106+
LOG_INF("\n%s: saving final output to session file '%s'\n", __func__, path_session.c_str());
11081107
llama_state_save_file(ctx, path_session.c_str(), session_tokens.data(), session_tokens.size());
11091108
}
11101109

1111-
LOG("\n\n");
1110+
console::write("\n\n");
11121111
common_perf_print(ctx, smpl);
11131112

11141113
common_sampler_free(smpl);

0 commit comments

Comments
 (0)