NAWA  0.8
Web Application Framework for C++
Log.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2019-2021 Tobias Flaig.
8  *
9  * This file is part of nawa.
10  *
11  * nawa is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License,
13  * version 3, as published by the Free Software Foundation.
14  *
15  * nawa is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with nawa. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef NAWA_LOG_H
25 #define NAWA_LOG_H
26 
27 #include <fstream>
28 #include <iostream>
29 #include <nawa/internal/macros.h>
30 #include <sstream>
31 
32 namespace nawa {
38  class Log {
40 
41  public:
47  enum class Level {
48  OFF,
49  ERROR,
50  WARNING,
52  DEBUG
53  };
54 
59  Log() noexcept;
60 
66  explicit Log(std::string appname, Level level = Level::INFORMATIONAL) noexcept;
67 
72  explicit Log(Level level) noexcept;
73 
74  Log(Log const& other) noexcept;
75 
76  Log& operator=(Log const& other) noexcept;
77 
78  virtual ~Log();
79 
89  static void setStream(std::ostream* os) noexcept;
90 
101  static void setOutfile(std::string const& filename);
102 
110  static void setOutputLevel(Level level);
111 
120  static void setExtendedFormat(bool useExtendedFormat);
121 
128  static void lockStream() noexcept;
129 
134  static bool isLocked() noexcept;
135 
141  void setAppname(std::string appname) noexcept;
142 
147  void setDefaultLogLevel(Level level) noexcept;
148 
153  void write(std::string const& msg);
154 
160  void write(std::string const& msg, Level logLevel);
161 
166  void operator()(std::string const& msg);
167 
173  void operator()(std::string const& msg, Level logLevel);
174  };
175 }// namespace nawa
176 
177 #define NLOG(Logger, Message) \
178  { \
179  std::ostringstream msgs; \
180  msgs << Message; \
181  (Logger).write(msgs.str()); \
182  }
183 #define NLOG_ERROR(Logger, Message) \
184  { \
185  std::ostringstream msgs; \
186  msgs << Message; \
187  (Logger).write(msgs.str(), nawa::Log::Level::ERROR); \
188  }
189 #define NLOG_WARNING(Logger, Message) \
190  { \
191  std::ostringstream msgs; \
192  msgs << Message; \
193  (Logger).write(msgs.str(), nawa::Log::Level::WARNING); \
194  }
195 #define NLOG_INFO(Logger, Message) \
196  { \
197  std::ostringstream msgs; \
198  msgs << Message; \
199  (Logger).write(msgs.str(), nawa::Log::Level::INFORMATIONAL); \
200  }
201 #define NLOG_DEBUG(Logger, Message) \
202  { \
203  std::ostringstream msgs; \
204  msgs << Message; \
205  (Logger).write(msgs.str(), nawa::Log::Level::DEBUG); \
206  }
207 
208 #endif//NAWA_LOG_H
Definition: Log.h:38
static void setOutputLevel(Level level)
Definition: Log.cpp:136
static void setOutfile(std::string const &filename)
Definition: Log.cpp:122
Level
Definition: Log.h:47
void setDefaultLogLevel(Level level) noexcept
Definition: Log.cpp:160
Log() noexcept
Definition: Log.cpp:59
static void lockStream() noexcept
Definition: Log.cpp:148
void write(std::string const &msg)
Definition: Log.cpp:164
void setAppname(std::string appname) noexcept
Definition: Log.cpp:156
static bool isLocked() noexcept
Definition: Log.cpp:152
static void setStream(std::ostream *os) noexcept
Definition: Log.cpp:116
static void setExtendedFormat(bool useExtendedFormat)
Definition: Log.cpp:142
Macros for frequently used patterns.
#define NAWA_PRIVATE_DATA()
Definition: macros.h:30
Definition: AppInit.h:31