11#ifndef UTILS_UTIL_IO_H
12#define UTILS_UTIL_IO_H
41 std::string tabS =
"";
42 for (
int i = 0; i < nt; i++)
56 std::ostringstream oss;
67template <
class T>
inline std::string
printStr(
const std::vector<T> &list,
71 std::ostringstream oss;
85template <>
inline std::string
printStr(
const std::vector<util::Point> &list,
89 std::ostringstream oss;
93 oss <<
"(" << l[0] <<
", " << l[1] <<
", " << l[2] <<
")";
128inline std::string
printStr(
const std::vector<std::vector<T>> &list,
132 std::ostringstream oss;
135 for (
auto l : list) {
137 for (
size_t k = 0; k < l.size(); k++) {
139 if (k < l.size() - 1)
145 if (i != list.size())
154template <
class T>
inline void print(
const std::vector<std::vector<T>> &list,
168inline std::string
printBoxStr(
const std::pair<util::Point, util::Point>
171 std::ostringstream oss;
172 oss << tabS <<
"Corner point 1 = " << box.first.printStr(nt, 0) << std::endl;
173 oss << tabS <<
"Corner point 2 = " << box.second.printStr(nt, 0) << std::endl;
184inline void printBox(
const std::pair<util::Point, util::Point> &box,
193inline std::string
printBoxStr(
const std::pair<std::vector<double>, std::vector<double>> &box,
196 std::ostringstream oss;
197 oss << tabS <<
"Corner point 1 = (" << printStr<double>(box.first, 0)
199 oss << tabS <<
"Corner point 2 = (" << printStr<double>(box.second, 0)
208inline void printBox(
const std::pair<std::vector<double>, std::vector<double>> &box,
282 void log(std::ostringstream &oss,
bool screen_out =
false,
285 log(oss.str(), screen_out, printMpiRank);
299 void log(
const std::string &str,
bool screen_out =
false,
304 std::cout << str << std::flush;
336void log(std::ostringstream &oss,
bool screen_out =
false,
346void log(
const std::string &str,
bool screen_out =
false,
363 for (
int i = 1; i < argc; ++i)
364 this->
tokens.push_back(std::string(argv[i]));
373 std::vector<std::string>::const_iterator itr;
374 itr = std::find(this->
tokens.begin(), this->tokens.end(), option);
375 if (itr != this->
tokens.end() && ++itr != this->tokens.end())
378 static const std::string empty_string(
"");
388 return std::find(this->
tokens.begin(), this->tokens.end(), option) != this->
tokens.end();
406 return path.substr(path.find_last_of(delims) + 1);
418 typename std::string::size_type
const p(filename.find_last_of(
'.'));
419 return p > 0 && p != std::string::npos ? filename.substr(0, p) : filename;
430 typename std::string::size_type
const p(filename.find_last_of(
'.'));
433 return p > 0 && p != std::string::npos ? filename.substr(p+1) :
"";
446 std::string f = filename;
448 f = f +
"." + filename_ext;
451 if (f_ext != filename_ext) {
452 std::cerr <<
"checkAndCreateNewFilename(): Argument filename = "
453 << filename <<
" has extension = "
454 << f_ext <<
" which does not match expected extension = "
455 << filename_ext << std::endl;
470 std::ifstream pFile(filename);
471 return pFile.peek() == std::ifstream::traits_type::eof();
481 return pFile.peek() == std::ifstream::traits_type::eof();
Prints log to std::cout and also write to the file.
Logger(LoggerDeck *deck=nullptr)
Constructor.
std::ofstream d_logFile
Filestream for logging.
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
LoggerDeck * d_deck_p
Pointer to logger deck.
void log(const std::string &str, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
std::string printBoxStr(const std::pair< util::Point, util::Point > &box, int nt=print_default_tab)
Returns formatted string for output.
std::string getTabS(int nt)
Returns tab spaces of given size.
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
const int print_default_tab
Default value of tab used in outputting formatted information.
void print(const T &msg, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted information.
const int logger_default_debug_lvl
Default debug level for logger.
std::string getExtensionFromFile(std::string const &filename)
Get extension from the filename.
void initLogger(int debug_level=logger_default_debug_lvl, std::string filename="")
Initializes the logger.
bool isFileEmpty(std::string filename)
Check if file is empty or null.
std::string checkAndCreateNewFilename(std::string const &filename, std::string filename_ext)
Check for extension and if possible create new filename from a given filename and a given extension.
void printBox(const std::pair< util::Point, util::Point > &box, int nt=print_default_tab, int printMpiRank=print_default_mpi_rank)
Prints formatted string for output.
std::string removeExtensionFromFile(std::string const &filename)
Remove extension from the filename Source - https://stackoverflow.com/a/24386991.
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Global method to log the message.
const int print_default_mpi_rank
Default mpi rank in printing information.
std::string getFilenameFromPath(std::string const &path, std::string const &delims="/\\")
Get filename removing path from the string Source - https://stackoverflow.com/a/24386991.
int mpiRank()
get rank (id) of this processor
Collection of methods useful in simulation.
Deck to store log parameters.
bool d_printFile
Print to file?
LoggerDeck(int debug_level, std::string filename)
Constructor.
std::string d_filename
Filename to print log.
int d_debugLevel
Debug level.
bool d_printScreen
Print to std::cout?