libstorage-ng
Logger.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2017] SUSE LLC
4  *
5  * All Rights Reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License as published
9  * by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, contact Novell, Inc.
18  *
19  * To contact Novell about this file by physical or electronic mail, you may
20  * find current contact information at www.novell.com.
21  */
22 
23 
24 #ifndef STORAGE_LOGGER_H
25 #define STORAGE_LOGGER_H
26 
27 
28 #include <string>
29 
30 
31 namespace storage
32 {
33 
34 
35  enum class LogLevel { DEBUG = 0, MILESTONE = 1, WARNING = 2, ERROR = 3 };
36 
37 
41  class Logger
42  {
43  public:
44 
45  Logger() {}
46  virtual ~Logger() {}
47 
52  virtual bool test(LogLevel log_level, const std::string& component);
53 
57  virtual void write(LogLevel log_level, const std::string& component, const std::string& file,
58  int line, const std::string& function, const std::string& content) = 0;
59 
60  };
61 
62 
66  Logger* get_logger();
67 
68 
73  void set_logger(Logger* logger);
74 
75 
81 
82 
89 
90 
94  class Silencer
95  {
96 
97  public:
98 
99  Silencer();
100  ~Silencer();
101 
102  void turn_on();
103  void turn_off();
104 
105  static bool is_any_active() { return count > 0; }
106 
107  private:
108 
109  bool active;
110 
111  static int count;
112 
113  };
114 
115 }
116 
117 
118 #endif
virtual bool test(LogLevel log_level, const std::string &component)
Function to control whether a log line with level and component should be logged. ...
Logger * get_logger()
Get the current logger object.
Logger * get_logfile_logger()
Returns a Logger that logs to the standard libstorage log file ("/var/log/libstorage.log").
void set_logger(Logger *logger)
Set the current logger object.
The Logger class.
Definition: Logger.h:41
virtual void write(LogLevel log_level, const std::string &component, const std::string &file, int line, const std::string &function, const std::string &content)=0
Function to log a line.
The storage namespace.
Definition: Actiongraph.h:37
Class to make some exceptions log-level DEBUG instead of WARNING.
Definition: Logger.h:94
Logger * get_stdout_logger()
Returns a Logger that logs to stdout.