//# TableTrace.cc: Class with static functions for tracing column IO //# Copyright (C) 2014 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id: BaseColumn.cc 21130 2011-10-18 07:39:05Z gervandiepen $ #include #include #include #include #include #include #include #include #include namespace casacore { //# NAMESPACE CASACORE - BEGIN // Define the statics. std::once_flag TableTrace::theirCallOnceFlag; std::mutex TableTrace::theirMutex; std::ofstream TableTrace::theirTraceFile; std::ostream* TableTrace::theirStream = 0; int TableTrace::theirDoTrace = 0; int TableTrace::theirOper = 0; int TableTrace::theirColType = 0; std::vector TableTrace::theirColumns; std::vector TableTrace::theirTables; int TableTrace::traceTable (const String& tableName, char oper) { // Open trace file if not done yet. std::call_once(theirCallOnceFlag, initTracing); int tabid = -1; if (theirDoTrace > 0) { std::lock_guard locker(theirMutex); // Table should not be found, but who knows ... tabid = findTable (tableName); int id = tabid; if (tabid < 0) { // Find a free table entry. If none, append. tabid = findTable (String()); if (tabid < 0) { tabid = theirTables.size(); theirTables.push_back (tableName); } else { theirTables[tabid] = tableName; } } writeTraceFirst (tabid, tableName, oper); if (id >= 0) { *theirStream << "**ERROR** table already in use"; } *theirStream << endl; } return tabid; } void TableTrace::traceClose (const String& tableName) { std::call_once(theirCallOnceFlag, initTracing); if (theirDoTrace > 0) { std::lock_guard locker(theirMutex); int tabid = findTable (tableName); writeTraceFirst (tabid, tableName, 'c'); if (tabid < 0) { *theirStream << "**ERROR** unknown table"; } else { // Free entry. theirTables[tabid] = String(); } *theirStream << endl; } } void TableTrace::traceFile (int tabid, const String& oper) { std::call_once(theirCallOnceFlag, initTracing); if (theirDoTrace > 0) { writeTraceFirst (tabid, '*'+oper+'*', 't'); *theirStream << endl; } } void TableTrace::traceRefTable (const String& parentName, char oper) { std::call_once(theirCallOnceFlag, initTracing); if (theirDoTrace > 1) { int tabid = findTable (parentName); writeTraceFirst (tabid, "*reftable*", oper); *theirStream << endl; } } int TableTrace::traceColumn (const ColumnDesc& cd) { std::call_once(theirCallOnceFlag, initTracing); int traceCol = 0; if (theirOper > 0) { // First test if all scalar, array, or record columns are traced. if ((cd.isScalar() && (theirColType&SCALAR) != 0) || (cd.isArray() && (theirColType&ARRAY) != 0) || ((theirColType&RECORD) != 0)) { traceCol = theirOper; } else { // Otherwise see if this column is traced. for (size_t i=0; i::find (fname, "table.trace.filename", ""); if (! fname.empty()) { if (fname == "stdout") { theirStream = &std::cout; } else if (fname == "stderr") { theirStream = &std::cerr; } else { String expName = Path(fname).expandedName(); theirTraceFile.open (fname.c_str()); if (! theirTraceFile) { throw TableError ("Could not open table column trace file " + fname); } theirStream = &theirTraceFile; } *theirStream << "# time oper tabid name row(s) shape blc/trc/inc" << endl; *theirStream << "# Note: shapes are in Fortran order" << endl << endl; theirDoTrace = 1; initOper(); initColumn(); } } void TableTrace::initOper() { // Get the operations to trace. String operStr; AipsrcValue::find (operStr, "table.trace.operation", ""); if (! operStr.empty()) { operStr.downcase(); for (uInt i=0; i::find (typeStr, "table.trace.columntype", ""); String colStr; AipsrcValue::find (colStr, "table.trace.column", ""); if (! typeStr.empty()) { typeStr.downcase(); for (uInt i=0; i cols = stringToVector (colStr, ','); theirColumns.reserve (cols.size()); for (uInt i=0; i