LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
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 #include <syscall.h>
38 
39 #include <log4cxx/helpers/stringhelper.h>
40 #include <log4cxx/helpers/pool.h>
41 #include <log4cxx/spi/location/locationinfo.h>
42 #include <log4cxx/helpers/loglog.h>
43 
47 
48 #include "lsst/pex/exceptions.h"
49 
50 namespace pexExceptions = lsst::pex::exceptions;
51 
52 using namespace log4cxx;
53 using namespace log4cxx::helpers;
54 
55 namespace ctrlEvents = lsst::ctrl::events;
56 
57 namespace lsst {
58 namespace ctrl {
59 namespace events {
60 
61 LSST_IMPLEMENT_LOG4CXX_OBJECT(EventAppender)
62 
64  _transmitter = NULL;
67 }
68 
71 EventAppender::~EventAppender() {
72  if (_transmitter != NULL)
73  delete _transmitter;
74  _transmitter = NULL;
75 }
76 
77 void EventAppender::setOption(const LogString& option, const LogString& value) {
78  if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("BROKER"), LOG4CXX_STR("broker"))) {
79  _broker = value;
80  } else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("PORT"), LOG4CXX_STR("port"))) {
81  _port = atoi(value.c_str());
82  } else if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("TOPIC"), LOG4CXX_STR("topic"))) {
83  _topic = value;
84  } else {
85  AppenderSkeleton::setOption(option, value);
86  }
87 }
88 
89 void EventAppender::activateOptions(Pool&)
90 {
91  if (_broker.empty()) {
92  LogLog::error((LogString) LOG4CXX_STR("Appender requires broker option to be specified"));
93  return;
94  }
95  try {
96  _transmitter = new EventTransmitter(_broker, _topic, _port);
97  } catch (pexExceptions::RuntimeError& rte) {
98  std::ostringstream msg;
99  msg << "Couldn't reach broker " << _broker << " at port " << _port;
100  LogLog::error((LogString) LOG4CXX_STR(msg.str()));
101  _transmitter = NULL;
102  }
103 }
104 
105 
106 EventTransmitter* EventAppender::getTransmitter() {
107  if (_transmitter == NULL) {
108  try {
109  _transmitter = new EventTransmitter(_broker, _topic, _port);
110  } catch (Exception& e) {
111  }
112  }
113  return _transmitter;
114 }
115 
116 
117 void EventAppender::append(const spi::LoggingEventPtr& event, helpers::Pool& p) {
118  PropertySet::Ptr psp(new PropertySet);
119 
120  psp->set("logger", event->getLoggerName());
121  psp->set("message", event->getMessage());
122  psp->set("timestamp", event->getTimeStamp());
123  psp->set("threadname", event->getThreadName());
124  psp->set("level", event->getLevel()->toInt());
125 
126  spi::LocationInfo location = event->getLocationInformation();
127 
128  PropertySet::Ptr loc(new PropertySet);
129  loc->set("filename", location.getFileName());
130  loc->set("classname", location.getClassName());
131  loc->set("methodname", location.getMethodName());
132  loc->set("linenumber", location.getLineNumber());
133 
134  psp->set("location", loc);
135 
137 
138  EventTransmitter *transmitter = getTransmitter();
139 
140  if (_transmitter == NULL)
141  return;
142 
143  try {
144  transmitter->publishEvent(e);
145  } catch (Exception& e) {
146  LogLog::error((LogString) LOG4CXX_STR("event transmitter failed"));
147  delete _transmitter;
148  _transmitter = NULL;
149  }
150 }
151 
152 void EventAppender::close() {
153  if (this->closed)
154  return;
155  this->closed = true;
156 }
157 
158 }}}
defines the EventAppender class
#define LSST_IMPLEMENT_LOG4CXX_OBJECT(object)
Definition: EventAppender.h:52
static const int DEFAULTHOSTPORT
Definition: EventBroker.h:47
void publishEvent(Event &event)
Publish an Event to this object&#39;s topic.
def error
Definition: log.py:108
Transmit events to the event bus.
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
boost::shared_ptr< PropertySet > Ptr
Definition: PropertySet.h:90
static const std::string LOGGING_TOPIC
Definition: LogEvent.h:79
defines the EventSystem class
Include files required for standard LSST Exception handling.