Browse Source

Add logging functions

pull/2/head
Nubben 5 years ago
parent
commit
699af03df6
  1. 14
      src/CommandLineParams.cpp
  2. 3
      src/CommandLineParams.h
  3. 76
      src/Log.cpp
  4. 47
      src/Log.h
  5. 17
      src/StringUtils.cpp
  6. 1
      src/StringUtils.h

14
src/CommandLineParams.cpp

@ -25,3 +25,17 @@ std::string getCLParamByIndex(unsigned int i) { @@ -25,3 +25,17 @@ std::string getCLParamByIndex(unsigned int i) {
}
return "";
}
std::string getRelativeCLParam(std::string const& param, int i) {
for (unsigned int index = 0; index < params.size(); ++index) {
if (params[index] == param) {
int si = static_cast<int>(index);
unsigned int finalPos = static_cast<unsigned int>(si + i);
if (finalPos < params.size()) {
return params[finalPos];
}
return "";
}
}
return "";
}

3
src/CommandLineParams.h

@ -11,4 +11,7 @@ bool getCLParam(std::string const& param); @@ -11,4 +11,7 @@ bool getCLParam(std::string const& param);
// 0 is the program name
std::string getCLParamByIndex(unsigned int i);
// USAGE: getRelativeCLParam("-example", 1) gives the param after -example
std::string getRelativeCLParam(std::string const& param, int i);
#endif

76
src/Log.cpp

@ -0,0 +1,76 @@ @@ -0,0 +1,76 @@
#include "Log.h"
#include "CommandLineParams.h"
#include <streambuf>
#include <map>
#include "StringUtils.h"
namespace {
class NoStream : public std::streambuf {
public:
int overflow(int c) { return c; }
};
NoStream noStream;
std::ostream nothing (&noStream);
std::map<LogType, Logger> loggerCache;
std::ostream& log(LogType lt) {
auto cacheEntry = loggerCache.find(lt);
if (cacheEntry == loggerCache.end()) {
auto& outStream = ((lt == LogType::ERROR) ? std::cerr : std::cout);
auto newEntry = std::make_pair(lt, Logger(lt, outStream));
loggerCache.insert(newEntry);
}
return loggerCache[lt]();
}
}
Logger::Logger() :
logType(LogType::DEBUG),
outStream(std::cout),
shouldLog(true) {
}
Logger::Logger(LogType lt, std::ostream& out) :
logType(lt),
outStream(out),
shouldLog(false) {
std::string specifiedLog = toLowercase(getRelativeCLParam("-log", 1));
LogType logLevel = LogType::ERROR;
if (specifiedLog == "warning") {
logLevel = LogType::WARNING;
} else if (specifiedLog == "info") {
logLevel = LogType::INFO;
} else if (specifiedLog == "debug") {
logLevel = LogType::DEBUG;
}
if (lt <= logLevel) {
shouldLog = true;
}
}
std::ostream& Logger::operator()() {
if (shouldLog) {
return outStream;
}
return nothing;
}
std::ostream& logError() {
return log(LogType::ERROR);
}
std::ostream& logWarning() {
return log(LogType::WARNING);
}
std::ostream& logInfo() {
return log(LogType::INFO);
}
std::ostream& logDebug() {
return log(LogType::DEBUG);
}

47
src/Log.h

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
#ifndef LOG_H
#define LOG_H
#include <iostream>
/*
USAGE:
The functions provided returns standard output/error streams depending
on the log level specified at startup. Example usage:
logWarning() << "WARNING WARNING" << std::endl;
You can create custom logger objects if you want different output
streams. You can then invoke these objects with function syntax:
Logger loggg (LogLevel::INFO, std::cerr);
loggg() << "HEY" << std::endl;
Use the command line flag "-log <logtype>" to print logtypes higher
than error. For example, if you specify "-log info", then the program
will print out info, and all lower prioritized logtypes (warning+error).
*/
enum class LogType {
ERROR,
WARNING,
INFO,
DEBUG,
};
class Logger {
public:
Logger();
Logger(LogType lt, std::ostream& out);
std::ostream& operator()();
private:
LogType logType;
std::ostream& outStream;
bool shouldLog;
};
std::ostream& logError();
std::ostream& logWarning();
std::ostream& logInfo();
std::ostream& logDebug();
#endif

17
src/StringUtils.cpp

@ -1,5 +1,8 @@ @@ -1,5 +1,8 @@
#include "StringUtils.h"
#include <iterator>
#include <algorithm>
/**
* get an extension from a filename
* @param a filename string
@ -50,3 +53,17 @@ const std::string getHostFromURL(const std::string &url) { @@ -50,3 +53,17 @@ const std::string getHostFromURL(const std::string &url) {
}
return "";
}
/**
* convert string to lowercase
* @param str string
* @return lowercased version of str
*/
const std::string toLowercase(const std::string &str) {
std::string returnString = "";
std::transform(str.begin(),
str.end(),
back_inserter(returnString),
tolower);
return returnString;
}

1
src/StringUtils.h

@ -6,5 +6,6 @@ @@ -6,5 +6,6 @@
std::string getFilenameExtension(std::string const& fileName);
const std::string getDocumentFromURL(const std::string &url);
const std::string getHostFromURL(const std::string &url);
const std::string toLowercase(const std::string &str);
#endif

Loading…
Cancel
Save