| 34 | TOutput::TOutput() : f_(&errorTimeWrapper) {} |
| 35 | |
| 36 | void TOutput::printf(const char* message, ...) { |
| 37 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
| 38 | // Try to reduce heap usage, even if printf is called rarely. |
| 39 | static const int STACK_BUF_SIZE = 256; |
| 40 | char stack_buf[STACK_BUF_SIZE]; |
| 41 | va_list ap; |
| 42 | |
| 43 | #ifdef _MSC_VER |
| 44 | va_start(ap, message); |
| 45 | int need = _vscprintf(message, ap); |
| 46 | va_end(ap); |
| 47 | |
| 48 | if (need < STACK_BUF_SIZE) { |
| 49 | va_start(ap, message); |
| 50 | vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); |
| 51 | va_end(ap); |
| 52 | f_(stack_buf); |
| 53 | return; |
| 54 | } |
| 55 | #else |
| 56 | va_start(ap, message); |
| 57 | int need = vsnprintf(stack_buf, STACK_BUF_SIZE, message, ap); |
| 58 | va_end(ap); |
| 59 | |
| 60 | if (need < STACK_BUF_SIZE) { |
| 61 | f_(stack_buf); |
| 62 | return; |
| 63 | } |
| 64 | #endif |
| 65 | |
| 66 | char* heap_buf = (char*)malloc((need + 1) * sizeof(char)); |
| 67 | if (heap_buf == nullptr) { |
| 68 | #ifdef _MSC_VER |
| 69 | va_start(ap, message); |
| 70 | vsnprintf_s(stack_buf, STACK_BUF_SIZE, _TRUNCATE, message, ap); |
| 71 | va_end(ap); |
| 72 | #endif |
| 73 | // Malloc failed. We might as well print the stack buffer. |
| 74 | f_(stack_buf); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | va_start(ap, message); |
| 79 | int rval = vsnprintf(heap_buf, need + 1, message, ap); |
| 80 | va_end(ap); |
| 81 | // TODO(shigin): inform user |
| 82 | if (rval != -1) { |
| 83 | f_(heap_buf); |
| 84 | } |
| 85 | free(heap_buf); |
| 86 | #else |
| 87 | THRIFT_UNUSED_VARIABLE(message); |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | void TOutput::errorTimeWrapper(const char* msg) { |
| 92 | #ifndef THRIFT_SQUELCH_CONSOLE_OUTPUT |
no outgoing calls