added logger name to output

This commit is contained in:
Bob Polis 2020-04-29 16:54:58 +02:00
parent 66cfaa36bc
commit ebae246a6b
3 changed files with 9 additions and 4 deletions

View File

@ -7,10 +7,12 @@
//
#include "logger.hpp"
#include <unistd.h>
using namespace sc;
logger::logger(loglevel level) : _level(level) {}
logger::logger(const std::string& name, loglevel level)
: _level(level), _name(name) {}
void logger::msg(loglevel level, const char* file, int line, const char* func) {
if (level >= _level) {
@ -24,6 +26,7 @@ void logger::msg(loglevel level, const char* file, int line, const char* func) {
}
void logger::write_prefix(loglevel level, const char* file, int line, const char* func) {
std::cerr << file << ":" << line << ", " << func << "() ";
std::cerr << _name << "[" << ::getpid() << "] ";
std::cerr << file << ":" << line << " " << func << "() ";
std::cerr << '[' << loglevel_desc(level) << "] ";
}

View File

@ -10,6 +10,7 @@
#define _logger_H_
#include <iostream>
#include <string>
namespace sc {
@ -40,7 +41,7 @@ namespace sc {
class logger {
public:
logger(loglevel level);
logger(const std::string& name, loglevel level);
void msg(loglevel level, const char* file, int line, const char* func);
@ -68,6 +69,7 @@ namespace sc {
loglevel _level {loglevel::info};
bool _topcall {true};
std::string _name;
};
}

View File

@ -59,7 +59,7 @@ int main(int argc, const char * argv[]) {
}
}
std::cout << "hello, tester\n";
sc::logger logger(sc::loglevel::debug);
sc::logger logger("tester", sc::loglevel::debug);
_info(logger, 42, " = ", 6, " x ", 7);
_debug(logger, "efkes proberen");
} catch (const std::exception& ex) {