Logger.h

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #ifndef _OASYS_LOGGER_H_
00040 #define _OASYS_LOGGER_H_
00041 
00042 #include "DebugUtils.h"
00043 #include "Log.h"
00044 
00045 namespace oasys {
00046 
00082 class Logger {
00083 public:
00088     inline Logger(const char* classname, const char* fmt, ...) PRINTFLIKE(3, 4);
00089 
00093     Logger(const char* classname, const std::string& logpath)
00094         : classname_(classname)
00095     {
00096         set_logpath(logpath.c_str());
00097     }
00098 
00102     inline void logpathf(const char* fmt, ...) PRINTFLIKE(2, 3);
00103 
00116     inline void logpath_appendf(const char* fmt, ...) PRINTFLIKE(2, 3);
00117     
00121     void set_logpath(const char* logpath)
00122     {
00123         if (logpath == 0) {
00124             strncpy(logpath_, "/", sizeof(logpath_));
00125             baselen_ = 1;
00126         } else {
00127             strncpy(logpath_, logpath, sizeof(logpath_));
00128             baselen_ = strlen(logpath);
00129         }
00130     }
00131 
00140     inline bool log_enabled(log_level_t level) const
00141     {
00142         return oasys::__log_enabled(level, logpath_) ||
00143             oasys::__log_enabled(level, classname_);
00144     }
00145 
00157     inline bool __log_enabled(log_level_t level, const char* path) const
00158     {
00159         (void)path;
00160         return oasys::__log_enabled(level, logpath_) ||
00161             oasys::__log_enabled(level, classname_);
00162     }
00163 
00164     
00168     inline int vlogf(log_level_t level, const char *fmt, va_list args) const
00169     {
00170         return Log::instance()->vlogf(logpath_, level, classname_, this,
00171                                       fmt, args);
00172     }
00173 
00177     inline int logf(log_level_t level, const char *fmt, ...) const
00178         PRINTFLIKE(3, 4);
00179 
00185     inline int logf(const char* logpath, log_level_t level,
00186                     const char* fmt, ...) const
00187         PRINTFLIKE(4, 5);
00188 
00194     inline int __logf(log_level_t level, const char *fmt, ...) const
00195         PRINTFLIKE(3, 4);
00196 
00200     inline int log_multiline(log_level_t level, const char* msg) const
00201     {
00202         return Log::instance()->log_multiline(logpath_, level, 
00203                                               classname_, this, msg);
00204     }
00205 
00209     const char* logpath() { return logpath_; }
00210 
00211 protected:
00212     const char* classname_;
00213     char logpath_[LOG_MAX_PATHLEN];
00214     size_t baselen_;
00215 };
00216 
00217 //----------------------------------------------------------------------
00218 Logger::Logger(const char* classname, const char* fmt, ...)
00219     : classname_(classname)
00220 {
00221     va_list ap;
00222     va_start(ap, fmt);
00223     vsnprintf(logpath_, sizeof(logpath_), fmt, ap);
00224     va_end(ap);
00225     baselen_ = strlen(logpath_);
00226 }
00227 
00228 //----------------------------------------------------------------------
00229 void
00230 Logger::logpathf(const char* fmt, ...)
00231 {
00232     va_list ap;
00233     va_start(ap, fmt);
00234     vsnprintf(logpath_, sizeof(logpath_), fmt, ap);
00235     va_end(ap);
00236 }
00237 
00238 //----------------------------------------------------------------------
00239 void
00240 Logger::logpath_appendf(const char* fmt, ...)
00241 {
00242     va_list ap;
00243     va_start(ap, fmt);
00244     vsnprintf(&logpath_[baselen_], sizeof(logpath_) - baselen_, fmt, ap);
00245     va_end(ap);
00246 }
00247 
00248 //----------------------------------------------------------------------
00249 int
00250 Logger::logf(log_level_t level, const char *fmt, ...) const
00251 {
00252     va_list ap;
00253     va_start(ap, fmt);
00254     int ret = vlogf(level, fmt, ap);
00255     va_end(ap);
00256     return ret;
00257 }
00258 
00259 //----------------------------------------------------------------------
00260 int
00261 Logger::__logf(log_level_t level, const char *fmt, ...) const
00262 {
00263     va_list ap;
00264     va_start(ap, fmt);
00265     int ret = vlogf(level, fmt, ap);
00266     va_end(ap);
00267     return ret;
00268 }
00269 
00270 //----------------------------------------------------------------------
00271 int
00272 Logger::logf(const char* logpath, log_level_t level,
00273              const char *fmt, ...) const
00274 {
00275     va_list ap;
00276     va_start(ap, fmt);
00277     int ret = Log::instance()->vlogf(logpath, level, classname_, this,
00278                                      fmt, ap);
00279     va_end(ap);
00280     return ret;
00281 }
00282 
00283 
00284 } // namespace oasys
00285 
00286 #endif /* _OASYS_LOGGER_H_ */

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1