blob: 5d99575d0e8d5cda06a2bc6ee0e9bee8bf943e4c [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{
38#ifdef _WIN32
39 return '\\';
40#else
41 return '/';
42#endif
43}
44
agsantosac1940d2020-09-17 10:18:40 -040045class Plog
46{
AGS555248cd2020-05-13 11:15:19 -040047private:
48 Plog() = delete;
49 Plog(const Plog&) = delete;
50 Plog(Plog&&) = default;
agsantosac1940d2020-09-17 10:18:40 -040051
AGS555248cd2020-05-13 11:15:19 -040052public:
agsantos5aa39652020-08-11 18:18:04 -040053 enum class LogPriority {
AGS555248cd2020-05-13 11:15:19 -040054 /** 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. */
agsantos5aa39652020-08-11 18:18:04 -040067 ERR,
AGS555248cd2020-05-13 11:15:19 -040068 /** Fatal logging. For use when aborting. */
69 FATAL,
70 /** For internal use only. */
71 SILENT, /* only for SetMinPriority(); must be last */
72 };
73
agsantosac1940d2020-09-17 10:18:40 -040074 static void log(const LogPriority priority, const std::string& tag, const std::string& s)
agsantos5aa39652020-08-11 18:18:04 -040075 {
AGS555248cd2020-05-13 11:15:19 -040076// 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;
agsantos5aa39652020-08-11 18:18:04 -040088 case LogPriority::ERR:
AGS555248cd2020-05-13 11:15:19 -040089 __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) {
agsantosac1940d2020-09-17 10:18:40 -040097 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;
AGS555248cd2020-05-13 11:15:19 -0400113 }
114
115#endif
AGS555248cd2020-05-13 11:15:19 -0400116 }
117};
118
119#endif // PLUGLOG_H