/src/duckdb/src/common/printer.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "duckdb/common/printer.hpp" |
2 | | #include "duckdb/common/progress_bar.hpp" |
3 | | #include "duckdb/common/windows_util.hpp" |
4 | | #include "duckdb/common/windows.hpp" |
5 | | #include <stdio.h> |
6 | | |
7 | | #ifndef DUCKDB_DISABLE_PRINT |
8 | | #ifdef DUCKDB_WINDOWS |
9 | | #include <io.h> |
10 | | #endif |
11 | | #endif |
12 | | |
13 | | namespace duckdb { |
14 | | |
15 | | // LCOV_EXCL_START |
16 | 0 | void Printer::Print(const string &str) { |
17 | 0 | #ifndef DUCKDB_DISABLE_PRINT |
18 | | #ifdef DUCKDB_WINDOWS |
19 | | if (IsTerminal()) { |
20 | | // print utf8 to terminal |
21 | | auto unicode = WindowsUtil::UTF8ToMBCS(str.c_str()); |
22 | | fprintf(stderr, "%s\n", unicode.c_str()); |
23 | | return; |
24 | | } |
25 | | #endif |
26 | 0 | fprintf(stderr, "%s\n", str.c_str()); |
27 | 0 | #endif |
28 | 0 | } |
29 | | |
30 | 0 | void Printer::PrintProgress(int percentage, const char *pbstr, int pbwidth) { |
31 | 0 | #ifndef DUCKDB_DISABLE_PRINT |
32 | 0 | int lpad = (int)(percentage / 100.0 * pbwidth); |
33 | 0 | int rpad = pbwidth - lpad; |
34 | 0 | printf("\r%3d%% [%.*s%*s]", percentage, lpad, pbstr, rpad, ""); |
35 | 0 | fflush(stdout); |
36 | 0 | #endif |
37 | 0 | } |
38 | | |
39 | 0 | void Printer::FinishProgressBarPrint(const char *pbstr, int pbwidth) { |
40 | 0 | #ifndef DUCKDB_DISABLE_PRINT |
41 | 0 | PrintProgress(100, pbstr, pbwidth); |
42 | 0 | printf(" \n"); |
43 | 0 | fflush(stdout); |
44 | 0 | #endif |
45 | 0 | } |
46 | | |
47 | 0 | bool Printer::IsTerminal() { |
48 | 0 | #ifndef DUCKDB_DISABLE_PRINT |
49 | | #ifdef DUCKDB_WINDOWS |
50 | | return GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_CHAR; |
51 | | #else |
52 | 0 | throw InternalException("IsTerminal is only implemented for Windows"); |
53 | 0 | #endif |
54 | 0 | #endif |
55 | 0 | return false; |
56 | 0 | } |
57 | | // LCOV_EXCL_STOP |
58 | | |
59 | | } // namespace duckdb |