FreeTensor
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_LOGGER_H
2#define FREE_TENSOR_LOGGER_H
3
4#include <iostream>
5#include <mutex>
6#include <thread>
7
8namespace freetensor {
9
10class LogCtrl {
11 bool enable_ = true;
12
13 static LogCtrl instance_;
14
15 public:
16 bool isEnabled() const { return enable_; }
17 void enable() { enable_ = true; }
18 void disable() { enable_ = false; }
19
20 static LogCtrl &instance() { return instance_; }
21};
22
23class Logger {
24 std::ostream &os_;
25
26 static std::mutex lock_;
27
28 public:
29 Logger() : os_(std::cerr) {
30 lock_.lock();
31 if (LogCtrl::instance().isEnabled()) {
32 os_ << "[tid " << std::hex << std::this_thread::get_id() << "] "
33 << std::dec;
34 }
35 }
36 ~Logger() { lock_.unlock(); }
37
38 Logger(const Logger &) = delete;
39 Logger(Logger &&other) = default;
40 Logger &operator=(const Logger &) = delete;
41
44
45 template <class T> Logger &operator<<(T &&x) {
46 if (LogCtrl::instance().isEnabled()) {
47 os_ << x;
48 }
49 return *this;
50 }
51
52 Logger &operator<<(std::ostream &(*manip)(std::ostream &)) {
53 if (LogCtrl::instance().isEnabled()) {
54 manip(os_);
55 }
56 return *this;
57 }
58};
59
60inline Logger logger() { return Logger(); }
61
62} // namespace freetensor
63
64#endif // FREE_TENSOR_LOGGER_H
Definition: logger.h:10
static LogCtrl & instance()
Definition: logger.h:20
void enable()
Definition: logger.h:17
bool isEnabled() const
Definition: logger.h:16
void disable()
Definition: logger.h:18
Definition: logger.h:23
Logger & operator=(const Logger &)=delete
Logger(Logger &&other)=default
Logger & operator<<(std::ostream &(*manip)(std::ostream &))
Definition: logger.h:52
void enable()
Definition: logger.h:42
Logger & operator<<(T &&x)
Definition: logger.h:45
Logger(const Logger &)=delete
void disable()
Definition: logger.h:43
~Logger()
Definition: logger.h:36
Logger()
Definition: logger.h:29
Definition: allocator.h:9
Logger logger()
Definition: logger.h:60
STL namespace.