libstorage-ng
Logger.h
1 /*
2  * Copyright (c) [2014-2015] Novell, Inc.
3  * Copyright (c) [2016-2020] 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 
37  enum class LogLevel { DEBUG = 0, MILESTONE = 1, WARNING = 2, ERROR = 3 };
38 
39 
43  class Logger
44  {
45  public:
46 
47  Logger() {}
48  virtual ~Logger() {}
49 
54  virtual bool test(LogLevel log_level, const std::string& component);
55 
59  virtual void write(LogLevel log_level, const std::string& component, const std::string& file,
60  int line, const std::string& function, const std::string& content) = 0;
61 
62  };
63 
64 
68  Logger* get_logger();
69 
70 
75  void set_logger(Logger* logger);
76 
77 
83 
84 
93  Logger* get_logfile_logger(const std::string& filename = "/var/log/libstorage.log");
94 
95 
99  class Silencer
100  {
101 
102  public:
103 
104  Silencer();
105  ~Silencer();
106 
107  void turn_on();
108  void turn_off();
109 
110  static bool is_any_active() { return count > 0; }
111 
112  private:
113 
114  bool active;
115 
116  static int count;
117 
118  };
119 
120 }
121 
122 
123 #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:43
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.
LogLevel
Enum with log levels.
Definition: Logger.h:37
The storage namespace.
Definition: Actiongraph.h:38
Class to make some exceptions log-level DEBUG instead of WARNING.
Definition: Logger.h:99
Logger * get_stdout_logger()
Returns a Logger that logs to stdout.