summaryrefslogtreecommitdiff
path: root/python/openvino/runtime/common/utils/include/samples/slog.hpp
blob: 3f237e5310c14b1f55d468951fbf2599dd41e770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2018-2022 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//

/**
 * @brief a header file with logging facility for common samples
 * @file log.hpp
 */

#pragma once

#include <ostream>
#include <string>
#include <vector>

namespace slog {
/**
 * @class LogStreamEndLine
 * @brief The LogStreamEndLine class implements an end line marker for a log stream
 */
class LogStreamEndLine {};

static constexpr LogStreamEndLine endl;

/**
 * @class LogStreamBoolAlpha
 * @brief The LogStreamBoolAlpha class implements bool printing for a log stream
 */
class LogStreamBoolAlpha {};

static constexpr LogStreamBoolAlpha boolalpha;

/**
 * @class LogStreamFlush
 * @brief The LogStreamFlush class implements flushing for a log stream
 */
class LogStreamFlush {};

static constexpr LogStreamFlush flush;

/**
 * @class LogStream
 * @brief The LogStream class implements a stream for sample logging
 */
class LogStream {
    std::string _prefix;
    std::ostream* _log_stream;
    bool _new_line;

public:
    /**
     * @brief A constructor. Creates an LogStream object
     * @param prefix The prefix to print
     */
    LogStream(const std::string& prefix, std::ostream& log_stream);

    /**
     * @brief A stream output operator to be used within the logger
     * @param arg Object for serialization in the logger message
     */
    template <class T>
    LogStream& operator<<(const T& arg) {
        if (_new_line) {
            (*_log_stream) << "[ " << _prefix << " ] ";
            _new_line = false;
        }

        (*_log_stream) << arg;
        return *this;
    }

    /**
     * @brief Overload output stream operator to print vectors in pretty form
     * [value1, value2, ...]
     */
    template <typename T>
    LogStream& operator<<(const std::vector<T>& v) {
        (*_log_stream) << "[ ";

        for (auto&& value : v)
            (*_log_stream) << value << " ";

        (*_log_stream) << "]";

        return *this;
    }

    // Specializing for LogStreamEndLine to support slog::endl
    LogStream& operator<<(const LogStreamEndLine&);

    // Specializing for LogStreamBoolAlpha to support slog::boolalpha
    LogStream& operator<<(const LogStreamBoolAlpha&);

    // Specializing for LogStreamFlush to support slog::flush
    LogStream& operator<<(const LogStreamFlush&);
};

extern LogStream info;
extern LogStream warn;
extern LogStream err;

}  // namespace slog