Browse Source

colorful logging

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
191c5034b8

+ 4
- 0
libswan/include/swan/OS.h View File

@@ -1,10 +1,14 @@
#pragma once

#include <string>
#include <ostream>
#include <stdio.h>

namespace Swan {
namespace OS {

bool isTTY(FILE *f);

class Dynlib {
public:
Dynlib(const std::string &path);

+ 14
- 5
libswan/include/swan/log.h View File

@@ -2,6 +2,8 @@

#include <iostream>

#include "OS.h"

namespace Swan {

class Logger {
@@ -23,21 +25,28 @@ public:
std::ostream &os_;
};

Logger(std::ostream &os, std::string name): os_(os), name_(std::move(name)) {}
Logger(std::ostream &os, std::string name, bool use_color = false, std::string color = ""):
os_(os), name_(std::move(name)), use_color_(use_color), color_(std::move(color)) {}

template<typename T>
NewlineStream operator<<(const T &val) {
os_ << name_ << ": " << val;
if (use_color_)
os_ << color_ << name_ << "\033[0m: " << val;
else
os_ << name_ << ": " << val;
return NewlineStream(os_);
}

private:
std::ostream &os_;
std::string name_;
bool use_color_;
std::string color_;
};

static Logger info(std::clog, "info");
static Logger warn(std::clog, "warning");
static Logger panic(std::clog, "panic");
static std::ostream &logstream = std::clog;
static Logger info(std::clog, "info", OS::isTTY(stderr), "\033[36m");
static Logger warn(std::clog, "warning", OS::isTTY(stderr), "\033[33m");
static Logger panic(std::clog, "panic", OS::isTTY(stderr), "\033[1m\033[31m");

}

+ 7
- 0
libswan/src/OS.cc View File

@@ -4,9 +4,16 @@
#include <unistd.h>
#include <dlfcn.h>

#include "log.h"

namespace Swan {
namespace OS {

bool isTTY(FILE *f) {
int fd = fileno(f);
return isatty(fd);
}

Dynlib::Dynlib(const std::string &path) {
handle_ = dlopen((path + ".so").c_str(), RTLD_LAZY);
if (handle_ == NULL) {

+ 3
- 2
libswan/src/World.cc View File

@@ -75,12 +75,13 @@ void World::setCurrentPlane(WorldPlane &plane) {

WorldPlane &World::addPlane(const std::string &gen) {
WorldPlane::ID id = planes_.size();
if (worldgens_.find(gen) == worldgens_.end()) {
auto it = worldgens_.find(gen);
if (it == end(worldgens_)) {
panic << "Tried to add plane with non-existant world gen " << gen << "!";
abort();
}

WorldGen *g = worldgens_[gen]->create(*this);
WorldGen *g = it->second->create(*this);
planes_.push_back(WorldPlane(id, this, std::shared_ptr<WorldGen>(g)));
return planes_[id];
}

+ 2
- 1
libswan/test/lib/test.cc View File

@@ -86,7 +86,8 @@ int main() {
testcase.func();
std::cout
<< "\r" << color(color_highlight + color_success, "✓ ")
<< color(color_success, "Success: ") << "\n";
<< color(color_success, "Success: ")
<< color(color_desc, testcase.description) << "\n";
} catch (const TestFailure &failure) {
failed = true;
std::cout << printFailure(failure).str();

+ 1
- 1
libswan/test/log.t.cc View File

@@ -4,7 +4,7 @@

#include "lib/test.h"

test("basic logging") {
test("Basic logging") {
std::stringstream ostream;
Swan::Logger log(ostream, "test");
log << "Hello World, a number: " << 100 << ", and a string";

Loading…
Cancel
Save