From: <sv...@ww...> - 2004-06-14 07:49:42
|
Author: mkrose Date: 2004-06-14 00:49:33 -0700 (Mon, 14 Jun 2004) New Revision: 1043 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Log.h trunk/CSP/SimData/Include/SimData/LogStream.h Log: Logging now checks SIMDATA_LOGFILE environment variable when opening the log. If not set or empty, log messages are initially directed to stderr (log output can also be redirected at runtime). Change the initial "SimData [version] loaded @ xxx" message to be a log message. With the SIMDATA_LOGFILE this makes it possible to suppress display of this message on startup while still allowing the version and load address to be determined.. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1043 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-06-14 06:55:18 UTC (rev 1042) +++ trunk/CSP/SimData/CHANGES.current 2004-06-14 07:49:33 UTC (rev 1043) @@ -5,6 +5,15 @@ * Fixup python call for saving/restoring ints from archives (now _int32). + * Logging now checks SIMDATA_LOGFILE environment variable when + opening the log. If not set or empty, log messages are initially + directed to stderr (log output can also be redirected at runtime). + + * Change the initial "SimData [version] loaded @ xxx" message to be + a log message. With the SIMDATA_LOGFILE this makes it possible to + suppress display of this message on startup while still allowing + the version and load address to be determined.. + 2004-06-12: onsight * Commented out throw statement for assignment to Ref<> from an incompatible type. Callers must check that the ref is not null Modified: trunk/CSP/SimData/Include/SimData/Log.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Log.h 2004-06-14 06:55:18 UTC (rev 1042) +++ trunk/CSP/SimData/Include/SimData/Log.h 2004-06-14 07:49:33 UTC (rev 1043) @@ -28,6 +28,7 @@ #define __SIMDATA_LOG_H__ +#include <cstdlib> #include <cstdio> #include <string> @@ -56,7 +57,14 @@ inline SIMDATA_EXPORT logstream& log() { //static logstream logstrm(std::cerr); static logstream *logstrm = 0; - if (logstrm == 0) logstrm = new logstream(std::cerr); + if (logstrm == 0) { + char *save = getenv("SIMDATA_LOGFILE"); + // default to stderr if SIMDATA_LOGFILE isn't set + logstrm = new logstream(std::cerr); + if (save and *save) { + logstrm->setOutput(save); + } + } return *logstrm; } Modified: trunk/CSP/SimData/Include/SimData/LogStream.h =================================================================== --- trunk/CSP/SimData/Include/SimData/LogStream.h 2004-06-14 06:55:18 UTC (rev 1042) +++ trunk/CSP/SimData/Include/SimData/LogStream.h 2004-06-14 07:49:33 UTC (rev 1043) @@ -1,18 +1,18 @@ /* SimData: Data Infrastructure for Simulations - * Copyright (C) 2002, 2003 Mark Rose <tm...@st...> - * + * Copyright 2002, 2003, 2004 Mark Rose <tm...@st...> + * * 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. @@ -24,13 +24,13 @@ * @brief Stream based logging mechanism. * * Stream based logging mechanism. - * + * * Based on the LogStream library written by Bernie Bright, 1998 * Copyright (C) 1998 Bernie Bright - bb...@c0... * * Source code from Bernie Bright's LogStream library is used * here under the turms of the GNU General Public License - * version 2 or later, as allowed by the GNU Library General + * version 2 or later, as allowed by the GNU Library General * Public License Version 2 (clause 3). */ @@ -53,13 +53,11 @@ using std::ostream; -// // TODO: // // 1. Change output destination. Done. // 2. Make logbuf thread safe. // 3. Read environment for default debugClass and debugPriority. -// /** An output-only, category-based log stream. * @@ -108,7 +106,7 @@ /** Get the logging classes currently enabled. * @return All enabled debug logging anded together. */ - int get_log_classes (); + int get_log_classes(); /** Set the logging priority. @@ -119,7 +117,7 @@ /** Get the current logging priority. - * + * * @return The priority cutoff for logging messages. */ int get_log_priority (); @@ -129,7 +127,7 @@ * * @param sb stream buffer */ - void set_sb( std::streambuf* sb ); + void set_sb(std::streambuf* sb); protected: @@ -155,37 +153,33 @@ void operator= ( const logbuf& ); }; -inline int -logbuf::sync() -{ + +inline int logbuf::sync() { if (!sbuf) return -1; return sbuf->pubsync(); } -inline void -logbuf::set_log_state(int c, int p) -{ + +inline void logbuf::set_log_state(int c, int p) { logging_enabled = ((c & logClass) != 0 && p >= logPriority); } + //inline logbuf::int_type -inline int -logbuf::overflow(int c) -{ +inline int logbuf::overflow(int c) { return logging_enabled ? sbuf->sputc(static_cast<char>(c)) : (EOF == 0 ? 1: 0); } /** LogStream manipulator for setting the log level of a message. */ -struct loglevel -{ - loglevel(int c, int p) - : logClass(c), logPriority(p) {} +struct loglevel { + loglevel(int c, int p): logClass(c), logPriority(p) {} int logClass; int logPriority; }; + /** A helper class for logstream construction. * * A helper class that ensures a streambuf and ostream are constructed and @@ -199,7 +193,8 @@ logstream_base() {} logbuf lbuf; }; - + + /** Class to manage the debug logging stream. */ class SIMDATA_EXPORT logstream : private logstream_base, public std::ostream @@ -207,14 +202,14 @@ std::ofstream *m_out; public: /** The default is to send messages to cerr. - * + * * @param out_ output stream */ logstream(std::ostream& out_) - : logstream_base(), - ostream(&lbuf), // msvc6 accepts ostream(&lbuf) _using std::ostream_, but not std::ostream(&lbuf) ... - m_out(NULL) - { + : logstream_base(), + ostream(&lbuf), // msvc6 accepts ostream(&lbuf) _using std::ostream_, but not std::ostream(&lbuf) ... + m_out(NULL) + { lbuf.set_sb(out_.rdbuf()); } @@ -234,19 +229,19 @@ * * @param out_ output stream */ - void setOutput(std::ostream& out_) { + void setOutput(std::ostream& out_) { _close(); - lbuf.set_sb(out_.rdbuf() ); + lbuf.set_sb(out_.rdbuf() ); } /** Set the output stream * * @param fn output file path */ - void setOutput(std::string const &fn) { + void setOutput(std::string const &fn) { _close(); m_out = new std::ofstream(fn.c_str()); - lbuf.set_sb( m_out->rdbuf() ); + lbuf.set_sb( m_out->rdbuf() ); } /** Set the global log class and priority level. @@ -265,13 +260,12 @@ /** Output operator to capture the debug level and priority of a message. * @param l log level */ - inline std::ostream& operator<< ( const loglevel& l ); + inline std::ostream& operator<< (const loglevel& l); }; -inline std::ostream& -logstream::operator<< ( const loglevel& l ) -{ - lbuf.set_log_state( l.logClass, l.logPriority ); + +inline std::ostream& logstream::operator<< (const loglevel& l) { + lbuf.set_log_state(l.logClass, l.logPriority); return *this; } |