| 37 | } |
| 38 | |
| 39 | LAMBDA_RUNTIME_API |
| 40 | void log(verbosity v, char const* tag, char const* msg, va_list args) |
| 41 | { |
| 42 | va_list copy; |
| 43 | va_copy(copy, args); |
| 44 | const int sz = vsnprintf(nullptr, 0, msg, args) + 1; |
| 45 | if (sz < 0) { |
| 46 | puts("error occurred during log formatting!\n"); |
| 47 | va_end(copy); |
| 48 | return; |
| 49 | } |
| 50 | constexpr int max_stack_buffer_size = 512; |
| 51 | std::array<char, max_stack_buffer_size> buf; |
| 52 | char* out = buf.data(); |
| 53 | if (sz >= max_stack_buffer_size) { |
| 54 | out = new char[sz]; |
| 55 | } |
| 56 | |
| 57 | vsnprintf(out, sz, msg, copy); |
| 58 | va_end(copy); |
| 59 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 60 | std::chrono::high_resolution_clock::now().time_since_epoch()); |
| 61 | printf("%s [%lld] %s %s\n", get_prefix(v), static_cast<long long>(ms.count()), tag, out); |
| 62 | // stdout is not line-buffered when redirected (for example to a file or to another process) so we must flush it |
| 63 | // manually. |
| 64 | fflush(stdout); |
| 65 | if (out != buf.data()) { |
| 66 | delete[] out; |
| 67 | } |
| 68 | } |
| 69 | } // namespace logging |
| 70 | } // namespace aws |
no test coverage detected