Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1/*
2 _ _
3 | | ___ __ _ __ _ ___ _ __ ___| | __ _ ___ ___
4 | | / _ \ / _` |/ _` |/ _ \ '__| / __| |/ _` / __/ __|
5 | |__| (_) | (_| | (_| | __/ | | (__| | (_| \__ \__ \
6 |_____\___/ \__, |\__, |\___|_| \___|_|\__,_|___/___/
7 |___/ |___/
8
9This class subscribes to all messages published by the broker and logs them to a
10MongoDB instance.
11
12Author(s): Paolo Bosetti
13*/
14
15#ifndef LOGGER_HPP
16#define LOGGER_HPP
17
18
19#include "mads.hpp"
20#include "agent.hpp"
21#include <memory>
22
23namespace Mads {
24
42class Logger : public Agent {
43public:
51 Logger(std::string name, std::string settings_path);
52
54
55 Logger(const Logger &) = delete;
56 Logger &operator=(const Logger &) = delete;
57
65 static bool has_mongo_support() noexcept;
66
75 void set_mongo(bool enabled = true, string uri = "");
76
84 void set_file(string filename, bool array = false);
85
92 void set_file(bool enabled = false);
93
100 void open_db();
101
108 void close_db();
109
115 void info(ostream &out = cout) override;
116
124 void register_event(event_type event);
125
131 void log(tuple<string, string> *message = nullptr);
132
138 void log(message_type type);
139
146 string truncated_message(const string &message);
147
148public:
149 bool paused = false; // Whether the logger is paused or not
150
151private:
152 void load_settings() override;
153
154 void connect_to_db();
155 void open_log_file(string filename, bool array = false);
156 void close_log_file();
157 void log_to_mongo(tuple<string, string> *message = nullptr);
158 void log_doc_to_mongo(const std::string &topic, const nlohmann::json &message);
159 void log_blob_to_mongo();
160 void log_to_file(tuple<string, string> *message = nullptr);
161
162 struct Impl;
163 std::unique_ptr<Impl> _impl;
164};
165
166} // namespace Mads
167
168#endif // LOGGER_HPP
std::string name()
Returns the name of the agent.
The Logger class is responsible for logging messages to a MongoDB instance.
Definition logger.hpp:42
void info(ostream &out=cout) override
Overrides the info method from the Agent class.
void close_db()
Stops the logger.
void set_mongo(bool enabled=true, string uri="")
Sets whether logging to MongoDB is enabled or disabled.
void register_event(event_type event)
Registers the startup/shutdown of the logger.
Logger(const Logger &)=delete
string truncated_message(const string &message)
Truncates the message to the maximum length.
void log(tuple< string, string > *message=nullptr)
Logs the given message to the MongoDB instance and to the log file.
static bool has_mongo_support() noexcept
Reports whether this build can log to MongoDB.
void open_db()
Starts the logger.
void set_file(string filename, bool array=false)
Sets the file name for logging.
Logger(std::string name, std::string settings_path)
Constructs a Logger object with the specified name and settings path.
Logger & operator=(const Logger &)=delete
Definition agent.hpp:67
nlohmann::json json