1
0
Fork 0
mirror of https://github.com/cosmo-sims/monofonIC.git synced 2024-09-19 17:03:45 +02:00
monofonIC/include/logger.hh

152 lines
3.7 KiB
C++
Raw Normal View History

2020-08-21 17:03:33 +02:00
// This file is part of monofonIC (MUSIC2)
// A software package to generate ICs for cosmological simulations
// Copyright (C) 2020 by Oliver Hahn & Michael Michaux (this file)
//
// monofonIC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// monofonIC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-05-07 01:05:16 +02:00
#pragma once
#include <algorithm>
#include <cassert>
#include <cstdarg>
#include <fstream>
#include <iostream>
2020-04-04 20:27:51 +02:00
namespace music {
2019-05-07 01:05:16 +02:00
2020-04-04 20:39:52 +02:00
enum log_level : int {
off = 0,
fatal = 1,
error = 2,
warning = 3,
info = 4,
debug = 5
2019-05-07 01:05:16 +02:00
};
2020-04-04 20:39:52 +02:00
class logger {
2019-05-07 01:05:16 +02:00
private:
2020-04-04 20:39:52 +02:00
static log_level log_level_;
2019-05-07 01:05:16 +02:00
static std::ofstream output_file_;
public:
2020-04-04 20:39:52 +02:00
logger() = default;
~logger() = default;
2019-05-07 01:05:16 +02:00
2020-04-04 20:39:52 +02:00
static void set_level(const log_level &level);
static log_level get_level();
2019-05-07 01:05:16 +02:00
2020-04-04 20:39:52 +02:00
static void set_output(const std::string filename);
static void unset_output();
2019-05-07 01:05:16 +02:00
2020-04-04 20:39:52 +02:00
static std::ofstream &get_output();
2019-05-07 01:05:16 +02:00
2020-04-04 20:39:52 +02:00
template <typename T> logger &operator<<(const T &item) {
2019-05-07 01:05:16 +02:00
std::cout << item;
if (output_file_.is_open()) {
output_file_ << item;
}
return *this;
}
2020-04-04 20:39:52 +02:00
logger &operator<<(std::ostream &(*fp)(std::ostream &)) {
2019-05-07 01:05:16 +02:00
std::cout << fp;
if (output_file_.is_open()) {
output_file_ << fp;
}
return *this;
}
};
2020-04-04 20:39:52 +02:00
class log_stream {
2019-05-07 01:05:16 +02:00
private:
2020-04-04 20:39:52 +02:00
logger &logger_;
log_level stream_level_;
2019-05-07 01:05:16 +02:00
std::string line_prefix_, line_postfix_;
bool newline;
public:
2020-04-04 20:39:52 +02:00
log_stream(logger &logger, const log_level &level)
2019-05-07 01:05:16 +02:00
: logger_(logger), stream_level_(level), newline(true) {
switch (stream_level_) {
2020-04-04 20:39:52 +02:00
case log_level::fatal:
2019-05-07 01:05:16 +02:00
line_prefix_ = "\033[31mFatal : ";
break;
2020-04-04 20:39:52 +02:00
case log_level::error:
2019-05-07 01:05:16 +02:00
line_prefix_ = "\033[31mError : ";
break;
2020-04-04 20:39:52 +02:00
case log_level::warning:
2019-05-07 01:05:16 +02:00
line_prefix_ = "\033[33mWarning : ";
break;
2020-04-04 20:39:52 +02:00
case log_level::info:
2019-05-07 01:05:16 +02:00
//line_prefix_ = " | Info | ";
line_prefix_ = " \033[0m";
break;
2020-04-04 20:39:52 +02:00
case log_level::debug:
2019-05-07 01:05:16 +02:00
line_prefix_ = "Debug : \033[0m";
break;
default:
line_prefix_ = "\033[0m";
break;
}
line_postfix_ = "\033[0m";
}
2020-04-04 20:39:52 +02:00
~log_stream() = default;
2019-05-07 01:05:16 +02:00
inline std::string GetPrefix() const {
return line_prefix_;
}
2020-04-04 20:39:52 +02:00
template <typename T> log_stream &operator<<(const T &item) {
if (logger::get_level() >= stream_level_) {
2019-05-07 01:05:16 +02:00
if (newline) {
logger_ << line_prefix_;
newline = false;
}
logger_ << item;
}
return *this;
}
2020-04-04 20:39:52 +02:00
log_stream &operator<<(std::ostream &(*fp)(std::ostream &)) {
if (logger::get_level() >= stream_level_) {
2019-05-07 01:05:16 +02:00
logger_ << fp;
logger_ << line_postfix_;
newline = true;
}
return *this;
}
inline void Print(const char *str, ...) {
char out[1024];
va_list argptr;
va_start(argptr, str);
vsprintf(out, str, argptr);
va_end(argptr);
std::string out_string = std::string(out);
out_string.erase(std::remove(out_string.begin(), out_string.end(), '\n'),
out_string.end());
(*this) << out_string << std::endl;
}
};
// global instantiations for different levels
2020-04-04 20:39:52 +02:00
extern logger glogger;
extern log_stream flog;
extern log_stream elog;
extern log_stream wlog;
extern log_stream ilog;
extern log_stream dlog;
2019-05-07 01:05:16 +02:00
2020-04-04 20:27:51 +02:00
} // namespace music