MOTION  0.01
Framework for mixed-protocol multi-party computation
logger.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Oleksandr Tkachenko
4 // Cryptography and Privacy Engineering Group (ENCRYPTO)
5 // TU Darmstadt, Germany
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #pragma once
26 
27 #include <atomic>
28 #include <boost/log/sinks/sync_frontend.hpp>
29 #include <boost/log/sinks/text_file_backend.hpp>
30 #include <boost/log/sources/severity_channel_logger.hpp>
31 #include <boost/log/trivial.hpp>
32 #include <memory>
33 #include <mutex>
34 
35 namespace encrypto::motion {
36 
37 using LoggerType =
38  boost::log::sources::severity_channel_logger<boost::log::trivial::severity_level, std::size_t>;
39 
40 class Logger {
41  public:
42  // multiple instantiations of Logger in one application will cause duplicates
43  // in logs
44  Logger(std::size_t my_id, boost::log::trivial::severity_level severity_level);
45 
46  ~Logger();
47 
48  void Log(boost::log::trivial::severity_level severity_level, const std::string& message);
49 
50  void Log(boost::log::trivial::severity_level severity_level, std::string&& message);
51 
52  void LogTrace(const std::string& message);
53 
54  void LogTrace(std::string&& message);
55 
56  void LogInfo(const std::string& message);
57 
58  void LogInfo(std::string&& message);
59 
60  void LogDebug(const std::string& message);
61 
62  void LogDebug(std::string&& message);
63 
64  void LogError(const std::string& message);
65 
66  void LogError(std::string&& message);
67 
68  bool IsEnabled() { return logging_enabled_; }
69 
70  void SetEnabled(bool enable = true);
71 
72  private:
73  boost::shared_ptr<boost::log::sinks::synchronous_sink<boost::log::sinks::text_file_backend>>
74  g_file_sink_;
75  std::unique_ptr<LoggerType> logger_;
76  const std::size_t my_id_;
77  std::atomic<bool> logging_enabled_ = true;
78  std::mutex write_mutex_;
79 
80  // aquire this on calls to boost::log::core
81  static std::mutex boost_log_core_mutex_;
82 
83  Logger() = delete;
84 };
85 
86 using LoggerPointer = std::shared_ptr<Logger>;
87 
88 } // namespace encrypto::motion
encrypto::motion::Logger::LogTrace
void LogTrace(const std::string &message)
Definition: logger.cpp:110
encrypto::motion::Logger::LogInfo
void LogInfo(const std::string &message)
Definition: logger.cpp:128
encrypto::motion::Logger
Definition: logger.h:40
encrypto::motion::Logger::LogError
void LogError(const std::string &message)
Definition: logger.cpp:160
encrypto::motion::Logger::Log
void Log(boost::log::trivial::severity_level severity_level, const std::string &message)
encrypto::motion::Logger::LogDebug
void LogDebug(const std::string &message)
Definition: logger.cpp:142
encrypto::motion::Logger::~Logger
~Logger()
Definition: logger.cpp:90
logger.h
encrypto::motion
Definition: algorithm_description.cpp:35
encrypto::motion::Logger::IsEnabled
bool IsEnabled()
Definition: logger.h:68
encrypto::motion::LoggerPointer
std::shared_ptr< Logger > LoggerPointer
Definition: backend.h:78
encrypto::motion::LoggerType
boost::log::sources::severity_channel_logger< boost::log::trivial::severity_level, std::size_t > LoggerType
Definition: logger.h:38
encrypto::motion::kVerboseDebug
constexpr bool kVerboseDebug
Definition: constants.h:50
encrypto::motion::Logger::SetEnabled
void SetEnabled(bool enable=true)
Definition: logger.cpp:174
constants.h
encrypto::motion::kDebug
constexpr bool kDebug
Definition: config.h:36
encrypto::motion::kMb
constexpr std::size_t kMb
Definition: constants.h:40