error.hxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA-L - Simple Hybesis Algorithm Logger
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms-Logger/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #ifndef MODULE_LOGGER_ERROR_HXX
21 #define MODULE_LOGGER_ERROR_HXX
22 
23 #include <Logger/typedef.hxx>
24 
25 namespace SHA_Logger
26 {
30  class Error
31  {
32  public:
33  // Assert correct JSON construction.
34  ~Error() { assert(this->writer->IsComplete()); }
35 
41  static Ostream& Build(Ostream& os, const String& file, int line, const String& message)
42  {
43  std::unique_ptr<Error> builder = std::unique_ptr<Error>(new Error(os));
44  builder->Write(file, line, message);
45 
46  return os;
47  }
48 
53  static Writer& Build(Writer& writer, const String& file, int line, const String& message)
54  {
55  Write(writer, file, line, message);
56 
57  return writer;
58  }
59 
60  private:
61  Error(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
62  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
63  Error operator=(Error&) {} // Not Implemented
64 
65  bool Write(const String& file, int line, const String& message)
66  { return Write(*this->writer, file, line, message); }
67 
68  static bool Write(Writer& writer, const String& file, int line, const String& message)
69  {
70  writer.StartObject();
71  writer.Key("type");
72  writer.String("error");
73  writer.Key("file");
74  writer.String(file);
75  writer.Key("line");
76  writer.Int(line);
77  writer.Key("message");
78  writer.String("Error: " + message);
79  writer.EndObject();
80 
81  return true;
82  }
83 
84  std::unique_ptr<Stream> stream; // Stream wrapper
85  std::unique_ptr<Writer> writer; // Writer used to fill the stream
86  };
87 }
88 
89 #endif // MODULE_LOGGER_ERROR_HXX
std::unique_ptr< Writer > writer
Definition: error.hxx:85
Error(Ostream &os)
Definition: error.hxx:61
static bool Write(Writer &writer, const String &file, int line, const String &message)
Definition: error.hxx:68
static Ostream & Build(Ostream &os, const String &file, int line, const String &message)
Definition: error.hxx:41
const std::string String
Definition: typedef.hxx:36
std::unique_ptr< Stream > stream
Definition: error.hxx:84
bool Write(const String &file, int line, const String &message)
Definition: error.hxx:65
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
static Writer & Build(Writer &writer, const String &file, int line, const String &message)
Definition: error.hxx:53
Error operator=(Error &)
Definition: error.hxx:63
std::ostream Ostream
Definition: typedef.hxx:37
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33