agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 1 | /** |
Sébastien Blin | cb783e3 | 2021-02-12 11:34:10 -0500 | [diff] [blame] | 2 | * Copyright (C) 2020-2021 Savoir-faire Linux Inc. |
agsantos | ef9f856 | 2020-06-25 16:43:25 -0400 | [diff] [blame] | 3 | * |
| 4 | * Author: Aline Gondim Santos <aline.gondimsantos@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 21 | #ifndef PLUGLOG_H |
| 22 | #define PLUGLOG_H |
agsantos | c9181b4 | 2020-11-26 12:03:04 -0500 | [diff] [blame] | 23 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <sstream> |
| 26 | |
| 27 | #ifndef __ANDROID__ |
| 28 | #include <iostream> |
| 29 | #endif |
| 30 | |
| 31 | #ifdef __ANDROID__ |
| 32 | #include <android/log.h> |
| 33 | #endif |
| 34 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 35 | inline char |
| 36 | separator() |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 37 | { |
| 38 | #ifdef _WIN32 |
| 39 | return '\\'; |
| 40 | #else |
| 41 | return '/'; |
| 42 | #endif |
| 43 | } |
| 44 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 45 | class Plog |
| 46 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 47 | private: |
| 48 | Plog() = delete; |
| 49 | Plog(const Plog&) = delete; |
| 50 | Plog(Plog&&) = default; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 51 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 52 | public: |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 53 | enum class LogPriority { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 54 | /** For internal use only. */ |
| 55 | UNKNOWN, |
| 56 | /** The default priority, for internal use only. */ |
| 57 | DEFAULT, |
| 58 | /** Verbose logging. Should typically be disabled for a release apk. */ |
| 59 | VERBOSE, |
| 60 | /** Debug logging. Should typically be disabled for a release apk. */ |
| 61 | DEBUG, |
| 62 | /** Informational logging. Should typically be disabled for a release apk. */ |
| 63 | INFO, |
| 64 | /** Warning logging. For use with recoverable failures. */ |
| 65 | WARN, |
| 66 | /** Error logging. For use with unrecoverable failures. */ |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 67 | ERR, |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 68 | /** Fatal logging. For use when aborting. */ |
| 69 | FATAL, |
| 70 | /** For internal use only. */ |
| 71 | SILENT, /* only for SetMinPriority(); must be last */ |
| 72 | }; |
| 73 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 74 | static void log(const LogPriority priority, const std::string& tag, const std::string& s) |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 75 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 76 | // Android only |
| 77 | #ifdef __ANDROID__ |
| 78 | switch (priority) { |
| 79 | case LogPriority::DEBUG: |
| 80 | __android_log_print(ANDROID_LOG_DEBUG, tag.c_str(), ": %s", s.c_str()); |
| 81 | break; |
| 82 | case LogPriority::INFO: |
| 83 | __android_log_print(ANDROID_LOG_INFO, tag.c_str(), ": %s", s.c_str()); |
| 84 | break; |
| 85 | case LogPriority::WARN: |
| 86 | __android_log_print(ANDROID_LOG_WARN, tag.c_str(), ": %s", s.c_str()); |
| 87 | break; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 88 | case LogPriority::ERR: |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 89 | __android_log_print(ANDROID_LOG_ERROR, tag.c_str(), ": %s", s.c_str()); |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | // Anything but Android |
| 95 | #else |
| 96 | switch (priority) { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 97 | case LogPriority::UNKNOWN: |
| 98 | case LogPriority::DEFAULT: |
| 99 | case LogPriority::VERBOSE: |
| 100 | case LogPriority::DEBUG: |
| 101 | case LogPriority::INFO: |
| 102 | std::cout << tag << ": " << s << std::endl; |
| 103 | break; |
| 104 | case LogPriority::WARN: |
| 105 | std::cout << tag << ": " << s << std::endl; |
| 106 | break; |
| 107 | case LogPriority::ERR: |
| 108 | case LogPriority::FATAL: |
| 109 | std::cerr << tag << ": " << s << std::endl; |
| 110 | break; |
| 111 | case LogPriority::SILENT: |
| 112 | break; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | #endif |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 116 | } |
| 117 | }; |
| 118 | |
| 119 | #endif // PLUGLOG_H |