SMORES Robot Platform Simulation
Modlab at Penn, ASL at Cornell
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends
ColorLog.hh
1 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 // Author: Joel Sjögren
3 // Description: This is a utility header file that help us to output
4 // color log texts. More information anout this can be
5 // found from its original location: http://stackoverflow.com/
6 // questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 #ifndef _COLOR_LOG_HH_
9 #define _COLOR_LOG_HH_
10 #include <ostream>
12 using std::cout;
13 using std::endl;
15 namespace Color {
17 enum Code {
18  FG_RED = 31,
19  FG_GREEN = 32,
20  FG_BLUE = 34,
21  FG_DEFAULT = 39,
22  BG_RED = 41,
23  BG_GREEN = 42,
24  BG_BLUE = 44,
25  BG_DEFAULT = 49,
26  FG_YELLOW = 33,
27 };
29 class Modifier {
31  Code code;
33  bool bold;
34  public:
36 
39  Modifier(Code pCode) : code(pCode) {bold = false;}
41 
45  Modifier(Code pCode,bool pBold) : code(pCode) {bold = pBold;}
46  friend std::ostream&
48 
53  operator<<(std::ostream& os, const Modifier& mod) {
54  if (mod.bold) {
55  return os << "\033[1;" << mod.code << "m";
56  }
57  return os << "\033[0;" << mod.code << "m";
58  }
59 };
61 
64 inline void Warning(const char* message_str)
65 {
66  Color::Modifier yellow_log(Color::FG_YELLOW, true);
67  Color::Modifier def_log(Color::FG_DEFAULT);
68  cout<<yellow_log<<"[Warning] "<<message_str<<def_log<<endl;
69 }
71 
74 inline void Error(const char* message_str)
75 {
76  Color::Modifier red_log(Color::FG_RED, true);
77  Color::Modifier def_log(Color::FG_DEFAULT);
78  cout<<red_log<<"[Error] "<<message_str<<def_log<<endl;
79 }
80 }
81 #endif
For colored log texts in terminal.
Definition: ColorLog.hh:15
Modifier(Code pCode)
Constructor.
Definition: ColorLog.hh:39
Can be used to generate a specific type of color logs.
Definition: ColorLog.hh:29
void Error(const char *message_str)
Error message log function.
Definition: ColorLog.hh:74
Modifier(Code pCode, bool pBold)
Constructor.
Definition: ColorLog.hh:45
friend std::ostream & operator<<(std::ostream &os, const Modifier &mod)
Defines a special color log operator.
Definition: ColorLog.hh:53
Code
Color code definition.
Definition: ColorLog.hh:17
void Warning(const char *message_str)
Warning message log function.
Definition: ColorLog.hh:64