log.h
#ifndef LOG_H
#define LOG_H
#include <vector>
#include <iostream>
#include <string>
struct LogFormat {
std::vector<std::string> perfixs;
LogFormat(const std::vector<std::string> &perfixs = {}) : perfixs(perfixs) {}
std::string perfixsString(){
std::string perfixsString;
for (auto &&one : perfixs) {
perfixsString += "[" + one + "]";
}
return perfixsString;
}
};
struct Stderr : LogFormat {
Stderr(const std::vector<std::string> &perfixs = {}) : LogFormat(perfixs)
{ std::cerr << perfixsString(); }
~Stderr() {std::cerr << std::endl;}
template<class T> Stderr& operator << (const T &t) {
std::cerr << t << " ";
return *this;
}
};
struct Stdout : LogFormat {
Stdout(const std::vector<std::string> &perfixs = {}) : LogFormat(perfixs)
{ std::cout << perfixsString(); }
~Stdout() {std::cout << std::endl;}
template<class T> Stdout& operator << (const T &t) {
std::cout << t << " ";
return *this;
}
};
#define LogErr Stderr({__FILE__, std::to_string(__LINE__), "Error"})
#define LogOut Stdout({__FILE__, std::to_string(__LINE__), "Output"})
#endif // LOG_H
std输出格式的默认兼容, 列表中可添加其他前缀,如下:
#define LogOut Stdout({__DATA__, __FILE__, std::to_string(__LINE__), "Output"})
使用方法:
LogErr << 10 << "string";
LogOut << 10 << "string";
输出格式:
[__DATA__][__FILE__][__LINE__][Output] 10 string