AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame^] | 1 | #ifndef PLUGLOG_H |
| 2 | #define PLUGLOG_H |
| 3 | #include <string> |
| 4 | #include <sstream> |
| 5 | |
| 6 | #ifndef __ANDROID__ |
| 7 | #include <iostream> |
| 8 | #endif |
| 9 | |
| 10 | #ifdef __ANDROID__ |
| 11 | #include <android/log.h> |
| 12 | #endif |
| 13 | |
| 14 | inline char separator() |
| 15 | { |
| 16 | #ifdef _WIN32 |
| 17 | return '\\'; |
| 18 | #else |
| 19 | return '/'; |
| 20 | #endif |
| 21 | } |
| 22 | |
| 23 | class Plog{ |
| 24 | private: |
| 25 | Plog() = delete; |
| 26 | Plog(const Plog&) = delete; |
| 27 | Plog(Plog&&) = default; |
| 28 | public: |
| 29 | enum class LogPriority{ |
| 30 | /** For internal use only. */ |
| 31 | UNKNOWN, |
| 32 | /** The default priority, for internal use only. */ |
| 33 | DEFAULT, |
| 34 | /** Verbose logging. Should typically be disabled for a release apk. */ |
| 35 | VERBOSE, |
| 36 | /** Debug logging. Should typically be disabled for a release apk. */ |
| 37 | DEBUG, |
| 38 | /** Informational logging. Should typically be disabled for a release apk. */ |
| 39 | INFO, |
| 40 | /** Warning logging. For use with recoverable failures. */ |
| 41 | WARN, |
| 42 | /** Error logging. For use with unrecoverable failures. */ |
| 43 | ERROR, |
| 44 | /** Fatal logging. For use when aborting. */ |
| 45 | FATAL, |
| 46 | /** For internal use only. */ |
| 47 | SILENT, /* only for SetMinPriority(); must be last */ |
| 48 | }; |
| 49 | |
| 50 | static void log(const LogPriority priority, const std::string& tag, const std::string& s) { |
| 51 | |
| 52 | // Android only |
| 53 | #ifdef __ANDROID__ |
| 54 | switch (priority) { |
| 55 | case LogPriority::DEBUG: |
| 56 | __android_log_print(ANDROID_LOG_DEBUG, tag.c_str(), ": %s", s.c_str()); |
| 57 | break; |
| 58 | case LogPriority::INFO: |
| 59 | __android_log_print(ANDROID_LOG_INFO, tag.c_str(), ": %s", s.c_str()); |
| 60 | break; |
| 61 | case LogPriority::WARN: |
| 62 | __android_log_print(ANDROID_LOG_WARN, tag.c_str(), ": %s", s.c_str()); |
| 63 | break; |
| 64 | case LogPriority::ERROR: |
| 65 | __android_log_print(ANDROID_LOG_ERROR, tag.c_str(), ": %s", s.c_str()); |
| 66 | default: |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | // Anything but Android |
| 71 | #else |
| 72 | switch (priority) { |
| 73 | case LogPriority::UNKNOWN: |
| 74 | case LogPriority::DEFAULT: |
| 75 | case LogPriority::VERBOSE: |
| 76 | case LogPriority::DEBUG: |
| 77 | case LogPriority::INFO: |
| 78 | case LogPriority::WARN: |
| 79 | std::cout<< tag <<": " << s <<std::endl; |
| 80 | break; |
| 81 | case LogPriority::ERROR: |
| 82 | case LogPriority::FATAL: |
| 83 | std::cerr<< tag <<": " << s <<std::endl; |
| 84 | break; |
| 85 | case LogPriority::SILENT: |
| 86 | break; |
| 87 | |
| 88 | } |
| 89 | |
| 90 | #endif |
| 91 | |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | #endif // PLUGLOG_H |