]> git.vomp.tv Git - vompclient.git/commitdiff
New log class using {fmt}
authorChris Tallon <chris@vomp.tv>
Fri, 3 Sep 2021 16:16:54 +0000 (17:16 +0100)
committerChris Tallon <chris@vomp.tv>
Fri, 3 Sep 2021 16:16:54 +0000 (17:16 +0100)
GNUmakefile
log.cc [new file with mode: 0644]
log.h [new file with mode: 0644]
main.cc
objects.mk
oldlog.h

index d3a4878eb950a981c34ea13dd0bc30849e090b64..1e8c8186ad4c8be4723cc65e4dea1b55201a23c1 100644 (file)
@@ -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 (file)
index 0000000..8eccbb1
--- /dev/null
+++ b/log.cc
@@ -0,0 +1,40 @@
+#include <iostream>
+
+#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 (file)
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 <https://www.gnu.org/licenses/>.
+*/
+
+#ifndef LOGNT_H
+#define LOGNT_H
+
+#include <string>
+#include <cstdarg>
+#include <iostream>
+#include <fstream>
+#include <mutex>
+#include <thread>
+#include <iomanip>
+#include <fmt/core.h>
+#include <fmt/ostream.h>
+#include <sys/time.h>
+
+#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<typename... Ts> void trace(const char* TAG, Ts... args) { log(TAG, TRACE, args...); }
+    template<typename... Ts> void debug(const char* TAG, Ts... args) { log(TAG, DEBUG, args...); }
+    template<typename... Ts> void info (const char* TAG, Ts... args) { log(TAG, INFO,  args...); }
+    template<typename... Ts> void warn (const char* TAG, Ts... args) { log(TAG, WARN,  args...); }
+    template<typename... Ts> void error(const char* TAG, Ts... args) { log(TAG, ERROR, args...); }
+    template<typename... Ts> void crit (const char* TAG, Ts... args) { log(TAG, CRIT,  args...); }
+
+
+
+
+    template<typename T, typename... Ts>
+    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<std::mutex> 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 2abc5ecb835c6345376005a741c8182e708c4963..b3c06f98abb9f42c7fae0dce015e829dd20bda84 100644 (file)
--- 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;
   }
index 84ddfa1191d00b07242ee15c43a050b06affe0b0..05d8e96b48c9f9b23f9709d4d06af1b80a3f049f 100644 (file)
@@ -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                                       \
index be9cf27269f18ff3e2b4cb66f117b263e860a7a3..f9869f9ae0b91761cf91eaeee32648ccd13638ad 100644 (file)
--- 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;
     }