-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.cpp
174 lines (153 loc) · 4.57 KB
/
util.cpp
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
#include "util.hpp"
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
#include <string_view>
#include <vector>
#include "constant.hpp"
#include "platform.hpp"
// Forward declaration for functions in auto/version.cpp
namespace Version {
std::string_view getString();
}
namespace {
std::filesystem::path getHomePath();
namespace globals {
constexpr std::string_view usage = R"(
Usage: yommd <options>
options:
--config <toml> Specify config file
--logfile <file> Output logs to <file>
-v|--version Show software version
-h|--help Show this help
)";
}
} // namespace
CmdArgs CmdArgs::Parse(const std::vector<std::string>& args) {
if (args.empty()) {
Err::Exit("Executable file name must be passed.");
}
std::filesystem::path executable(args[0]);
CmdArgs cmdArgs;
const auto end = args.cend();
auto itr = ++args.cbegin(); // The first item is the executable path. Skip it.
while (itr != end) {
if (*itr == "-h" || *itr == "--help") {
Info::Log(globals::usage);
std::exit(0);
} else if (*itr == "-v" || *itr == "--version") {
Info::Log("version:", Version::getString());
std::exit(0);
} else if (*itr == "--config") {
if (++itr == end) {
Err::Log("No toml file name specified after \"--config\"");
Err::Exit(globals::usage);
} else if (!cmdArgs.configFile.empty()) {
Err::Log("Multiple config file detected. Use the last one.");
}
cmdArgs.configFile = *itr;
} else if (*itr == "--logfile") {
if (++itr == end) {
Err::Log("No log file name specified after \"--logfile\"");
Err::Exit(globals::usage);
} else if (!cmdArgs.logFile.empty()) {
Err::Log("Multiple log file specified. Use the last one.");
}
cmdArgs.logFile = *itr;
} else {
Err::Exit("Unknown option:", *itr, '\n', globals::usage);
}
++itr;
}
// Fallback
if (cmdArgs.logFile.empty())
cmdArgs.logFile = Constant::DefaultLogFilePath;
// Make absolute if necessary.
if (!cmdArgs.configFile.empty())
cmdArgs.configFile =
::Path::makeAbsolute(cmdArgs.configFile, ::Path::getWorkingDirectory());
if (!cmdArgs.logFile.empty())
cmdArgs.logFile = ::Path::makeAbsolute(cmdArgs.logFile, ::Path::getWorkingDirectory());
return cmdArgs;
}
namespace Path {
std::filesystem::path getWorkingDirectory() {
// TODO: Can I truely initialize this here?
static const auto cwd = std::filesystem::current_path();
return cwd;
}
std::filesystem::path makeAbsolute(
const std::filesystem::path& path,
const std::filesystem::path& cwd) {
namespace fs = std::filesystem;
static const auto homePath = getHomePath();
if (path.is_absolute())
return path;
else if (const auto u8path = path.generic_u8string(); u8path.starts_with(u8"~/"))
return fs::weakly_canonical(homePath / fs::path(u8path.substr(2)));
else
return fs::weakly_canonical(cwd / path);
}
} // namespace Path
namespace {
std::filesystem::path getHomePath() {
#ifdef PLATFORM_WINDOWS
const wchar_t *wpath = _wgetenv(L"USERPROFILE");
if (!wpath)
Err::Exit("%USERPROFILE% is not set");
return std::filesystem::path(String::wideToMulti<char8_t>(wpath));
#else
const char *path = std::getenv("HOME");
if (!path)
Err::Exit("$HOME is not set");
return std::filesystem::path(String::tou8(std::string_view(path)));
#endif
}
} // namespace
namespace Slog {
void Logger(
const char *tag,
uint32_t logLevel,
uint32_t logItem,
const char *message,
uint32_t linenr,
const char *filename,
void *user_data) {
(void)user_data;
std::stringstream ss;
if (tag) {
ss << '[' << tag << ']';
}
switch (logLevel) {
case 0:
ss << "panic:";
break;
case 1:
ss << "error:";
break;
case 2:
ss << "warning:";
break;
default:
ss << "info:";
break;
}
ss << " [id:" << logItem << ']';
if (filename) {
ss << ' ' << filename << ':' << linenr << ":0: ";
} else {
ss << "[line:" << linenr << "] ";
}
if (message) {
ss << "\n\t" << message;
}
if (logLevel == 0) {
ss << "\nAborting because of panic.";
Err::Exit(ss.str());
} else {
Err::Log(ss.str());
}
}
} // namespace Slog