LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
EventAppender.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008-2014 AURA/LSST.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <https://www.lsstcorp.org/LegalNotices/>.
23  */
24 
30 #include <iomanip>
31 #include <iostream>
32 #include <sstream>
33 #include <stdexcept>
34 #include <limits>
35 #include <cstring>
36 #include <unistd.h>
37 
38 #include <log4cxx/helpers/stringhelper.h>
39 #include <log4cxx/helpers/pool.h>
40 #include <log4cxx/spi/location/locationinfo.h>
41 #include <log4cxx/helpers/loglog.h>
42 
46 
47 #include "lsst/pex/exceptions.h"
48 
49 namespace pexExceptions = lsst::pex::exceptions;
50 
51 using namespace log4cxx;
52 using namespace log4cxx::helpers;
53 
54 namespace ctrlEvents = lsst::ctrl::events;
55 
56 namespace lsst {
57 namespace ctrl {
58 namespace events {
59 
60 LSST_IMPLEMENT_LOG4CXX_OBJECT(EventAppender)
61 
63  _transmitter = NULL;
66 }
67 
70 EventAppender::~EventAppender() {
71  if (_transmitter != NULL)
72  delete _transmitter;
73  _transmitter = NULL;
74 }
75 
76 void EventAppender::setOption(const LogString& option, const LogString& value) {
77  if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("BROKER"), LOG4CXX_STR("broker"))) {
78  _broker = value;
79  } else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PORT"), LOG4CXX_STR("port"))) {
80  _port = atoi(value.c_str());
81  } else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("TOPIC"), LOG4CXX_STR("topic"))) {
82  _topic = value;
83  } else {
84  AppenderSkeleton::setOption(option, value);
85  }
86 }
87 
88 void EventAppender::activateOptions(Pool&)
89 {
90  if (_broker.empty()) {
91  LogLog::error((LogString) LOG4CXX_STR("Appender requires broker option to be specified"));
92  return;
93  }
94  try {
95  _transmitter = new EventTransmitter(_broker, _topic, _port);
96  } catch (pexExceptions::RuntimeError& rte) {
97  std::ostringstream msg;
98  msg << "Couldn't reach broker " << _broker << " at port " << _port;
99  LogLog::error((LogString) LOG4CXX_STR(msg.str()));
100  _transmitter = NULL;
101  }
102 }
103 
104 
105 EventTransmitter* EventAppender::getTransmitter() {
106  if (_transmitter == NULL) {
107  try {
108  _transmitter = new EventTransmitter(_broker, _topic, _port);
109  } catch (Exception& e) {
110  }
111  }
112  return _transmitter;
113 }
114 
115 
116 void EventAppender::append(const spi::LoggingEventPtr& event, helpers::Pool& p) {
117  PropertySet::Ptr psp(new PropertySet);
118 
119  psp->set("logger", event->getLoggerName());
120  psp->set("message", event->getMessage());
121  psp->set("timestamp", event->getTimeStamp());
122  psp->set("threadname", event->getThreadName());
123  psp->set("level", event->getLevel()->toInt());
124 
125  spi::LocationInfo location = event->getLocationInformation();
126 
127  PropertySet::Ptr loc(new PropertySet);
128  loc->set("filename", location.getFileName());
129  loc->set("classname", location.getClassName());
130  loc->set("methodname", location.getMethodName());
131  loc->set("linenumber", location.getLineNumber());
132 
133  psp->set("location", loc);
134 
136 
137  EventTransmitter *transmitter = getTransmitter();
138 
139  if (_transmitter == NULL)
140  return;
141 
142  try {
143  transmitter->publishEvent(e);
144  } catch (Exception& e) {
145  LogLog::error((LogString) LOG4CXX_STR("event transmitter failed"));
146  delete _transmitter;
147  _transmitter = NULL;
148  }
149 }
150 
151 void EventAppender::close() {
152  if (this->closed)
153  return;
154  this->closed = true;
155 }
156 
157 }}}
static const std::string LOGGING_TOPIC
Definition: LogEvent.h:79
defines the EventAppender class
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
#define LSST_IMPLEMENT_LOG4CXX_OBJECT(object)
Definition: EventAppender.h:52
def error
Definition: log.py:108
Transmit events to the event bus.
static const int DEFAULTHOSTPORT
Definition: EventBroker.h:47
void publishEvent(Event &event)
Publish an Event to this object&#39;s topic.
information about the Event Broker
Representation of an LSST Event.
Definition: Event.h:62
Class for storing generic metadata.
Definition: PropertySet.h:82
log4cxx appender class which sends logging messages out on the event stream
Definition: EventAppender.h:75
defines the EventSystem class
Include files required for standard LSST Exception handling.