logger update, stderr stream is added

This commit is contained in:
Marko Budiselic 2016-08-14 12:43:34 +01:00
parent 2113546b9c
commit a3a8dc7c28
6 changed files with 96 additions and 49 deletions

View File

@ -111,4 +111,3 @@ private:
Log* log;
std::string name;
};

View File

@ -0,0 +1,30 @@
#pragma once
#include <fmt/format.h>
#include <string>
#include "logging/log.hpp"
// TODO: in c++17 replace with logging::format
namespace logging
{
namespace format
{
std::string out = "{} {:<5} [{}] {}\n";
std::string err = out;
// TODO: configurable formats
}
class Formatter
{
public:
static std::string format(const std::string &format,
const Log::Record &record)
{
return fmt::format(format, static_cast<std::string>(record.when()),
record.level_str(), record.where(),
record.text());
}
};
}

View File

@ -0,0 +1,9 @@
#pragma once
#include "logging/log.hpp"
class Stderr : public Log::Stream
{
public:
void emit(const Log::Record&) override;
};

View File

@ -0,0 +1,9 @@
#include "logging/streams/stderr.hpp"
#include <iostream>
#include "logging/streams/format.hpp"
void Stderr::emit(const Log::Record& record)
{
std::cerr << logging::Formatter::format(logging::format::err, record);
}

View File

@ -1,17 +1,9 @@
#include "logging/streams/stdout.hpp"
#include <iostream>
#include <fmt/format.h>
#include "logging/streams/format.hpp"
void Stdout::emit(const Log::Record& record)
{
auto s = fmt::format("{} {:<5} [{}] {}\n", static_cast<std::string>(
record.when()), record.level_str(), record.where(),
record.text());
std::cout << s;
/* fmt::printf("{} {:<5} [{}] {}\n", static_cast<std::string>(record.when()), */
/* record.level_str(), record.where(), record.text()); */
std::cout << logging::Formatter::format(logging::format::out, record);
}

View File

@ -1,5 +1,7 @@
#include <iostream>
#include "logging/default.hpp"
#include "logging/streams/stdout.hpp"
#include "data_structures/concurrent/concurrent_map.hpp"
#include "utils/assert.hpp"
@ -8,59 +10,65 @@ using std::endl;
using skiplist_t = ConcurrentMap<int, int>;
void print_skiplist(const skiplist_t::Accessor &skiplist) {
cout << "---- skiplist now has: ";
void print_skiplist(const skiplist_t::Accessor &skiplist)
{
cout << "---- skiplist now has: ";
for (auto &kv : skiplist)
cout << "(" << kv.first << ", " << kv.second << ") ";
for (auto &kv : skiplist)
cout << "(" << kv.first << ", " << kv.second << ") ";
cout << "----" << endl;
cout << "----" << endl;
}
int main(void) {
skiplist_t skiplist;
auto accessor = skiplist.access();
int main(void)
{
logging::init_async();
logging::log->pipe(std::make_unique<Stdout>());
// insert 10
permanent_assert(accessor.insert(1, 10).second == true, "add first element");
skiplist_t skiplist;
auto accessor = skiplist.access();
// try insert 10 again (should fail)
permanent_assert(accessor.insert(1, 10).second == false,
"add the same element, should fail");
// insert 10
permanent_assert(accessor.insert(1, 10).second == true,
"add first element");
// insert 20
permanent_assert(accessor.insert(2, 20).second == true,
"insert new unique element");
// try insert 10 again (should fail)
permanent_assert(accessor.insert(1, 10).second == false,
"add the same element, should fail");
print_skiplist(accessor);
// insert 20
permanent_assert(accessor.insert(2, 20).second == true,
"insert new unique element");
// value at key 3 shouldn't exist
permanent_assert((accessor.find(3) == accessor.end()) == true,
"try to find element which doesn't exist");
print_skiplist(accessor);
// value at key 2 should exist
permanent_assert((accessor.find(2) != accessor.end()) == true,
"find iterator");
// value at key 3 shouldn't exist
permanent_assert((accessor.find(3) == accessor.end()) == true,
"try to find element which doesn't exist");
// at key 2 is 20 (true)
permanent_assert(accessor.find(2)->second == 20, "find element");
// value at key 2 should exist
permanent_assert((accessor.find(2) != accessor.end()) == true,
"find iterator");
// removed existing (1)
permanent_assert(accessor.remove(1) == true, "try to remove element");
// at key 2 is 20 (true)
permanent_assert(accessor.find(2)->second == 20, "find element");
// removed non-existing (3)
permanent_assert(accessor.remove(3) == false,
"try to remove element which doesn't exist");
// removed existing (1)
permanent_assert(accessor.remove(1) == true, "try to remove element");
// insert (1, 10)
permanent_assert(accessor.insert(1, 10).second == true,
"insert unique element");
// removed non-existing (3)
permanent_assert(accessor.remove(3) == false,
"try to remove element which doesn't exist");
// insert (4, 40)
permanent_assert(accessor.insert(4, 40).second == true,
"insert unique element");
// insert (1, 10)
permanent_assert(accessor.insert(1, 10).second == true,
"insert unique element");
print_skiplist(accessor);
// insert (4, 40)
permanent_assert(accessor.insert(4, 40).second == true,
"insert unique element");
return 0;
print_skiplist(accessor);
return 0;
}