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 = (9 - 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 
93  static void PrintParameter(GbxResponseParameter parameter, int paramId, std::string spaces = " ", std::string parameterName = "")
94  {
95  if(parameter.Type.find("array") != std::string::npos)
96  {
97  std::cout << spaces << "Parameter #" << paramId << ": array" << std::endl;
98  spaces += " ";
99  std::vector<GbxResponseParameter> arrayParam = parameter.GetArray();
100  for(int subParamId = 0; subParamId < arrayParam.size(); subParamId++)
101  {
102  GbxResponseParameter arrayParameter = arrayParam.at(subParamId);
103  PrintParameter(arrayParameter, subParamId, spaces);
104  }
105  }
106  else if(parameter.Type.find("struct") != std::string::npos)
107  {
108  std::cout << spaces << "Parameter #" << paramId << ": struct" << std::endl;
109  spaces += " ";
110  std::map<std::string, GbxResponseParameter> structParam = parameter.GetStruct();
111  int subParamId = 0;
112  for(std::map<std::string, GbxResponseParameter>::iterator subParam = structParam.begin(); subParam != structParam.end(); ++subParam)
113  {
114  PrintParameter(subParam->second, subParamId, spaces, subParam->first);
115  subParamId++;
116  }
117  }
118  else
119  {
120  std::cout << spaces << "Parameter #" << paramId << ": " << parameter.GetString() << " (" << parameter.Type << ")";
121  if(parameterName != "")
122  {
123  std::cout << " (" << parameterName << ")";
124  }
125  std::cout << std::endl;
126  }
127  }
128 };
129 
130 #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:93
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
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