Zane Shelley | 0c44c2f | 2020-06-05 17:14:31 -0500 | [diff] [blame^] | 1 | #pragma once |
| 2 | |
| 3 | #include <inttypes.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | #include <cstdarg> |
| 7 | |
| 8 | #ifndef TEST_TRACE |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | #endif |
| 11 | |
| 12 | namespace trace |
| 13 | { |
| 14 | |
| 15 | #ifndef TEST_TRACE |
| 16 | constexpr size_t MSG_MAX_LEN = 256; |
| 17 | #endif |
| 18 | |
| 19 | /** @brief Information trace (va_list format). */ |
| 20 | inline void inf(const char* format, va_list args) |
| 21 | { |
| 22 | #ifdef TEST_TRACE |
| 23 | |
| 24 | vfprintf(stdout, format, args); |
| 25 | fprintf(stdout, "\n"); |
| 26 | |
| 27 | #else |
| 28 | |
| 29 | char msg[MSG_MAX_LEN]; |
| 30 | vsnprintf(msg, MSG_MAX_LEN, format, args); |
| 31 | phosphor::logging::log<phosphor::logging::level::INFO>(msg); |
| 32 | |
| 33 | #endif |
| 34 | } |
| 35 | |
| 36 | /** @brief Error trace (va_list format). */ |
| 37 | inline void err(const char* format, va_list args) |
| 38 | { |
| 39 | #ifdef TEST_TRACE |
| 40 | |
| 41 | vfprintf(stderr, format, args); |
| 42 | fprintf(stderr, "\n"); |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | char msg[MSG_MAX_LEN]; |
| 47 | vsnprintf(msg, MSG_MAX_LEN, format, args); |
| 48 | phosphor::logging::log<phosphor::logging::level::ERR>(msg); |
| 49 | |
| 50 | #endif |
| 51 | } |
| 52 | |
| 53 | /** @brief Information trace (printf format). */ |
| 54 | inline void inf(const char* format, ...) |
| 55 | { |
| 56 | va_list args; |
| 57 | va_start(args, format); |
| 58 | trace::inf(format, args); |
| 59 | va_end(args); |
| 60 | } |
| 61 | |
| 62 | /** @brief Error trace (printf format). */ |
| 63 | inline void err(const char* format, ...) |
| 64 | { |
| 65 | va_list args; |
| 66 | va_start(args, format); |
| 67 | trace::err(format, args); |
| 68 | va_end(args); |
| 69 | } |
| 70 | |
| 71 | } // namespace trace |