Improve debug logger to write to file as well (#115)
This commit is contained in:
parent
51552e5f01
commit
73be756123
@ -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
34
src/cpp/logger.cpp
Normal 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
41
src/cpp/logger.h
Normal 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
|
||||
@ -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"),
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user