PLaSK library
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1/*
2 * This file is part of PLaSK (https://plask.app) by Photonics Group at TUL
3 * Copyright (c) 2022 Lodz University of Technology
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14#ifndef PLASK__LOG_LOG_H
15#define PLASK__LOG_LOG_H
16
17#include <iostream>
18#include <string>
19
20#include "../memory.hpp"
21#include "../utils/format.hpp"
22
23namespace plask {
24
25PLASK_API std::string host_name();
26
39
42PLASK_API extern bool forcedLoglevel;
43
49 bool old_state;
50
51 public:
52 NoLogging();
53
54 NoLogging(bool silent);
55
56 ~NoLogging();
57
59 void set(bool silent);
60
62 void silence() { set(true); }
63};
64
69
71 bool silent;
72
73 friend class NoLogging;
74
75 friend void writelog(LogLevel level, const std::string& msg);
76
77 template<typename... Args>
78 friend void writelog(LogLevel level, const std::string& msg, Args&&... params);
79
80 protected:
81
83 std::string prefix;
84
85 public:
86
87 enum ColorMode {
89 COLOR_ANSI
90#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
92#endif
93 };
94
97
98 Logger();
99
100 virtual ~Logger() {}
101
107 virtual void writelog(LogLevel level, const std::string& msg) = 0;
108
109};
110
115
117
123inline void writelog(LogLevel level, const std::string& msg) {
125 if (level <= maxLoglevel && (!default_logger->silent || level <= LOG_WARNING)) {
126 default_logger->writelog(level, msg);
127 }
128}
129
136template<typename... Args>
137inline void writelog(LogLevel level, const std::string& msg, Args&&... params) {
139 if (level <= maxLoglevel && (!default_logger->silent || level <= LOG_WARNING)) {
140 default_logger->writelog(level, format(msg, std::forward<Args>(params)...));
141 }
142}
143
144} // namespace plask
145
146#ifdef NDEBUG
147# define write_debug(...) void(0)
148#else
149# define write_debug(...) writelog(LOG_DEBUG, __VA_ARGS__)
150#endif
151
152#endif // PLASK__LOG_LOG_H