79 lines
1.7 KiB
C++
79 lines
1.7 KiB
C++
|
|
#include "log.h"
|
||
|
|
|
||
|
|
Log::Log(std::string file_name, int max_size) :
|
||
|
|
log_file{nullptr}
|
||
|
|
{
|
||
|
|
bool out_mode = false;
|
||
|
|
if(std::filesystem::exists(file_name))
|
||
|
|
if(std::filesystem::file_size(file_name) < max_size)
|
||
|
|
log_file_s.open(file_name, std::fstream::app);
|
||
|
|
else {
|
||
|
|
out_mode = true;
|
||
|
|
std::cout << "Log file size greater than: " << max_size << std::endl;
|
||
|
|
std::cout << "Truncating..." << std::endl;
|
||
|
|
log_file_s.open(file_name, std::fstream::out);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
log_file_s.open(file_name, std::fstream::out);
|
||
|
|
out_mode = true;
|
||
|
|
}
|
||
|
|
if(log_file_s.is_open())
|
||
|
|
{
|
||
|
|
std::cout << "Logs openned in " << (( out_mode ) ? "truncate" : "append") << " mode" << std::endl;
|
||
|
|
log_file.rdbuf(log_file_s.rdbuf());
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
std::cout << "Unable to open log file" << std::endl;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::Log(int *other) :
|
||
|
|
log_file{nullptr}
|
||
|
|
{
|
||
|
|
if(other == nullptr)
|
||
|
|
return;
|
||
|
|
exit(-1);
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::~Log()
|
||
|
|
{
|
||
|
|
if(log_file_s.is_open()) {
|
||
|
|
log_file_s.flush();
|
||
|
|
log_file_s.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Log::Log(const Log &other):
|
||
|
|
log_file{nullptr}
|
||
|
|
{
|
||
|
|
log_file.rdbuf(other.log_file.rdbuf());
|
||
|
|
}
|
||
|
|
|
||
|
|
void Log::copyFromOther(const Log &other)
|
||
|
|
{
|
||
|
|
log_file.rdbuf(other.log_file.rdbuf());
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Log::isOpen()
|
||
|
|
{
|
||
|
|
return log_file_s.is_open();
|
||
|
|
}
|
||
|
|
|
||
|
|
std::ostream &Log::rdbuf()
|
||
|
|
{
|
||
|
|
return log_file;
|
||
|
|
}
|
||
|
|
|
||
|
|
Log &Log::operator<<(std::ostream &(*f)(std::ostream &)) {
|
||
|
|
if(f == (std::basic_ostream<char>& (*)(std::basic_ostream<char>&)) &std::endl) {
|
||
|
|
log_file << f;
|
||
|
|
std::cout << f;
|
||
|
|
}
|
||
|
|
return *this;
|
||
|
|
}
|
||
|
|
|
||
|
|
Log &Log::operator<<(std::filesystem::path p) {
|
||
|
|
log_file << p.string();
|
||
|
|
std::cout << p.string();
|
||
|
|
return *this;
|
||
|
|
}
|