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