DNDSR 0.1.0.dev1+gcd065ad
Distributed Numeric Data Structure for CFV
Loading...
Searching...
No Matches
Defines.cpp
Go to the documentation of this file.
1/// @file Defines.cpp
2/// @brief Implementations of symbols declared in @ref Defines.hpp: log stream
3/// management, signal handler body, OpenMP helpers, version string accessor,
4/// terminal / progress utilities.
5
6#include "Defines.hpp"
7
8// #ifdef _MSC_VER
9// #define _SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING
10// #endif
11#include <codecvt>
12#include <boost/stacktrace.hpp>
13// #include <cpptrace.hpp>
14
15#ifdef DNDS_UNIX_LIKE
16# include <unistd.h>
17# include <sys/ioctl.h>
18# define _isatty isatty
19#endif
20#if defined(_WIN32) || defined(__WINDOWS_)
21# define NOMINMAX
22# include <io.h>
23# include <windows.h>
24#endif
25
26extern "C" void DNDS_signal_handler(int signal)
27{
28 std::cerr << ::DNDS::getTraceString() << "\n";
29 std::cerr << "Signal " + std::to_string(signal) << std::endl;
30 std::signal(signal, SIG_DFL);
31 std::raise(signal);
32}
33
34namespace DNDS
35{
36 bool ostreamIsTTY(std::ostream &ostream)
37 {
38 if (&ostream == &std::cout)
39 return _isatty(fileno(stdout));
40 if (&ostream == &std::cerr)
41 return _isatty(fileno(stderr));
42 return false;
43 }
44
46
47 bool useCout = true;
48
49 std::ostream &log() { return useCout ? std::cout : *logStream; }
50
51 bool logIsTTY() { return ostreamIsTTY(*logStream); }
52
53 void setLogStream(ssp<std::ostream> nstream) { useCout = false, logStream = nstream; }
54
55 void setLogStreamCout() { useCout = true, logStream.reset(); }
56
58 {
59#ifdef _WIN32
60 CONSOLE_SCREEN_BUFFER_INFO csbi;
61 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
62 {
63 return csbi.srWindow.Right - csbi.srWindow.Left + 1;
64 }
65#else
66 struct winsize w;
67 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0)
68 {
69 return w.ws_col;
70 }
71#endif
72 return 80; // Default width if detection fails
73 }
74
75 void print_progress(std::ostream &os, double progress)
76 {
77 progress = std::clamp(progress, 0.0, 1.0);
78 int term_width = ostreamIsTTY(os) ? get_terminal_width() : 80;
79 int bar_width = std::max(10, term_width - 10);
80
81 int pos = static_cast<int>(bar_width * progress);
82
83 if (ostreamIsTTY(os))
84 {
85 os << "\r[";
86 for (int i = 0; i < bar_width; ++i)
87 {
88 if (i < pos)
89 os << "=";
90 else if (i == pos)
91 os << ">";
92 else
93 os << " ";
94 }
95 os << "] " << std::setw(3) << static_cast<int>(progress * 100) << "% " << std::flush;
96 }
97 else
98 {
99 os << "[" << std::string(pos, '=') << ">" << std::string(bar_width - pos, ' ')
100 << "] " << std::setw(3) << static_cast<int>(progress * 100) << "%" << std::endl;
101 }
102 }
103
104 std::string getStringForceWString(const std::wstring &v)
105 {
106 // std::vector<char> buf(v.size());
107 // std::wcstombs(buf.data(), v.data(), v.size());
108 // return std::string{buf.data()};
110 DISABLE_WARNING_DEPRECATED_DECLARATIONS
111 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
112 return converter.to_bytes(v); // TODO: on windows use WideCharToMultiByte()
114 }
115}
116
117namespace DNDS
118{
120 {
121 static int ret{-1};
122 if (ret == -1)
123 {
124 const char *env = std::getenv("OMP_NUM_THREADS");
125 ret = 0;
126 if (env)
127 try
128 {
129 ret = std::stoi(env);
130 }
131 catch (...)
132 {
133 ret = -1;
134 }
135 }
136 return ret;
137 }
138
140 {
141 static int ret{-1};
142 if (ret == -1)
143 {
144 const char *env = std::getenv("DNDS_DIST_OMP_NUM_THREADS");
145 ret = 0;
146 if (env)
147 try
148 {
149 ret = std::stoi(env);
150 }
151 catch (...)
152 {
153 ret = -1;
154 }
155 }
156 return ret;
157 }
158}
159
160/********************************/
161// workaround for cpp trace
163{
164 std::stringstream ss;
165 ss << boost::stacktrace::stacktrace();
166 return ss.str();
167 // return cpptrace::generate_trace().to_string();
168}
169
170namespace DNDS
171{
172 std::string GetSetVersionName(const std::string &ver)
173 {
174 static std::string ver_name = "UNKNOWN";
175 if (ver.length())
176 ver_name = ver;
177 return ver_name;
178 }
179}
void DNDS_signal_handler(int signal)
Definition Defines.cpp:26
Core type aliases, constants, and metaprogramming utilities for the DNDS framework.
#define DISABLE_WARNING_PUSH
Definition Warnings.hpp:73
#define DISABLE_WARNING_POP
Definition Warnings.hpp:74
the host side operators are provided as implemented
ssp< std::ostream > logStream
Shared output stream: where DNDS::log() writes progress / diagnostics.
Definition Defines.cpp:45
int get_env_OMP_NUM_THREADS()
Read OMP_NUM_THREADS env var, returning 1 if unset / invalid.
Definition Defines.cpp:119
void setLogStream(ssp< std::ostream > nstream)
Redirect log() output to a user-supplied stream. Ownership is shared.
Definition Defines.cpp:53
bool logIsTTY()
Convenience: ostreamIsTTY applied to the current log() stream.
Definition Defines.cpp:51
std::string getTraceString()
Return a symbolicated stack trace for the calling thread.
Definition Defines.cpp:162
int get_terminal_width()
Terminal width in columns (falls back to a fixed default when not a TTY).
Definition Defines.cpp:57
std::string getStringForceWString(const std::wstring &v)
Convert a wstring to string (UTF-8 on Windows, byte-cast elsewhere).
Definition Defines.cpp:104
int get_env_DNDS_DIST_OMP_NUM_THREADS()
Read the DNDSR-specific DNDS_DIST_OMP_NUM_THREADS override, falling back to get_env_OMP_NUM_THREADS.
Definition Defines.cpp:139
std::shared_ptr< T > ssp
Shortened alias for std::shared_ptr used pervasively in DNDSR.
Definition Defines.hpp:138
bool ostreamIsTTY(std::ostream &ostream)
Heuristic detection of whether ostream is attached to a terminal.
Definition Defines.cpp:36
void print_progress(std::ostream &os, double progress)
Render a textual progress bar to os for progress in [0, 1].
Definition Defines.cpp:75
std::ostream & log()
Return the current DNDSR log stream (either std::cout or the installed file).
Definition Defines.cpp:49
bool useCout
Whether DNDS::log() is currently routed to std::cout.
Definition Defines.cpp:47
void setLogStreamCout()
Restore the default std::cout routing for log().
Definition Defines.cpp:55
std::string GetSetVersionName(const std::string &ver)
Read/set the build version string accessible from code.
Definition Defines.cpp:172
Eigen::Matrix< real, 5, 1 > v