comment.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_COMMENT_HXX
21 #define MODULE_LOGGER_COMMENT_HXX
22 
23 #include <Logger/typedef.hxx>
24 
25 namespace SHA_Logger
26 {
30  class Comment
31  {
32  public:
33  // Assert correct JSON construction.
34  ~Comment() { assert(this->writer->IsComplete()); }
35 
43  static Ostream& Build(Ostream& os,
44  const String& message, int level = 0, const String extent = "normal")
45  {
46  std::unique_ptr<Comment> builder = std::unique_ptr<Comment>(new Comment(os));
47  builder->Write(message, level, extent);
48 
49  return os;
50  }
51 
57  const String& message, int level = 0, const String extent = "normal")
58  {
59  Write(writer, message, level, extent);
60 
61  return writer;
62  }
63 
64  private:
65  Comment(Ostream& os) : stream(std::unique_ptr<Stream>(new Stream(os))),
66  writer(std::unique_ptr<Writer>(new Writer(*this->stream))) {}
67  Comment operator=(Comment&) {} // Not Implemented
68 
69  bool Write(const String& message, int level, const String& extent)
70  { return Write(*this->writer, message, level, extent); }
71 
72  static bool Write(Writer& writer, const String& message, int level, const String& extent)
73  {
74  writer.StartObject();
75  writer.Key("type");
76  writer.String("comment");
77  writer.Key("message");
78  writer.String(message);
79  writer.Key("level");
80  writer.Int(level);
81  writer.Key("extent");
82  writer.String(extent);
83  writer.EndObject();
84 
85  return true;
86  }
87 
88  std::unique_ptr<Stream> stream; // Stream wrapper
89  std::unique_ptr<Writer> writer; // Writer used to fill the stream
90  };
91 }
92 
93 #endif // MODULE_LOGGER_COMMENT_HXX
static bool Write(Writer &writer, const String &message, int level, const String &extent)
Definition: comment.hxx:72
static Writer & Build(Writer &writer, const String &message, int level=0, const String extent="normal")
Definition: comment.hxx:56
static Ostream & Build(Ostream &os, const String &message, int level=0, const String extent="normal")
Definition: comment.hxx:43
const std::string String
Definition: typedef.hxx:36
std::unique_ptr< Writer > writer
Definition: comment.hxx:89
rapidjson::OStreamWrapper Stream
Definition: typedef.hxx:32
Comment operator=(Comment &)
Definition: comment.hxx:67
std::unique_ptr< Stream > stream
Definition: comment.hxx:88
bool Write(const String &message, int level, const String &extent)
Definition: comment.hxx:69
Comment(Ostream &os)
Definition: comment.hxx:65
std::ostream Ostream
Definition: typedef.hxx:37
rapidjson::PrettyWriter< Stream > Writer
Definition: typedef.hxx:33