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
23 namespace plask {
24
25 PLASK_API std::string host_name();
26
27 enum LogLevel {
28 LOG_CRITICAL_ERROR = 0,
29 LOG_ERROR = 1,
30 LOG_ERROR_DETAIL = 2,
31 LOG_WARNING = 3,
32 LOG_IMPORTANT = 4,
33 LOG_INFO = 5,
34 LOG_RESULT = 6,
35 LOG_DATA = 7,
36 LOG_DETAIL = 8,
37 LOG_DEBUG = 9
38};
39
41 PLASK_API extern LogLevel maxLoglevel;
42 PLASK_API extern bool forcedLoglevel;
43
48 class PLASK_API NoLogging {
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
68 class PLASK_API Logger {
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 {
88 COLOR_NONE,
89 COLOR_ANSI
90#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
91 , COLOR_WINDOWS
92#endif
93 };
94
96 ColorMode color;
97
98 Logger();
99
100 virtual ~Logger() {}
101
107 virtual void writelog(LogLevel level, const std::string& msg) = 0;
108
109};
110
114 PLASK_API extern shared_ptr<Logger> default_logger;
115
116 PLASK_API void createDefaultLogger();
117
123 inline void writelog(LogLevel level, const std::string& msg) {
124 if (!default_logger) createDefaultLogger();
125 if (level <= maxLoglevel && (!default_logger->silent || level <= LOG_WARNING)) {
126 default_logger->writelog(level, msg);
127 }
128}
129
136 template<typename... Args>
137 inline void writelog(LogLevel level, const std::string& msg, Args&&... params) {
138 if (!default_logger) createDefaultLogger();
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