DrawCaml
utils.h
1 #pragma once
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <ctime>
6 #include <thread>
7 #include <atomic>
8 
9 using namespace std;
10 
11 #define RED "\033[31m"
12 #define GREEN "\033[32m"
13 #define YELLOW "\033[33m"
14 #define DEFAULT "\033[0m"
15 
16 // #define DEBUG
17 
18 inline size_t _readableThreadId() {
19  static atomic<size_t> thread_idx{0};
20  thread_local size_t id = thread_idx;
21  thread_idx++;
22  return id;
23 }
24 
25 inline void _print_logs(const string message, const char* level) {
26 #ifdef DEBUG
27  auto time = std::time(nullptr);
28  stringstream msg;
29  msg << "[" << put_time(localtime(&time), "%T") << "][" << level << "] " << _readableThreadId() << ": " << message;
30  cout << msg.str(); // thread safe
31 #endif
32 }
33 
34 inline void LOG(const string message) {
35  _print_logs(message, GREEN "LOG" DEFAULT);
36 }
37 
38 inline void ERROR(const string message) {
39  _print_logs(message, RED "ERROR" DEFAULT);
40 }
41 
42 inline void WARNING(const string message) {
43  _print_logs(message, YELLOW "WARNING" DEFAULT);
44 }