From: <sv...@ww...> - 2004-09-13 09:06:11
|
Author: mkrose Date: 2004-09-13 02:06:02 -0700 (Mon, 13 Sep 2004) New Revision: 1228 Modified: trunk/CSP/SimData/CHANGES.current trunk/CSP/SimData/Include/SimData/Log.h trunk/CSP/SimData/Include/SimData/LogStream.h trunk/CSP/SimData/Source/LogStream.cpp Log: Add an interface for creating and retrieving log streams based on a unique id string. This allows multiple related libraries in an application to easily share the same log. Browse at: https://www.zerobar.net/viewcvs/viewcvs.cgi?view=rev&rev=1228 Modified: trunk/CSP/SimData/CHANGES.current =================================================================== --- trunk/CSP/SimData/CHANGES.current 2004-09-13 04:07:17 UTC (rev 1227) +++ trunk/CSP/SimData/CHANGES.current 2004-09-13 09:06:02 UTC (rev 1228) @@ -1,6 +1,11 @@ Version 0.4.0 (in progress) =========================== +2004-09-12: onsight + * Add an interface for creating and retrieving log streams based on + a unique id string. This allows multiple related libraries in an + application to easily share the same log. + 2004-08-25: onsight * Clean up some compiler warnings, and a few misc. fixes. Modified: trunk/CSP/SimData/Include/SimData/Log.h =================================================================== --- trunk/CSP/SimData/Include/SimData/Log.h 2004-09-13 04:07:17 UTC (rev 1227) +++ trunk/CSP/SimData/Include/SimData/Log.h 2004-09-13 09:06:02 UTC (rev 1228) @@ -90,7 +90,7 @@ // settings. note that this LogStream instance is never freed, so // it is safe to log messages from static destructors. log_stream = new LogStream(std::cerr); - log_stream->initFromEnvironment(); + log_stream->initFromEnvironment("SIMDATA_LOGFILE", "SIMDATA_LOGPRIORITY"); } return *log_stream; } Modified: trunk/CSP/SimData/Include/SimData/LogStream.h =================================================================== --- trunk/CSP/SimData/Include/SimData/LogStream.h 2004-09-13 04:07:17 UTC (rev 1227) +++ trunk/CSP/SimData/Include/SimData/LogStream.h 2004-09-13 09:06:02 UTC (rev 1228) @@ -111,12 +111,15 @@ bool getTimeLogging() const { return m_log_time; } /** Configure logging parameters and destination base on - * environment variables. The default implementation uses - * SIMDATA_LOGFILE and SIMDATA_PRIORITY to set the output - * file and priority threshold. This method is not called - * by the constructor; call it explicitly if needed. + * environment variables. This method is not called by + * the constructor; call it explicitly if needed. + * + * @param log_file the environment variable specifying the log + * output path (e.g. "SIMDATA_LOGFILE"). + * @param log_priority the environment variable specifying the log + * priority threshold (e.g. "SIMDATA_LOGPRIORITY"). */ - virtual void initFromEnvironment(); + void initFromEnvironment(const char *log_file, const char *log_priority); /** Close the underlying output stream. * @@ -203,9 +206,21 @@ */ std::ostream & entry(int priority, int category=~0, const char *file=0, int line=0); + /** Get or create a new log stream assocated with a string identifier. + * + * This method can be used to easily share one log file across multiple + * components. + * + * @param name a unique identifier for the log stream. + * @return the existing logstream associated with the identifier, or a + * new logstream. + */ + static LogStream *getOrCreateNamedLog(const std::string &name); + }; + NAMESPACE_SIMDATA_END Modified: trunk/CSP/SimData/Source/LogStream.cpp =================================================================== --- trunk/CSP/SimData/Source/LogStream.cpp 2004-09-13 04:07:17 UTC (rev 1227) +++ trunk/CSP/SimData/Source/LogStream.cpp 2004-09-13 09:06:02 UTC (rev 1228) @@ -37,22 +37,27 @@ #include <SimData/Log.h> #include <cstdlib> +#include <map> NAMESPACE_SIMDATA -void LogStream::initFromEnvironment() { - char *env_logfile = getenv("SIMDATA_LOGFILE"); - if (env_logfile && *env_logfile) { - setOutput(env_logfile); +void LogStream::initFromEnvironment(const char *log_file, const char *log_priority) { + if (log_file) { + char *env_logfile = getenv(log_file); + if (env_logfile && *env_logfile) { + setOutput(env_logfile); + } } - char *env_priority = getenv("SIMDATA_LOGPRIORITY"); - if (env_priority && *env_priority) { - int priority = atoi(env_priority); - if (priority < 0) priority = 0; - if (priority > LOG_ERROR) priority = LOG_ERROR; - setLogPriority(priority); + if (log_priority) { + char *env_priority = getenv(log_priority); + if (env_priority && *env_priority) { + int priority = atoi(env_priority); + if (priority < 0) priority = 0; + if (priority > LOG_ERROR) priority = LOG_ERROR; + setLogPriority(priority); + } } } @@ -75,5 +80,22 @@ } +// nothing very fancy. the logstreams persist unless explicitly +// deleted by a caller, since they may be needed even during static +// destruction. +typedef std::map<std::string, LogStream *> LogStreamRegistry; +LogStreamRegistry NamedLogStreamRegistry; + +LogStream *LogStream::getOrCreateNamedLog(const std::string &name) { + LogStreamRegistry::iterator iter = NamedLogStreamRegistry.find(name); + if (iter == NamedLogStreamRegistry.end()) { + LogStream *logstream = new LogStream(std::cerr); + NamedLogStreamRegistry[name] = logstream; + return logstream; + } + return iter->second; +} + + NAMESPACE_SIMDATA_END |