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>
40 virtual bool LogExtern(const char* message)=0;
48 static LogNT* getInstance();
50 bool init(const std::string& fileName, bool enabled);
52 enum levels { TRACE, DEBUG, INFO, WARN, ERROR, CRIT };
54 template<typename... Ts> void trace(const char* TAG, Ts... args) { ilog(TAG, TRACE, args...); }
55 template<typename... Ts> void debug(const char* TAG, Ts... args) { ilog(TAG, DEBUG, args...); }
56 template<typename... Ts> void info (const char* TAG, Ts... args) { ilog(TAG, INFO, args...); }
57 template<typename... Ts> void warn (const char* TAG, Ts... args) { ilog(TAG, WARN, args...); }
58 template<typename... Ts> void error(const char* TAG, Ts... args) { ilog(TAG, ERROR, args...); }
59 template<typename... Ts> void crit (const char* TAG, Ts... args) { ilog(TAG, CRIT, args...); }
61 void setExternLogger(void*) {};
62 void unsetExternLogger() {};
65 template<typename T, typename... Ts>
66 void ilog(const char* TAG, int level, T fmtString, Ts... args)
72 gettimeofday(&tv, NULL);
73 LOCALTIME_R(&tv.tv_sec, &tms);
75 std::lock_guard<std::mutex> lg(outLock);
77 *outstream << std::setfill('0')
78 << std::setw(2) << tms.tm_hour << ":"
79 << std::setw(2) << tms.tm_min << ":"
80 << std::setw(2) << tms.tm_sec << "."
81 << std::setw(6) << tv.tv_usec;
85 case TRACE: *outstream << " [trace] "; break;
86 case DEBUG: *outstream << " [debug] "; break;
87 case INFO: *outstream << " [info] "; break;
88 case WARN: *outstream << " [warn] "; break;
89 case ERROR: *outstream << " [error] "; break;
90 case CRIT: *outstream << " [CRIT] "; break;
93 *outstream << " " << std::this_thread::get_id() << " " << TAG << " - ";
97 fmt::print(*outstream, fmtString, args...);
99 catch (std::exception& e)
101 fmt::print(*outstream, "<<< EXCEPTION GENERATED BY THIS LOG MESSAGE >>>");
105 *outstream << std::endl;
109 static LogNT* instance;
112 std::string fileName;
113 std::ofstream logFile;
115 std::ostream* outstream;