29 #include "utilities/globalDefinitions.hpp" |
29 #include "utilities/globalDefinitions.hpp" |
30 |
30 |
31 #include <stdarg.h> |
31 #include <stdarg.h> |
32 |
32 |
33 // Simple class to format the ctor arguments into a fixed-sized buffer. |
33 // Simple class to format the ctor arguments into a fixed-sized buffer. |
|
34 class FormatBufferBase { |
|
35 protected: |
|
36 char* _buf; |
|
37 inline FormatBufferBase(char* buf) : _buf(buf) {} |
|
38 public: |
|
39 operator const char *() const { return _buf; } |
|
40 }; |
|
41 |
|
42 // Use resource area for buffer |
|
43 #define RES_BUFSZ 256 |
|
44 class FormatBufferResource : public FormatBufferBase { |
|
45 public: |
|
46 FormatBufferResource(const char * format, ...); |
|
47 }; |
|
48 |
|
49 // Use stack for buffer |
34 template <size_t bufsz = 256> |
50 template <size_t bufsz = 256> |
35 class FormatBuffer { |
51 class FormatBuffer : public FormatBufferBase { |
36 public: |
52 public: |
37 inline FormatBuffer(const char * format, ...); |
53 inline FormatBuffer(const char * format, ...); |
38 inline void append(const char* format, ...); |
54 inline void append(const char* format, ...); |
39 inline void print(const char* format, ...); |
55 inline void print(const char* format, ...); |
40 inline void printv(const char* format, va_list ap); |
56 inline void printv(const char* format, va_list ap); |
41 operator const char *() const { return _buf; } |
|
42 |
57 |
43 char* buffer() { return _buf; } |
58 char* buffer() { return _buf; } |
44 int size() { return bufsz; } |
59 int size() { return bufsz; } |
45 |
60 |
46 private: |
61 private: |
47 FormatBuffer(const FormatBuffer &); // prevent copies |
62 FormatBuffer(const FormatBuffer &); // prevent copies |
|
63 char _buffer[bufsz]; |
48 |
64 |
49 protected: |
65 protected: |
50 char _buf[bufsz]; |
|
51 |
|
52 inline FormatBuffer(); |
66 inline FormatBuffer(); |
53 }; |
67 }; |
54 |
68 |
55 template <size_t bufsz> |
69 template <size_t bufsz> |
56 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) { |
70 FormatBuffer<bufsz>::FormatBuffer(const char * format, ...) : FormatBufferBase(_buffer) { |
57 va_list argp; |
71 va_list argp; |
58 va_start(argp, format); |
72 va_start(argp, format); |
59 jio_vsnprintf(_buf, bufsz, format, argp); |
73 jio_vsnprintf(_buf, bufsz, format, argp); |
60 va_end(argp); |
74 va_end(argp); |
61 } |
75 } |
62 |
76 |
63 template <size_t bufsz> |
77 template <size_t bufsz> |
64 FormatBuffer<bufsz>::FormatBuffer() { |
78 FormatBuffer<bufsz>::FormatBuffer() : FormatBufferBase(_buffer) { |
65 _buf[0] = '\0'; |
79 _buf[0] = '\0'; |
66 } |
80 } |
67 |
81 |
68 template <size_t bufsz> |
82 template <size_t bufsz> |
69 void FormatBuffer<bufsz>::print(const char * format, ...) { |
83 void FormatBuffer<bufsz>::print(const char * format, ...) { |
91 va_end(argp); |
105 va_end(argp); |
92 } |
106 } |
93 |
107 |
94 // Used to format messages for assert(), guarantee(), fatal(), etc. |
108 // Used to format messages for assert(), guarantee(), fatal(), etc. |
95 typedef FormatBuffer<> err_msg; |
109 typedef FormatBuffer<> err_msg; |
|
110 typedef FormatBufferResource err_msg_res; |
96 |
111 |
97 // assertions |
112 // assertions |
98 #ifdef ASSERT |
113 #ifdef ASSERT |
99 #ifndef USE_REPEATED_ASSERTS |
114 #ifndef USE_REPEATED_ASSERTS |
100 #define assert(p, msg) \ |
115 #define assert(p, msg) \ |