1313
1414static const char hex_digits [] = "0123456789abcdef" ;
1515static const char caps_hex_digits [] = "0123456789ABCDEF" ;
16+ static stream_t printf_stream ;
1617
1718string last_filename = "unknown" ; // for warn, info, err, done
1819string last_print_file = "unknown" ;
@@ -31,11 +32,7 @@ bool enable_logging = true;
3132 */
3233void warn (cstring message , cstring file ) {
3334 cstring warn_message = yellow_color "[***] → " reset_color ;
34- print (warn_message );
35- print (message );
36- print (" at " blue_color );
37- print (file );
38- print (reset_color "\n" );
35+ printf ("%s%s at " blue_color "%s" reset_color , warn_message , message , file );
3936
4037 debug_print (warn_message );
4138 debug_print (message );
@@ -56,11 +53,7 @@ void warn(cstring message, cstring file) {
5653 */
5754void error (cstring message , cstring file ) {
5855 cstring err_message = red_color "[***] → " reset_color ;
59- print (err_message );
60- print (message );
61- print (" at " blue_color );
62- print (file );
63- print (reset_color "\n" );
56+ eprintf ("%s%s at " blue_color "%s" reset_color , err_message , message , file );
6457
6558 debug_print (err_message );
6659 debug_print (message );
@@ -81,11 +74,7 @@ void error(cstring message, cstring file) {
8174 */
8275void info (cstring message , cstring file ) {
8376 cstring info_message = blue_color "[***] → " reset_color ;
84- print (info_message );
85- print (message );
86- print (" at " blue_color );
87- print (file );
88- print (reset_color "\n" );
77+ printf ("%s%s at " blue_color "%s" reset_color , info_message , message , file );
8978
9079 debug_print (info_message );
9180 debug_print (message );
@@ -106,11 +95,7 @@ void info(cstring message, cstring file) {
10695 */
10796void done (cstring message , cstring file ) {
10897 cstring done_message = green_color "[***] → " reset_color ;
109- print (done_message );
110- print (message );
111- print (" at " blue_color );
112- print (file );
113- print (reset_color "\n" );
98+ printf ("%s%s at " blue_color "%s" reset_color , done_message , message , file );
11499
115100 debug_print (done_message );
116101 debug_print (message );
@@ -128,10 +113,7 @@ void done(cstring message, cstring file) {
128113 * @note Internally using Flanterm's putchar function
129114 */
130115void vputc (char c ) {
131- char str [2 ];
132- str [0 ] = c ;
133- str [1 ] = '\0' ;
134- print (str );
116+ stream_putc (printf_stream , c );
135117}
136118
137119void printdec_fmt (int num , int width , bool zero_pad )
@@ -250,13 +232,15 @@ static void printstr_fmt(const char* s, int width)
250232}
251233
252234
253- void vprintf_internal (cstring file , cstring func , int64 line , bool newline , cstring format , va_list argp ) {
235+ void vprintf_internal (stream_t stream , cstring file , cstring func , int64 line , bool newline , cstring format , va_list argp ) {
254236 if (enable_logging ) {
255237 last_print_file = file ;
256238 last_print_func = func ;
257239 last_print_line = line ;
258240 }
259241
242+ printf_stream = stream ;
243+
260244 while (* format != '\0' ) {
261245 if (* format == '%' ) {
262246 format ++ ;
@@ -331,14 +315,21 @@ void vprintf_internal(cstring file, cstring func, int64 line, bool newline, cstr
331315void printf_internal (cstring file , cstring func , int64 line , cstring format , ...) {
332316 va_list argp ;
333317 va_start (argp , format );
334- vprintf_internal (file , func , line , true, format , argp );
318+ vprintf_internal (STDOUT , file , func , line , true, format , argp );
335319 va_end (argp );
336320}
337321
338322void printfnoln_internal (cstring file , cstring func , int64 line , cstring format , ...) {
339323 va_list argp ;
340324 va_start (argp , format );
341- vprintf_internal (file , func , line , false, format , argp );
325+ vprintf_internal (STDOUT , file , func , line , false, format , argp );
326+ va_end (argp );
327+ }
328+
329+ void eprintf_internal (cstring file , cstring func , int64 line , cstring format , ...) {
330+ va_list argp ;
331+ va_start (argp , format );
332+ vprintf_internal (STDERR , file , func , line , true, format , argp );
342333 va_end (argp );
343334}
344335
0 commit comments