2 Copyright 2021 Chris Tallon
4 This file is part of VOMP.
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.
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.
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/>.
31 #include <fmt/ostream.h>
42 static LogNT* getInstance();
44 bool init(const std::string& fileName, bool enabled);
46 enum levels { TRACE, DEBUG, INFO, WARN, ERROR, CRIT };
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...); }
58 template<typename T, typename... Ts>
59 void log(const char* TAG, int level, T fmtString, Ts... args)
65 gettimeofday(&tv, NULL);
66 LOCALTIME_R(&tv.tv_sec, &tms);
68 std::lock_guard<std::mutex> lg(outLock);
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;
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;
86 *outstream << " " << std::this_thread::get_id() << " " << TAG << " - ";
87 fmt::print(*outstream, fmtString, args...);
88 *outstream << std::endl;
92 static LogNT* instance;
96 std::ofstream logFile;
98 std::ostream* outstream;