roo_monitoring
API Documentation for roo_monitoring
Loading...
Searching...
No Matches
common.cpp
Go to the documentation of this file.
1#include "common.h"
2
3#include <algorithm>
4#include <memory>
5
6#include "roo_logging.h"
7
8namespace roo_monitoring {
9
10const char* kMonitoringBasePath = "/monitoring";
11const char* kLogSubPath = "log";
12
13String subdir(String base, const String& sub) {
14 base += '/';
15 base += sub;
16 return base;
17}
18
19String filepath(String dir, int64_t file) {
20 dir += "/";
21 dir += Filename::forTimestamp(file).filename();
22 return dir;
23}
24
25namespace {
26
27static uint8_t fromHexDigit(char c) {
28 if (c >= '0' && c <= '9') return c - '0';
29 if (c >= 'A' && c <= 'F') return c - 'A' + 10;
30 if (c >= 'a' && c <= 'f') return c - 'a' + 10;
31 return 0;
32}
33
34static int64_t decodeHex(const char* filename) {
35 int64_t result = 0;
36 for (const char* c = filename; *c != 0; ++c) {
37 result <<= 4;
38 result |= (fromHexDigit(*c));
39 }
40 return result;
41}
42
43} // namespace
44
45std::vector<int64_t> listFiles(roo_io::Mount& fs, const char* dirname) {
46 std::vector<int64_t> result;
47 roo_io::Directory dir = fs.opendir(dirname);
48 if (!dir.isOpen()) {
49 LOG(WARNING) << "Failed to open directory " << dirname << ": "
50 << roo_io::StatusAsString(dir.status());
51 return result;
52 }
53 while (dir.read()) {
54 if (dir.entry().isDirectory()) {
55 // Skip "." and ".."
56 continue;
57 }
58 if (strlen(dir.entry().name()) != 12) {
59 // Skip .cursor files, and other auxiliary files.
60 continue;
61 }
62 result.push_back(decodeHex(dir.entry().name()));
63 }
64 std::sort(result.begin(), result.end());
65 dir.close();
66 return result;
67}
68
69Filename Filename::forTimestamp(int64_t timestamp_ms) {
71 for (int i = 11; i >= 0; --i) {
72 filename.data_[i] = toHexDigit(timestamp_ms & 0xF);
73 timestamp_ms >>= 4;
74 }
75 filename.data_[12] = 0;
76 return filename;
77}
78
79} // namespace roo_monitoring
Helper class for generating filenames corresponding to timestamps.
Definition common.h:45
const char * filename() const
Returns the generated filename as a null-terminated string.
Definition common.h:50
static Filename forTimestamp(int64_t nanosSinceEpoch)
Creates a filename for the specified timestamp.
Definition common.cpp:69
Umbrella header for the roo_monitoring module.
const char * kLogSubPath
Subdirectory name used for raw log files.
Definition common.cpp:11
String filepath(String dir, int64_t file)
Returns a file path for the given directory and timestamp-like value.
Definition common.cpp:19
const char * kMonitoringBasePath
Base directory for monitoring storage on the filesystem.
Definition common.cpp:10
std::vector< int64_t > listFiles(roo_io::Mount &fs, const char *dirname)
Lists timestamp-named files in the directory and returns their timestamps.
Definition common.cpp:45
constexpr char toHexDigit(int d)
Converts a 0-15 value to an uppercase hex digit.
Definition common.h:30
String subdir(String base, const String &sub)
Returns a path formed by joining the base directory and subdirectory.
Definition common.cpp:13