CPN
Computational Process Networks
Logger.cc
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
23 #include "common_priv.h"
24 #include <cpn/utils/Logger.h>
26 #include <vector>
27 #include <iostream>
28 #include <stdio.h>
29 #include <stdarg.h>
30 
32 }
33 
35  : logout(0), loglevel(WARNING), defaultlevel(INFO), adjust(0)
36 {}
37 
38 Logger::Logger(int dfltlvl)
39  : logout(0), loglevel(WARNING), defaultlevel(dfltlvl), adjust(0)
40 {}
41 
43 
44 Logger::Logger(LoggerOutput *lo, int dfltlvl)
45  : logout(lo), loglevel(WARNING), defaultlevel(dfltlvl), adjust(0)
46 {
47  ASSERT(logout);
49 }
50 
51 Logger::Logger(LoggerOutput *lo, int dfltlevel, const std::string &nm)
52  : logout(lo), loglevel(WARNING), defaultlevel(dfltlevel), name(nm)
53 {
54  ASSERT(logout);
56 }
57 
58 int Logger::LogLevel() const {
60  return loglevel;
61 }
62 
63 int Logger::LogLevel(int level) {
65  return loglevel = level;
66 }
67 
68 int Logger::DefaultLevel() const {
70  return defaultlevel;
71 }
72 
73 int Logger::DefaultLevel(int level) {
75  return defaultlevel = level;
76 }
77 
78 int Logger::Adjust() const {
80  return adjust;
81 }
82 
83 int Logger::Adjust(int a) {
85  return adjust = a;
86 }
87 
88 const std::string &Logger::Name() const {
90  return name;
91 }
92 
93 const std::string &Logger::Name(const std::string &nm) {
95  return name = nm;
96 }
97 
100  return logout;
101 }
102 
105  return logout = output;
106 }
107 
108 void Logger::Log(int level, const std::string &msg) {
110  if (level < loglevel) { return; }
111  if (logout) { logout->Log(level + adjust, name + ":" + msg); }
112 }
113 
114 void Logger::Logf(int level, const char *fmt, ...) {
116  if (level < loglevel) { return; }
117  va_list ap;
118  va_start(ap, fmt);
119  vLogf(level, fmt, ap);
120  va_end(ap);
121 }
122 
123 void Logger::Logf(const char *fmt, ...) {
125  if (defaultlevel < loglevel) { return; }
126  va_list ap;
127  va_start(ap, fmt);
128  vLogf(defaultlevel, fmt, ap);
129  va_end(ap);
130 }
131 
132 void Logger::vLogf(int level, const char *fmt, va_list ap) {
134  if (level < loglevel) { return; }
135  // This code was based on an example of how
136  // to use vsnprintf in the unix man pages.
137  std::vector<char> buff(128);
138  while (1) {
139  /* Try to print in the allocated space. */
140  va_list ap_copy;
141  va_copy(ap_copy, ap);
142  int n = vsnprintf(&buff[0], buff.size(), fmt, ap_copy);
143  /* If that worked, return the string. */
144  if (n > -1 && unsigned(n) < buff.size()) {
145  std::string ret = &buff[0];
146  Log(level, ret);
147  return;
148  }
149  /* Else try again with more space. */
150  if (n > -1) { /* glibc 2.1 */ /* precisely what is needed */
151  buff.resize(n+1);
152  }
153  else { /* glibc 2.0 */ /* twice the old size */
154  buff.resize(buff.size()*2);
155  }
156  }
157 }
158 
159 void Logger::Error(const char *fmt, ...) {
161  if (ERROR < loglevel) { return; }
162  va_list ap;
163  va_start(ap, fmt);
164  vLogf(ERROR, fmt, ap);
165  va_end(ap);
166 }
167 
168 void Logger::Warn(const char *fmt, ...) {
170  if (WARNING < loglevel) { return; }
171  va_list ap;
172  va_start(ap, fmt);
173  vLogf(WARNING, fmt, ap);
174  va_end(ap);
175 }
176 
177 void Logger::Info(const char *fmt, ...) {
179  if (INFO < loglevel) { return; }
180  va_list ap;
181  va_start(ap, fmt);
182  vLogf(INFO, fmt, ap);
183  va_end(ap);
184 }
185 
186 void Logger::Debug(const char *fmt, ...) {
188  if (DEBUG < loglevel) { return; }
189  va_list ap;
190  va_start(ap, fmt);
191  vLogf(DEBUG, fmt, ap);
192  va_end(ap);
193 }
194 
195 void Logger::Trace(const char *fmt, ...) {
197  if (TRACE < loglevel) { return; }
198  va_list ap;
199  va_start(ap, fmt);
200  vLogf(TRACE, fmt, ap);
201  va_end(ap);
202 }
203 
206  return loglevel = level;
207 }
208 
211  return loglevel;
212 }
213 
214 void LoggerStdOutput::Log(int level, const std::string &msg) {
216  if (level >= loglevel) {
217  std::cout << level << ":" << msg << std::endl;
218  }
219 }
220 
void vLogf(int level, const char *fmt, va_list ap)
Definition: Logger.cc:132
virtual ~LoggerOutput()
Definition: Logger.cc:31
std::string name
Definition: Logger.h:112
int Adjust() const
Definition: Logger.cc:78
virtual void Log(int level, const std::string &msg)=0
Log a message to this outputer.
virtual ~Logger()
Definition: Logger.cc:42
LoggerOutput * Output()
Definition: Logger.cc:98
virtual int LogLevel() const =0
void Trace(const char *fmt,...)
Definition: Logger.cc:195
Logger()
Definition: Logger.cc:34
int defaultlevel
Definition: Logger.h:110
void Error(const char *fmt,...)
Definition: Logger.cc:159
LoggerOutput * logout
Definition: Logger.h:108
void Log(int level, const std::string &msg)
Log a message to this outputer.
Definition: Logger.cc:214
void Info(const char *fmt,...)
Definition: Logger.cc:177
virtual void Log(int level, const std::string &msg)
Log a message to this outputer.
Definition: Logger.cc:108
Abstract base class for logger outputers. Any object who wishes to be a place for logging messages to...
Definition: Logger.h:37
const std::string & Name() const
Definition: Logger.cc:88
int LogLevel() const
Definition: Logger.cc:209
Sync::ReentrantLock lock
Definition: Logger.h:107
int LogLevel() const
Definition: Logger.cc:58
Sync::ReentrantLock lock
Definition: Logger.h:125
A very simple logging interface.
int loglevel
Definition: Logger.h:109
int adjust
Definition: Logger.h:111
void Warn(const char *fmt,...)
Definition: Logger.cc:168
#define ASSERT(exp,...)
void Logf(int level, const char *fmt,...)
Definition: Logger.cc:114
int DefaultLevel() const
Definition: Logger.cc:68
void Debug(const char *fmt,...)
Definition: Logger.cc:186