From: <sv...@ww...> - 2004-10-02 17:11:35
|
Author: mkrose Date: 2004-10-02 10:06:06 -0700 (Sat, 02 Oct 2004) New Revision: 1270 Added: trunk/CSP/SimData/Source/TaggedRecordRegistry.cpp trunk/CSP/SimData/Source/ThreadLog.cpp trunk/CSP/SimData/Source/Trace.cpp Log: Oops, forgot a few files! Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1270 Added: trunk/CSP/SimData/Source/TaggedRecordRegistry.cpp =================================================================== --- trunk/CSP/SimData/Source/TaggedRecordRegistry.cpp 2004-10-02 16:51:30 UTC (rev 1269) +++ trunk/CSP/SimData/Source/TaggedRecordRegistry.cpp 2004-10-02 17:06:06 UTC (rev 1270) @@ -0,0 +1,47 @@ +/* SimData: Data Infrastructure for Simulations + * Copyright (C) 2004 Mark Rose <mk...@us...> + * + * This file is part of SimData. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/** + * @file TaggedRecordRegistry.cpp + * @brief Classes for storing and accessing tagged record factories. + */ + + +#include <SimData/TaggedRecordRegistry.h> +#include <SimData/Log.h> + + +NAMESPACE_SIMDATA + + +void TaggedRecordRegistry::registerFactory(TaggedRecordFactoryBase *factory) { + assert(factory != 0); + assert(!hasFactory(factory->getName())); + SIMDATA_LOG(LOG_ALL, LOG_INFO, "Registering TaggedRecordFactory<" << factory->getName() << "> [" << factory->getId() << "]"); + TaggedRecord::Id id = factory->getId(); + HashT key(static_cast<uint32>(id), static_cast<uint32>(id>>32)); + _map[factory->getName()] = factory; + _id_map[key] = factory; +} + + +NAMESPACE_SIMDATA_END + Added: trunk/CSP/SimData/Source/ThreadLog.cpp =================================================================== --- trunk/CSP/SimData/Source/ThreadLog.cpp 2004-10-02 16:51:30 UTC (rev 1269) +++ trunk/CSP/SimData/Source/ThreadLog.cpp 2004-10-02 17:06:06 UTC (rev 1270) @@ -0,0 +1,128 @@ +/* SimData: Data Infrastructure for Simulations + * Copyright 2004 Mark Rose <mk...@us...> + * + * This file is part of SimData. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/** + * @file ThreadLog.cpp + * @brief Threadsafe logging classes. + **/ + +#ifndef SIMDATA_NOTHREADS + +// Log.h and ThreadLog.h are co-dependent; always use Log.h. +#include <SimData/Log.h> + +#include <iostream> + + +NAMESPACE_SIMDATA + + +/** A steam buffer class for thread-safe logging. + * + * StringBufs are used internally by ThreadLog to provide serialized + * access to a LogStream. + */ +class ThreadLog::StringBuf: public NonCopyable, public std::streambuf +{ + std::string m_buffer; + int m_category; + int m_priority; + const char *m_file; + int m_line; + LogStream &m_log; + + void init() { + m_buffer.reserve(1024); + } + + typedef ScopedLock<LogStream> ScopedLogLock; + +public: + + /** Default constructor, connects to the global simdata log. + */ + StringBuf(): m_category(LOG_ALL), m_priority(LOG_INFO), m_file(0), m_line(0), m_log(::simdata::log()) { + init(); + } + + /** Constructor, connects to the specified LogStream + */ + StringBuf(LogStream &base): m_category(LOG_ALL), m_priority(LOG_INFO), m_log(base) { + init(); + } + + /** Cache the log entry parameters. + */ + inline void setPrefix(int priority, int category, const char *file, int line) { + m_priority = priority; + m_category = category; + m_file = file; + m_line = line; + } + + /** Retrieve the associated LogStream. + */ + inline LogStream &getLogStream() const { return m_log; } + +protected: + + /** Overflow (cache characters internally) + */ + virtual int overflow(int c) { + m_buffer.push_back(static_cast<char>(c)); + return 1; + } + + /** Sync/flush the cached log entry to the underlying LogStream + */ + virtual int sync() { + ScopedLogLock lock(m_log); + std::ostream &entry = m_log.entry(m_priority, m_category, m_file, m_line); + entry << m_buffer; + entry.flush(); + m_buffer.clear(); + return 1; + } +}; + + +std::ostream & ThreadLog::entry(int priority, int category, const char *file, int line) { + m_stringbuf->setPrefix(priority, category, file, line); + return *m_logstream; +} + +ThreadLog::ThreadLog(): + m_stringbuf(new StringBuf()), + m_logstream(new std::ostream(m_stringbuf.get())), + m_log(m_stringbuf->getLogStream()) {} + +ThreadLog::ThreadLog(LogStream &base): + m_stringbuf(new StringBuf(base)), + m_logstream(new std::ostream(m_stringbuf.get())), + m_log(m_stringbuf->getLogStream()) {} + +ThreadLog::~ThreadLog() {} + +NAMESPACE_SIMDATA_END + + +#endif // SIMDATA_NOTHREADS + Added: trunk/CSP/SimData/Source/Trace.cpp =================================================================== --- trunk/CSP/SimData/Source/Trace.cpp 2004-10-02 16:51:30 UTC (rev 1269) +++ trunk/CSP/SimData/Source/Trace.cpp 2004-10-02 17:06:06 UTC (rev 1270) @@ -0,0 +1,80 @@ +/* SimData: Data Infrastructure for Simulations + * Copyright 2003, 2004 Mark Rose <mk...@us...> + * + * This file is part of SimData. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/** + * @file Trace.cpp + * @brief Automatic callstack traces reporting on error. + * + */ + +#include <SimData/Trace.h> +#include <iostream> + +NAMESPACE_SIMDATA + +std::ostream &TraceBase::log() { + if (_log) { + return _log->entry(LOG_ERROR, LOG_ALL); + } else { + return simdata::log().entry(LOG_ERROR, LOG_ALL); + } +} + +void TraceBase::error(int skip, bool segv) { + if (_traced) return; + _traced = true; + if (segv) { + log() << "FATAL ERROR: segmentation fault." << std::endl; + } + _preCallback(); + _backtrace(log(), skip); + _postCallback(); +} + +#if defined(__GNUC__) && !defined(__MINGW32__) + +void Trace::StackDump(std::ostream &out, int skip) { + void *trace[64]; + char **messages = (char **)NULL; + int i, trace_size = 0; + + trace_size = backtrace(trace, 64); + out << "CALL STACK BACKTRACE:" << std::endl; + messages = backtrace_symbols(trace, trace_size); + if (messages) { + for (i=skip; i<trace_size; ++i) { + out << " " << messages[i] << std::endl; + } + free(messages); + } else { + out << " unavailable!" << std::endl; + } +} + +#else + +void Trace::StackDump(std::ostream &, int) { } + +#endif // __GNUC__ + + +NAMESPACE_SIMDATA_END + |