7 * Logging. Main ideas from http://www.drdobbs.com/cpp/201804215
9 * (c) 2011-2012 Björn Döbel <doebel@os.inf.tu-dresden.de>,
10 * economic rights: Technische Universität Dresden (Germany)
11 * This file is part of TUD:OS and distributed under the terms of the
12 * GNU General Public License 2.
13 * Please see the COPYING-GPL-2 file for details.
28 void dump_mem(T const * ptr, unsigned count, unsigned brk = 10)
30 for (unsigned i = 0; i < count; ++i) {
31 unsigned char const *base = reinterpret_cast<unsigned char const*>(ptr) + i;
32 printf("%02x", *base);
33 if (i % brk == brk-1 || count-1 == i) {
49 enum LogLevel { ERROR, WARN, INFO, DEBUG };
65 static LogLevel maxLog;
67 static l4_umword_t logFlags;
71 std::ostringstream& get(LogLevel lvl = INFO)
76 _oss << tv.tv_sec << ":" << std::setw(3) << tv.tv_usec / 1000;
78 _oss << " " << std::setw(5) << Log::LogLevelToString(lvl) << " ";
82 static char const* LogLevelToString(LogLevel l)
85 case ERROR: return "ERR";
86 case WARN: return "WARN";
87 case DEBUG: return "DBG";
88 case INFO: return "INFO";
89 default: return "unknown?";
95 std::cout << _oss.str() << "\n";
100 std::ostringstream _oss;
104 Log& operator=(const Log&) { return *this; }
109 * Wrapper to map L4Re dbg interface to the log facility.
113 enum { bufsize = 60 };
116 int cprintf(char const *fmt, ...) const
118 static char buf[bufsize];
121 int ret = vsnprintf(buf, bufsize, fmt, arg);
124 if (buf[strlen(buf)-1] == '\n')
125 buf[strlen(buf)-1] = 0;
126 Romain::Log().get(Romain::Log::DEBUG) << buf;
132 int printf(char const *fmt, ...) const
134 static char buf[bufsize];
137 int ret = vsnprintf(buf, bufsize, fmt, arg);
140 if (buf[strlen(buf)-1] == '\n')
141 buf[strlen(buf)-1] = 0;
142 Romain::Log().get(Romain::Log::DEBUG) << buf;
151 * Dirty trick from http://www.drdobbs.com/cpp/201804215
153 * Must keep this ordering and *not* have a ; at the and of the
156 #define LOG(lvl, flags) \
157 if (!((lvl <= Romain::Log::maxLog) || (Romain::Log::logFlags & flags))) \
160 Romain::Log().get(lvl) << std::setw(15) << __func__ << "\033[0m "
162 #define INFO() LOG(Romain::Log::INFO, Romain::Log::All)
163 #define DEBUG() LOG(Romain::Log::DEBUG, Romain::Log::All)
164 #define DEBUGf(flag) LOG(Romain::Log::DEBUG, flag)
165 #define WARN() LOG(Romain::Log::WARN, Romain::Log::All)
166 #define ERROR() LOG(Romain::Log::ERROR, Romain::Log::All)