LSSTApplications  11.0-13-gbb96280,12.1.rc1,12.1.rc1+1,12.1.rc1+2,12.1.rc1+5,12.1.rc1+8,12.1.rc1-1-g06d7636+1,12.1.rc1-1-g253890b+5,12.1.rc1-1-g3d31b68+7,12.1.rc1-1-g3db6b75+1,12.1.rc1-1-g5c1385a+3,12.1.rc1-1-g83b2247,12.1.rc1-1-g90cb4cf+6,12.1.rc1-1-g91da24b+3,12.1.rc1-2-g3521f8a,12.1.rc1-2-g39433dd+4,12.1.rc1-2-g486411b+2,12.1.rc1-2-g4c2be76,12.1.rc1-2-gc9c0491,12.1.rc1-2-gda2cd4f+6,12.1.rc1-3-g3391c73+2,12.1.rc1-3-g8c1bd6c+1,12.1.rc1-3-gcf4b6cb+2,12.1.rc1-4-g057223e+1,12.1.rc1-4-g19ed13b+2,12.1.rc1-4-g30492a7
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 
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 if (StringHelper::equalsIgnoreCase(option, LOG4CXX_STR("RUNID"), LOG4CXX_STR("runid"))) {
85  _runid = value;
86  } else {
87  AppenderSkeleton::setOption(option, value);
88  }
89 }
90 
91 void EventAppender::activateOptions(Pool&)
92 {
93  if (_broker.empty()) {
94  LogLog::error((LogString) LOG4CXX_STR("Appender requires broker option to be specified"));
95  return;
96  }
97  try {
98  _transmitter = new EventTransmitter(_broker, _topic, _port);
99  } catch (pexExceptions::RuntimeError& rte) {
100  std::ostringstream msg;
101  msg << "Couldn't reach broker " << _broker << " at port " << _port;
102  LogLog::error((LogString) LOG4CXX_STR(msg.str()));
103  _transmitter = NULL;
104  }
105 }
106 
107 
108 EventTransmitter* EventAppender::getTransmitter() {
109  if (_transmitter == NULL) {
110  try {
111  _transmitter = new EventTransmitter(_broker, _topic, _port);
112  } catch (Exception& e) {
113  }
114  }
115  return _transmitter;
116 }
117 
118 
119 void EventAppender::append(const spi::LoggingEventPtr& event, helpers::Pool& p) {
120  PTR(PropertySet) logProp (new PropertySet);
121  logProp->set(LogEvent::TYPE, EventTypes::LOG);
122 
123  logProp->set(LogEvent::LOGGER, event->getLoggerName());
124  logProp->set(LogEvent::LEVEL, event->getLevel()->toInt());
125 
126  logProp->set(LogEvent::MESSAGE, event->getMessage());
127  logProp->set(LogEvent::TIMESTAMP, event->getTimeStamp());
128  logProp->set(LogEvent::THREADNAME, event->getThreadName());
129 
130  spi::LocationInfo location = event->getLocationInformation();
131 
132  PTR(PropertySet) loc(new PropertySet);
133  loc->set(LogEvent::FILENAME, location.getFileName());
134  loc->set(LogEvent::CLASSNAME, location.getClassName());
135  loc->set(LogEvent::METHODNAME, location.getMethodName());
136  loc->set(LogEvent::LINENUMBER, location.getLineNumber());
137 
138  logProp->set(LogEvent::LOCATION, loc);
139 
140  PTR(LocationId) originatorId(new LocationId());
141 
142  ctrlEvents::LogEvent e = ctrlEvents::LogEvent(*originatorId, *logProp);
143  if (!_runid.empty())
144  e.setRunId(_runid);
145 
146  EventTransmitter *transmitter = getTransmitter();
147 
148  if (_transmitter == NULL)
149  return;
150 
151  try {
152  transmitter->publishEvent(e);
153  } catch (Exception& e) {
154  LogLog::error((LogString) LOG4CXX_STR("event transmitter failed"));
155  delete _transmitter;
156  _transmitter = NULL;
157  }
158 }
159 
160 void EventAppender::close() {
161  if (this->closed)
162  return;
163  this->closed = true;
164 }
165 
166 }}}
defines the LocationId class
static const std::string LOGGING_TOPIC
Definition: LogEvent.h:79
defines the EventAppender class
#define LSST_IMPLEMENT_LOG4CXX_OBJECT(object)
Definition: EventAppender.h:52
Representation of an LSST Event.
Definition: LogEvent.h:63
defines EventTypes
def error
Definition: log.py:103
void setRunId(std::string runid)
set the RunId for this Event
Definition: Event.cc:322
Transmit events to the event bus.
defines the LogEvent class
static const int DEFAULTHOSTPORT
Definition: EventBroker.h:47
Class for storing generic metadata.
Definition: PropertySet.h:82
#define LOG(logger, level, message...)
Definition: Log.h:249
#define PTR(...)
Definition: base.h:41
log4cxx appender class which sends logging messages out on the event stream
Definition: EventAppender.h:75
Represent process that created an event.
Definition: LocationId.h:52
void publishEvent(Event &event)
Publish an Event to this object&#39;s topic.
Definition: Transmitter.cc:116
Include files required for standard LSST Exception handling.