Skip to content

Commit

Permalink
Changes to the API. See the README for new usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
evan1026 committed Oct 3, 2013
1 parent be974c0 commit 645324a
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 49 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 2.8)
project(Logger)

set(SOURCE_DIR "src")
set(Logger_VERSION_MAJOR 1)
set(Logger_VERSION_MAJOR 2)
set(Logger_VERSION_MINOR 0)
set(Logger_VERSION_PATCH 0)

Expand Down
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,29 @@ then add the library:
Usage
=====

Just declare a logger:
If you're fine with the default settings (info->white, warning->yellow, error->red), just declare a logger:

Logger logger(bool color); //true if you want unix-style color in the outputs
Logger logger;

Otherwise, make your own:

Settings settings = Logger::Settings(<INFO COLOR STRING>, <WARNING COLOR STRING>, <ERROR COLOR STRING>);
Logger logger = Logger(settings);

For ease, I've put the Linux colors into a namespace called `LogType`. In it, you'll find `Black`, `Red`, `Green`, `Yellow`, `Blue`, `Magenta`, `Cyan`, and `White`.

And you have access to its functions:
There are many functions:

void log(int type, std::string message);
void log(LogType type, std::string message);
void log(std::string message);
void logNoEndl(int type, std::string message);
void logNoEndl(LogType type, std::string message);
void logNoEndl(std::string message);
void pause();
void pause(std::string message);
bool clearLine();
bool logrw(int type, std::string message);
bool logrw(LogType type, std::string message);
bool logrw(std::string message);
bool logrwNoEndl(int type, std::string message);
bool logrwNoEndl(LogType type, std::string message);
bool logrwNoEndl(std::string message);
void continueln(std::string message);
void continuelnNoEndl(std::string message);
Expand All @@ -60,15 +67,15 @@ Explaination

The string is the message you want to log, the type is one of three types of output:

Logger::LOG_INFO
Logger::LOG_WARNING
Logger::LOG_ERROR
Logger::LogType::Info
Logger::LogType::Warning
Logger::LogType::Error

The default type when not given is `LOG_INFO`.
The default type when not given is `LogInfo`.

The commands ending in `NoEndl` do not add a newline (`\n`) at the end of the line. This allows for the current line to be rewritten using the `rw` varieties. The line can also be cleared with `clearLine()`.

Where applicable, functions returning a boolean return whether or not the command was successful. An error indicates that the command could not be carried out (for instance, when clearing/rewriting a line that had a newline (i.e. clearing/rewriting even though previous command was not a `NoEndl` (p.s. nested parentheses ftw))).
Where applicable, functions returning a boolean indicate whether or not the command was successful. An error indicates that the command could not be carried out (for instance, when clearing/rewriting a line that had a newline (i.e. clearing/rewriting even though previous command was not a `NoEndl` (p.s. nested parentheses ftw))).

`pause` outputs `Press enter to continue...` (unless another message is supplied) and then waits for the user to press enter before returning and allowing the program to continue.

Expand All @@ -80,17 +87,17 @@ Example Usage
#include <Logger/Logger.hpp>
Logger logger(true);
logger.log("This is a normal message");
logger.log(Logger::LOG_WARNING, "This one is a warning.");
logger.log(Logger::LOG_ERROR, "This one is an error.");
logger.log(Logger::LogType::Warning, "This one is a warning.");
logger.log(Logger::LogType::Error, "This one is an error.");
logger.pause("Now I'm going to wait until you press enter.");
logger.logNoEndl("I think this line has a tipo.");
logger.logrw("I can correct it though."); //Note: if you actually use it to correct typos, you should maybe get your head examined
logger.logNoEndl("I don't ");
logger.continuelnNoEndl("need to");
logger.continuelnNoEndl(" do outputs in all one line.");
logger.log("The level of fun using variatic templates is over ", 9000, "!!!");
logger.log(Logger::LOG_WARNING, "The level should always go first!");
logger.log("Otherwise it looks like this ->", Logger::LOG_WARNING);
logger.log(Logger::LogType::Warning, "The level should always go first!");
logger.log("Otherwise it looks like this ->", Logger::LogType::Warning);
logger.log(Logger::makeString("Logger::makeString will", " make strings ", "for you..."));
logger.log("But the ", "provided functions ", "are easier and ", "more readable.");

Expand Down
48 changes: 29 additions & 19 deletions src/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#include "Logger.hpp"

Logger::Logger(bool _color){
Logger::Logger() : settings() {
canRewrite = false;
rewriting = false;
lastLength = 0;
color = _color;
}

void Logger::log(int type, std::string message){
Logger::Logger(Settings _settings) : settings(_settings) {
canRewrite = false;
rewriting = false;
lastLength = 0;
}

Logger::Settings::Settings(){
infoColor = "\e[37m";
warningColor = "\e[33m";
errorColor = "\e[31m";
}

void Logger::log(LogType type, std::string message){

this->logNoEndl(type, message);
this->finishLine();
Expand All @@ -16,29 +27,29 @@ void Logger::log(int type, std::string message){
}

void Logger::log(std::string message){
this->log(Logger::LOG_INFO, message);
this->log(Logger::LogType::Info, message);
}

void Logger::logNoEndl(int type, std::string message){
void Logger::logNoEndl(LogType type, std::string message){
if (canRewrite) this->finishLine();

std::string output = "";
std::size_t thisLength = message.length();
switch (type) {
case Logger::LOG_INFO:
output += "[INFO] ";
case Logger::LogType::Info:
output += settings.infoColor + "[INFO] ";
thisLength += 7;
break;
case Logger::LOG_WARNING:
output += (color ? "\e[33m[WARNING] " : "[WARNING] ");
case Logger::LogType::Warning:
output += settings.warningColor + "[WARNING] ";
thisLength += 10;
break;
case Logger::LOG_ERROR:
output += (color ? "\e[31m[ERROR] " : "[ERROR] ");
case Logger::LogType::Error:
output += settings.errorColor + "[ERROR] ";
thisLength += 8;
break;
default:
this->log(LOG_ERROR, "Log type not found");
this->log(Logger::LogType::Error, "Log type not found");
break;
}

Expand All @@ -63,7 +74,7 @@ void Logger::logNoEndl(int type, std::string message){
}

void Logger::logNoEndl(std::string message){
this->logNoEndl(Logger::LOG_INFO, message);
this->logNoEndl(Logger::LogType::Info, message);
}

void Logger::pause(){
Expand Down Expand Up @@ -91,7 +102,7 @@ bool Logger::clearLine(){
return true;
}

bool Logger::logrw(int type, std::string message){
bool Logger::logrw(LogType type, std::string message){
if (!this->clearLine()) return false;

this->log(type, message);
Expand All @@ -100,10 +111,10 @@ bool Logger::logrw(int type, std::string message){
}

bool Logger::logrw(std::string message){
return this->logrw(Logger::LOG_INFO, message);
return this->logrw(Logger::LogType::Info, message);
}

bool Logger::logrwNoEndl(int type, std::string message){
bool Logger::logrwNoEndl(LogType type, std::string message){
if (!this->clearLine()) return false;

this->logNoEndl(type, message);
Expand All @@ -112,7 +123,7 @@ bool Logger::logrwNoEndl(int type, std::string message){
}

bool Logger::logrwNoEndl(std::string message){
return this->logrwNoEndl(Logger::LOG_INFO, message);
return this->logrwNoEndl(Logger::LogType::Info, message);
}

void Logger::continueln(std::string message){
Expand All @@ -127,7 +138,6 @@ void Logger::continuelnNoEndl(std::string message){
}

void Logger::finishLine(){
std::cout << (color ? "\e[m" : "") << std::endl;
std::cout << settings.infoColor << std::endl;
currentLine = "";
}

56 changes: 43 additions & 13 deletions src/Logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,45 @@
#include <sstream>

struct Logger {
static const int LOG_INFO = 0;
static const int LOG_WARNING = 1;
static const int LOG_ERROR = 2;

Logger(bool _color);
enum LogType {
Info,
Warning,
Error
};

struct Settings {

std::string infoColor,
warningColor,
errorColor;

Settings();
Settings(std::string _infoColor, std::string _warningColor, std::string _errorColor) :
infoColor(_infoColor), warningColor(_warningColor), errorColor(_errorColor) {}

};

Settings settings;

void log(int type, std::string message);
Logger();
Logger(Settings _settings);

void log(LogType type, std::string message);
void log(std::string message);

void logNoEndl(int type, std::string message);
void logNoEndl(LogType type, std::string message);
void logNoEndl(std::string message);

void pause();
void pause(std::string message);

bool clearLine();

bool logrw(int type, std::string message);
bool logrw(LogType type, std::string message);
bool logrw(std::string message);

bool logrwNoEndl(int type, std::string message);
bool logrwNoEndl(LogType type, std::string message);
bool logrwNoEndl(std::string message);

void continueln(std::string message);
Expand All @@ -46,7 +64,7 @@ struct Logger {
}

template<class... Ts>
void log(int type, Ts... parts){
void log(LogType type, Ts... parts){
this->log(type, Logger::makeString(parts...));
}
template<class... Ts>
Expand All @@ -55,7 +73,7 @@ struct Logger {
}

template<class... Ts>
void logNoEndl(int type, Ts... parts){
void logNoEndl(LogType type, Ts... parts){
this->logNoEndl(type, Logger::makeString(parts...));
}
template<class... Ts>
Expand All @@ -69,7 +87,7 @@ struct Logger {
}

template<class... Ts>
bool logrw(int type, Ts... parts){
bool logrw(LogType type, Ts... parts){
return this->logrw(type, Logger::makeString(parts...));
}
template<class... Ts>
Expand All @@ -78,7 +96,7 @@ struct Logger {
}

template<class... Ts>
bool logrwNoEndl(int type, Ts... parts){
bool logrwNoEndl(LogType type, Ts... parts){
return this->logrwNoEndl(type, Logger::makeString(parts...));
}
template<class... Ts>
Expand All @@ -98,11 +116,23 @@ struct Logger {
private:
bool canRewrite;
bool rewriting;
bool color;
std::size_t lastLength;
std::string currentLine;

void finishLine();

};

namespace LogColor {
static const std::string Black = "\e[30m";
static const std::string Red = "\e[31m";
static const std::string Green = "\e[32m";
static const std::string Yellow = "\e[33m";
static const std::string Blue = "\e[34m";
static const std::string Magenta = "\e[35m";
static const std::string Cyan = "\e[36m";
static const std::string White = "\e[37m";

};

#endif

0 comments on commit 645324a

Please sign in to comment.