LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
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 if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("RUNID"), LOG4CXX_STR("runid"))) {
84  _runid = value;
85  } else {
86  AppenderSkeleton::setOption(option, value);
87  }
88 }
89 
90 void EventAppender::activateOptions(Pool&)
91 {
92  if (_broker.empty()) {
93  LogLog::error((LogString) LOG4CXX_STR("Appender requires broker option to be specified"));
94  return;
95  }
96  try {
97  _transmitter = new EventTransmitter(_broker, _topic, _port);
98  } catch (pexExceptions::RuntimeError& rte) {
99  std::ostringstream msg;
100  msg << "Couldn't reach broker " << _broker << " at port " << _port;
101  LogLog::error((LogString) LOG4CXX_STR(msg.str()));
102  _transmitter = NULL;
103  }
104 }
105 
106 
107 EventTransmitter* EventAppender::getTransmitter() {
108  if (_transmitter == NULL) {
109  try {
110  _transmitter = new EventTransmitter(_broker, _topic, _port);
111  } catch (Exception& e) {
112  }
113  }
114  return _transmitter;
115 }
116 
117 
118 void EventAppender::append(const spi::LoggingEventPtr& event, helpers::Pool& p) {
119  PropertySet::Ptr psp(new PropertySet);
120 
121  psp->set("logger", event->getLoggerName());
122  psp->set("message", event->getMessage());
123  psp->set("timestamp", event->getTimeStamp());
124  psp->set("threadname", event->getThreadName());
125  psp->set("level", event->getLevel()->toInt());
126 
127  spi::LocationInfo location = event->getLocationInformation();
128 
129  PropertySet::Ptr loc(new PropertySet);
130  loc->set("filename", location.getFileName());
131  loc->set("classname", location.getClassName());
132  loc->set("methodname", location.getMethodName());
133  loc->set("linenumber", location.getLineNumber());
134 
135  psp->set("location", loc);
136 
138  if (_runid.empty()) {
139  e = ctrlEvents::Event(*psp);
140  } else {
141  e = ctrlEvents::Event(_runid, *psp);
142  }
143 
144  EventTransmitter *transmitter = getTransmitter();
145 
146  if (_transmitter == NULL)
147  return;
148 
149  try {
150  transmitter->publishEvent(e);
151  } catch (Exception& e) {
152  LogLog::error((LogString) LOG4CXX_STR("event transmitter failed"));
153  delete _transmitter;
154  _transmitter = NULL;
155  }
156 }
157 
158 void EventAppender::close() {
159  if (this->closed)
160  return;
161  this->closed = true;
162 }
163 
164 }}}
static const std::string LOGGING_TOPIC
Definition: LogEvent.h:79
defines the EventAppender class
Include files required for standard LSST Exception handling.
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