LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
LogUtils.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
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 <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
33 #include <cstring>
34 #include <sstream>
35 
37 
38 #include "lsst/ctrl/events.h"
41 
42 using namespace std;
43 
48 
49 namespace lsst {
50 namespace pex {
51 namespace harness {
52 
56 LogUtils::LogUtils()
57  : pipelineLog(Log::getDefaultLog(),"harness"), _evbHost(""), outlog(0)
58 { }
59 
63  delete outlog;
64 }
65 
70 void LogUtils::initializeLogger(bool isLocalLogMode,
71  const std::string& name,
72  const std::string& runId,
73  const std::string& logdir,
74  const std::string& workerid,
75  int resourceUsageFlags
76  ) {
77  std::stringstream logfileBuffer;
78  std::string logfile;
79 
80  if (logdir.length() > 0) {
81  logfileBuffer << logdir;
82  logfileBuffer << "/";
83  }
84 
85  logfileBuffer << "Pipeline.log";
86  logfileBuffer >> logfile;
87 
88  if(isLocalLogMode) {
89  /* Make output file stream */
90  outlog = new ofstream(logfile.c_str());
91  }
92 
93  /* Create LSSTLogging transmitter here. (Moved from setupHarnessLogging in TracingLog.cc
94  Need to re-check this for MPI SLices. */
95  if (_evbHost.length() > 0) {
97  /* eventSystem.createTransmitter(_evbHost, EventLog::LOGGING_TOPIC); */
98  eventSystem.createTransmitter(_evbHost, "logging");
99  }
100 
101  boost::shared_ptr<BlockTimingLog>
102  lp(setupHarnessLogging(std::string(runId), -1, _evbHost, name, workerid,
103  outlog, "harness.pipeline",resourceUsageFlags));
104 
105 
106  pipelineLog = *lp;
107 
109  "Pipeline Logger initialized for pid=%d", getpid());
110  if (outlog)
111  pipelineLog.format(Log::INFO, "replicating messages to %s", logfile.c_str());
112 }
113 
117 void LogUtils::initializeSliceLogger(bool isLocalLogMode,
118  const std::string& name,
119  const std::string& runId,
120  const std::string& logdir,
121  const int rank,
122  const std::string& workerid,
123  int resourceUsageFlags
124  )
125 {
126  std::string logfile;
127  if(isLocalLogMode) {
128  /* Make a log file name coded to the rank */
129  std::stringstream logfileBuffer;
130 
131  if (logdir.length() > 0) {
132  logfileBuffer << logdir;
133  logfileBuffer << "/";
134  }
135 
136  logfileBuffer << "Slice";
137  logfileBuffer << rank;
138  logfileBuffer << ".log";
139 
140  logfileBuffer >> logfile;
141 
142  /* Make output file stream */
143  outlog = new ofstream(logfile.c_str());
144  }
145 
146 
147  boost::shared_ptr<BlockTimingLog>
148  lp(setupHarnessLogging(std::string(runId), rank, _evbHost,
149  name, workerid, outlog, "harness.slice",
150  resourceUsageFlags));
151  pipelineLog = *lp;
152 
153  pipelineLog.format(Log::INFO, "Slice Logger initialized for pid=%d", getpid());
154 
155  if (outlog)
157  "replicating messages to %s", logfile.c_str());
158 }
159 
167 
168 BlockTimingLog *setupHarnessLogging(const std::string& runId, int sliceId,
169  const std::string& eventBrokerHost,
170  const std::string& pipename,
171  const std::string& workerid,
172  std::ostream *messageStrm,
173  const std::string& logname,
174  int resourceUsageFlags)
175 {
176  if (eventBrokerHost.length() > 0) {
177  /* Move this to LogUtils.cc and only call for the Pipeline
178  Re-examine this for MPI Slices
179  EventSystem& eventSystem = EventSystem::getDefaultEventSystem();
180  eventSystem.createTransmitter(eventBrokerHost, "LSSTLogging");
181  */
182  EventLog::createDefaultLog(runId, sliceId);
183  }
184  Log& root = Log::getDefaultLog();
185  root.addPreambleProperty("PIPELINE", pipename);
186  root.addPreambleProperty("workerid", workerid);
187 
188  if (messageStrm != 0) {
189  boost::shared_ptr<LogFormatter> frmtr(new IndentedFormatter(true));
190  boost::shared_ptr<LogDestination>
191  dest(new LogDestination(messageStrm, frmtr, Log::DEBUG));
192  root.addDestination(dest);
193  }
194 
195  BlockTimingLog *out = 0;
196  try {
197  out = new BlockTimingLog(root, logname);
198  out->setUsageFlags(resourceUsageFlags);
199  out->format(Log::INFO,
200  "Harness Logger initialized with message threshold = %i",
201  out->getThreshold());
202  return out;
203  }
204  catch (...) {
205  delete out;
206  throw;
207  }
208 }
209 
210 }
211 }
212 }
static void createDefaultLog(const std::string runId, int slideId, const std::string hostId="", int threshold=Log::INFO)
creates a default log
Definition: EventLog.cc:131
void createTransmitter(const pexPolicy::Policy &policy)
create an EventTransmitter to send messages to the message broker
Definition: EventSystem.cc:115
defines the EventLog class
int getThreshold() const
Definition: Log.h:281
LogRec Rec
Definition: Log.h:838
an encapsulation of a logging stream that will filter messages based on their volume (importance) lev...
LogUtils class creates and manages the logger of a Pipeline and Slice.
void format(int importance, const char *fmt,...)
static EventSystem & getDefaultEventSystem()
return the default EventSystem object, which can access all previously created Transmitters and recei...
Definition: EventSystem.cc:80
a place to record messages and descriptions of the state of processing.
Definition: Log.h:154
static Log & getDefaultLog()
void initializeSliceLogger(bool isLocalLogMode, const std::string &name, const std::string &runId, const std::string &logdir, const int rank, const std::string &workerid, int resourceUsageFlags=0)
Definition: LogUtils.cc:117
provides access to primary application interface classes for sending and receiving events...
void addDestination(std::ostream &destination, int threshold)
static const int INFO
Definition: Log.h:171
void initializeLogger(bool isLocalLogMode, const std::string &name, const std::string &runId, const std::string &logdir, const std::string &workerid, int resourceUsageFlags=0)
Definition: LogUtils.cc:70
lsst::pex::logging::BlockTimingLog pipelineLog
Definition: LogUtils.h:92
std::ofstream * outlog
Definition: LogUtils.h:94
BlockTimingLog * setupHarnessLogging(const std::string &runId, int sliceId, const std::string &eventBrokerHost, const std::string &pipename, const std::string &workerid, std::ostream *messageStrm, const std::string &logname, int resourceUsageFlags)
create and configure logging for a harness application
Definition: LogUtils.cc:168
static const int DEBUG
Definition: Log.h:163
void addPreambleProperty(const std::string &name, const T &val)
Definition: Log.h:694
Coordinate publishing and receiving events.
Definition: EventSystem.h:65
a Log for transmitting log messages as events.
Definition: EventLog.h:58
defines the EventSystem class
an abstract class for formatting log records into a text stream.
Definition: LogFormatter.h:73