USBGuard
Software framework that protects your computer against rogue USB devices by implementing basic whitelisting and blacklisting capabilities.
Logger.hpp
1 //
2 // Copyright (C) 2016 Red Hat, Inc.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
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 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // Authors: Daniel Kopecek <dkopecek@redhat.com>
18 //
19 #pragma once
20 
21 #include "Typedefs.hpp"
22 
23 #include <fstream>
24 #include <map>
25 #include <memory>
26 #include <mutex>
27 #include <sstream>
28 #include <string>
29 
30 namespace usbguard
31 {
32  class Logger;
33 
37  class DLL_PUBLIC LogStream : public std::ostringstream
38  {
39  public:
40 
44  struct Source {
45  std::string file;
46  int line;
47  std::string function;
48  };
49 
60  static const std::string sourceToString(const Source& source);
61 
65  enum class Level : int {
66  Audit = -2,
67  Error = -1,
68  Warning = 0,
69  Info = 1,
70  Debug = 2,
71  Trace = 3
72  };
73 
84  static const std::string levelToString(Level level);
85 
94  LogStream(Logger& logger, const Source& source, Level level);
95 
101  LogStream(const LogStream& rhs);
102 
106  ~LogStream();
107 
108  private:
109  Logger& _logger;
110  Source _source;
111  Level _level;
112  };
113 
117  class DLL_PUBLIC LogSink
118  {
119  public:
120 
126  LogSink(const std::string& name);
127 
131  virtual ~LogSink();
132 
138  const std::string& name() const;
139 
147  virtual void write(const LogStream::Source& source, LogStream::Level level, const std::string& message) = 0;
148 
149  private:
150  std::string _name;
151  };
152 
156  class DLL_PUBLIC Logger
157  {
158  public:
159 
163  Logger();
164 
168  ~Logger();
169 
178  void setEnabled(bool state, LogStream::Level level = LogStream::Level::Warning);
179 
188  bool isEnabled(LogStream::Level level) const;
189 
196  void setOutputConsole(bool state);
197 
205  void setOutputFile(bool state, const std::string& filepath = std::string(), bool append = true);
206 
213  void setOutputSyslog(bool state, const std::string& ident = std::string());
214 
221  void setAuditFile(bool state, const std::string& filepath);
222 
228  void addOutputSink(std::unique_ptr<LogSink>& sink);
229 
235  void delOutputSink(const std::string& name);
236 
248  LogStream operator()(const std::string& file, int line, const std::string& function, LogStream::Level level);
249 
257  void write(const LogStream::Source& source, LogStream::Level level, const std::string& message);
258 
266  static const std::string timestamp();
267 
268  private:
269 
275  void addOutputSink_nolock(std::unique_ptr<LogSink>& sink);
276 
282  void delOutputSink_nolock(const std::string& name);
283 
289  std::unique_lock<std::mutex> lock() const;
290 
291  mutable std::mutex _mutex;
292  bool _enabled;
293  LogStream::Level _level;
294  std::map<std::string, std::unique_ptr<LogSink>> _sinks;
295  };
296 
297  extern DLL_PUBLIC Logger G_logger;
298 
299 #if defined(__GNUC__)
300  #define USBGUARD_SOURCE_FILE __BASE_FILE__
301 #else
302  #define USBGUARD_SOURCE_FILE __FILE__
303 #endif
304 
305 #define USBGUARD_LOGGER usbguard::G_logger
306 
307 #define USBGUARD_FUNCTION __func__
308 
309 #define USBGUARD_LOG(level) \
310  if (USBGUARD_LOGGER.isEnabled(usbguard::LogStream::Level::level)) \
311  USBGUARD_LOGGER(USBGUARD_SOURCE_FILE, __LINE__, USBGUARD_FUNCTION, usbguard::LogStream::Level::level)
312 
313 } /* namespace usbguard */
314 
315 /* vim: set ts=2 sw=2 et */
Logger.
Definition: Logger.hpp:156
Logger output sink.
Definition: Logger.hpp:117
std::string file
Definition: Logger.hpp:45
Contains information about the source.
Definition: Logger.hpp:44
Level
Log levels.
Definition: Logger.hpp:65
Generates audit events for given policy or device events.
Definition: Audit.hpp:233
int line
Definition: Logger.hpp:46
Log stream.
Definition: Logger.hpp:37