ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
ocpp_logging.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_COMMON_LOGGING_HPP
4#define OCPP_COMMON_LOGGING_HPP
5
6#include <filesystem>
7#include <fstream>
8#include <iostream>
9#include <map>
10#include <memory>
11#include <mutex>
12#include <ocpp/common/types.hpp>
13#include <thread>
14
15namespace ocpp {
16
19 std::string message_type;
20 std::string message;
21};
22
27 uint64_t
30
35 }
36};
37
38enum class LogRotationStatus {
39 NotRotated,
40 Rotated,
41 RotatedWithDeletion
42};
43
46private:
47 bool log_messages;
48 std::string message_log_path; // FIXME: use fs::path here
49 std::string output_file_name;
50 bool log_to_console;
51 bool detailed_log_to_console;
52 bool log_to_file;
53 bool log_to_html;
54 bool log_security;
55 bool session_logging;
56 std::filesystem::path log_file;
57 std::ofstream log_os;
58 std::filesystem::path html_log_file;
59 std::ofstream html_log_os;
60 std::filesystem::path security_log_file;
61 std::ofstream security_log_os;
62 std::mutex output_file_mutex;
63 std::function<void(const std::string& message, MessageDirection direction)> message_callback;
64 std::function<void(LogRotationStatus status)> status_callback;
65 std::map<std::string, std::string> lookup_map;
66 std::recursive_mutex session_id_logging_mutex;
67 std::map<std::string, std::shared_ptr<MessageLogging>> session_id_logging;
68 bool rotate_logs;
69 bool date_suffix;
70 std::string logfile_basename;
71 uint64_t maximum_file_size_bytes;
72 uint64_t maximum_file_count;
73
75 void initialize();
76
78 void log_output(unsigned int typ, const std::string& message_type, const std::string& json_str);
79
81 std::string html_encode(const std::string& msg);
82
84 FormattedMessageWithType format_message(const std::string& message_type, const std::string& json_str);
85
87 void open_html_tags(std::ofstream& os);
88
90 void close_html_tags(std::ofstream& os);
91
93 std::string get_datetime_string();
94
96 std::uintmax_t file_size(const std::filesystem::path& path);
97
100 LogRotationStatus rotate_log(const std::string& file_basename);
101
103 LogRotationStatus rotate_log_if_needed(const std::filesystem::path& path, std::ofstream& os);
104
107 LogRotationStatus rotate_log_if_needed(const std::filesystem::path& path, std::ofstream& os,
108 std::function<void(std::ofstream& os)> before_close_of_os,
109 std::function<void(std::ofstream& os)> after_open_of_os);
110
111public:
113 explicit MessageLogging(
114 bool log_messages, const std::string& message_log_path, const std::string& output_file_name,
115 bool log_to_console, bool detailed_log_to_console, bool log_to_file, bool log_to_html, bool log_security,
116 bool session_logging,
117 std::function<void(const std::string& message, MessageDirection direction)> message_callback);
118
120 explicit MessageLogging(
121 bool log_messages, const std::string& message_log_path, const std::string& output_file_name,
122 bool log_to_console, bool detailed_log_to_console, bool log_to_file, bool log_to_html, bool log_security,
123 bool session_logging,
124 std::function<void(const std::string& message, MessageDirection direction)> message_callback,
125 LogRotationConfig log_rotation_config, std::function<void(LogRotationStatus status)> status_callback);
127
129 void charge_point(const std::string& message_type, const std::string& json_str);
130
132 void central_system(const std::string& message_type, const std::string& json_str);
133
135 void sys(const std::string& msg);
136
138 void security(const std::string& msg);
139
141 void start_session_logging(const std::string& session_id, const std::string& log_path);
142
144 void stop_session_logging(const std::string& session_id);
145
147 std::string get_message_log_path();
148
151};
152
153} // namespace ocpp
154#endif // OCPP_COMMON_LOGGING_HPP
contains a ocpp message logging abstraction
Definition: ocpp_logging.hpp:45
void central_system(const std::string &message_type, const std::string &json_str)
Log a message originating from the central system.
Definition: ocpp_logging.cpp:290
MessageLogging(bool log_messages, const std::string &message_log_path, const std::string &output_file_name, bool log_to_console, bool detailed_log_to_console, bool log_to_file, bool log_to_html, bool log_security, bool session_logging, std::function< void(const std::string &message, MessageDirection direction)> message_callback)
Creates a new MessageLogging object with the provided configuration.
Definition: ocpp_logging.cpp:17
bool session_logging_active()
Definition: ocpp_logging.cpp:428
void security(const std::string &msg)
Log a security message.
Definition: ocpp_logging.cpp:314
void stop_session_logging(const std::string &session_id)
Stop session logging.
Definition: ocpp_logging.cpp:413
std::string get_message_log_path()
Definition: ocpp_logging.cpp:424
void start_session_logging(const std::string &session_id, const std::string &log_path)
Start session logging (without log rotation)
Definition: ocpp_logging.cpp:407
void sys(const std::string &msg)
Log a system message.
Definition: ocpp_logging.cpp:304
void charge_point(const std::string &message_type, const std::string &json_str)
Log a message originating from the charge point.
Definition: ocpp_logging.cpp:276
Container for a formatted OCPP message with a message type.
Definition: ocpp_logging.hpp:18
std::string message
The message content.
Definition: ocpp_logging.hpp:20
std::string message_type
The message type.
Definition: ocpp_logging.hpp:19
Configuration for log rotation.
Definition: ocpp_logging.hpp:24
uint64_t maximum_file_size_bytes
The maximum size of the log file in bytes after which the file will be rotated.
Definition: ocpp_logging.hpp:28
bool date_suffix
Definition: ocpp_logging.hpp:25
uint64_t maximum_file_count
The maximum number of log files to keep in rotation.
Definition: ocpp_logging.hpp:29