]> git.vomp.tv Git - vompclient.git/blob - log.h
Implement new config for command line args
[vompclient.git] / log.h
1 /*
2     Copyright 2021 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #ifndef LOGNT_H
21 #define LOGNT_H
22
23 #include <string>
24 #include <cstdarg>
25 #include <iostream>
26 #include <fstream>
27 #include <mutex>
28 #include <thread>
29 #include <iomanip>
30 #include <fmt/core.h>
31 #include <fmt/ostream.h>
32 #include <sys/time.h>
33
34 #include "defines.h"
35
36
37 class LogNT
38 {
39   public:
40     LogNT();
41     ~LogNT();
42     static LogNT* getInstance();
43
44     bool init(const std::string& fileName, bool enabled);
45
46     enum levels { TRACE, DEBUG, INFO, WARN, ERROR, CRIT };
47
48     template<typename... Ts> void trace(const char* TAG, Ts... args) { ilog(TAG, TRACE, args...); }
49     template<typename... Ts> void debug(const char* TAG, Ts... args) { ilog(TAG, DEBUG, args...); }
50     template<typename... Ts> void info (const char* TAG, Ts... args) { ilog(TAG, INFO,  args...); }
51     template<typename... Ts> void warn (const char* TAG, Ts... args) { ilog(TAG, WARN,  args...); }
52     template<typename... Ts> void error(const char* TAG, Ts... args) { ilog(TAG, ERROR, args...); }
53     template<typename... Ts> void crit (const char* TAG, Ts... args) { ilog(TAG, CRIT,  args...); }
54
55     void setExternLogger(void*) {};
56     void unsetExternLogger() {};
57
58
59     template<typename T, typename... Ts>
60     void ilog(const char* TAG, int level, T fmtString, Ts... args)
61     {
62       if (!enabled) return;
63
64       struct timeval tv;
65       struct tm tms;
66       gettimeofday(&tv, NULL);
67       LOCALTIME_R(&tv.tv_sec, &tms);
68
69       std::lock_guard<std::mutex> lg(outLock);
70
71       *outstream << std::setfill('0')
72         << std::setw(2) << tms.tm_hour << ":"
73         << std::setw(2) << tms.tm_min  << ":"
74         << std::setw(2) << tms.tm_sec << "."
75         << std::setw(6) << tv.tv_usec;
76
77       switch(level)
78       {
79         case TRACE: *outstream << " [trace] "; break;
80         case DEBUG: *outstream << " [debug] "; break;
81         case INFO:  *outstream << " [info]  "; break;
82         case WARN:  *outstream << " [warn]  "; break;
83         case ERROR: *outstream << " [error] "; break;
84         case CRIT:  *outstream << " [CRIT]  "; break;
85       }
86
87       *outstream << " " << std::this_thread::get_id() << " " << TAG << " - ";
88       fmt::print(*outstream, fmtString, args...);
89       *outstream << std::endl;
90     }
91
92   private:
93     static LogNT* instance;
94
95     bool enabled{};
96     std::string fileName;
97     std::ofstream logFile;
98     std::mutex outLock;
99     std::ostream* outstream;
100 };
101
102 #endif