WPILibC++  2018.4.1-20180926150325-1206-g8b1274d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Log.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2016-2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <chrono>
11 #include <string>
12 
13 #include <wpi/SmallString.h>
14 #include <wpi/raw_ostream.h>
15 
16 inline std::string NowTime();
17 
18 enum TLogLevel {
19  logNONE,
20  logERROR,
21  logWARNING,
22  logINFO,
23  logDEBUG,
24  logDEBUG1,
25  logDEBUG2,
26  logDEBUG3,
27  logDEBUG4
28 };
29 
30 class Log {
31  public:
32  Log();
33  virtual ~Log();
34  wpi::raw_ostream& Get(TLogLevel level = logINFO);
35 
36  public:
37  static TLogLevel& ReportingLevel();
38  static std::string ToString(TLogLevel level);
39  static TLogLevel FromString(const std::string& level);
40 
41  protected:
43  wpi::raw_svector_ostream oss{buf};
44 
45  private:
46  Log(const Log&);
47  Log& operator=(const Log&);
48 };
49 
50 inline Log::Log() {}
51 
52 inline wpi::raw_ostream& Log::Get(TLogLevel level) {
53  oss << "- " << NowTime();
54  oss << " " << ToString(level) << ": ";
55  if (level > logDEBUG) {
56  oss << std::string(level - logDEBUG, '\t');
57  }
58  return oss;
59 }
60 
61 inline Log::~Log() {
62  oss << "\n";
63  wpi::errs() << oss.str();
64 }
65 
66 inline TLogLevel& Log::ReportingLevel() {
67  static TLogLevel reportingLevel = logDEBUG4;
68  return reportingLevel;
69 }
70 
71 inline std::string Log::ToString(TLogLevel level) {
72  static const char* const buffer[] = {"NONE", "ERROR", "WARNING",
73  "INFO", "DEBUG", "DEBUG1",
74  "DEBUG2", "DEBUG3", "DEBUG4"};
75  return buffer[level];
76 }
77 
78 inline TLogLevel Log::FromString(const std::string& level) {
79  if (level == "DEBUG4") return logDEBUG4;
80  if (level == "DEBUG3") return logDEBUG3;
81  if (level == "DEBUG2") return logDEBUG2;
82  if (level == "DEBUG1") return logDEBUG1;
83  if (level == "DEBUG") return logDEBUG;
84  if (level == "INFO") return logINFO;
85  if (level == "WARNING") return logWARNING;
86  if (level == "ERROR") return logERROR;
87  if (level == "NONE") return logNONE;
88  Log().Get(logWARNING) << "Unknown logging level '" << level
89  << "'. Using INFO level as default.";
90  return logINFO;
91 }
92 
93 using FILELog = Log; // NOLINT
94 
95 #define FILE_LOG(level) \
96  if (level > FILELog::ReportingLevel()) \
97  ; \
98  else \
99  Log().Get(level)
100 
101 inline std::string NowTime() {
103  wpi::raw_svector_ostream oss(buf);
104 
105  using std::chrono::duration_cast;
106 
107  auto now = std::chrono::system_clock::now().time_since_epoch();
108 
109  // Hours
110  auto count = duration_cast<std::chrono::hours>(now).count() % 24;
111  if (count < 10) oss << "0";
112  oss << count << ":";
113 
114  // Minutes
115  count = duration_cast<std::chrono::minutes>(now).count() % 60;
116  if (count < 10) oss << "0";
117  oss << count << ":";
118 
119  // Seconds
120  count = duration_cast<std::chrono::seconds>(now).count() % 60;
121  if (count < 10) oss << "0";
122  oss << count << ".";
123 
124  // Milliseconds
125  oss << duration_cast<std::chrono::milliseconds>(now).count() % 1000;
126 
127  return oss.str();
128 }
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
Definition: Log.h:30
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:509
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(adl_begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition: STLExtras.h:941
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.