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 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 38 | return '/'; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 39 | } |
| 40 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 41 | class Plog |
| 42 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 43 | private: |
| 44 | Plog() = delete; |
| 45 | Plog(const Plog&) = delete; |
| 46 | Plog(Plog&&) = default; |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 47 | |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 48 | public: |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 49 | enum class LogPriority { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 50 | /** For internal use only. */ |
| 51 | UNKNOWN, |
| 52 | /** The default priority, for internal use only. */ |
| 53 | DEFAULT, |
| 54 | /** Verbose logging. Should typically be disabled for a release apk. */ |
| 55 | VERBOSE, |
| 56 | /** Debug logging. Should typically be disabled for a release apk. */ |
| 57 | DEBUG, |
| 58 | /** Informational logging. Should typically be disabled for a release apk. */ |
| 59 | INFO, |
| 60 | /** Warning logging. For use with recoverable failures. */ |
| 61 | WARN, |
| 62 | /** Error logging. For use with unrecoverable failures. */ |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 63 | ERR, |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 64 | /** Fatal logging. For use when aborting. */ |
| 65 | FATAL, |
| 66 | /** For internal use only. */ |
| 67 | SILENT, /* only for SetMinPriority(); must be last */ |
| 68 | }; |
| 69 | |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 70 | 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] | 71 | { |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 72 | // Android only |
| 73 | #ifdef __ANDROID__ |
| 74 | switch (priority) { |
| 75 | case LogPriority::DEBUG: |
| 76 | __android_log_print(ANDROID_LOG_DEBUG, tag.c_str(), ": %s", s.c_str()); |
| 77 | break; |
| 78 | case LogPriority::INFO: |
| 79 | __android_log_print(ANDROID_LOG_INFO, tag.c_str(), ": %s", s.c_str()); |
| 80 | break; |
| 81 | case LogPriority::WARN: |
| 82 | __android_log_print(ANDROID_LOG_WARN, tag.c_str(), ": %s", s.c_str()); |
| 83 | break; |
agsantos | 5aa3965 | 2020-08-11 18:18:04 -0400 | [diff] [blame] | 84 | case LogPriority::ERR: |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 85 | __android_log_print(ANDROID_LOG_ERROR, tag.c_str(), ": %s", s.c_str()); |
| 86 | default: |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | // Anything but Android |
| 91 | #else |
| 92 | switch (priority) { |
agsantos | ac1940d | 2020-09-17 10:18:40 -0400 | [diff] [blame] | 93 | case LogPriority::UNKNOWN: |
| 94 | case LogPriority::DEFAULT: |
| 95 | case LogPriority::VERBOSE: |
| 96 | case LogPriority::DEBUG: |
| 97 | case LogPriority::INFO: |
| 98 | std::cout << tag << ": " << s << std::endl; |
| 99 | break; |
| 100 | case LogPriority::WARN: |
| 101 | std::cout << tag << ": " << s << std::endl; |
| 102 | break; |
| 103 | case LogPriority::ERR: |
| 104 | case LogPriority::FATAL: |
| 105 | std::cerr << tag << ": " << s << std::endl; |
| 106 | break; |
| 107 | case LogPriority::SILENT: |
| 108 | break; |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | #endif |
AGS5 | 55248cd | 2020-05-13 11:15:19 -0400 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
| 115 | #endif // PLUGLOG_H |