]> git.vomp.tv Git - vompclient.git/blob - log.h
New log class using {fmt}
[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) { log(TAG, TRACE, args...); }
49     template<typename... Ts> void debug(const char* TAG, Ts... args) { log(TAG, DEBUG, args...); }
50     template<typename... Ts> void info (const char* TAG, Ts... args) { log(TAG, INFO,  args...); }
51     template<typename... Ts> void warn (const char* TAG, Ts... args) { log(TAG, WARN,  args...); }
52     template<typename... Ts> void error(const char* TAG, Ts... args) { log(TAG, ERROR, args...); }
53     template<typename... Ts> void crit (const char* TAG, Ts... args) { log(TAG, CRIT,  args...); }
54
55
56
57
58     template<typename T, typename... Ts>
59     void log(const char* TAG, int level, T fmtString, Ts... args)
60     {
61       if (!enabled) return;
62
63       struct timeval tv;
64       struct tm tms;
65       gettimeofday(&tv, NULL);
66       LOCALTIME_R(&tv.tv_sec, &tms);
67
68       std::lock_guard<std::mutex> lg(outLock);
69
70       *outstream << std::setfill('0')
71         << std::setw(2) << tms.tm_hour << ":"
72         << std::setw(2) << tms.tm_min  << ":"
73         << std::setw(2) << tms.tm_sec << "."
74         << std::setw(6) << tv.tv_usec;
75
76       switch(level)
77       {
78         case TRACE: *outstream << " [trace] "; break;
79         case DEBUG: *outstream << " [debug] "; break;
80         case INFO:  *outstream << " [info]  "; break;
81         case WARN:  *outstream << " [warn]  "; break;
82         case ERROR: *outstream << " [error] "; break;
83         case CRIT:  *outstream << " [CRIT]  "; break;
84       }
85
86       *outstream << " " << std::this_thread::get_id() << " " << TAG << " - ";
87       fmt::print(*outstream, fmtString, args...);
88       *outstream << std::endl;
89     }
90
91   private:
92     static LogNT* instance;
93
94     bool enabled{};
95     std::string fileName;
96     std::ofstream logFile;
97     std::mutex outLock;
98     std::ostream* outstream;
99 };
100
101 #endif