Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PrintMsg.h
Go to the documentation of this file.
1 
8 #include <cstdarg>
9 #include <cassert>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 
14 #ifndef LIMBO_PREPROCESSOR_PRINTMSG_H
15 #define LIMBO_PREPROCESSOR_PRINTMSG_H
16 
18 namespace limbo
19 {
20 
24  kNONE = 0,
25  kINFO = 1,
26  kWARN = 2,
27  kERROR = 3,
28  kDEBUG = 4,
29  kASSERT = 5
30 };
31 
34 int limboPrint(MessageType m, const char* format, ...);
35 int limboPrintStream(MessageType m, FILE* stream, const char* format, ...);
36 int limboVPrintStream(MessageType m, FILE* stream, const char* format, va_list args);
37 int limboSPrint(MessageType m, char* buf, const char* format, ...);
38 int limboVSPrint(MessageType m, char* buf, const char* format, va_list args);
39 int limboSPrintPrefix(MessageType m, char* prefix);
40 void limboPrintAssertMsg(const char* expr, const char* fileName, unsigned lineNum, const char* funcName, const char* format, ...);
41 void limboPrintAssertMsg(const char* expr, const char* fileName, unsigned lineNum, const char* funcName);
43 
44 
49 inline int limboPrint(MessageType m, const char* format, ...)
50 {
51  va_list args;
52  va_start(args, format);
53  int ret = limboVPrintStream(m, stdout, format, args);
54  va_end(args);
55 
56  return ret;
57 }
58 
64 inline int limboPrintStream(MessageType m, FILE* stream, const char* format, ...)
65 {
66  va_list args;
67  va_start(args, format);
68  int ret = limboVPrintStream(m, stream, format, args);
69  va_end(args);
70 
71  return ret;
72 }
73 
80 inline int limboVPrintStream(MessageType m, FILE* stream, const char* format, va_list args)
81 {
82  // print prefix
83  char prefix[8];
84  limboSPrintPrefix(m, prefix);
85  // merge prefix and format
86  char formatBuf[256];
87  sprintf(formatBuf, "%s%s", prefix, format);
88 
89  // print message
90  // only print once to ensure multi-thread safe
91  int ret = vfprintf(stream, formatBuf, args);
92 
93  return ret;
94 }
95 
101 inline int limboSPrint(MessageType m, char* buf, const char* format, ...)
102 {
103  va_list args;
104  va_start(args, format);
105  int ret = limboVSPrint(m, buf, format, args);
106  va_end(args);
107 
108  return ret;
109 }
110 
117 inline int limboVSPrint(MessageType m, char* buf, const char* format, va_list args)
118 {
119  // print prefix
120  char prefix[8];
121  limboSPrintPrefix(m, prefix);
122  sprintf(buf, "%s", prefix);
123 
124  // print message
125  int ret = vsprintf(buf+strlen(prefix), format, args);
126 
127  return ret;
128 }
129 
134 inline int limboSPrintPrefix(MessageType m, char* prefix)
135 {
136  switch (m)
137  {
138  case kNONE:
139  return sprintf(prefix, "%c", '\0');
140  case kINFO:
141  return sprintf(prefix, "(I) ");
142  case kWARN:
143  return sprintf(prefix, "(W) ");
144  case kERROR:
145  return sprintf(prefix, "(E) ");
146  case kDEBUG:
147  return sprintf(prefix, "(D) ");
148  case kASSERT:
149  return sprintf(prefix, "(A) ");
150  default:
151  return sprintf(prefix, "(?) ");
152  }
153  return 0;
154 }
155 
163 inline void limboPrintAssertMsg(const char* expr, const char* fileName, unsigned lineNum, const char* funcName, const char* format, ...)
164 {
165  // construct message
166  char buf[1024];
167  va_list args;
168  va_start(args, format);
169  vsprintf(buf, format, args);
170  va_end(args);
171 
172  // print message
173  limboPrintStream(kASSERT, stderr, "%s:%u: %s: Assertion `%s' failed: %s\n", fileName, lineNum, funcName, expr, buf);
174 }
175 
182 inline void limboPrintAssertMsg(const char* expr, const char* fileName, unsigned lineNum, const char* funcName)
183 {
184  // print message
185  limboPrintStream(kASSERT, stderr, "%s:%u: %s: Assertion `%s' failed\n", fileName, lineNum, funcName, expr);
186 }
187 
188 } // namespace limbo
189 
190 #endif
int limboPrintStream(MessageType m, FILE *stream, const char *format,...)
formatted print with prefix to stream
Definition: PrintMsg.h:64
MessageType
message type for print functions
Definition: PrintMsg.h:23
int limboSPrint(MessageType m, char *buf, const char *format,...)
formatted print with prefix to buffer
Definition: PrintMsg.h:101
void limboPrintAssertMsg(const char *expr, const char *fileName, unsigned lineNum, const char *funcName, const char *format,...)
print message for assertion failure with additional message, see limboAssertMsg(condition, args...)
Definition: PrintMsg.h:163
namespace for Limbo
Definition: GraphUtility.h:22
int limboPrint(MessageType m, const char *format,...)
formatted print with prefix
Definition: PrintMsg.h:49
int limboVPrintStream(MessageType m, FILE *stream, const char *format, va_list args)
formatted print with prefix to stream
Definition: PrintMsg.h:80
int limboSPrintPrefix(MessageType m, char *prefix)
print prefix message to buffer
Definition: PrintMsg.h:134
int limboVSPrint(MessageType m, char *buf, const char *format, va_list args)
formatted print with prefix to buffer
Definition: PrintMsg.h:117