From 60a2b0c0fe00c0a2eca030e462fa2492e58ae6e1 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Fri, 3 Sep 2021 17:16:54 +0100 Subject: [PATCH] New log class using {fmt} --- GNUmakefile | 5 +-- log.cc | 40 +++++++++++++++++++++ log.h | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cc | 47 +++++++++++++++++------- objects.mk | 2 +- oldlog.h | 6 ++++ 6 files changed, 186 insertions(+), 15 deletions(-) create mode 100644 log.cc create mode 100644 log.h diff --git a/GNUmakefile b/GNUmakefile index d3a4878..1e8c818 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -93,7 +93,7 @@ LDFLAGS = -Wall $(PICTURES) $(SYSROOT) \ -Wl,-rpath-link,$(CROSSROOT)/opt/vc/lib -Wl,-rpath-link,$(CROSSROOT)/usr/lib/arm-linux-gnueabihf \ -L=/opt/vc/lib -L=/usr/lib/arm-linux-gnueabihf LIBPATHS = -LIBS = -lpthread -lstdc++ -lrt -lbrcmEGL -lbrcmOpenVG -lopenmaxil -lbcm_host -lavformat -lavcodec \ +LIBS = -lpthread -lstdc++ -lrt -lbrcmEGL -lbrcmOpenVG -lopenmaxil -lbcm_host -lavformat -lavcodec -lfmt \ -lavutil -lswresample -lm -ldl -lfontconfig -lfreetype -lMagick++-6.Q16 -lMagickWand-6.Q16 -lMagickCore-6.Q16 -ljsoncpp OBJECTS = $(OBJ_COMMON) $(OBJ_RASPBERRY) INCLUDES = -isystem=/usr/include/arm-linux-gnueabihf -isystem=/opt/vc/include -isystem=/usr/include/freetype2 -isystem=/usr/include/arm-linux-gnueabihf/ImageMagick-6 -isystem=/usr/include/ImageMagick-6 @@ -104,7 +104,8 @@ endif -CXXFLAGS_DEV = $(DEFINES) -DDEV -g -O0 -Wall -Wextra -Wshadow -Werror=return-type -Wmissing-format-attribute -Wdisabled-optimization -Wmissing-declarations -Wmissing-noreturn -Winit-self -Woverloaded-virtual -Wold-style-cast -Wconversion -std=c++14 $(CXXFLAGS_EXTRA) $(INCLUDES) +#CXXFLAGS_DEV = $(DEFINES) -DDEV -g -O0 -Wall -Wextra -Wshadow -Werror=return-type -Wmissing-format-attribute -Wdisabled-optimization -Wmissing-declarations -Wmissing-noreturn -Winit-self -Woverloaded-virtual -Wold-style-cast -Wconversion -std=c++14 $(CXXFLAGS_EXTRA) $(INCLUDES) +CXXFLAGS_DEV = $(DEFINES) -DDEV -g -O0 -std=c++14 $(CXXFLAGS_EXTRA) $(INCLUDES) CXXFLAGS_REL = $(DEFINES) -O3 -Wall -Werror -std=c++14 $(CXXFLAGS_EXTRA) $(INCLUDES) .PHONY: clean fresh all install strip diff --git a/log.cc b/log.cc new file mode 100644 index 0000000..8eccbb1 --- /dev/null +++ b/log.cc @@ -0,0 +1,40 @@ +#include + +#include "log.h" + +LogNT* LogNT::instance = NULL; + +LogNT::LogNT() +{ + instance = this; +} + +LogNT::~LogNT() +{ + instance = NULL; +} + +LogNT* LogNT::getInstance() +{ + return instance; +} + +bool LogNT::init(const std::string& tfileName, bool tenabled) +{ + if (!tenabled) return true; + + enabled = true; + fileName = tfileName; // Keep the filename for later? + + if (!fileName.compare("stdout")) + { + outstream = &std::cout; + return true; + } + + outstream = &logFile; + logFile.open(fileName); + if (!logFile.is_open()) return false; + + return true; +} diff --git a/log.h b/log.h new file mode 100644 index 0000000..5312544 --- /dev/null +++ b/log.h @@ -0,0 +1,101 @@ +/* + Copyright 2021 Chris Tallon + + This file is part of VOMP. + + VOMP 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. + + VOMP 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 VOMP. If not, see . +*/ + +#ifndef LOGNT_H +#define LOGNT_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "defines.h" + + +class LogNT +{ + public: + LogNT(); + ~LogNT(); + static LogNT* getInstance(); + + bool init(const std::string& fileName, bool enabled); + + enum levels { TRACE, DEBUG, INFO, WARN, ERROR, CRIT }; + + template void trace(const char* TAG, Ts... args) { log(TAG, TRACE, args...); } + template void debug(const char* TAG, Ts... args) { log(TAG, DEBUG, args...); } + template void info (const char* TAG, Ts... args) { log(TAG, INFO, args...); } + template void warn (const char* TAG, Ts... args) { log(TAG, WARN, args...); } + template void error(const char* TAG, Ts... args) { log(TAG, ERROR, args...); } + template void crit (const char* TAG, Ts... args) { log(TAG, CRIT, args...); } + + + + + template + void log(const char* TAG, int level, T fmtString, Ts... args) + { + if (!enabled) return; + + struct timeval tv; + struct tm tms; + gettimeofday(&tv, NULL); + LOCALTIME_R(&tv.tv_sec, &tms); + + std::lock_guard lg(outLock); + + *outstream << std::setfill('0') + << std::setw(2) << tms.tm_hour << ":" + << std::setw(2) << tms.tm_min << ":" + << std::setw(2) << tms.tm_sec << "." + << std::setw(6) << tv.tv_usec; + + switch(level) + { + case TRACE: *outstream << " [trace] "; break; + case DEBUG: *outstream << " [debug] "; break; + case INFO: *outstream << " [info] "; break; + case WARN: *outstream << " [warn] "; break; + case ERROR: *outstream << " [error] "; break; + case CRIT: *outstream << " [CRIT] "; break; + } + + *outstream << " " << std::this_thread::get_id() << " " << TAG << " - "; + fmt::print(*outstream, fmtString, args...); + *outstream << std::endl; + } + + private: + static LogNT* instance; + + bool enabled{}; + std::string fileName; + std::ofstream logFile; + std::mutex outLock; + std::ostream* outstream; +}; + +#endif diff --git a/main.cc b/main.cc index 2abc5ec..b3c06f9 100644 --- a/main.cc +++ b/main.cc @@ -43,6 +43,7 @@ #endif #include "config.h" +#include "log.h" #include "oldlog.h" #include "util.h" #include "control.h" @@ -64,6 +65,7 @@ const std::string& getCommandLineServer(); // NCONFIG Config* config; Log* logger; +LogNT* loggerNT; Control* control; // Temporary, will move to Config system NCONFIG @@ -74,6 +76,8 @@ int fdtty; struct vt_mode old_vtmode; #endif +const char* TAG = "Main"; + // Linux main function and sighandler #ifndef WIN32 int main(int argc, char** argv) @@ -131,7 +135,25 @@ int main(int argc, char** argv) shutdown(1); } - logger->log("Main", Log::INFO, "Starting up..."); + logger->log("Main", Log::INFO, "Old log starting up..."); + + + + loggerNT = new LogNT(); + if (!loggerNT) + { + printf("Failed to create LogNT object\n"); + shutdown(1); + } + + if (!loggerNT->init("stdout", debugEnabled ? 1 : 0)) // NCONFIG x2 + { + printf("Could not initialise log object. Aborting.\n"); + shutdown(1); + } + + //logger->log("Main", Log::INFO, "Starting up..."); + loggerNT->info(TAG, "Starting up..."); // Daemonize -------------------------------------------------------------------------------------------------- @@ -162,7 +184,8 @@ int main(int argc, char** argv) int sigBlockResult = pthread_sigmask(SIG_SETMASK, &set, NULL); if (sigBlockResult) { - logger->log("Main", Log::EMERG, "Could not block signals: %i", sigBlockResult); + //logger->log("Main", Log::EMERG, "Could not block signals: %i", sigBlockResult); + loggerNT->crit(TAG, "Could not block signals: {}", sigBlockResult); shutdown(1); } @@ -170,7 +193,7 @@ int main(int argc, char** argv) std::thread threadSignalReceiver(threadSignalReceiverFunction); threadSignalReceiver.detach(); - logger->log("Main", Log::INFO, "Signal handlers set up successfully"); + loggerNT->info(TAG, "Signal handlers set up successfully"); // VT Switching ----------------------------------------------------------------------------------------------- @@ -183,14 +206,14 @@ int main(int argc, char** argv) ) { - logger->log("Main", Log::EMERG, "Could not open /dev/tty. Please change permissions"); + loggerNT->warn(TAG, "Could not open /dev/tty. Please change permissions"); } else { int free_vt; if (ioctl(fdtty, VT_OPENQRY, &free_vt) == -1 || free_vt == -1) { - logger->log("Main", Log::EMERG, "Could not retrieve free virtual console, please change permissions"); + loggerNT->crit(TAG, "Could not retrieve free virtual console, please change permissions"); } else { @@ -203,20 +226,20 @@ int main(int argc, char** argv) // Test area ------------------------------------------------------------------------------------------------------ - + //shutdown(0); // Run control ---------------------------------------------------------------------------------------------------- control = new Control(); if (!control) { - logger->log("Main", Log::EMERG, "Control module failed to create"); + loggerNT->crit(TAG, "Control module failed to create"); shutdown(1); } if (!control->init(crashed)) { - logger->log("Main", Log::EMERG, "Control module failed to initialise"); + loggerNT->crit(TAG, "Control module failed to initialise"); delete control; control = NULL; shutdown(1); @@ -239,11 +262,11 @@ void threadSignalReceiverFunction() { if(sigwait(&set, &sig)) { - logger->log("Main", Log::CRIT, "Sigwait returned fail - signal handler exiting"); + loggerNT->crit(TAG, "Sigwait returned fail - signal handler exiting"); return; } - logger->log("Main", Log::NOTICE, "Signal received: %i", sig); + loggerNT->info(TAG, "Signal received: {}", sig); if ((sig == SIGINT) || (sig == SIGTERM)) control->stop(); } @@ -260,7 +283,7 @@ void threadSignalReceiverFunction() { delete control; control = NULL; - logger->log("Main", Log::NOTICE, "Control module shut down"); + loggerNT->info(TAG, "Control module shut down"); } #ifdef HANDLE_VT_SWITCHING @@ -270,7 +293,7 @@ void threadSignalReceiverFunction() if (logger) { - logger->log("Main", Log::NOTICE, "Log module shutting down... bye!\n\n"); + loggerNT->info(TAG, "Log module shutting down... bye!\n\n"); logger->shutdown(); delete logger; } diff --git a/objects.mk b/objects.mk index 84ddfa1..05d8e96 100644 --- a/objects.mk +++ b/objects.mk @@ -18,7 +18,7 @@ OBJ_COMMON = util.o control.o thread.o timers.o i18n.o udp4.o udp6.o vdpc.o tcp. wprogressbar.o bitmap.o dvbsubtitles.o tfeed.o vteletextview.o \ teletextdecodervbiebu.o teletxt/txtfont.o movieinfo.o seriesinfo.o \ wmovieview.o wseriesview.o tvmedia.o wtvmedia.o wpictureview.o \ - osdvector.o surfacevector.o buffer.o config.o \ + osdvector.o surfacevector.o buffer.o config.o log.o \ playervideorec.o playervideolive.o playerradiolive.o playerradiorec.o OBJ_RASPBERRY = main.o threadp.o osdopenvg.o \ diff --git a/oldlog.h b/oldlog.h index be9cf27..f9869f9 100644 --- a/oldlog.h +++ b/oldlog.h @@ -52,12 +52,18 @@ class Log int init(int defaultLevel,const char* fileName, int enabled); int shutdown(); + + [[deprecated]] int log(const char *fromModule, int level,const char *message, ...); + + [[deprecated]] void logLongString(const char *fromModule, int level,const char *message); int status(); void upLogLevel(); void downLogLevel(); + [[deprecated]] void setExternLogger(ExternLogger* log); + [[deprecated]] void unsetExternLogger() { extlog=NULL; } -- 2.39.5