Debian Codegraph @

android/app/jni/src/log.cpp

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "ta-log.h"
21 #include "fileutils.h"
22 #include <thread>
23 #include <condition_variable>
24
25 #ifdef _WIN32
26 #include <windows.h>
27 #endif
28
29 #ifdef IS_MOBILE
30 int logLevel=LOGLEVEL_TRACE;
31 #else
32 int logLevel=LOGLEVEL_TRACE; // until done
33 #endif
34
35 FILE* logOut;
36 FILE* logFile;
37 char* logFileBuf;
38 char* logFileWriteBuf;
39 unsigned int logFilePosI;
40 unsigned int logFilePosO;
41 std::thread* logFileThread;
42 std::mutex logFileLock;
43 std::mutex logFileLockI;
44 std::condition_variable logFileNotify;
45 std::atomic<bool> logFileAvail(false);
46 std::atomic<bool> iAmReallyDead(false);
47
48 std::atomic<unsigned short> logPosition;
49
50 LogEntry logEntries[TA_LOG_SIZE];
51
52 static constexpr unsigned int TA_LOG_MASK=TA_LOG_SIZE-1;
53 static constexpr unsigned int TA_LOGFILE_BUF_MASK=TA_LOGFILE_BUF_SIZE-1;
54
55 const char* logTypes[5]={
56 "ERROR",
57 "warning",
58 "info",
59 "debug",
60 "trace"
61 };
62
63 void appendLogBuf(const LogEntry& entry) {
64 logFileLockI.lock();
65
66 std::string toWrite=fmt::sprintf(
67 "%02d:%02d:%02d [%s] %s\n",
68 entry.time.tm_hour,
69 entry.time.tm_min,
70 entry.time.tm_sec,
71 logTypes[entry.loglevel],
72 entry.text
73 );
74
75 const char* msg=toWrite.c_str();
76 size_t len=toWrite.size();
77
78 int remaining=(logFilePosO-logFilePosI-1)&TA_LOGFILE_BUF_SIZE;
79
80 if (len>=(unsigned int)remaining) {
81 printf("line too long to fit in log buffer!\n");
82 logFileLockI.unlock();
83 return;
84 }
85
86 if ((logFilePosI+len)>=TA_LOGFILE_BUF_SIZE) {
87 size_t firstWrite=TA_LOGFILE_BUF_SIZE-logFilePosI;
88 memcpy(logFileBuf+logFilePosI,msg,firstWrite);
89 memcpy(logFileBuf,msg+firstWrite,len-firstWrite);
90 } else {
91 memcpy(logFileBuf+logFilePosI,msg,len);
92 }
93
94 logFilePosI=(logFilePosI+len)&TA_LOGFILE_BUF_MASK;
95 logFileLockI.unlock();
96 }
97
98 int writeLog(int level, const char* msg, fmt::printf_args args) {
99 time_t thisMakesNoSense=time(NULL);
100 int pos=(logPosition.fetch_add(1))&TA_LOG_MASK;
101
102 #if FMT_VERSION >= 100100
103 #ifdef _MSVC_LANG
104 #if _MSVC_LANG >= 201703L
105 logEntries[pos].text.assign(fmt::vsprintf(std::basic_string_view(msg),args));
106 #else
107 logEntries[pos].text.assign(fmt::vsprintf(fmt::basic_string_view<char>(msg),args));
108 #endif
109 #else
110 #if __cplusplus >= 201703L
111 logEntries[pos].text.assign(fmt::vsprintf(std::basic_string_view(msg),args));
112 #else
113 logEntries[pos].text.assign(fmt::vsprintf(fmt::basic_string_view<char>(msg),args));
114 #endif
115 #endif
116 #else
117 logEntries[pos].text.assign(fmt::vsprintf(msg,args));
118 #endif
119 // why do I have to pass a pointer
120 // can't I just pass the time_t directly?!
121 #ifdef _WIN32
122 struct tm* tempTM=localtime(&thisMakesNoSense);
123 if (tempTM==NULL) {
124 memset(&logEntries[pos].time,0,sizeof(struct tm));
125 } else {
126 memcpy(&logEntries[pos].time,tempTM,sizeof(struct tm));
127 }
128 #else
129 if (localtime_r(&thisMakesNoSense,&logEntries[pos].time)==NULL) {
130 memset(&logEntries[pos].time,0,sizeof(struct tm));
131 }
132 #endif
133 logEntries[pos].loglevel=level;
134 logEntries[pos].ready=true;
135
136 // write to log file
137 if (logFileAvail) {
138 appendLogBuf(logEntries[pos]);
139 logFileNotify.notify_one();
140 }
141
142 if (logLevel<level) return 0;
143 switch (level) {
144 case LOGLEVEL_ERROR:
145 return fmt::fprintf(logOut,"\x1b[1;31m[ERROR]\x1b[m %s\n",logEntries[pos].text);
146 case LOGLEVEL_WARN:
147 return fmt::fprintf(logOut,"\x1b[1;33m[warning]\x1b[m %s\n",logEntries[pos].text);
148 case LOGLEVEL_INFO:
149 return fmt::fprintf(logOut,"\x1b[1;32m[info]\x1b[m %s\n",logEntries[pos].text);
150 case LOGLEVEL_DEBUG:
151 return fmt::fprintf(logOut,"\x1b[1;34m[debug]\x1b[m %s\n",logEntries[pos].text);
152 case LOGLEVEL_TRACE:
153 return fmt::fprintf(logOut,"\x1b[1;37m[trace]\x1b[m %s\n",logEntries[pos].text);
154 }
155 return -1;
156 }
157
158 void initLog(FILE* where) {
159 logOut=where;
160
161 // initialize coloring on Windows
162 #ifdef _WIN32
163 HANDLE winout=GetStdHandle(STD_OUTPUT_HANDLE);
164 int termprop=0;
165 GetConsoleMode(winout,(LPDWORD)&termprop);
166 termprop|=ENABLE_VIRTUAL_TERMINAL_PROCESSING;
167 SetConsoleMode(winout,termprop);
168 #endif
169
170 // initialize log buffer
171 logPosition=0;
172 for (int i=0; i<TA_LOG_SIZE; i++) {
173 logEntries[i].text.reserve(128);
174 }
175
176 // initialize log to file thread
177 logFileAvail=false;
178 }
179
180 void changeLogOutput(FILE* where) {
181 logOut=where;
182 }
183
184 void _logFileThread() {
185 std::unique_lock<std::mutex> lock(logFileLock);
186 while (true) {
187 unsigned int logFilePosICopy=logFilePosI;
188 if (logFilePosICopy!=logFilePosO) {
189 // write
190 if (logFilePosO>logFilePosICopy) {
191 fwrite(logFileBuf+logFilePosO,1,TA_LOGFILE_BUF_SIZE-logFilePosO,logFile);
192 logFilePosO=0;
193 } else {
194 fwrite(logFileBuf+logFilePosO,1,logFilePosICopy-logFilePosO,logFile);
195 logFilePosO=logFilePosICopy&TA_LOGFILE_BUF_MASK;
196 }
197 } else {
198 // wait
199 fflush(logFile);
200 if (!logFileAvail) break;
201 logFileNotify.wait(lock);
202 }
203 }
204 iAmReallyDead=true;
205 }
206
207 bool startLogFile(const char* path) {
208 if (logFileAvail) return true;
209
210 // rotate log file if possible
211 char oldPath[4096];
212 char newPath[4096];
213
214 if (fileExists(path)==1) {
215 for (int i=4; i>=0; i--) {
216 if (i>0) {
217 snprintf(oldPath,4095,"%s.%d",path,i);
218 } else {
219 strncpy(oldPath,path,4095);
220 }
221 snprintf(newPath,4095,"%s.%d",path,i+1);
222
223 if (i>=4) {
224 deleteFile(oldPath);
225 } else {
226 moveFiles(oldPath,newPath);
227 }
228 }
229 }
230
231 // open log file
232 if ((logFile=ps_fopen(path,"w+"))==NULL) {
233 logFileAvail=false;
234 logW("could not open log file! (%s)",strerror(errno));
235 return false;
236 }
237
238 logFileBuf=new char[TA_LOGFILE_BUF_SIZE];
239 logFileWriteBuf=new char[TA_LOGFILE_BUF_SIZE];
240 logFilePosI=0;
241 logFilePosO=0;
242 logFileAvail=true;
243
244 logFileThread=new std::thread(_logFileThread);
245 return true;
246 }
247
248 bool finishLogFile() {
249 if (!logFileAvail) return false;
250
251 logFileAvail=false;
252 iAmReallyDead=false;
253
254 // flush
255 logFileLockI.lock();
256 logFileNotify.notify_one();
257 while (!iAmReallyDead) {
258 std::this_thread::yield();
259 std::this_thread::sleep_for(std::chrono::milliseconds(10));
260 }
261 // this join is guaranteed to work
262 logFileThread->join();
263 logFileLockI.unlock();
264
265 fclose(logFile);
266 return true;
267 }