Peano
Loading...
Searching...
No Matches
TP_Logging.cpp
Go to the documentation of this file.
2
3#include <vector>
4#include <cstdio> // vsnprintf
5
6// source: http://stackoverflow.com/a/69911
7std::string vformat (const char *fmt, va_list ap) {
8 // Allocate a buffer on the stack that's big enough for us almost
9 // all the time. Be prepared to allocate dynamically if it doesn't fit.
10 size_t size = 1024;
11 char stackbuf[1024];
12 std::vector<char> dynamicbuf;
13 char *buf = &stackbuf[0];
14 va_list ap_copy;
15
16 while (1) {
17 // Try to vsnprintf into our buffer.
18 va_copy(ap_copy, ap);
19 int needed = vsnprintf (buf, size, fmt, ap);
20 va_end(ap_copy);
21
22 // NB. C99 (which modern Linux and OS X follow) says vsnprintf
23 // failure returns the length it would have needed. But older
24 // glibc and current Windows return -1 for failure, i.e., not
25 // telling us how much was needed.
26
27 if (needed <= (int)size && needed >= 0) {
28 // It fit fine so we're done.
29 return std::string (buf, (size_t) needed);
30 }
31
32 // vsnprintf reported that it wanted to write more characters
33 // than we allotted. So try again using a dynamic buffer. This
34 // doesn't happen very often if we chose our initial size well.
35 size = (needed > 0) ? (needed+1) : (size*2);
36 dynamicbuf.resize (size);
37 buf = &dynamicbuf[0];
38 }
39}
40
41using namespace TP;
42
43
44void LoggingAdapter::TP_LOG(const char *fmt, ...) {
45 va_list ap;
46 va_start (ap, fmt);
47 std::string buf = vformat (fmt, ap);
48 va_end (ap);
49 if(log) log->log(buf);
50}
51
52void LoggingAdapter::TP_ERROR(const char *fmt, ...) {
53 va_list ap;
54 va_start (ap, fmt);
55 std::string buf = vformat (fmt, ap);
56 va_end (ap);
57 if(log) log->error(buf);
58}
59
60void LoggingAdapter::TP_INFO(const char *fmt, ...) {
61 va_list ap;
62 va_start (ap, fmt);
63 std::string buf = vformat (fmt, ap);
64 va_end (ap);
65 if(log) log->info(buf);
66}
67
68void LoggingAdapter::TP_WARN(const char *fmt, ...) {
69 va_list ap;
70 va_start (ap, fmt);
71 std::string buf = vformat (fmt, ap);
72 va_end (ap);
73 if(log) log->warn(buf);
74}
std::string vformat(const char *fmt, va_list ap)
Definition TP_Logging.cpp:7
This file contains aliases for making access to the long state vector Q as used eg.
void TP_INFO(const char *fmt,...)
void TP_ERROR(const char *fmt,...)
void TP_LOG(const char *fmt,...)
void TP_WARN(const char *fmt,...)