summaryrefslogtreecommitdiff
path: root/inference-engine/src/inference_engine/debug.h
blob: 15792797e06062daa5ddf3d9c7b303b78bd2e82c (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright (C) 2018 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
//

/**
 * \brief Basic debugging tools
 * \file debug.h
 */
#pragma once

#include <cstdlib>
#include <cstdarg>
#include <string>
#include <ctime>
#include <cstdint>
#include <algorithm>
#include <functional>
#include <cctype>
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
#include <numeric>
#include <w_unistd.h>
#include "ie_algorithm.hpp"

#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>

#define POSIX_EPOCH_AS_FILETIME 116444736000000000ULL
#define OPT_USAGE
static void gettimeofday(struct timeval * tp, struct timezone *) {
    SYSTEMTIME  system_time;
    FILETIME    file_time;
    uint64_t    time;

    GetSystemTime(&system_time);
    SystemTimeToFileTime(&system_time, &file_time);

    time = file_time.dwLowDateTime;
    time += static_cast<uint64_t>(file_time.dwHighDateTime) << 32;

    tp->tv_sec = static_cast<long>((time - POSIX_EPOCH_AS_FILETIME) / 10000000L);
    tp->tv_usec = (system_time.wMilliseconds * 1000);
}
#else
#define vsnprintf_s vsnprintf

#include <string.h>
#include <sys/time.h>

#ifndef OPT_USAGE
#ifdef __GNUC__
#define OPT_USAGE __attribute__ ((unused))
#else
#define OPT_USAGE
#endif
#endif
#endif
/// Diff in uSec
inline int64_t operator-(const timeval& lhs, const timeval& rhs) {
    return static_cast<int64_t>(lhs.tv_sec - rhs.tv_sec) * 1000000 + lhs.tv_usec - rhs.tv_usec;
}

namespace InferenceEngine {
namespace details {

/**
* @brief vector serialisation to be used in exception
*/
template <typename T>
inline std::ostream & operator << (std::ostream &out, const std::vector<T> &vec) {
    if (vec.empty()) return std::operator<<(out, "[]");
    out << "[" << vec[0];
    for (unsigned i=1; i < vec.size(); i++) {
        out << ", " << vec[i];
    }
    return out << "]";
}


/**
 * @brief trim from start (in place)
 * @param s - string to trim
 */
inline void ltrim(std::string &s) {
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
}

/**
 * @brief trim from end (in place)
 * @param s - string to trim
 */
inline void rtrim(std::string &s) {
    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
}

/**
 * @brief trim from both ends (in place)
 * @param s - string to trim
 */
inline std::string &trim(std::string &s) {
    ltrim(s);
    rtrim(s);
    return s;
}

/**
 * @brief split string into a vector of substrings
 * @param src - string to split
 * @param delimiter - string used as a delimiter
 * @return vector of substrings
 */
inline std::vector<std::string> split(const std::string &src, const std::string &delimiter) {
    std::vector<std::string> tokens;
    std::string tokenBuf;
    size_t prev = 0,
            pos = 0,
            srcLength = src.length(),
            delimLength = delimiter.length();
    do {
        pos = src.find(delimiter, prev);
        if (pos == std::string::npos) {
            pos = srcLength;
        }
        tokenBuf = src.substr(prev, pos - prev);
        if (!tokenBuf.empty()) {
            tokens.push_back(tokenBuf);
        }
        prev = pos + delimLength;
    } while (pos < srcLength && prev < srcLength);
    return tokens;
}

/**
 * @brief create a string representation for a vector of values
 * @param vec - vector of values
 * @return string representation
 */
template<typename T, typename A>
std::string dumpVec(std::vector<T, A> const &vec) {
    if (vec.empty()) return "[]";
    std::stringstream oss;
    oss << "[" << vec[0];
    for (size_t i = 1; i < vec.size(); i++) oss << "," << vec[i];
    oss << "]";
    return oss.str();
}

/**
 * @brief multiply vector's values
 * @param vec - vector with values
 * @return result of multiplication
 */
template<typename T, typename A>
T product(std::vector<T, A> const &vec) {
    if (vec.empty()) return 0;
    T ret = vec[0];
    for (size_t i = 1; i < vec.size(); ++i) ret *= vec[i];
    return ret;
}

/**
 * @brief check if vectors contain same values
 * @param v1 - first vector
 * @param v2 - second vector
 * @return true if vectors contain same values
 */
template<typename T, typename A>
bool equal(const std::vector<T, A> &v1, const std::vector<T, A> &v2) {
    if (v1.size() != v2.size()) return false;
    for (auto i1 = v1.cbegin(), i2 = v2.cbegin(); i1 != v1.cend(); ++i1, ++i2) {
        if (*i1 != *i2)
            return false;
    }
    return true;
}

inline bool equal(const std::string &lhs, const std::string &rhs, bool ignoreCase = true) {
    return (lhs.size() == rhs.size()) && (ignoreCase ?
        0 == strncasecmp(lhs.c_str(), rhs.c_str(), lhs.size()) :
        0 == strncmp(lhs.c_str(), rhs.c_str(), lhs.size()));
}

/**
 * @brief check string end with given substring
 * @param src - string to check
 * @param with - given substring
 * @return true if string end with given substring
 */
inline bool endsWith(const std::string &src, const char *with) {
    int wl = static_cast<int>(strlen(with));
    int so = static_cast<int>(src.length()) - wl;
    if (so < 0) return false;
    return 0 == strncmp(with, &src[so], wl);
}

/**
* @brief converts all upper-case letters in a string to lower case
* @param s - string to convert
*/
inline std::string tolower(const std::string &s) {
    std::string ret;
    ret.resize(s.length());
    std::transform(s.begin(), s.end(), ret.begin(), ::tolower);
    return ret;
}

/**
 * @brief Wierd function to perform string formatting
 * @param msg - base format
 * @param ... - arguments for formatting
 * @return formatted string
 */
static inline std::string stringFormat(const char *msg, ...) {
    va_list va;
    va_start(va, msg);
    char buffer[65536];

    vsnprintf_s(buffer, 65535, msg, va);
    va_end(va);
    return buffer;
}
}  // namespace details
}  // namespace InferenceEngine

/**
 * @brief print a log message
 * @param isErr - is message an error or not
 * @param level - string containing message level, like "ERROR" or "DEBUG"
 * @param file - file the message was produced by
 * @param line - string in file the message was dispatched from
 * @param msg - format for message + arguments
 */
static void OPT_USAGE print_log(bool isErr, const char *level, const char *file, int line, const char *msg, ...) {
    va_list va;
    va_start(va, msg);
    char buffer[64];
    struct tm tm_info;

    struct timeval tval;
    gettimeofday(&tval, NULL);

    auto outFd = isErr ? stderr : stdout;

#ifdef _WIN32
    time_t timer;
    timer = tval.tv_sec;
    localtime_s(&tm_info, &timer);
#else
    localtime_r(&tval.tv_sec, &tm_info);
#endif

    strftime(buffer, 64, "%Y:%m:%d %H:%M:%S", &tm_info);

    fprintf(outFd, "%s.%06ld [%s] %s:%d : ", buffer, (long)tval.tv_usec, level, file, line);
    vfprintf(outFd, msg, va);
    va_end(va);
    fprintf(outFd, "\r\n");
    fflush(outFd);
}

/**
 * @def LogError
 * @brief Log an error message
 */
#define LogError(...) {print_log(true , "ERROR", __FILE__, __LINE__, ##__VA_ARGS__); }

/**
 * @def LogWarning
 * @brief Log warning message
 */
#define LogWarning(...) { print_log(true , "WARNING", __FILE__, __LINE__, ##__VA_ARGS__); }

/**
 * @def LogInfo
 * @brief Log info message
 */
#define LogInfo(...) { print_log(false, "INFO", __FILE__, __LINE__, ##__VA_ARGS__); }

/**
 * @def LogDebug
 * @brief Log debug message
 */
#define LogDebug(...) { print_log(false, "DEBUG", __FILE__, __LINE__, ##__VA_ARGS__); }

/**
 * @def OssDebug
 * @brief Log oss debug message
 */
#define OssDebug(x) { std::stringstream oss; oss << x ; LogDebug("%s", oss.str().c_str()); }