HILA
Loading...
Searching...
No Matches
logger.h
1#ifndef LOGGER_H_
2#define LOGGER_H_
3
4#include <iostream>
5#include <fstream>
6
7/// Define the logger class here.
9 /// The output stream logs go to
10 std::ostream output;
11 /// An output file stream, for opening and closing files
12 std::ofstream output_file;
13 /// Default logging level is 1, only print only at main function level
14 int logging_level = 1;
15 /// Current logging level
16 int current_level = 0;
17
18 public:
19 /// Constructor without parameters, set the logger to std::out
20 logger_class() : output(NULL) { output.rdbuf(std::cout.rdbuf()); }
21
22 /// Construct with filename
23 logger_class(std::string filename) : output(NULL) { set_output(filename); }
24
25 /// Construct with stream
26 logger_class(std::ostream stream) : output(NULL) { output.rdbuf(stream.rdbuf()); }
27
28 /// Close the file in the destructor
30 if (output_file.is_open()) {
31 output_file.close();
32 }
33 }
34
35 /// Set logging level
36 void set_verbosity(int level) { logging_level = level; }
37
38 /// Increase logging level. Should be called when entering a specialized
39 /// area of code.
40 void increase_level() { current_level += 1; }
41
42 /// Decrease logging level. Should be called when exiting a specialized
43 /// area of code.
44 void decrease_level() { current_level -= 1; }
45
46 /// Set the output stream
47 void set_output(std::ostream stream) {
48 output.rdbuf(stream.rdbuf());
49 if (output_file.is_open()) {
50 output_file.close();
51 }
52 }
53
54 /// Open stream to a file. Interprets strings as filenames
55 void set_output(std::string filename) {
56 if (output_file.is_open()) {
57 output_file.close();
58 }
59 output_file.open(filename, std::ios::out);
60 output.rdbuf(output_file.rdbuf());
61 }
62
63 /// Log function
64 template <typename T> void log(T text) {
65 if (hila::myrank() == 0) {
66 if (current_level < logging_level) {
67 output << text;
68 }
69 }
70 }
71
72 template <typename T> friend logger_class &operator<<(logger_class &logger, T rhs);
73};
74
75/// Streaming operator for the logger class. Only node 0 prints.
76/// This should also accept formatting correctly.
77template <typename T> logger_class &operator<<(logger_class &logger, T rhs) {
78 if (hila::myrank() == 0) {
79 if (logger.current_level < logger.logging_level) {
80 logger.output << rhs;
81 }
82 }
83 return logger;
84}
85
86#endif
std::ostream & operator<<(std::ostream &strm, const Array< n, m, T > &A)
Stream operator.
Definition array.h:916
Define the logger class here.
Definition logger.h:8
void decrease_level()
Definition logger.h:44
logger_class(std::string filename)
Construct with filename.
Definition logger.h:23
void set_output(std::string filename)
Open stream to a file. Interprets strings as filenames.
Definition logger.h:55
void set_output(std::ostream stream)
Set the output stream.
Definition logger.h:47
void log(T text)
Log function.
Definition logger.h:64
friend logger_class & operator<<(logger_class &logger, T rhs)
Definition logger.h:77
logger_class(std::ostream stream)
Construct with stream.
Definition logger.h:26
logger_class()
Constructor without parameters, set the logger to std::out.
Definition logger.h:20
void set_verbosity(int level)
Set logging level.
Definition logger.h:36
void increase_level()
Definition logger.h:40
~logger_class()
Close the file in the destructor.
Definition logger.h:29
int myrank()
rank of this node
Definition com_mpi.cpp:235