Logger.h

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2004-2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 
00018 #ifndef _OASYS_LOGGER_H_
00019 #define _OASYS_LOGGER_H_
00020 
00021 #include "DebugUtils.h"
00022 #include "Log.h"
00023 
00024 namespace oasys {
00025 
00061 class Logger {
00062 public:    
00067     inline Logger(const char* classname, const char* fmt, ...) PRINTFLIKE(3, 4);
00068 
00072     Logger(const char* classname, const std::string& logpath)
00073         : classname_(classname)
00074     {
00075         set_logpath(logpath.c_str());
00076     }
00077 
00081     inline void logpathf(const char* fmt, ...) PRINTFLIKE(2, 3);
00082 
00095     inline void logpath_appendf(const char* fmt, ...) PRINTFLIKE(2, 3);
00096     
00100     void set_logpath(const char* logpath)
00101     {
00102         if (logpath == 0) {
00103             strncpy(logpath_, "/", sizeof(logpath_));
00104             baselen_ = 1;
00105         } else {
00106             strncpy(logpath_, logpath, sizeof(logpath_));
00107             baselen_ = strlen(logpath);
00108         }
00109     }
00110 
00119     inline bool log_enabled(log_level_t level) const
00120     {
00121         return oasys::log_enabled(level, logpath_) ||
00122             oasys::log_enabled(level, classname_);
00123     }
00124 
00129     typedef log_level_t Can_Only_Be_Called_By_A_Logger;
00130     
00134     inline int vlogf(log_level_t level, const char *fmt, va_list args) const
00135     {
00136         return Log::instance()->vlogf(logpath_, level, classname_, this,
00137                                       fmt, args);
00138     }
00139 
00143     inline int logf(log_level_t level, const char *fmt, ...) const
00144         PRINTFLIKE(3, 4);
00145 
00151     inline int logf(const char* logpath, log_level_t level,
00152                     const char* fmt, ...) const
00153         PRINTFLIKE(4, 5);
00154 
00158     inline int log_multiline(log_level_t level, const char* msg) const
00159     {
00160         return Log::instance()->log_multiline(logpath_, level, 
00161                                               classname_, this, msg);
00162     }
00163 
00167     const char* logpath() { return logpath_; }
00168 
00169 protected:
00170     const char* classname_;
00171     char logpath_[LOG_MAX_PATHLEN];
00172     size_t baselen_;
00173 };
00174 
00175 //----------------------------------------------------------------------
00176 Logger::Logger(const char* classname, const char* fmt, ...)
00177     : classname_(classname)
00178 {
00179     va_list ap;
00180     va_start(ap, fmt);
00181     vsnprintf(logpath_, sizeof(logpath_), fmt, ap);
00182     va_end(ap);
00183     baselen_ = strlen(logpath_);
00184 }
00185 
00186 //----------------------------------------------------------------------
00187 void
00188 Logger::logpathf(const char* fmt, ...)
00189 {
00190     va_list ap;
00191     va_start(ap, fmt);
00192     vsnprintf(logpath_, sizeof(logpath_), fmt, ap);
00193     va_end(ap);
00194 }
00195 
00196 //----------------------------------------------------------------------
00197 void
00198 Logger::logpath_appendf(const char* fmt, ...)
00199 {
00200     va_list ap;
00201     va_start(ap, fmt);
00202     vsnprintf(&logpath_[baselen_], sizeof(logpath_) - baselen_, fmt, ap);
00203     va_end(ap);
00204 }
00205 
00206 //----------------------------------------------------------------------
00207 int
00208 Logger::logf(log_level_t level, const char *fmt, ...) const
00209 {
00210     va_list ap;
00211     va_start(ap, fmt);
00212     int ret = vlogf(level, fmt, ap);
00213     va_end(ap);
00214     return ret;
00215 }
00216 
00217 //----------------------------------------------------------------------
00218 int
00219 Logger::logf(const char* logpath, log_level_t level,
00220              const char *fmt, ...) const
00221 {
00222     va_list ap;
00223     va_start(ap, fmt);
00224     int ret = Log::instance()->vlogf(logpath, level, classname_, this,
00225                                      fmt, ap);
00226     va_end(ap);
00227     return ret;
00228 }
00229 
00230 
00231 } // namespace oasys
00232 
00233 #endif /* _OASYS_LOGGER_H_ */

Generated on Sat Sep 8 08:43:30 2007 for DTN Reference Implementation by  doxygen 1.5.3