forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
64 lines (55 loc) · 1.72 KB
/
Logger.cpp
File metadata and controls
64 lines (55 loc) · 1.72 KB
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
/*******************************************************
* Copyright (c) 2018, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#ifdef _WIN32
#include <windows.h> // spdlog needs this
#endif
#include <common/Logger.hpp>
#include <common/util.hpp>
#include <spdlog/sinks/stdout_sinks.h>
#include <array>
#include <cstdlib>
#include <memory>
#include <mutex>
#include <string>
using std::array;
using std::shared_ptr;
using std::string;
using spdlog::get;
using spdlog::logger;
using spdlog::stdout_logger_mt;
namespace common {
shared_ptr<logger> loggerFactory(const string& name) {
shared_ptr<logger> logger;
if (!(logger = get(name))) {
logger = stdout_logger_mt(name);
logger->set_pattern("[%n][%E][%t] %v");
// Log mode
string env_var = getEnvVar("AF_TRACE");
if (env_var.find("all") != string::npos ||
env_var.find(name) != string::npos) {
logger->set_level(spdlog::level::trace);
} else {
logger->set_level(spdlog::level::off);
}
}
return logger;
}
string bytesToString(size_t bytes) {
constexpr array<const char*, 7> units{
{"B", "KB", "MB", "GB", "TB", "PB", "EB"}};
size_t count = 0;
auto fbytes = static_cast<double>(bytes);
size_t num_units = units.size();
for (count = 0; count < num_units && fbytes > 1000.0f; count++) {
fbytes *= (1.0f / 1024.0f);
}
if (count == units.size()) { count--; }
return fmt::format("{:.3g} {}", fbytes, units[count]);
}
} // namespace common