PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1/*
2* -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef UTILS_IO_LOGGER_H
12#define UTILS_IO_LOGGER_H
13
14#include "loggerDeck.h"
15#include "parallelUtil.h" // to make prints MPI aware
16#include "constants.h"
17
18namespace util {
19 namespace io {
23 class Logger {
24 public:
30 Logger(LoggerDeck *deck = nullptr) : d_deck_p(deck) {
31 if (d_deck_p == nullptr)
32 d_deck_p = new LoggerDeck();
33 }
34
40 d_logFile.close();
41 }
42
50 void log(std::ostringstream &oss, bool screen_out = false,
51 int printMpiRank = print_default_mpi_rank) {
52 log(oss.str(), screen_out, printMpiRank);
53
54 // reset oss
55 oss.str("");
56 oss.clear();
57 };
58
66 void log(const std::string &str, bool screen_out = false,
67 int printMpiRank = print_default_mpi_rank) {
68 if (printMpiRank < 0 or util::parallel::mpiRank() == printMpiRank) {
69 if (d_deck_p->d_printScreen || screen_out)
70 std::cout << str << std::flush;
71
72 // log
73 if (d_deck_p->d_printFile) {
74 d_logFile.open(d_deck_p->d_filename, std::ios_base::app);
75 d_logFile << str;
76 d_logFile.close();
77 }
78 }
79 };
80
83
85 std::ofstream d_logFile;
86 };
87 } // namespace io
88} // namespace util
89
90#endif // UTILS_IO_LOGGER_H
91
Prints log to std::cout and also write to the file.
Definition logger.h:23
Logger(LoggerDeck *deck=nullptr)
Constructor.
Definition logger.h:30
std::ofstream d_logFile
Filestream for logging.
Definition logger.h:85
void log(std::ostringstream &oss, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
Definition logger.h:50
LoggerDeck * d_deck_p
Pointer to logger deck.
Definition logger.h:82
void log(const std::string &str, bool screen_out=false, int printMpiRank=print_default_mpi_rank)
Log the message.
Definition logger.h:66
~Logger()
Destructor.
Definition logger.h:38
const int print_default_mpi_rank
Default mpi rank in printing information.
Definition constants.h:21
int mpiRank()
get rank (id) of this processor
Collection of methods useful in simulation.
Definition constants.h:14
Deck to store log parameters.
Definition loggerDeck.h:22
bool d_printFile
Print to file?
Definition loggerDeck.h:33
std::string d_filename
Filename to print log.
Definition loggerDeck.h:27
bool d_printScreen
Print to std::cout?
Definition loggerDeck.h:30