1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Logger.cpp Mon Jan 04 15:45:12 2021 +0100
1.3 @@ -0,0 +1,78 @@
1.4 +/**
1.5 + * DJM-Fix
1.6 + * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 3 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
1.19 + */
1.20 +#include <chrono>
1.21 +#include <iomanip>
1.22 +#include <sstream>
1.23 +
1.24 +#include "Logger.h"
1.25 +
1.26 +namespace djmfix {
1.27 +namespace logging {
1.28 +
1.29 +class Blackhole : public Logger {
1.30 +public:
1.31 +
1.32 + virtual void log(Level level, const std::string& message) override {
1.33 + }
1.34 +};
1.35 +
1.36 +class LoggerImpl : public Logger {
1.37 +private:
1.38 + std::ostream& output;
1.39 + Level minLevel;
1.40 +
1.41 + std::string getTimestamp() {
1.42 + auto now = std::chrono::system_clock::now();
1.43 + auto itt = std::chrono::system_clock::to_time_t(now);
1.44 + std::ostringstream ss;
1.45 + ss << std::put_time(localtime(&itt), "%FT%T%z");
1.46 + return ss.str();
1.47 + }
1.48 +
1.49 + std::string toString(Level level) {
1.50 + if (level == Level::SEVERE) return "SEVERE";
1.51 + else if (level == Level::WARNING) return "WARNING";
1.52 + else if (level == Level::INFO) return "INFO";
1.53 + else if (level == Level::CONFIG) return "CONFIG";
1.54 + else if (level == Level::FINE) return "FINE";
1.55 + else if (level == Level::FINER) return "FINER";
1.56 + else if (level == Level::FINEST) return "FINEST";
1.57 + else return "UNKNOWN";
1.58 + }
1.59 +
1.60 +public:
1.61 +
1.62 + LoggerImpl(std::ostream& output, Level minLevel) : output(output), minLevel(minLevel) {
1.63 + }
1.64 +
1.65 + virtual void log(Level level, const std::string& message) override {
1.66 + if (level <= minLevel) {
1.67 + output << getTimestamp() << " " << std::setw(8) << toString(level) << ": " << message << std::endl;
1.68 + }
1.69 + }
1.70 +};
1.71 +
1.72 +Logger* create(std::ostream& output, Level minLevel) {
1.73 + return new LoggerImpl(output, minLevel);
1.74 +}
1.75 +
1.76 +Logger* blackhole() {
1.77 + return new Blackhole();
1.78 +}
1.79 +
1.80 +}
1.81 +}