jGL
Loading...
Searching...
No Matches
jLog.h
Go to the documentation of this file.
1#ifndef jLOG_H
2#define jLOG_H
3
4/*
5
6 jLog a quick and dirty header only logging library
7
8*/
9
10// MIT License
11
12// Copyright (c) 2024 Jerboa
13
14// Permission is hereby granted, free of charge, to any person obtaining a copy
15// of this software and associated documentation files (the "Software"), to deal
16// in the Software without restriction, including without limitation the rights
17// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18// copies of the Software, and to permit persons to whom the Software is
19// furnished to do so, subject to the following conditions:
20
21// The above copyright notice and this permission notice shall be included in all
22// copies or substantial portions of the Software.
23
24// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30// SOFTWARE.
31
32
33#include <iostream>
34#include <fstream>
35#include <sstream>
36#include <string>
37#include <vector>
38#include <map>
39
40#include <ctime>
41
42#ifndef ANDROID
43#else
44 #include <android/log.h>
45#endif
46
47namespace jLog
48{
49
50 inline std::string get_time()
51 {
52 time_t time = std::time(nullptr);
53 char time_buf[80];
54 struct tm ts;
55 ts = *localtime(&time);
56 strftime(time_buf, sizeof(time_buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
57
58 return std::string(time_buf);
59 }
60
61 class Log
62 {
63
64 public:
65
66 Log(const char * s){
67 logStream.clear();
68 logStream.push_back(std::string(s));
69 }
70
71 Log(){}
72
74 if (logStream.size()>0){
75 #if defined(ANDROID) || defined(WINDOWS)
76 put("Destructor called on non-empty log, contents:\n\n");
77 #else
78 put("\033[1;33m[WARN] \033[0mDestructor called on non-empty log, contents:\n\n");
79 #endif
80
81 #if !defined(ANDROID)
82 std::cout << get();
83 #else
84 __android_log_print(ANDROID_LOG_WARN, "", "%s", get().c_str());
85 #endif
86 }
87 }
88
89 #if defined(ANDROID)
90 void androidLog(){ if(logStream.size()>0){__android_log_print(ANDROID_LOG_INFO,"Hop","%s",get().c_str());} }
91 #endif
92
93 void put(std::string s){
94 s = get_time() + " " + s;
95 logStream.push_back(s);
96 }
97
98 inline void put(const char * c){
99 std::string s = get_time() + " " + std::string(c);
100 logStream.push_back(s);
101 }
102
103 inline std::string get() {
104 std::string l;
105 for (unsigned i = 0; i < logStream.size(); i++){
106 l += logStream[i] + "\n";
107 }
108 logStream.clear();
109 return l;
110 }
111
112 inline size_t size(){return logStream.size();}
113
114 private:
115 std::vector<std::string> logStream;
116 };
117
118 inline std::ostream & operator<<(std::ostream & o, Log & l)
119 {
120 o << l.get();
121 return o;
122 }
123
124 class LogType {
125 public:
127 LogType(std::string s):msg(s){}
128 LogType(const char * c):msg(c){}
129 virtual std::string get()const {return std::string("");};
130 void operator>> (Log & l){l.put(this->get());}
131 protected:
132 std::string msg;
133 };
134
135 /*
136 INFO log type leaves a [INFO] declarator and a msg
137 */
138
139 class INFO : public LogType{
140 public:
141 using LogType::LogType; // inherit all constructors
142 std::string get()const{return u+msg;}
143 private:
144 #if !defined(ANDROID) && !defined(WINDOWS)
145 const char * u = "\033[1;36m[INFO] \033[0m";
146 #else
147 const char * u = "[INFO] ";
148 #endif
149 };
150
151 /*
152 WARN log type leaves a [WARN] declarator and a msg
153 */
154
155 class WARN : public LogType{
156 public:
157 using LogType::LogType; // inherit all constructors
158 std::string get()const{return u+msg;}
159 private:
160 #if !defined(ANDROID) && !defined(WINDOWS)
161 const char * u = "\033[1;33m[WARN] \033[0m";
162 #else
163 const char * u = "[WARN] ";
164 #endif
165
166 };
167
168 /*
169 ERR log type leaves a [ERR] declarator follwed by a (ERRORCODE) and
170 finally a msg
171 */
172
174
175 inline std::string operator+ (std::string s, const ERRORCODE e)
176 {
177 switch (e)
178 {
180 return s+std::string("UNSPECIFIED");
181 break;
183 return s+std::string("LUA");
184 break;
185 default:
186 return s+std::string("UNSPECIFIED");
187 }
188 }
189
190 class ERR : public LogType{
191 public:
192 ERR(std::string s) : LogType(s) {}
193 ERR(const char * c) : LogType(c) {}
194 ERR(ERRORCODE xx, std::string s) : LogType(s), x(xx) {}
195 ERR(ERRORCODE xx, const char * c) : LogType(c), x(xx) {}
196 std::string get()const{return u+std::string("\033[1;33m(")+x+std::string(")\033[0m ")+msg;}
197 private:
198 const char * u = "\033[1;31m[ERR] \033[0m";
200 };
201
202 /*
203 PROGRESS log type leaves a msg followed by a progress meter
204 */
205
206 class Progress {
207 public:
209 uint64_t l,
210 uint64_t p,
211 std::vector<std::string> s = std::vector<std::string>{"\\","-","/","-"}
212 )
213 : length(l), progress(p), sequence(s)
214 {}
215 bool next(std::ostream & o){
216 o << '\r';
217 o.flush();
218 if (progress < length){
219 progress++;
220 o << '\t' << sequence[progress % sequence.size()];
221 return false;
222 }
223 else {
224 return true;
225 }
226 }
227 private:
228 uint64_t length;
229 uint64_t progress;
230 std::vector<std::string> sequence;
231 };
232}
233#endif /* jLOG_H */
Definition jLog.h:190
ERR(const char *c)
Definition jLog.h:193
std::string get() const
Definition jLog.h:196
ERR(std::string s)
Definition jLog.h:192
ERR(ERRORCODE xx, std::string s)
Definition jLog.h:194
ERR(ERRORCODE xx, const char *c)
Definition jLog.h:195
Definition jLog.h:139
std::string get() const
Definition jLog.h:142
Definition jLog.h:124
LogType(std::string s)
Definition jLog.h:127
std::string msg
Definition jLog.h:132
void operator>>(Log &l)
Definition jLog.h:130
LogType()
Definition jLog.h:126
LogType(const char *c)
Definition jLog.h:128
virtual std::string get() const
Definition jLog.h:129
Definition jLog.h:62
void put(const char *c)
Definition jLog.h:98
void put(std::string s)
Definition jLog.h:93
Log(const char *s)
Definition jLog.h:66
size_t size()
Definition jLog.h:112
Log()
Definition jLog.h:71
~Log()
Definition jLog.h:73
std::string get()
Definition jLog.h:103
Definition jLog.h:206
bool next(std::ostream &o)
Definition jLog.h:215
Progress(uint64_t l, uint64_t p, std::vector< std::string > s=std::vector< std::string >{"\\","-","/","-"})
Definition jLog.h:208
Definition jLog.h:155
std::string get() const
Definition jLog.h:158
Definition jLog.h:48
std::ostream & operator<<(std::ostream &o, Log &l)
Definition jLog.h:118
std::string get_time()
Definition jLog.h:50
std::string operator+(std::string s, const ERRORCODE e)
Definition jLog.h:175
ERRORCODE
Definition jLog.h:173
@ UNSPECIFIED
Definition jLog.h:173
@ LUA_ERROR
Definition jLog.h:173