blob: 68e99fbd7696313900cbab5d2f7c22c14efe33ea [file] [log] [blame]
agsantos5aa39652020-08-11 18:18:04 -04001/**
Sébastien Blincb783e32021-02-12 11:34:10 -05002 * Copyright (C) 2020-2021 Savoir-faire Linux Inc.
agsantosef9f8562020-06-25 16:43:25 -04003 *
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
AGS555248cd2020-05-13 11:15:19 -040021#ifndef PLUGLOG_H
22#define PLUGLOG_H
agsantosc9181b42020-11-26 12:03:04 -050023
AGS555248cd2020-05-13 11:15:19 -040024#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
agsantosac1940d2020-09-17 10:18:40 -040035inline char
36separator()
AGS555248cd2020-05-13 11:15:19 -040037{
AGS555248cd2020-05-13 11:15:19 -040038 return '/';
AGS555248cd2020-05-13 11:15:19 -040039}
40
agsantosac1940d2020-09-17 10:18:40 -040041class Plog
42{
AGS555248cd2020-05-13 11:15:19 -040043private:
44 Plog() = delete;
45 Plog(const Plog&) = delete;
46 Plog(Plog&&) = default;
agsantosac1940d2020-09-17 10:18:40 -040047
AGS555248cd2020-05-13 11:15:19 -040048public:
agsantos5aa39652020-08-11 18:18:04 -040049 enum class LogPriority {
AGS555248cd2020-05-13 11:15:19 -040050 /** 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. */
agsantos5aa39652020-08-11 18:18:04 -040063 ERR,
AGS555248cd2020-05-13 11:15:19 -040064 /** Fatal logging. For use when aborting. */
65 FATAL,
66 /** For internal use only. */
67 SILENT, /* only for SetMinPriority(); must be last */
68 };
69
agsantosac1940d2020-09-17 10:18:40 -040070 static void log(const LogPriority priority, const std::string& tag, const std::string& s)
agsantos5aa39652020-08-11 18:18:04 -040071 {
AGS555248cd2020-05-13 11:15:19 -040072// 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;
agsantos5aa39652020-08-11 18:18:04 -040084 case LogPriority::ERR:
AGS555248cd2020-05-13 11:15:19 -040085 __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) {
agsantosac1940d2020-09-17 10:18:40 -040093 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;
AGS555248cd2020-05-13 11:15:19 -0400109 }
110
111#endif
AGS555248cd2020-05-13 11:15:19 -0400112 }
113};
114
115#endif // PLUGLOG_H