log_node.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <aws/core/utils/logging/AWSLogging.h>
17 #include <aws/core/utils/logging/LogMacros.h>
26 #include <ros/ros.h>
27 #include <rosgraph_msgs/Log.h>
28 #include <std_srvs/Trigger.h>
29 #include <std_srvs/Empty.h>
30 
31 
32 namespace Aws {
33 namespace CloudWatchLogs {
34 namespace Utils {
35 
36 LogNode::LogNode(const Options & options)
37  : min_log_severity_(options.min_log_severity),
38  ignore_nodes_(options.ignore_nodes),
39  publish_topic_names_(options.publish_topic_names) {}
40 
41 LogNode::LogNode(int8_t min_log_severity, std::unordered_set<std::string> ignore_nodes)
42  : LogNode(Options(min_log_severity, true, std::move(ignore_nodes))) {}
43 
44 LogNode::~LogNode() { this->log_service_ = nullptr; }
45 
46 void LogNode::Initialize(const std::string & log_group, const std::string & log_stream,
47  const Aws::Client::ClientConfiguration & config, Aws::SDKOptions & sdk_options,
48  const Aws::CloudWatchLogs::CloudWatchOptions & cloudwatch_options,
49  const std::shared_ptr<LogServiceFactory>& factory)
50 {
51  this->log_service_ = factory->CreateLogService(log_group, log_stream, config, sdk_options, cloudwatch_options);
52 }
53 
54 bool LogNode::checkIfOnline(std_srvs::Trigger::Request& request, std_srvs::Trigger::Response& response) {
55 
56  AWS_LOGSTREAM_DEBUG(__func__, "received request " << request);
57 
58  if (!this->log_service_) {
59  response.success = false;
60  response.message = "The LogService is not initialized";
61  return true;
62  }
63 
64  response.success = this->log_service_->isConnected();
65  response.message = response.success ? "The LogService is connected" : "The LogService is not connected";
66 
67  return true;
68 }
69 
71  bool is_started = true;
72  if (this->log_service_) {
73  is_started &= this->log_service_->start();
74  }
75  is_started &= Service::start();
76  return is_started;
77 }
78 
80  bool is_shutdown = Service::shutdown();
81  if (this->log_service_) {
82  is_shutdown &= this->log_service_->shutdown();
83  }
84  return is_shutdown;
85 }
86 
87 void LogNode::RecordLogs(const rosgraph_msgs::Log::ConstPtr & log_msg)
88 {
89  if (0 == this->ignore_nodes_.count(log_msg->name)) {
90  if (nullptr == this->log_service_) {
91  AWS_LOG_ERROR(__func__,
92  "Cannot publish CloudWatch logs with NULL CloudWatch LogManager instance.");
93  return;
94  }
95  if (ShouldSendToCloudWatchLogs(log_msg->level)) {
96  auto message = FormatLogs(log_msg);
97  this->log_service_->batchData(message);
98  }
99  }
100 }
101 
103  this->log_service_->publishBatchedData();
104 }
105 
106 bool LogNode::ShouldSendToCloudWatchLogs(const int8_t log_severity_level)
107 {
108  return log_severity_level >= this->min_log_severity_;
109 }
110 
111 const std::string LogNode::FormatLogs(const rosgraph_msgs::Log::ConstPtr & log_msg)
112 {
113  std::stringstream ss;
114  ss << log_msg->header.stamp << " ";
115 
116  switch (log_msg->level) {
117  case rosgraph_msgs::Log::FATAL:
118  ss << "FATAL ";
119  break;
120  case rosgraph_msgs::Log::ERROR:
121  ss << "ERROR ";
122  break;
123  case rosgraph_msgs::Log::WARN:
124  ss << "WARN ";
125  break;
126  case rosgraph_msgs::Log::DEBUG:
127  ss << "DEBUG ";
128  break;
129  case rosgraph_msgs::Log::INFO:
130  ss << "INFO ";
131  break;
132  default:
133  ss << log_msg->level << " ";
134  }
135  ss << "[node name: " << log_msg->name << "] ";
136 
137  if (publish_topic_names_) {
138  ss << "[topics: ";
139  auto it = log_msg->topics.begin();
140  auto end = log_msg->topics.end();
141  for (; it != end; ++it) {
142  const std::string & topic = *it;
143  if (it != log_msg->topics.begin()) {
144  ss << ", ";
145  }
146  ss << topic;
147  }
148  ss << "] ";
149  }
150 
151  ss << log_msg->msg << "\n";
152 
153  return ss.str();
154 }
155 
156 } // namespace Utils
157 } // namespace CloudWatchLogs
158 } // namespace Aws
~LogNode() override
Tears down a AWSCloudWatchLogNode object.
Definition: log_node.cpp:44
LogNode(const Options &options)
Definition: log_node.cpp:36
bool checkIfOnline(std_srvs::Trigger::Request &request, std_srvs::Trigger::Response &response)
Definition: log_node.cpp:54
const std::string FormatLogs(const rosgraph_msgs::Log::ConstPtr &log_msg)
Definition: log_node.cpp:111
std::unordered_set< std::string > ignore_nodes_
Definition: log_node.h:122
void TriggerLogPublisher(const ros::TimerEvent &)
Trigger the log manager to call its Service function to publish logs to cloudwatch periodically...
Definition: log_node.cpp:102
void RecordLogs(const rosgraph_msgs::Log::ConstPtr &log_msg)
Emits RecordLog using the log manager.
Definition: log_node.cpp:87
bool ShouldSendToCloudWatchLogs(const int8_t log_severity_level)
Definition: log_node.cpp:106
std::shared_ptr< Aws::CloudWatchLogs::LogService > log_service_
Definition: log_node.h:120
virtual bool shutdown()
void Initialize(const std::string &log_group, const std::string &log_stream, const Aws::Client::ClientConfiguration &config, Aws::SDKOptions &sdk_options, const Aws::CloudWatchLogs::CloudWatchOptions &cloudwatch_options, const std::shared_ptr< LogServiceFactory > &log_service_factory=std::make_shared< LogServiceFactory >())
Reads creds, region, and SDK option to configure log manager.
Definition: log_node.cpp:46
virtual bool start()


cloudwatch_logger
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:55:46