Improve debug logger to write to file as well (#115)

This commit is contained in:
mihacooper 2018-11-14 23:04:43 +03:00 committed by Ilia
parent 51552e5f01
commit 73be756123
5 changed files with 85 additions and 13 deletions

View File

@ -40,7 +40,8 @@ void GC::collect() {
}
// Sweep phase
DEBUG << "Removing " << (objects_.size() - black.size()) << " out of " << objects_.size() << std::endl;
DEBUG("gc") << "Removing " << (objects_.size() - black.size())
<< " out of " << objects_.size() << std::endl;
objects_ = std::move(black);
lastCleanup_.store(std::max(objects_.size(), MINIMUN_SIZE_LEFT));

34
src/cpp/logger.cpp Normal file
View File

@ -0,0 +1,34 @@
#include "logger.h"
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstring>
namespace effil {
static std::unique_ptr<std::ostream> getLoggerStream() {
const char* logFile = getenv("EFFIL_LOG");
if (logFile == nullptr) {
return std::make_unique<std::ostream>(nullptr);
}
else if (strcmp(logFile, "term") == 0) {
return std::make_unique<std::ostream>(std::cout.rdbuf());
}
return std::make_unique<std::ofstream>(logFile);
}
std::mutex Logger::lock_;
std::unique_ptr<std::ostream> Logger::stream_(getLoggerStream());
std::string getCurrentTime() {
const auto currTime = std::time(nullptr);
const auto tm = std::localtime(&currTime);
std::stringstream ss;
ss << std::put_time(tm, "%F %T");
return ss.str();
}
} // namespace effil

41
src/cpp/logger.h Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include <string>
#include <mutex>
#include <memory>
#include <iostream>
#include <thread>
namespace effil {
class Logger
{
public:
Logger()
: lockGuard_(lock_)
{}
~Logger() {
*stream_ << std::endl;
}
std::ostream& getStream() { return *stream_; }
private:
static std::mutex lock_;
static std::unique_ptr<std::ostream> stream_;
std::lock_guard<std::mutex> lockGuard_;
};
std::string getCurrentTime();
#ifdef NDEBUG
# define DEBUG(x) if (false) std::cout
#else
# define DEBUG(name) Logger().getStream() << getCurrentTime() \
<< " " << "[" << std::this_thread::get_id() \
<< "][" << name << "] "
#endif
} // effil

View File

@ -144,7 +144,7 @@ void Thread::runThread(Thread thread,
} catch (const LuaHookStopException&) {
thread.ctx_->changeStatus(Status::Canceled);
} catch (const sol::error& err) {
DEBUG << "Failed with msg: " << err.what() << std::endl;
DEBUG("thread") << "Failed with msg: " << err.what() << std::endl;
auto& returns = thread.ctx_->result();
returns.insert(returns.begin(),
{ createStoredObject("failed"),

View File

@ -1,9 +1,8 @@
#pragma once
#include <iostream>
#include <sstream>
#include <thread>
#include "logger.h"
#include <sstream>
#include <sol.hpp>
#if LUA_VERSION_NUM < 501 || LUA_VERSION_NUM > 503
@ -52,13 +51,10 @@ private:
std::function<void()> f_;
};
} // effil
#define REQUIRE(cond) if (!(cond)) throw effil::Exception()
#define RETHROW_WITH_PREFIX(preff) catch(const effil::Exception& err) { throw effil::Exception() << preff << ": " << err.what(); }
#define RETHROW_WITH_PREFIX(preff) catch(const effil::Exception& err) { \
DEBUG(preff) << err.what(); \
throw effil::Exception() << preff << ": " << err.what(); \
}
#ifdef NDEBUG
#define DEBUG if (false) std::cout
#else
#define DEBUG std::cout << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << " tid:" << std::this_thread::get_id() << " "
#endif
} // namespace effil