Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / samples / common / samples / slog.hpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4
5 /**
6  * @brief a header file with logging facility for common samples
7  * @file log.hpp
8  */
9
10 #pragma once
11
12 #include <string>
13
14 namespace slog {
15
16 /**
17  * @class LogStreamEndLine
18  * @brief The LogStreamEndLine class implements an end line marker for a log stream
19  */
20 class LogStreamEndLine { };
21
22 static constexpr LogStreamEndLine endl;
23
24
25 /**
26  * @class LogStream
27  * @brief The LogStream class implements a stream for sample logging
28  */
29 class LogStream {
30     std::string _prefix;
31     std::ostream* _log_stream;
32     bool _new_line;
33
34 public:
35     /**
36      * @brief A constructor. Creates an LogStream object
37      * @param prefix The prefix to print
38      */
39     LogStream(const std::string &prefix, std::ostream& log_stream)
40             : _prefix(prefix), _new_line(true) {
41         _log_stream = &log_stream;
42     }
43
44     /**
45      * @brief A stream output operator to be used within the logger
46      * @param arg Object for serialization in the logger message
47      */
48     template<class T>
49     LogStream &operator<<(const T &arg) {
50         if (_new_line) {
51             (*_log_stream) << "[ " << _prefix << " ] ";
52             _new_line = false;
53         }
54
55         (*_log_stream) << arg;
56         return *this;
57     }
58
59     // Specializing for LogStreamEndLine to support slog::endl
60     LogStream& operator<< (const LogStreamEndLine &/*arg*/) {
61         _new_line = true;
62
63         (*_log_stream) << std::endl;
64         return *this;
65     }
66 };
67
68
69 static LogStream info("INFO", std::cout);
70 static LogStream warn("WARNING", std::cout);
71 static LogStream err("ERROR", std::cerr);
72
73 }  // namespace slog