Mania++
 All Classes Functions Variables Pages
Logging.h
1 #ifndef LOGGING_H_
2 #define LOGGING_H_
3 
4 #include <cstring>
5 #include <cmath>
6 #include "../GbxRemote/GbxStructs.h"
7 #include "../GbxRemote/ServerResponse/GbxServerResponse.h"
8 
9 //* Logging
13 class Logging
14 {
15 public:
21  static void PrintError(GbxError error)
22  {
23  std::stringstream numberString;
24  numberString << error.number;
25  const char* errorNumber = numberString.str().c_str();
26  int errorNumberLength = strlen(errorNumber);
27  int spaces = (10 - errorNumberLength);
28 
29  if(spaces > 1)
30  {
31  double halfSpaces = ((double)spaces / 2);
32 
33  std::stringstream output;
34  output << "[\033[0;31m";
35  for(int i = 0; i < ceil(halfSpaces); i++)
36  {
37  output << " ";
38  }
39  output << error.number;
40  for(int i = 0; i < floor(halfSpaces); i++)
41  {
42  output << " ";
43  }
44 
45  output << "\033[0;0m] ERROR: " + error.message;
46 
47  std::cout << output.str() << std::endl;
48  }
49  else
50  {
51  std::cout << "[\033[0;31m" << errorNumber << "\033[0;0m] ERROR: " << error.message << std::endl;
52  }
53  }
54 
61  static void PrintError(int number, std::string message)
62  {
63  GbxError error = GbxError();
64  error.number = number;
65  error.message = message;
66  PrintError(error);
67  }
68 
72  static void PrintOKFlush()
73  {
74  std::cout << "[ \033[0;32mOK\033[0;0m" << std::endl;
75  }
76 
80  static void PrintFailedFlush()
81  {
82  std::cout << "[ \033[0;31mFAILED\033[0;0m" << std::endl;
83  }
84 
90  static void PrintParameters(std::vector<GbxResponseParameter> parameters)
91  {
92  for(int paramId = 0; paramId < parameters.size(); paramId++)
93  {
94  PrintParameter(parameters.at(paramId), paramId);
95  }
96  }
97 
106  static void PrintParameter(GbxResponseParameter parameter, int paramId, std::string spaces = " ", std::string parameterName = "")
107  {
108  if(parameter.Type.find("array") != std::string::npos)
109  {
110  std::cout << spaces << "Parameter #" << paramId << ": array" << std::endl;
111  if(parameterName != "")
112  {
113  std::cout << " (" << parameterName << ")";
114  }
115  spaces += " ";
116  std::vector<GbxResponseParameter> arrayParam = parameter.GetArray();
117  for(int subParamId = 0; subParamId < arrayParam.size(); subParamId++)
118  {
119  GbxResponseParameter arrayParameter = arrayParam.at(subParamId);
120  PrintParameter(arrayParameter, subParamId, spaces);
121  }
122  }
123  else if(parameter.Type.find("struct") != std::string::npos)
124  {
125  std::cout << spaces << "Parameter #" << paramId << ": struct" << std::endl;
126  if(parameterName != "")
127  {
128  std::cout << " (" << parameterName << ")";
129  }
130  spaces += " ";
131  std::map<std::string, GbxResponseParameter> structParam = parameter.GetStruct();
132  int subParamId = 0;
133  for(std::map<std::string, GbxResponseParameter>::iterator subParam = structParam.begin(); subParam != structParam.end(); ++subParam)
134  {
135  PrintParameter(subParam->second, subParamId, spaces, subParam->first);
136  subParamId++;
137  }
138  }
139  else
140  {
141  std::cout << spaces << "Parameter #" << paramId << ": " << parameter.GetString() << " (" << parameter.Type << ")";
142  if(parameterName != "")
143  {
144  std::cout << " (" << parameterName << ")";
145  }
146  std::cout << std::endl;
147  }
148  }
149 };
150 
151 #endif // LOGGING_H_
std::vector< GbxResponseParameter > GetArray()
Gets the value as vector of parameters.
Definition: GbxServerResponse.h:28
Utility to print information to the console.
Definition: Logging.h:13
std::string GetString()
Gets the value as string.
Definition: GbxServerResponse.h:54
static void PrintParameter(GbxResponseParameter parameter, int paramId, std::string spaces=" ", std::string parameterName="")
Prints a GbxResponseParameter (for DEBUG purposes).
Definition: Logging.h:106
int number
Number of the error (default: 0, no error).
Definition: GbxStructs.h:29
std::string message
Error message.
Definition: GbxStructs.h:30
static void PrintError(GbxError error)
Prints error to console.
Definition: Logging.h:21
std::string Type
XML-RPC data type.
Definition: GbxServerResponse.h:20
static void PrintOKFlush()
Prints OK in [ ]-spaces in console.
Definition: Logging.h:72
static void PrintParameters(std::vector< GbxResponseParameter > parameters)
Prints a vector with GbxResponseParameter objects (for DEBUG purposes).
Definition: Logging.h:90
Stores error details from the communication with the server.
Definition: GbxStructs.h:27
std::map< std::string, GbxResponseParameter > GetStruct()
Gets the value as map of parameters.
Definition: GbxServerResponse.h:41
static void PrintFailedFlush()
Prints Failed in [ ]-spaces in console.
Definition: Logging.h:80
Parameter deducted from server response.
Definition: GbxServerResponse.h:17
static void PrintError(int number, std::string message)
Prints error to console.
Definition: Logging.h:61