-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef LOGGER_H | ||
#define LOGGER_H | ||
|
||
//#define DEBUG | ||
|
||
#ifdef DEBUG | ||
#define LOG(...) logger::log(logger::get_log_header(__FILE__, __LINE__), __VA_ARGS__) | ||
#else | ||
#define LOG(...) | ||
#endif | ||
|
||
#include <iostream> | ||
#include <sstream> | ||
#include <string> | ||
#include <utility> | ||
|
||
class logger { | ||
static std::stringstream log_data; | ||
|
||
public: | ||
static long long start_time; | ||
static long long get_time(); | ||
static std::string get_log_header(const char* filename, int line); | ||
|
||
static void log(); | ||
|
||
template <typename First, typename... Rest> | ||
static void log(First&& first, Rest&&... rest); | ||
|
||
static void print(); | ||
}; | ||
|
||
template<typename First, typename ...Rest> | ||
void logger::log(First && first, Rest && ...rest) { | ||
log_data << std::forward<First>(first) << " "; | ||
log(std::forward<Rest>(rest)...); | ||
} | ||
|
||
#endif // LOGGER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include "logger.h" | ||
|
||
#include <chrono> | ||
#include <cstdlib> | ||
|
||
using namespace std::chrono; | ||
|
||
static const int header_fixed_len = 25; | ||
static const int time_fixed_len = 5; | ||
|
||
std::stringstream logger::log_data; | ||
|
||
long long logger::start_time = 0; | ||
|
||
long long logger::get_time() { | ||
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); | ||
} | ||
|
||
std::string logger::get_log_header(const char* filename, int line) { | ||
std::string header = filename; | ||
auto ll = header.find_last_of('\\'); | ||
if (ll == std::string::npos) { ll = header.find_last_of('/');} | ||
if (ll != std::string::npos) { header = header.substr(ll + 1);} | ||
header += ":" + std::to_string(line) + ":"; | ||
auto len = header_fixed_len - header.length(); | ||
std::string fixed_end = " " + std::string(len > 0 ? len : 0, '.') + " "; | ||
std::string ms = std::to_string(get_time() - start_time); | ||
auto time_len = time_fixed_len - ms.length(); | ||
std::string fixed_time_head = std::string(time_len > 0 ? time_len : 0, ' '); | ||
return fixed_time_head + ms + " " + header + fixed_end; | ||
} | ||
|
||
void logger::log() { | ||
log_data << std::endl; | ||
} | ||
|
||
void logger::print() { | ||
std::string all_data = log_data.str(); | ||
if (!all_data.empty()) { | ||
std::cerr << " time\tfile:line\t\tdata" << std::endl; | ||
std::cerr << all_data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters