|
| 1 | +#pragma once |
| 2 | + |
| 3 | +// |
| 4 | +// This defines a logging system that can be turned on and off by command line |
| 5 | +// |
| 6 | +// -DLOG_LEVEL=SPBLAS_TRACE (or any other level or undefined LOG_LEVEL to |
| 7 | +// turn it off) |
| 8 | +// |
| 9 | +// SPBLAS_DEBUG (level 0) not meant to be kept in code, but good for debugging |
| 10 | +// SPBLAS_WARNING (level 1) for giving developers a more complete warning |
| 11 | +// message before throwing error or exiting |
| 12 | +// SPBLAS_TRACE (level 2) provides trace comments of files and line numbers |
| 13 | +// SPBLAS_INFO (level 3) for providing deep information about algorithm or |
| 14 | +// details |
| 15 | +// |
| 16 | +// |
| 17 | +// Add to code any of the following |
| 18 | +// |
| 19 | +// log_debug("formatted message"); // behaves like printf so can add formatting |
| 20 | +// log_warning("formated message"); // warnings or early exit extra detail |
| 21 | +// log_trace(""); // can also add formatted message, but often file/line is |
| 22 | +// // sufficient |
| 23 | +// log_info("formatted message") // any extra info or data we find useful for |
| 24 | +// // analyzing algorithm or debugging |
| 25 | +// |
| 26 | +// when LOG_LEVEL is not defined, it does nothing, but when defined, it prints |
| 27 | +// as desired for all levels up to specified one |
| 28 | +// |
| 29 | + |
| 30 | +enum { SPBLAS_DEBUG, SPBLAS_WARNING, SPBLAS_TRACE, SPBLAS_INFO }; |
| 31 | + |
| 32 | +#define log_debug(fmt, ...) \ |
| 33 | + _log_(SPBLAS_DEBUG, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \ |
| 34 | + ##__VA_ARGS__) |
| 35 | +#define log_warning(fmt, ...) \ |
| 36 | + _log_(SPBLAS_WARNING, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \ |
| 37 | + ##__VA_ARGS__) |
| 38 | +#define log_trace(fmt, ...) \ |
| 39 | + _log_(SPBLAS_TRACE, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \ |
| 40 | + ##__VA_ARGS__) |
| 41 | +#define log_info(fmt, ...) \ |
| 42 | + _log_(SPBLAS_INFO, __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, \ |
| 43 | + ##__VA_ARGS__) |
| 44 | + |
| 45 | +#if defined(LOG_LEVEL) |
| 46 | +#define _log_(l, file, line, func, fmt, ...) \ |
| 47 | + spblas_log_(l, #l, file, line, func, fmt, ##__VA_ARGS__) |
| 48 | +#else |
| 49 | +#define _log_(l, file, line, func, fmt, ...) \ |
| 50 | + do { \ |
| 51 | + } while (0) |
| 52 | +#endif |
| 53 | + |
| 54 | +#ifdef LOG_LEVEL |
| 55 | + |
| 56 | +#include <stdarg.h> // va_start, va_list, va_end |
| 57 | +#include <stdio.h> // printf, vprintf |
| 58 | + |
| 59 | +static void spblas_log_(int level, const char* pref, const char* file, |
| 60 | + const int line, const char* func, const char* fmt, |
| 61 | + ...) { |
| 62 | + va_list args; |
| 63 | + va_start(args, fmt); |
| 64 | + |
| 65 | + if (level <= LOG_LEVEL) { // add all smaller logtype enums |
| 66 | + |
| 67 | + if (isatty(1)) { |
| 68 | + // color log if isatty(1) ie file descriptor is from terminal |
| 69 | + printf("\x1b[48;5;14m"); // background is high intensity light blue |
| 70 | + printf("\x1b[38;5;0m"); // foreground is black |
| 71 | + } |
| 72 | + |
| 73 | + // printf("[%s] %s:%d: %s()", pref, file, line, func); // print out |
| 74 | + // preamble: [logtype] file:<line#>: functionname() message |
| 75 | + printf("[%s] %s:%d: ", pref, file, line); |
| 76 | + // print out preamble: [logtype] file:<line#>: message |
| 77 | + vprintf(fmt, args); // print out message |
| 78 | + |
| 79 | + // end color for log printing |
| 80 | + if (isatty(1)) { |
| 81 | + printf("\x1b[0m"); |
| 82 | + } |
| 83 | + printf("\n"); |
| 84 | + } |
| 85 | + fflush(0); |
| 86 | + va_end(args); |
| 87 | +} |
| 88 | +#endif // LOG_LEVEL |
0 commit comments