Ylva And Malin
Macros | Functions
logger.h File Reference
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
Include dependency graph for logger.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LOG_COLOR_RESET   "\033[0m"
 Logger.h //////////////////////////////////////////////////////////////////////////////////////////////// Description. More...
 
#define LOG_COLOR_FG_BLACK   "\033[0;30m"
 
#define LOG_COLOR_FG_RED   "\033[0;31m"
 
#define LOG_COLOR_FG_GREEN   "\033[0;32m"
 
#define LOG_COLOR_FG_YELLOW   "\033[0;33m"
 
#define LOG_COLOR_FG_BLUE   "\033[0;34m"
 
#define LOG_COLOR_FG_MAGENTA   "\033[0;35m"
 
#define LOG_COLOR_FG_CYAN   "\033[0;36m"
 
#define LOG_COLOR_FG_GREY   "\033[0;37m"
 
#define LOG_COLOR_FG_WHITE   "\033[0m"
 
#define LOG_COLOR_BG_BLACK   "\033[0;40m"
 
#define LOG_COLOR_BG_RED   "\033[0;41m"
 
#define LOG_COLOR_BG_GREEN   "\033[0;42m"
 
#define LOG_COLOR_BG_YELLOW   "\033[0;43m"
 
#define LOG_COLOR_BG_BLUE   "\033[0;44m"
 
#define LOG_COLOR_BG_MAGENTA   "\033[0;45m"
 
#define LOG_COLOR_BG_CYAN   "\033[0;46m"
 
#define LOG_COLOR_BG_GREY   "\033[0;47m"
 
#define LOG_COLOR_BG_WHITE   "\033[0m"
 
#define LOG_TERMINAL_PAUSE
 
#define LOG_ERROR(fmt, ...)
 Prints the specified error to stderr and terminates the program. More...
 
#define LOG_WARN(fmt, ...)   log_internal(stderr, "WARN", LOG_COLOR_FG_YELLOW, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
 Prints the specified warning to stderr. Use for non breaking situations. More...
 
#define LOG_DEBUG(fmt, ...)   log_internal(stdout, "DEBUG", LOG_COLOR_FG_CYAN, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
 Prints debug information to stdout. More...
 
#define LOG_INFO(fmt, ...)   log_internal(stdout, "INFO", LOG_COLOR_FG_WHITE, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);
 Prints regular info to stdout. More...
 

Functions

void log_internal (FILE *file, const char *type, const char *color, const char *filepath, const char *func, const int line, const char *fmt,...)
 Actual function which is called by log macros. More...
 
void log_internal (std::FILE *file, const char *type, const char *color, const char *filepath, const char *func, const int line, const char *fmt,...)
 

Macro Definition Documentation

#define LOG_COLOR_BG_BLACK   "\033[0;40m"
#define LOG_COLOR_BG_BLUE   "\033[0;44m"
#define LOG_COLOR_BG_CYAN   "\033[0;46m"
#define LOG_COLOR_BG_GREEN   "\033[0;42m"
#define LOG_COLOR_BG_GREY   "\033[0;47m"
#define LOG_COLOR_BG_MAGENTA   "\033[0;45m"
#define LOG_COLOR_BG_RED   "\033[0;41m"
#define LOG_COLOR_BG_WHITE   "\033[0m"
#define LOG_COLOR_BG_YELLOW   "\033[0;43m"
#define LOG_COLOR_FG_BLACK   "\033[0;30m"
#define LOG_COLOR_FG_BLUE   "\033[0;34m"
#define LOG_COLOR_FG_CYAN   "\033[0;36m"
#define LOG_COLOR_FG_GREEN   "\033[0;32m"
#define LOG_COLOR_FG_GREY   "\033[0;37m"
#define LOG_COLOR_FG_MAGENTA   "\033[0;35m"
#define LOG_COLOR_FG_RED   "\033[0;31m"
#define LOG_COLOR_FG_WHITE   "\033[0m"
#define LOG_COLOR_FG_YELLOW   "\033[0;33m"
#define LOG_COLOR_RESET   "\033[0m"

Logger.h //////////////////////////////////////////////////////////////////////////////////////////////// Description.

Contains basic functionality for categorized logging to console. The logging statements will either print to stdout or to stderr depending on the category of the message.

Logging should be done through the different macros, NOT through the log_internal function!

All messages will be following the specified format: <category>: <file>: <function>: <line_number>: user specified message.

Example: [INFO ]: main.cpp : main : 7: User specified message

The logs must be written with printf syntax, which can be found here: http://en.cppreference.com/w/cpp/io/c/fprintf

note, you must use .c_str() or .data() when sending a std::string to "%s".

Example of usage: #include <string> #include "logger.h"

int main() { std::string str = "hey"; LOG_DEBUG("Logging debug: %s yas", str.c_str()); return 0; }

Will return something like: [DEBUG]: main.cpp : main : 7: Logging debug: hey yas

#define LOG_DEBUG (   fmt,
  ... 
)    log_internal(stdout, "DEBUG", LOG_COLOR_FG_CYAN, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);

Prints debug information to stdout.

Use this for debug related information, can be turned off by defining LOG_NO_DEBUG before including logger.h

#define LOG_ERROR (   fmt,
  ... 
)
Value:
{ \
log_internal(stderr, "ERROR", LOG_COLOR_FG_RED, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__); \
std::exit(EXIT_FAILURE); \
}
#define LOG_TERMINAL_PAUSE
Definition: logger.h:101
void log_internal(FILE *file, const char *type, const char *color, const char *filepath, const char *func, const int line, const char *fmt,...)
Actual function which is called by log macros.
#define LOG_COLOR_FG_RED
Definition: logger.h:54

Prints the specified error to stderr and terminates the program.

This error is supposed to be used for unrecoverable errors.

#define LOG_INFO (   fmt,
  ... 
)    log_internal(stdout, "INFO", LOG_COLOR_FG_WHITE, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);

Prints regular info to stdout.

#define LOG_TERMINAL_PAUSE

Convenience macro for letting the console "hang" on windows systems.

#define LOG_WARN (   fmt,
  ... 
)    log_internal(stderr, "WARN", LOG_COLOR_FG_YELLOW, __FILE__, __func__, __LINE__, fmt, ##__VA_ARGS__);

Prints the specified warning to stderr. Use for non breaking situations.

Function Documentation

void log_internal ( FILE *  file,
const char *  type,
const char *  color,
const char *  filepath,
const char *  func,
const int  line,
const char *  fmt,
  ... 
)

Actual function which is called by log macros.

Note
ATTENTION! Do not use this function! You are supposed to use the logger macros!
void log_internal ( std::FILE *  file,
const char *  type,
const char *  color,
const char *  filepath,
const char *  func,
const int  line,
const char *  fmt,
  ... 
)
inline