| 
									
										
										
										
											2022-02-14 22:12:20 -05:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Furnace Tracker - multi-system chiptune tracker | 
					
						
							|  |  |  |  * Copyright (C) 2021-2022 tildearrow and contributors | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License along | 
					
						
							|  |  |  |  * with this program; if not, write to the Free Software Foundation, Inc., | 
					
						
							|  |  |  |  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-11 16:08:08 -04:00
										 |  |  | #ifndef _TA_LOG_H
 | 
					
						
							|  |  |  | #define _TA_LOG_H
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdarg.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-11 01:12:24 -04:00
										 |  |  | #include <time.h>
 | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | #include <atomic>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <fmt/printf.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-11 16:08:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define LOGLEVEL_ERROR 0
 | 
					
						
							|  |  |  | #define LOGLEVEL_WARN 1
 | 
					
						
							|  |  |  | #define LOGLEVEL_INFO 2
 | 
					
						
							|  |  |  | #define LOGLEVEL_DEBUG 3
 | 
					
						
							| 
									
										
										
										
											2022-03-23 23:05:09 -04:00
										 |  |  | #define LOGLEVEL_TRACE 4
 | 
					
						
							| 
									
										
										
										
											2021-05-11 16:08:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | // this has to be a power of 2
 | 
					
						
							|  |  |  | #define TA_LOG_SIZE 2048
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-09 13:28:46 -04:00
										 |  |  | extern int logLevel; | 
					
						
							| 
									
										
										
										
											2021-05-11 16:08:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | extern std::atomic<unsigned short> logPosition; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct LogEntry { | 
					
						
							|  |  |  |   int loglevel; | 
					
						
							| 
									
										
										
										
											2022-04-11 01:12:24 -04:00
										 |  |  |   struct tm time; | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  |   std::string text; | 
					
						
							|  |  |  |   bool ready; | 
					
						
							|  |  |  |   LogEntry(): | 
					
						
							|  |  |  |     loglevel(0), | 
					
						
							| 
									
										
										
										
											2022-04-14 19:25:59 -04:00
										 |  |  |     ready(false) { | 
					
						
							|  |  |  |     memset(&time,0,sizeof(struct tm)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-11 14:08:12 -04:00
										 |  |  | int writeLog(int level, const char* msg, fmt::printf_args args); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | extern LogEntry logEntries[TA_LOG_SIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename... T> int logV(const char* msg, const T&... args) { | 
					
						
							| 
									
										
										
										
											2022-04-11 14:36:02 -04:00
										 |  |  |   return writeLog(LOGLEVEL_TRACE,msg,fmt::make_printf_args(args...)); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename... T> int logD(const char* msg, const T&... args) { | 
					
						
							| 
									
										
										
										
											2022-04-11 14:36:02 -04:00
										 |  |  |   return writeLog(LOGLEVEL_DEBUG,msg,fmt::make_printf_args(args...)); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename... T> int logI(const char* msg, const T&... args) { | 
					
						
							| 
									
										
										
										
											2022-04-11 14:36:02 -04:00
										 |  |  |   return writeLog(LOGLEVEL_INFO,msg,fmt::make_printf_args(args...)); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename... T> int logW(const char* msg, const T&... args) { | 
					
						
							| 
									
										
										
										
											2022-04-11 14:36:02 -04:00
										 |  |  |   return writeLog(LOGLEVEL_WARN,msg,fmt::make_printf_args(args...)); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<typename... T> int logE(const char* msg, const T&... args) { | 
					
						
							| 
									
										
										
										
											2022-04-11 14:36:02 -04:00
										 |  |  |   return writeLog(LOGLEVEL_ERROR,msg,fmt::make_printf_args(args...)); | 
					
						
							| 
									
										
										
										
											2022-04-10 23:12:02 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void initLog(); | 
					
						
							| 
									
										
										
										
											2021-05-11 16:08:08 -04:00
										 |  |  | #endif
 |