| 1 | #include "debug.h" |
| 2 | #include "utils.h" |
| 3 | #include <Rcpp.h> |
| 4 | #include <unistd.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdarg.h> |
| 7 | |
| 8 | // For debug.h |
| 9 | #if defined(DEBUG_THREAD) |
| 10 | tct_thrd_t __main_thread__; |
| 11 | tct_thrd_t __background_thread__; |
| 12 | #endif |
| 13 | |
| 14 | |
| 15 | // It's not safe to call REprintf from the background thread but we need some |
| 16 | // way to output error messages. R CMD check does not it if the code uses the |
| 17 | // symbols stdout, stderr, and printf, so this function is a way to avoid |
| 18 | // those. It's to calling `fprintf(stderr, ...)`. |
| 19 | void err_printf(const char *fmt, ...) { |
| 20 | const size_t max_size = 4096; |
| 21 | char buf[max_size]; |
| 22 | |
| 23 | va_list args; |
| 24 | va_start(args, fmt); |
| 25 | int n = vsnprintf(buf, max_size, fmt, args); |
| 26 | va_end(args); |
| 27 | |
| 28 | if (n == -1) |
| 29 | return; |
| 30 | |
| 31 | if (write(STDERR_FILENO, buf, n)) {} |
| 32 | // This is here simply to avoid a warning about "ignoring return value" of |
| 33 | // the write(), on some compilers. (Seen with gcc 4.4.7 on RHEL 6) |
| 34 | } |
| 35 | |
| 36 | // Set the default log level |
| 37 | LogLevel log_level_ = LOG_ERROR; |
| 38 | |
| 39 | |
| 40 | // Sets the current log level and returns previous value. |
| 41 | // [[Rcpp::export(rng = false)]] |
| 42 | std::string log_level(std::string level) { |
| 43 | LogLevel old_level = log_level_; |
| 44 | |
| 45 | if (level == "") { |
| 46 | // Do nothing |
| 47 | } else if (level == "OFF") { |
| 48 | log_level_ = LOG_OFF; |
| 49 | } else if (level == "ERROR") { |
| 50 | log_level_ = LOG_ERROR; |
| 51 | } else if (level == "WARN") { |
| 52 | log_level_ = LOG_WARN; |
| 53 | } else if (level == "INFO") { |
| 54 | log_level_ = LOG_INFO; |
| 55 | } else if (level == "DEBUG") { |
| 56 | log_level_ = LOG_DEBUG; |
| 57 | } else { |
| 58 | Rcpp::stop("Unknown value for `level`"); |
| 59 | } |
| 60 | |
| 61 | switch(old_level) { |
| 62 | case LOG_OFF: return "OFF"; |
| 63 | case LOG_ERROR: return "ERROR"; |
| 64 | case LOG_WARN: return "WARN"; |
| 65 | case LOG_INFO: return "INFO"; |
| 66 | case LOG_DEBUG: return "DEBUG"; |
| 67 | default: return ""; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Reports whether package was compiled with UBSAN |
| 72 | // [[Rcpp::export(rng = false)]] |
| 73 | bool using_ubsan() { |
| 74 | #ifdef USING_UBSAN |
| 75 | return true; |
| 76 | #else |
| 77 | return false; |
| 78 | #endif |
| 79 | } |