libstorage-ng
Logger.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2018] 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 
91  Logger* get_logfile_logger(const std::string& filename = "/var/log/libstorage.log");
92 
93 
97  class Silencer
98  {
99 
100  public:
101 
102  Silencer();
103  ~Silencer();
104 
105  void turn_on();
106  void turn_off();
107 
108  static bool is_any_active() { return count > 0; }
109 
110  private:
111 
112  bool active;
113 
114  static int count;
115 
116  };
117 
118 }
119 
120 
121 #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.
void set_logger(Logger *logger)
Set the current logger object.
Logger * get_logfile_logger(const std::string &filename="/var/log/libstorage.log")
Returns a Logger that logs to the standard libstorage log file ("/var/log/libstorage.log") or to a given file.
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:97
Logger * get_stdout_logger()
Returns a Logger that logs to stdout.